@avalabs/glacier-sdk 2.4.1-alpha.6 → 2.4.1-alpha.7

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.
@@ -0,0 +1,250 @@
1
+ /* eslint-disable */
2
+ /* tslint:disable */
3
+ /*
4
+ * ---------------------------------------------------------------
5
+ * ## THIS FILE WAS GENERATED VIA SWAGGER-TYPESCRIPT-API ##
6
+ * ## ##
7
+ * ## AUTHOR: acacode ##
8
+ * ## SOURCE: https://github.com/acacode/swagger-typescript-api ##
9
+ * ---------------------------------------------------------------
10
+ */
11
+
12
+ export type QueryParamsType = Record<string | number, any>;
13
+ export type ResponseFormat = keyof Omit<Body, 'body' | 'bodyUsed'>;
14
+
15
+ export interface FullRequestParams extends Omit<RequestInit, 'body'> {
16
+ /** set parameter to `true` for call `securityWorker` for this request */
17
+ secure?: boolean;
18
+ /** request path */
19
+ path: string;
20
+ /** content type of request body */
21
+ type?: ContentType;
22
+ /** query params */
23
+ query?: QueryParamsType;
24
+ /** format of response (i.e. response.json() -> format: "json") */
25
+ format?: ResponseFormat;
26
+ /** request body */
27
+ body?: unknown;
28
+ /** base url */
29
+ baseUrl?: string;
30
+ /** request cancellation token */
31
+ cancelToken?: CancelToken;
32
+ }
33
+
34
+ export type RequestParams = Omit<
35
+ FullRequestParams,
36
+ 'body' | 'method' | 'query' | 'path'
37
+ >;
38
+
39
+ export interface ApiConfig<SecurityDataType = unknown> {
40
+ baseUrl?: string;
41
+ baseApiParams?: Omit<RequestParams, 'baseUrl' | 'cancelToken' | 'signal'>;
42
+ securityWorker?: (
43
+ securityData: SecurityDataType | null
44
+ ) => Promise<RequestParams | void> | RequestParams | void;
45
+ customFetch?: typeof fetch;
46
+ }
47
+
48
+ export interface HttpResponse<D extends unknown, E extends unknown = unknown>
49
+ extends Response {
50
+ data: D;
51
+ error: E;
52
+ }
53
+
54
+ type CancelToken = Symbol | string | number;
55
+
56
+ export enum ContentType {
57
+ Json = 'application/json',
58
+ FormData = 'multipart/form-data',
59
+ UrlEncoded = 'application/x-www-form-urlencoded',
60
+ }
61
+
62
+ export class HttpClient<SecurityDataType = unknown> {
63
+ public baseUrl: string = '';
64
+ private securityData: SecurityDataType | null = null;
65
+ private securityWorker?: ApiConfig<SecurityDataType>['securityWorker'];
66
+ private abortControllers = new Map<CancelToken, AbortController>();
67
+ private customFetch = (...fetchParams: Parameters<typeof fetch>) =>
68
+ fetch(...fetchParams);
69
+
70
+ private baseApiParams: RequestParams = {
71
+ credentials: 'same-origin',
72
+ headers: {},
73
+ redirect: 'follow',
74
+ referrerPolicy: 'no-referrer',
75
+ };
76
+
77
+ constructor(apiConfig: ApiConfig<SecurityDataType> = {}) {
78
+ Object.assign(this, apiConfig);
79
+ }
80
+
81
+ public setSecurityData = (data: SecurityDataType | null) => {
82
+ this.securityData = data;
83
+ };
84
+
85
+ private encodeQueryParam(key: string, value: any) {
86
+ const encodedKey = encodeURIComponent(key);
87
+ return `${encodedKey}=${encodeURIComponent(
88
+ typeof value === 'number' ? value : `${value}`
89
+ )}`;
90
+ }
91
+
92
+ private addQueryParam(query: QueryParamsType, key: string) {
93
+ return this.encodeQueryParam(key, query[key]);
94
+ }
95
+
96
+ private addArrayQueryParam(query: QueryParamsType, key: string) {
97
+ const value = query[key];
98
+ return value.map((v: any) => this.encodeQueryParam(key, v)).join('&');
99
+ }
100
+
101
+ protected toQueryString(rawQuery?: QueryParamsType): string {
102
+ const query = rawQuery || {};
103
+ const keys = Object.keys(query).filter(
104
+ (key) => 'undefined' !== typeof query[key]
105
+ );
106
+ return keys
107
+ .map((key) =>
108
+ Array.isArray(query[key])
109
+ ? this.addArrayQueryParam(query, key)
110
+ : this.addQueryParam(query, key)
111
+ )
112
+ .join('&');
113
+ }
114
+
115
+ protected addQueryParams(rawQuery?: QueryParamsType): string {
116
+ const queryString = this.toQueryString(rawQuery);
117
+ return queryString ? `?${queryString}` : '';
118
+ }
119
+
120
+ private contentFormatters: Record<ContentType, (input: any) => any> = {
121
+ [ContentType.Json]: (input: any) =>
122
+ input !== null && (typeof input === 'object' || typeof input === 'string')
123
+ ? JSON.stringify(input)
124
+ : input,
125
+ [ContentType.FormData]: (input: any) =>
126
+ Object.keys(input || {}).reduce((formData, key) => {
127
+ const property = input[key];
128
+ formData.append(
129
+ key,
130
+ property instanceof Blob
131
+ ? property
132
+ : typeof property === 'object' && property !== null
133
+ ? JSON.stringify(property)
134
+ : `${property}`
135
+ );
136
+ return formData;
137
+ }, new FormData()),
138
+ [ContentType.UrlEncoded]: (input: any) => this.toQueryString(input),
139
+ };
140
+
141
+ private mergeRequestParams(
142
+ params1: RequestParams,
143
+ params2?: RequestParams
144
+ ): RequestParams {
145
+ return {
146
+ ...this.baseApiParams,
147
+ ...params1,
148
+ ...(params2 || {}),
149
+ headers: {
150
+ ...(this.baseApiParams.headers || {}),
151
+ ...(params1.headers || {}),
152
+ ...((params2 && params2.headers) || {}),
153
+ },
154
+ };
155
+ }
156
+
157
+ private createAbortSignal = (
158
+ cancelToken: CancelToken
159
+ ): AbortSignal | undefined => {
160
+ if (this.abortControllers.has(cancelToken)) {
161
+ const abortController = this.abortControllers.get(cancelToken);
162
+ if (abortController) {
163
+ return abortController.signal;
164
+ }
165
+ return void 0;
166
+ }
167
+
168
+ const abortController = new AbortController();
169
+ this.abortControllers.set(cancelToken, abortController);
170
+ return abortController.signal;
171
+ };
172
+
173
+ public abortRequest = (cancelToken: CancelToken) => {
174
+ const abortController = this.abortControllers.get(cancelToken);
175
+
176
+ if (abortController) {
177
+ abortController.abort();
178
+ this.abortControllers.delete(cancelToken);
179
+ }
180
+ };
181
+
182
+ public request = async <T = any, E = any>({
183
+ body,
184
+ secure,
185
+ path,
186
+ type,
187
+ query,
188
+ format,
189
+ baseUrl,
190
+ cancelToken,
191
+ ...params
192
+ }: FullRequestParams): Promise<HttpResponse<T, E>> => {
193
+ const secureParams =
194
+ ((typeof secure === 'boolean' ? secure : this.baseApiParams.secure) &&
195
+ this.securityWorker &&
196
+ (await this.securityWorker(this.securityData))) ||
197
+ {};
198
+ const requestParams = this.mergeRequestParams(params, secureParams);
199
+ const queryString = query && this.toQueryString(query);
200
+ const payloadFormatter = this.contentFormatters[type || ContentType.Json];
201
+ const responseFormat = format || requestParams.format;
202
+
203
+ return this.customFetch(
204
+ `${baseUrl || this.baseUrl || ''}${path}${
205
+ queryString ? `?${queryString}` : ''
206
+ }`,
207
+ {
208
+ ...requestParams,
209
+ headers: {
210
+ ...(type && type !== ContentType.FormData
211
+ ? { 'Content-Type': type }
212
+ : {}),
213
+ ...(requestParams.headers || {}),
214
+ },
215
+ signal: cancelToken ? this.createAbortSignal(cancelToken) : void 0,
216
+ body:
217
+ typeof body === 'undefined' || body === null
218
+ ? null
219
+ : payloadFormatter(body),
220
+ }
221
+ ).then(async (response) => {
222
+ const r = response as HttpResponse<T, E>;
223
+ r.data = null as unknown as T;
224
+ r.error = null as unknown as E;
225
+
226
+ const data = !responseFormat
227
+ ? r
228
+ : await response[responseFormat]()
229
+ .then((data) => {
230
+ if (r.ok) {
231
+ r.data = data;
232
+ } else {
233
+ r.error = data;
234
+ }
235
+ return r;
236
+ })
237
+ .catch((e) => {
238
+ r.error = e;
239
+ return r;
240
+ });
241
+
242
+ if (cancelToken) {
243
+ this.abortControllers.delete(cancelToken);
244
+ }
245
+
246
+ if (!response.ok) throw data;
247
+ return data;
248
+ });
249
+ };
250
+ }