|
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"; |
4 | 4 |
|
5 | 5 | export default class ContentstackClient { |
6 | | - private instance: AxiosInstance; |
| 6 | + private readonly instance: AxiosInstance; |
7 | 7 |
|
8 | 8 | constructor(cmaHost: string, authToken: string) { |
| 9 | + const headerName = authToken.includes("Bearer") |
| 10 | + ? "authorization" |
| 11 | + : "authtoken"; |
9 | 12 | this.instance = axios.create({ |
10 | 13 | baseURL: `https://${cmaHost}/v3/`, |
11 | 14 | headers: { |
12 | | - authtoken: authToken, |
| 15 | + [headerName]: authToken, |
13 | 16 | }, |
14 | | - }) |
| 17 | + }); |
15 | 18 | } |
16 | 19 |
|
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> { |
18 | 25 | 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 }], |
23 | 30 | }, |
24 | | - headers: {api_key}, |
25 | | - }) |
| 31 | + }, |
| 32 | + headers: { api_key }, |
| 33 | + }); |
26 | 34 |
|
27 | | - return response.data |
| 35 | + return response.data; |
28 | 36 | } catch (error) { |
29 | | - cliux.loaderV2('', spinner) |
30 | | - throw this.buildError(error, {api_key}) |
| 37 | + cliux.loaderV2("", spinner); |
| 38 | + throw this.buildError(error, { api_key }); |
31 | 39 | } |
32 | 40 | } |
33 | 41 |
|
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> { |
35 | 47 | 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 | + ); |
44 | 59 |
|
45 | | - return response.data |
| 60 | + return response.data; |
46 | 61 | } catch (error) { |
47 | | - cliux.loaderV2('', spinner) |
48 | | - throw this.buildError(error, {api_key}) |
| 62 | + cliux.loaderV2("", spinner); |
| 63 | + throw this.buildError(error, { api_key }); |
49 | 64 | } |
50 | 65 | } |
51 | 66 |
|
52 | 67 | 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."); |
55 | 71 |
|
56 | | - let message = data.error_message |
57 | | - const code = data.error_code |
| 72 | + let message = data.error_message; |
| 73 | + const code = data.error_code; |
58 | 74 |
|
59 | 75 | 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}'.`; |
61 | 77 | } |
62 | | - return new ContentstackError(message, code) |
| 78 | + return new ContentstackError(message, code); |
63 | 79 | } |
64 | 80 | } |
0 commit comments