Skip to content

Commit 4a3eed2

Browse files
author
naman-contentstack
committed
add condition for headers in audit
1 parent 97101fe commit 4a3eed2

File tree

1 file changed

+50
-34
lines changed

1 file changed

+50
-34
lines changed

src/core/contentstack/client.ts

Lines changed: 50 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,64 +1,80 @@
1-
import axios, {AxiosInstance} from 'axios'
2-
import ContentstackError from './error'
3-
import {cliux} from '@contentstack/cli-utilities'
1+
import axios, { AxiosInstance } from "axios";
2+
import ContentstackError from "./error";
3+
import { cliux } from "@contentstack/cli-utilities";
44

55
export default class ContentstackClient {
6-
private instance: AxiosInstance;
6+
private readonly instance: AxiosInstance;
77

88
constructor(cmaHost: string, authToken: string) {
9+
const headerName = authToken.includes("Bearer")
10+
? "authorization"
11+
: "authtoken";
912
this.instance = axios.create({
1013
baseURL: `https://${cmaHost}/v3/`,
1114
headers: {
12-
authtoken: authToken,
15+
[headerName]: authToken,
1316
},
14-
})
17+
});
1518
}
1619

17-
async getContentTypeAuditLogs(api_key: string, uid: string, spinner: any): Promise<any> {
20+
async getContentTypeAuditLogs(
21+
api_key: string,
22+
uid: string,
23+
spinner: any
24+
): Promise<any> {
1825
try {
19-
const response = await this.instance.get('/audit-logs',
20-
{
21-
params: {
22-
query: {$and: [{module: 'content_type'}, {'metadata.uid': uid}]},
26+
const response = await this.instance.get("/audit-logs", {
27+
params: {
28+
query: {
29+
$and: [{ module: "content_type" }, { "metadata.uid": uid }],
2330
},
24-
headers: {api_key},
25-
})
31+
},
32+
headers: { api_key },
33+
});
2634

27-
return response.data
35+
return response.data;
2836
} catch (error) {
29-
cliux.loaderV2('', spinner)
30-
throw this.buildError(error, {api_key})
37+
cliux.loaderV2("", spinner);
38+
throw this.buildError(error, { api_key });
3139
}
3240
}
3341

34-
async getContentTypeReferences(api_key: string, uid: string, spinner: any): Promise<any> {
42+
async getContentTypeReferences(
43+
api_key: string,
44+
uid: string,
45+
spinner: any
46+
): Promise<any> {
3547
try {
36-
const response = await this.instance.get(`/content_types/${uid}/references`, {
37-
params: {
38-
include_global_fields: true,
39-
},
40-
headers: {
41-
api_key,
42-
},
43-
})
48+
const response = await this.instance.get(
49+
`/content_types/${uid}/references`,
50+
{
51+
params: {
52+
include_global_fields: true,
53+
},
54+
headers: {
55+
api_key,
56+
},
57+
}
58+
);
4459

45-
return response.data
60+
return response.data;
4661
} catch (error) {
47-
cliux.loaderV2('', spinner)
48-
throw this.buildError(error, {api_key})
62+
cliux.loaderV2("", spinner);
63+
throw this.buildError(error, { api_key });
4964
}
5065
}
5166

5267
private buildError(error: any, context: any = {}) {
53-
const data = error?.response?.data
54-
if (!data || !data.errors) return new Error('Unrecognized error. Please try again.')
68+
const data = error?.response?.data;
69+
if (!data || !data.errors)
70+
return new Error("Unrecognized error. Please try again.");
5571

56-
let message = data.error_message
57-
const code = data.error_code
72+
let message = data.error_message;
73+
const code = data.error_code;
5874

5975
if (data.errors.api_key && context.api_key) {
60-
message += ` This is in regards to the Stack API Key '${context.api_key}'.`
76+
message += ` This is in regards to the Stack API Key '${context.api_key}'.`;
6177
}
62-
return new ContentstackError(message, code)
78+
return new ContentstackError(message, code);
6379
}
6480
}

0 commit comments

Comments
 (0)