@elasticpath/component-test-framework 1.0.19 → 1.0.21
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/api.d.ts +6 -6
- package/lib/api.interfaces.d.ts +3 -0
- package/lib/api.interfaces.js.map +1 -1
- package/lib/api.js +18 -2
- package/lib/api.js.map +1 -1
- package/package.json +1 -1
package/lib/api.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { AxiosInstance } from 'axios';
|
|
1
|
+
import { AxiosInstance, AxiosResponse } from 'axios';
|
|
2
2
|
import * as FormData from 'form-data';
|
|
3
3
|
import { ApiPayload, ApiResponse, ApiArrayResponse, Error, V2Response, V3Response } from './api.interfaces';
|
|
4
4
|
declare class Api {
|
|
@@ -16,11 +16,11 @@ declare class Api {
|
|
|
16
16
|
get errors(): Array<Error> | Array<any>;
|
|
17
17
|
get response(): ApiResponse | ApiArrayResponse;
|
|
18
18
|
validateUrl(endpoint: string): void;
|
|
19
|
-
get(endpoint: string, abortOnError?: boolean): Promise<
|
|
20
|
-
post(endpoint: string, payload: ApiPayload, abortOnError?: boolean): Promise<
|
|
21
|
-
form(endpoint: string, form: FormData, abortOnError?: boolean): Promise<
|
|
22
|
-
put(endpoint: string, payload: ApiPayload, abortOnError?: boolean): Promise<
|
|
23
|
-
delete(endpoint: string, payload?: ApiPayload, abortOnError?: boolean): Promise<
|
|
19
|
+
get(endpoint: string, abortOnError?: boolean): Promise<AxiosResponse<any, any>>;
|
|
20
|
+
post(endpoint: string, payload: ApiPayload, abortOnError?: boolean): Promise<AxiosResponse<any, any>>;
|
|
21
|
+
form(endpoint: string, form: FormData, abortOnError?: boolean): Promise<AxiosResponse<any, any>>;
|
|
22
|
+
put(endpoint: string, payload: ApiPayload, abortOnError?: boolean): Promise<AxiosResponse<any, any>>;
|
|
23
|
+
delete(endpoint: string, payload?: ApiPayload, abortOnError?: boolean): Promise<AxiosResponse<any, any>>;
|
|
24
24
|
setHeaders(newHeaders: object, reset?: boolean, preserveValueType?: boolean): void;
|
|
25
25
|
deleteHeader(key: string): void;
|
|
26
26
|
private fail;
|
package/lib/api.interfaces.d.ts
CHANGED
|
@@ -28,6 +28,7 @@ export interface ApiResponse {
|
|
|
28
28
|
status: number;
|
|
29
29
|
data: V2Response | V3Response;
|
|
30
30
|
links?: ApiLinks;
|
|
31
|
+
meta?: any;
|
|
31
32
|
}
|
|
32
33
|
interface V2Payload {
|
|
33
34
|
[key: string]: any;
|
|
@@ -42,6 +43,8 @@ interface V3Payload {
|
|
|
42
43
|
export interface ApiArrayResponse {
|
|
43
44
|
status: number;
|
|
44
45
|
data: Array<V2Response> | Array<V3Response>;
|
|
46
|
+
links?: ApiLinks;
|
|
47
|
+
meta?: any;
|
|
45
48
|
}
|
|
46
49
|
export interface ApiPayload {
|
|
47
50
|
data: V2Payload | V3Payload;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"api.interfaces.js","sourceRoot":"","sources":["../src/api.interfaces.ts"],"names":[],"mappings":"","sourcesContent":["export interface Error {\n status: string\n title: string\n detail: string\n}\n\nexport interface V2Response {\n id: string\n type: string\n [key: string]: any\n errors?: Array<any> // V2 has
|
|
1
|
+
{"version":3,"file":"api.interfaces.js","sourceRoot":"","sources":["../src/api.interfaces.ts"],"names":[],"mappings":"","sourcesContent":["export interface Error {\n status: string\n title: string\n detail: string\n}\n\nexport interface V2Response {\n id: string\n type: string\n [key: string]: any\n errors?: Array<any> // V2 has inconsistent error responses\n}\n\nexport interface V3Response {\n id: string\n type: string\n attributes: {\n [key: string]: any\n }\n relationships?: any\n errors?: Array<Error>\n}\n\nexport interface ApiLinks {\n next: string | null\n prev: string | null\n first: string\n last: string | null\n}\n\nexport interface ApiResponse {\n status: number\n data: V2Response | V3Response\n links?: ApiLinks\n meta?: any\n}\n\ninterface V2Payload {\n [key: string]: any\n}\n\ninterface V3Payload {\n id?: string\n type?: string\n attributes: {\n [key: string]: string\n }\n}\n\nexport interface ApiArrayResponse {\n status: number\n data: Array<V2Response> | Array<V3Response>\n links?: ApiLinks\n meta?: any\n}\n\nexport interface ApiPayload {\n data: V2Payload | V3Payload\n}\n"]}
|
package/lib/api.js
CHANGED
|
@@ -88,9 +88,13 @@ class Api {
|
|
|
88
88
|
this.axiosResponseObject = yield this.axiosInstance
|
|
89
89
|
.get(endpoint, { headers: this.headers })
|
|
90
90
|
.catch((err) => this.fail(err, abortOnError));
|
|
91
|
-
if (!this.isError(this.axiosResponseObject)
|
|
92
|
-
|
|
91
|
+
if (!this.isError(this.axiosResponseObject)) {
|
|
92
|
+
if (!this.isEmpty(this.axiosResponseObject)) {
|
|
93
|
+
resource_store_1.default.Store(this.axiosResponseObject.data);
|
|
94
|
+
}
|
|
95
|
+
return this.axiosResponseObject;
|
|
93
96
|
}
|
|
97
|
+
return this.axiosResponseObject.response;
|
|
94
98
|
});
|
|
95
99
|
}
|
|
96
100
|
post(endpoint, payload, abortOnError = true) {
|
|
@@ -103,7 +107,9 @@ class Api {
|
|
|
103
107
|
.catch((err) => this.fail(err, abortOnError));
|
|
104
108
|
if (!this.isError(this.axiosResponseObject)) {
|
|
105
109
|
resource_store_1.default.Store(this.axiosResponseObject.data);
|
|
110
|
+
return this.axiosResponseObject;
|
|
106
111
|
}
|
|
112
|
+
return this.axiosResponseObject.response;
|
|
107
113
|
});
|
|
108
114
|
}
|
|
109
115
|
form(endpoint, form, abortOnError = true) {
|
|
@@ -117,7 +123,9 @@ class Api {
|
|
|
117
123
|
.catch((err) => this.fail(err, abortOnError));
|
|
118
124
|
if (!this.isError(this.axiosResponseObject)) {
|
|
119
125
|
resource_store_1.default.Store(this.axiosResponseObject.data);
|
|
126
|
+
return this.axiosResponseObject;
|
|
120
127
|
}
|
|
128
|
+
return this.axiosResponseObject.response;
|
|
121
129
|
});
|
|
122
130
|
}
|
|
123
131
|
put(endpoint, payload, abortOnError = true) {
|
|
@@ -128,6 +136,10 @@ class Api {
|
|
|
128
136
|
this.axiosResponseObject = yield this.axiosInstance
|
|
129
137
|
.put(endpoint, payload, { headers: this.headers })
|
|
130
138
|
.catch((err) => this.fail(err, abortOnError));
|
|
139
|
+
if (!this.isError(this.axiosResponseObject)) {
|
|
140
|
+
return this.axiosResponseObject;
|
|
141
|
+
}
|
|
142
|
+
return this.axiosResponseObject.response;
|
|
131
143
|
});
|
|
132
144
|
}
|
|
133
145
|
delete(endpoint, payload, abortOnError = true) {
|
|
@@ -140,6 +152,10 @@ class Api {
|
|
|
140
152
|
this.axiosResponseObject = yield this.axiosInstance
|
|
141
153
|
.delete(endpoint, config)
|
|
142
154
|
.catch((err) => this.fail(err, abortOnError));
|
|
155
|
+
if (!this.isError(this.axiosResponseObject)) {
|
|
156
|
+
return this.axiosResponseObject;
|
|
157
|
+
}
|
|
158
|
+
return this.axiosResponseObject.response;
|
|
143
159
|
});
|
|
144
160
|
}
|
|
145
161
|
// setHeaders will merge the new headers with the current ones, overwriting if
|
package/lib/api.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"api.js","sourceRoot":"","sources":["../src/api.ts"],"names":[],"mappings":";;;;;;;;;;;AAAA,iCAAuH;AACvH,4CAA2C;AAE3C,+BAA6B;AAG7B,qDAA4C;AAC5C,mCAAkC;AAClC,mCAAqC;AAErC,MAAM,GAAG;IAiBP;QACE,MAAM,EAAE,YAAY,EAAE,iBAAiB,EAAE,GAAG,OAAO,CAAC,GAAG,CAAA;QAEvD,IAAI,CAAC,cAAc,GAAG;YACpB,cAAc,EAAE,kBAAkB;SACnC,CAAA;QACD,IAAI,CAAC,OAAO,GAAG,IAAI,CAAC,cAAc,CAAA;QAElC,IAAI,CAAC,aAAa,GAAG,eAAK,CAAC,MAAM,CAAC;YAChC,OAAO,EAAE,YAAY;YACrB,OAAO,EAAE,IAAI;YACb,mBAAmB,EAAE,2DAA2D,YAAY,EAAE;SAC/F,CAAC,CAAA;QAEF,IAAI,CAAC,aAAa,CAAC,YAAY,CAAC,OAAO,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AAE,MAAM,MAAK,SAAS,EAAE;gBAC9C,MAAM,CAAC,KAAK,CAAC,MAAA,MAAA,KAAK,CAAC,QAAQ,0CAAE,IAAI,0CAAE,MAAM,CAAC,CAAA;aAC3C;SACF;aAAM;YACL,MAAM,CAAC,KAAK,CAAC,eAAe,EAAE,KAAK,EAAE,IAAI,CAAC,CAAA;SAC3C;IACH,CAAC;IAED,kDAAkD;IAC1C,OAAO,CAAC,QAAuB;QACrC,IAAI,CAAC,QAAQ,CAAC,IAAI,EAAE;YAClB,OAAO,IAAI,CAAA;SACZ;QAED,IAAI,KAAK,CAAC,OAAO,CAAC,QAAQ,CAAC,IAAI,CAAC,IAAI,CAAC,EAAE;YACrC,IAAI,QAAQ,CAAC,IAAI,CAAC,IAAI,CAAC,MAAM,KAAK,CAAC,EAAE;gBACnC,OAAO,IAAI,CAAA;aACZ;SACF;aAAM,IAAI,MAAM,CAAC,IAAI,CAAC,QAAQ,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,MAAM,KAAK,CAAC,EAAE;YACvD,OAAO,IAAI,CAAA;SACZ;QAED,OAAO,KAAK,CAAA;IACd,CAAC;CACF;AAED,kBAAe,GAAG,CAAA","sourcesContent":["import Axios, { AxiosInstance, AxiosError, AxiosResponse, AxiosRequestConfig, InternalAxiosRequestConfig } from 'axios'\nimport * as AxiosLogger from 'axios-logger'\nimport * as FormData from 'form-data'\nimport { expect } from 'chai'\n\nimport { ApiPayload, ApiResponse, ApiArrayResponse, Error, V2Response, V3Response } from './api.interfaces'\nimport ResourceStore from './resource_store'\nimport * as Logger from './logger'\nimport { nullOrValue } from './utils'\n\nclass Api {\n public axiosInstance: AxiosInstance\n\n private axiosResponseObject: AxiosResponse | AxiosError\n\n private requestPath: string\n\n private requestMethod: string\n\n // We can't add this to the interceptors because we sometimes\n // need to overwrite this\n private readonly defaultHeaders: object\n\n private headers: object\n\n private readonly baseUrl: string\n\n public constructor() {\n const { CTF_BASE_URL, CTF_LOGGING_LEVEL } = process.env\n\n this.defaultHeaders = {\n 'content-type': 'application/json',\n }\n this.headers = this.defaultHeaders\n\n this.axiosInstance = Axios.create({\n baseURL: CTF_BASE_URL,\n timeout: 4000,\n timeoutErrorMessage: `[Fail] Api request timed out. Is the service running on ${CTF_BASE_URL}`,\n })\n\n this.axiosInstance.interceptors.request.use((config: InternalAxiosRequestConfig) => {\n // eslint-disable-next-line no-param-reassign\n config.headers.accept = 'application/json'\n return config\n })\n\n if (CTF_LOGGING_LEVEL === 'debug') {\n this.axiosInstance.interceptors.request.use((internalrequest: InternalAxiosRequestConfig) => {\n const { headers } = internalrequest\n const request: AxiosRequestConfig = {\n ...internalrequest,\n }\n const logrequest = AxiosLogger.requestLogger(request)\n return {\n ...logrequest,\n headers,\n }\n }, AxiosLogger.errorLogger)\n this.axiosInstance.interceptors.response.use(AxiosLogger.responseLogger)\n }\n\n this.baseUrl = CTF_BASE_URL\n }\n\n private isError(response): response is AxiosError {\n return (<AxiosError>this.axiosResponseObject).isAxiosError !== undefined\n }\n\n public isV3Response(data: V2Response | V3Response): data is V3Response {\n if (this.isError(this.axiosResponseObject)) {\n return false\n }\n\n return <V3Response>data.attributes !== undefined\n }\n\n public IsV2Response(data: V2Response | V3Response): data is V2Response {\n return !this.isV3Response(data)\n }\n\n public get errors(): Array<Error> | Array<any> {\n // Response data is never an array if there's errors\n if (Array.isArray(this.response.data)) {\n Logger.error('Attempted to access errors on an array response')\n return null\n }\n\n return this.response.data.errors\n }\n\n public get response(): ApiResponse | ApiArrayResponse {\n if (this.isError(this.axiosResponseObject)) {\n return {\n status: this.axiosResponseObject?.response?.status,\n data: this.axiosResponseObject?.response?.data,\n } as ApiResponse\n }\n\n return {\n status: this.axiosResponseObject.status,\n // add everything else from the response\n ...this.axiosResponseObject.data,\n }\n }\n\n public validateUrl(endpoint: string) {\n const regex = /(?<!http:)\\/{2,}/\n const fqURL = `${this.baseUrl}${endpoint}`\n\n if (regex.test(fqURL)) {\n throw new Error(`invalid URL: ${fqURL}`)\n }\n }\n\n public async get(endpoint: string, abortOnError: boolean = true): Promise<void> {\n this.validateUrl(endpoint)\n this.requestPath = endpoint\n this.requestMethod = 'GET'\n\n this.axiosResponseObject = await this.axiosInstance\n .get(endpoint, { headers: this.headers })\n .catch((err) => this.fail(err, abortOnError))\n\n if (!this.isError(this.axiosResponseObject) && !this.isEmpty(this.axiosResponseObject)) {\n ResourceStore.Store(this.axiosResponseObject.data)\n }\n }\n\n public async post(endpoint: string, payload: ApiPayload, abortOnError: boolean = true): Promise<void> {\n this.validateUrl(endpoint)\n this.requestPath = endpoint\n this.requestMethod = 'POST'\n\n this.axiosResponseObject = await this.axiosInstance\n .post(endpoint, payload, { headers: this.headers })\n .catch((err) => this.fail(err, abortOnError))\n\n if (!this.isError(this.axiosResponseObject)) {\n ResourceStore.Store(this.axiosResponseObject.data)\n }\n }\n\n public async form(endpoint: string, form: FormData, abortOnError: boolean = true): Promise<void> {\n this.validateUrl(endpoint)\n this.requestPath = endpoint\n this.requestMethod = 'POST'\n const headers = { ...this.headers, ...form.getHeaders() }\n\n this.axiosResponseObject = await this.axiosInstance\n .post(endpoint, form, { headers })\n .catch((err) => this.fail(err, abortOnError))\n\n if (!this.isError(this.axiosResponseObject)) {\n ResourceStore.Store(this.axiosResponseObject.data)\n }\n }\n\n public async put(endpoint: string, payload: ApiPayload, abortOnError: boolean = true): Promise<void> {\n this.validateUrl(endpoint)\n this.requestPath = endpoint\n this.requestMethod = 'PUT'\n\n this.axiosResponseObject = await this.axiosInstance\n .put(endpoint, payload, { headers: this.headers })\n .catch((err) => this.fail(err, abortOnError))\n }\n\n public async delete(endpoint: string, payload?: ApiPayload, abortOnError: boolean = true): Promise<void> {\n this.validateUrl(endpoint)\n this.requestPath = endpoint\n this.requestMethod = 'DELETE'\n // For some reason, axios delete requests specify the payload and headers in the same object\n const config = {\n ...payload,\n ...{ headers: this.headers },\n }\n\n this.axiosResponseObject = await this.axiosInstance\n .delete(endpoint, config)\n .catch((err) => this.fail(err, abortOnError))\n }\n\n // setHeaders will merge the new headers with the current ones, overwriting if\n // the same key exists. The value will use nullOrValue to properly map strings to literals ('null' => null)\n public setHeaders(newHeaders: object, reset: boolean = false, preserveValueType: boolean = false): void {\n if (reset) {\n this.headers = {}\n }\n\n Object.entries(newHeaders).forEach((h) => {\n let value = h[1]\n if (!preserveValueType) {\n value = nullOrValue(value)\n }\n return Object.assign(this.headers, {\n [h[0]]: value,\n })\n })\n }\n\n public deleteHeader(key: string) {\n delete this.headers[key]\n }\n\n private fail(error: AxiosError, abortOnError: boolean): AxiosError {\n if (abortOnError) {\n this.beautifyError(error)\n expect.fail(error.message)\n }\n\n return error\n }\n\n private beautifyError(error: any): void {\n if (error.response) {\n Logger.error(\n `Failed requesting ${this.requestMethod.toUpperCase()} ${process.env.CTF_BASE_URL}${this.requestPath}`,\n )\n\n if (error.response?.data?.errors !== undefined) {\n Logger.error(error.response?.data?.errors)\n }\n } else {\n Logger.error('[FATAL ERROR]', error, '\\n')\n }\n }\n\n // eslint-disable-next-line class-methods-use-this\n private isEmpty(response: AxiosResponse): boolean {\n if (!response.data) {\n return true\n }\n\n if (Array.isArray(response.data.data)) {\n if (response.data.data.length === 0) {\n return true\n }\n } else if (Object.keys(response.data.data).length === 0) {\n return true\n }\n\n return false\n }\n}\n\nexport default Api\n"]}
|
|
1
|
+
{"version":3,"file":"api.js","sourceRoot":"","sources":["../src/api.ts"],"names":[],"mappings":";;;;;;;;;;;AAAA,iCAAuH;AACvH,4CAA2C;AAE3C,+BAA6B;AAG7B,qDAA4C;AAC5C,mCAAkC;AAClC,mCAAqC;AAErC,MAAM,GAAG;IAiBP;QACE,MAAM,EAAE,YAAY,EAAE,iBAAiB,EAAE,GAAG,OAAO,CAAC,GAAG,CAAA;QAEvD,IAAI,CAAC,cAAc,GAAG;YACpB,cAAc,EAAE,kBAAkB;SACnC,CAAA;QACD,IAAI,CAAC,OAAO,GAAG,IAAI,CAAC,cAAc,CAAA;QAElC,IAAI,CAAC,aAAa,GAAG,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I,CAAC,IAAI,CAAC,EAAE;YACrC,IAAI,QAAQ,CAAC,IAAI,CAAC,IAAI,CAAC,MAAM,KAAK,CAAC,EAAE;gBACnC,OAAO,IAAI,CAAA;aACZ;SACF;aAAM,IAAI,MAAM,CAAC,IAAI,CAAC,QAAQ,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,MAAM,KAAK,CAAC,EAAE;YACvD,OAAO,IAAI,CAAA;SACZ;QAED,OAAO,KAAK,CAAA;IACd,CAAC;CACF;AAED,kBAAe,GAAG,CAAA","sourcesContent":["import Axios, { AxiosInstance, AxiosError, AxiosResponse, AxiosRequestConfig, InternalAxiosRequestConfig } from 'axios'\nimport * as AxiosLogger from 'axios-logger'\nimport * as FormData from 'form-data'\nimport { expect } from 'chai'\n\nimport { ApiPayload, ApiResponse, ApiArrayResponse, Error, V2Response, V3Response } from './api.interfaces'\nimport ResourceStore from './resource_store'\nimport * as Logger from './logger'\nimport { nullOrValue } from './utils'\n\nclass Api {\n public axiosInstance: AxiosInstance\n\n private axiosResponseObject: AxiosResponse | AxiosError\n\n private requestPath: string\n\n private requestMethod: string\n\n // We can't add this to the interceptors because we sometimes\n // need to overwrite this\n private readonly defaultHeaders: object\n\n private headers: object\n\n private readonly baseUrl: string\n\n public constructor() {\n const { CTF_BASE_URL, CTF_LOGGING_LEVEL } = process.env\n\n this.defaultHeaders = {\n 'content-type': 'application/json',\n }\n this.headers = this.defaultHeaders\n\n this.axiosInstance = Axios.create({\n baseURL: CTF_BASE_URL,\n timeout: 4000,\n timeoutErrorMessage: `[Fail] Api request timed out. Is the service running on ${CTF_BASE_URL}`,\n })\n\n this.axiosInstance.interceptors.request.use((config: InternalAxiosRequestConfig) => {\n // eslint-disable-next-line no-param-reassign\n config.headers.accept = 'application/json'\n return config\n })\n\n if (CTF_LOGGING_LEVEL === 'debug') {\n this.axiosInstance.interceptors.request.use((internalrequest: InternalAxiosRequestConfig) => {\n const { headers } = internalrequest\n const request: AxiosRequestConfig = {\n ...internalrequest,\n }\n const logrequest = AxiosLogger.requestLogger(request)\n return {\n ...logrequest,\n headers,\n }\n }, AxiosLogger.errorLogger)\n this.axiosInstance.interceptors.response.use(AxiosLogger.responseLogger)\n }\n\n this.baseUrl = CTF_BASE_URL\n }\n\n private isError(response): response is AxiosError {\n return (<AxiosError>this.axiosResponseObject).isAxiosError !== undefined\n }\n\n public isV3Response(data: V2Response | V3Response): data is V3Response {\n if (this.isError(this.axiosResponseObject)) {\n return false\n }\n\n return <V3Response>data.attributes !== undefined\n }\n\n public IsV2Response(data: V2Response | V3Response): data is V2Response {\n return !this.isV3Response(data)\n }\n\n public get errors(): Array<Error> | Array<any> {\n // Response data is never an array if there's errors\n if (Array.isArray(this.response.data)) {\n Logger.error('Attempted to access errors on an array response')\n return null\n }\n\n return this.response.data.errors\n }\n\n public get response(): ApiResponse | ApiArrayResponse {\n if (this.isError(this.axiosResponseObject)) {\n return {\n status: this.axiosResponseObject?.response?.status,\n data: this.axiosResponseObject?.response?.data,\n } as ApiResponse\n }\n\n return {\n status: this.axiosResponseObject.status,\n // add everything else from the response\n ...this.axiosResponseObject.data,\n }\n }\n\n public validateUrl(endpoint: string) {\n const regex = /(?<!http:)\\/{2,}/\n const fqURL = `${this.baseUrl}${endpoint}`\n\n if (regex.test(fqURL)) {\n throw new Error(`invalid URL: ${fqURL}`)\n }\n }\n\n public async get(endpoint: string, abortOnError: boolean = true): Promise<AxiosResponse<any, any>> {\n this.validateUrl(endpoint)\n this.requestPath = endpoint\n this.requestMethod = 'GET'\n\n this.axiosResponseObject = await this.axiosInstance\n .get(endpoint, { headers: this.headers })\n .catch((err) => this.fail(err, abortOnError))\n\n if (!this.isError(this.axiosResponseObject)) {\n if (!this.isEmpty(this.axiosResponseObject)) {\n ResourceStore.Store(this.axiosResponseObject.data)\n }\n return this.axiosResponseObject\n }\n\n return this.axiosResponseObject.response\n }\n\n public async post(\n endpoint: string,\n payload: ApiPayload,\n abortOnError: boolean = true,\n ): Promise<AxiosResponse<any, any>> {\n this.validateUrl(endpoint)\n this.requestPath = endpoint\n this.requestMethod = 'POST'\n\n this.axiosResponseObject = await this.axiosInstance\n .post(endpoint, payload, { headers: this.headers })\n .catch((err) => this.fail(err, abortOnError))\n\n if (!this.isError(this.axiosResponseObject)) {\n ResourceStore.Store(this.axiosResponseObject.data)\n return this.axiosResponseObject\n }\n\n return this.axiosResponseObject.response\n }\n\n public async form(endpoint: string, form: FormData, abortOnError: boolean = true): Promise<AxiosResponse<any, any>> {\n this.validateUrl(endpoint)\n this.requestPath = endpoint\n this.requestMethod = 'POST'\n const headers = { ...this.headers, ...form.getHeaders() }\n\n this.axiosResponseObject = await this.axiosInstance\n .post(endpoint, form, { headers })\n .catch((err) => this.fail(err, abortOnError))\n\n if (!this.isError(this.axiosResponseObject)) {\n ResourceStore.Store(this.axiosResponseObject.data)\n return this.axiosResponseObject\n }\n\n return this.axiosResponseObject.response\n }\n\n public async put(\n endpoint: string,\n payload: ApiPayload,\n abortOnError: boolean = true,\n ): Promise<AxiosResponse<any, any>> {\n this.validateUrl(endpoint)\n this.requestPath = endpoint\n this.requestMethod = 'PUT'\n\n this.axiosResponseObject = await this.axiosInstance\n .put(endpoint, payload, { headers: this.headers })\n .catch((err) => this.fail(err, abortOnError))\n\n if (!this.isError(this.axiosResponseObject)) {\n return this.axiosResponseObject\n }\n\n return this.axiosResponseObject.response\n }\n\n public async delete(\n endpoint: string,\n payload?: ApiPayload,\n abortOnError: boolean = true,\n ): Promise<AxiosResponse<any, any>> {\n this.validateUrl(endpoint)\n this.requestPath = endpoint\n this.requestMethod = 'DELETE'\n // For some reason, axios delete requests specify the payload and headers in the same object\n const config = {\n ...payload,\n ...{ headers: this.headers },\n }\n\n this.axiosResponseObject = await this.axiosInstance\n .delete(endpoint, config)\n .catch((err) => this.fail(err, abortOnError))\n\n if (!this.isError(this.axiosResponseObject)) {\n return this.axiosResponseObject\n }\n\n return this.axiosResponseObject.response\n }\n\n // setHeaders will merge the new headers with the current ones, overwriting if\n // the same key exists. The value will use nullOrValue to properly map strings to literals ('null' => null)\n public setHeaders(newHeaders: object, reset: boolean = false, preserveValueType: boolean = false): void {\n if (reset) {\n this.headers = {}\n }\n\n Object.entries(newHeaders).forEach((h) => {\n let value = h[1]\n if (!preserveValueType) {\n value = nullOrValue(value)\n }\n return Object.assign(this.headers, {\n [h[0]]: value,\n })\n })\n }\n\n public deleteHeader(key: string) {\n delete this.headers[key]\n }\n\n private fail(error: AxiosError, abortOnError: boolean): AxiosError {\n if (abortOnError) {\n this.beautifyError(error)\n expect.fail(error.message)\n }\n\n return error\n }\n\n private beautifyError(error: any): void {\n if (error.response) {\n Logger.error(\n `Failed requesting ${this.requestMethod.toUpperCase()} ${process.env.CTF_BASE_URL}${this.requestPath}`,\n )\n\n if (error.response?.data?.errors !== undefined) {\n Logger.error(error.response?.data?.errors)\n }\n } else {\n Logger.error('[FATAL ERROR]', error, '\\n')\n }\n }\n\n // eslint-disable-next-line class-methods-use-this\n private isEmpty(response: AxiosResponse): boolean {\n if (!response.data) {\n return true\n }\n\n if (Array.isArray(response.data.data)) {\n if (response.data.data.length === 0) {\n return true\n }\n } else if (Object.keys(response.data.data).length === 0) {\n return true\n }\n\n return false\n }\n}\n\nexport default Api\n"]}
|