@mapcreator/api 0.0.0-saga.1 → 0.0.0-saga.3

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.
Files changed (90) hide show
  1. package/cjs/api/choropleth.d.ts +3 -3
  2. package/cjs/api/choropleth.d.ts.map +1 -1
  3. package/cjs/api/choropleth.js +17 -4
  4. package/cjs/api/choropleth.js.map +1 -1
  5. package/cjs/api/insetMap.d.ts +37 -0
  6. package/cjs/api/insetMap.d.ts.map +1 -0
  7. package/cjs/api/insetMap.js +49 -0
  8. package/cjs/api/insetMap.js.map +1 -0
  9. package/cjs/api/job.d.ts +1 -0
  10. package/cjs/api/job.d.ts.map +1 -1
  11. package/cjs/api/job.js.map +1 -1
  12. package/cjs/api/jobResult.d.ts +1 -1
  13. package/cjs/api/jobResult.d.ts.map +1 -1
  14. package/cjs/api/jobResult.js +1 -1
  15. package/cjs/api/jobResult.js.map +1 -1
  16. package/cjs/api/jobRevision.d.ts +2 -2
  17. package/cjs/api/jobRevision.d.ts.map +1 -1
  18. package/cjs/api/jobRevision.js +1 -1
  19. package/cjs/api/jobRevision.js.map +1 -1
  20. package/cjs/api/organisation.d.ts +2 -1
  21. package/cjs/api/organisation.d.ts.map +1 -1
  22. package/cjs/api/organisation.js +1 -2
  23. package/cjs/api/organisation.js.map +1 -1
  24. package/cjs/api/resources.d.ts +1 -1
  25. package/cjs/api/resources.d.ts.map +1 -1
  26. package/cjs/api/resources.js.map +1 -1
  27. package/cjs/index.d.ts +1 -0
  28. package/cjs/index.d.ts.map +1 -1
  29. package/cjs/index.js +1 -0
  30. package/cjs/index.js.map +1 -1
  31. package/esm/api/choropleth.d.ts +3 -3
  32. package/esm/api/choropleth.d.ts.map +1 -1
  33. package/esm/api/choropleth.js +17 -4
  34. package/esm/api/choropleth.js.map +1 -1
  35. package/esm/api/insetMap.d.ts +37 -0
  36. package/esm/api/insetMap.d.ts.map +1 -0
  37. package/esm/api/insetMap.js +44 -0
  38. package/esm/api/insetMap.js.map +1 -0
  39. package/esm/api/job.d.ts +1 -0
  40. package/esm/api/job.d.ts.map +1 -1
  41. package/esm/api/job.js.map +1 -1
  42. package/esm/api/jobResult.d.ts +1 -1
  43. package/esm/api/jobResult.d.ts.map +1 -1
  44. package/esm/api/jobResult.js +1 -1
  45. package/esm/api/jobResult.js.map +1 -1
  46. package/esm/api/jobRevision.d.ts +2 -2
  47. package/esm/api/jobRevision.d.ts.map +1 -1
  48. package/esm/api/jobRevision.js +1 -1
  49. package/esm/api/jobRevision.js.map +1 -1
  50. package/esm/api/organisation.d.ts +2 -1
  51. package/esm/api/organisation.d.ts.map +1 -1
  52. package/esm/api/organisation.js +1 -2
  53. package/esm/api/organisation.js.map +1 -1
  54. package/esm/api/resources.d.ts +1 -1
  55. package/esm/api/resources.d.ts.map +1 -1
  56. package/esm/api/resources.js.map +1 -1
  57. package/esm/index.d.ts +1 -0
  58. package/esm/index.d.ts.map +1 -1
  59. package/esm/index.js +1 -0
  60. package/esm/index.js.map +1 -1
  61. package/package.json +2 -2
  62. package/src/api/apiCommon.ts +70 -70
  63. package/src/api/choropleth.ts +125 -105
  64. package/src/api/color.ts +22 -22
  65. package/src/api/dimension.ts +44 -44
  66. package/src/api/dimensionSet.ts +20 -20
  67. package/src/api/feature.ts +22 -22
  68. package/src/api/font.ts +49 -49
  69. package/src/api/fontFamily.ts +43 -43
  70. package/src/api/highlight.ts +87 -87
  71. package/src/api/insetMap.ts +96 -0
  72. package/src/api/job.ts +130 -129
  73. package/src/api/jobResult.ts +95 -95
  74. package/src/api/jobRevision.ts +279 -278
  75. package/src/api/jobShare.ts +35 -35
  76. package/src/api/jobType.ts +26 -26
  77. package/src/api/language.ts +19 -19
  78. package/src/api/layer.ts +38 -38
  79. package/src/api/layerFaq.ts +53 -53
  80. package/src/api/layerGroup.ts +69 -69
  81. package/src/api/mapstyleSet.ts +48 -48
  82. package/src/api/message.ts +80 -80
  83. package/src/api/organisation.ts +95 -95
  84. package/src/api/resources.ts +145 -143
  85. package/src/api/svg.ts +33 -33
  86. package/src/api/svgSet.ts +56 -56
  87. package/src/api/user.ts +327 -327
  88. package/src/index.ts +43 -42
  89. package/src/oauth.ts +314 -314
  90. package/src/utils.ts +342 -342
package/src/utils.ts CHANGED
@@ -1,342 +1,342 @@
1
- import type { CamelCasedProperties, SnakeCasedProperties } from 'type-fest';
2
- import { apiHost, authenticate, token } from './oauth.js';
3
-
4
- export type Flatten<Type> = Type extends Array<infer Item> ? Item : Type;
5
-
6
- export type ApiCommonData = {
7
- created_at: string | null;
8
- deleted_at: string | null;
9
- updated_at: string | null;
10
- };
11
-
12
- export interface ApiSuccess {
13
- success: true;
14
- data?: Record<string, unknown> | Array<Record<string, unknown>> | string | string[];
15
- }
16
-
17
- export interface ApiError {
18
- success: false;
19
- error: {
20
- type: string;
21
- message: string;
22
- validation_errors?: Record<string, string[]>;
23
- // schema_errors?: Record<string, unknown>[];
24
- };
25
- }
26
-
27
- export type ApiCommon = ApiSuccess | ApiError;
28
-
29
- export const myUser = 'me';
30
- export const myOrganisations = 'mine';
31
- export const lastJobRevision = 'last';
32
-
33
- export const defaultListHeader = { 'X-Per-Page': '50' };
34
- export const deletedNoneParam = getSearchParams({ deleted: 'none' });
35
-
36
- export const keysToRemove = ['created_at', 'deleted_at', 'updated_at'];
37
- export const staticContext = { keysToRemove: new Set(keysToRemove), keysToAdd: [], revivers: {} };
38
-
39
- // class AuthorizationError extends Error {}
40
- export class NetworkError extends Error {}
41
-
42
- export class HTTPError extends Error {
43
- statusCode: number;
44
- constructor(response: Response) {
45
- super(response.statusText);
46
- this.statusCode = response.status;
47
- }
48
- }
49
-
50
- export class APIError extends Error {
51
- code: string;
52
- constructor(apiError: ApiError) {
53
- super(apiError.error.message);
54
- this.code = apiError.error.type;
55
- }
56
- }
57
-
58
- export function getSearchParams(search: Record<string, unknown>): URLSearchParams {
59
- return new URLSearchParams(
60
- Object.entries(search).reduce<string[][]>((arr, [key, value]) => {
61
- if (Array.isArray(value)) {
62
- arr.push(...value.map(val => [`${key}[]`, String(val)]));
63
- } else if (value !== null && typeof value === 'object') {
64
- const params = getSearchParams(value as Record<string, unknown>);
65
-
66
- arr.push(
67
- ...Array.from(params.entries()).map(([subKey, val]) => [
68
- ~subKey.indexOf('[')
69
- ? `${key}[${subKey.slice(0, subKey.indexOf('['))}]${subKey.slice(subKey.indexOf('['))}`
70
- : `${key}[${subKey}]`,
71
- val,
72
- ]),
73
- );
74
- } else {
75
- arr.push([key, String(value)]);
76
- }
77
-
78
- return arr;
79
- }, []),
80
- );
81
- }
82
-
83
- export type Revivers<
84
- I extends ApiCommon,
85
- O extends Record<string, unknown>,
86
- R = Exclude<Flatten<Exclude<Exclude<I, ApiError>['data'], string | undefined>>, string>,
87
- > = {
88
- [K in keyof O | keyof R]?: K extends keyof O & keyof R
89
- ? ((data: R) => O[K]) | undefined
90
- : K extends Exclude<keyof O, keyof R>
91
- ? (data: R) => O[K]
92
- : K extends Exclude<keyof R, keyof O>
93
- ? undefined
94
- : never;
95
- };
96
- export interface ExtraOptions<I extends ApiCommon, O extends Record<string, unknown> | string> {
97
- method?: 'GET' | 'HEAD' | 'POST' | 'PUT' | 'DELETE' | 'PATCH'; // 'CONNECT' | 'OPTIONS' | 'TRACE'
98
- revivers?: O extends Record<string, unknown> ? Revivers<I, O> : never;
99
- sendNull?: boolean;
100
- withMeta?: boolean;
101
- }
102
-
103
- export class APIMeta<
104
- I extends ApiCommon,
105
- O extends Record<string, unknown> | string,
106
- A = Exclude<I, ApiError>['data'] extends unknown[] ? true : false,
107
- > extends Error {
108
- data: A extends true ? O[] : O;
109
- status: number;
110
- headers: Headers;
111
-
112
- constructor(data: A extends true ? O[] : O, headers: Headers, status: number) {
113
- super();
114
-
115
- this.data = data;
116
- this.status = status;
117
- this.headers = headers;
118
- }
119
- }
120
-
121
- export async function request<
122
- I extends ApiCommon,
123
- O extends Record<string, unknown> | string,
124
- A = Exclude<I, ApiError>['data'] extends unknown[] ? true : false,
125
- >(
126
- path: string,
127
- body?: XMLHttpRequestBodyInit | Record<string | number, unknown> | null,
128
- extraHeaders?: Record<string, string> | null,
129
- extraOptions?: ExtraOptions<I, O>,
130
- ): Promise<A extends true ? O[] : O> {
131
- type R = A extends true ? O[] : O;
132
-
133
- const href = `${apiHost}${path}`;
134
- const init = getRequestInit(body, extraHeaders, extraOptions);
135
-
136
- const response = await fetch(href, init).catch((error: Error) => {
137
- throw new NetworkError(error?.message ?? error);
138
- });
139
-
140
- if (response.ok) {
141
- const json = (await response.json().catch(() => ({
142
- success: false,
143
- error: { type: 'SyntaxError', message: 'Malformed JSON response' },
144
- }))) as I;
145
-
146
- if (json.success) {
147
- const { data } = json;
148
-
149
- const withMeta = extraOptions?.withMeta;
150
- const revivers = extraOptions?.revivers;
151
- const keys = revivers ? Object.keys(revivers) : [];
152
-
153
- const context =
154
- revivers && keys.length
155
- ? getContext(revivers as Revivers<I, Record<string, unknown>>, keys)
156
- : staticContext;
157
-
158
- let result: R;
159
-
160
- if (Array.isArray(data)) {
161
- let moreData = [] as unknown as R;
162
- const headers = response.headers;
163
- // @ts-expect-error TS2362, get valid number or 0
164
- const total = headers.get('X-Paginate-Total') | 0;
165
-
166
- if (!withMeta && ((data.length && total > data.length) || response.status === 206)) {
167
- // @ts-expect-error TS2362, get valid number or 0
168
- const offset = `${(headers.get('X-Paginate-Offset') | 0) + data.length}`;
169
-
170
- moreData = await request<I, O, A>(
171
- path, body, { ...extraHeaders, 'X-Offset': offset }, extraOptions,
172
- );
173
- }
174
-
175
- if (data.length) {
176
- result = (
177
- typeof data[0] !== 'string'
178
- ? (data as Array<Record<string, unknown>>).map(processData, context)
179
- : data
180
- ).concat(moreData) as R;
181
- } else {
182
- result = moreData;
183
- }
184
- } else {
185
- if (data !== undefined) {
186
- // @ts-expect-error TS2345, TODO: fix type error for the `context` object
187
- result = (typeof data !== 'string' ? processData.call(context, data) : data) as R;
188
- } else {
189
- result = {} as R;
190
- }
191
- }
192
-
193
- if (withMeta) {
194
- throw new APIMeta<I, O, A>(result, response.headers, response.status);
195
- }
196
-
197
- return result;
198
- } else {
199
- throw new APIError(json);
200
- }
201
- } else {
202
- // TODO: parse and process all typical errors
203
- // eslint-disable-next-line default-case
204
- switch (response.status) {
205
- case 401:
206
- await authenticate();
207
- break; // NO-OP
208
- case 403:
209
- case 404:
210
- case 406:
211
- throw new APIError(
212
- (await response.json().catch(() => ({
213
- success: false,
214
- error: { type: 'HttpException', message: response.statusText },
215
- }))) as ApiError,
216
- );
217
- case 429:
218
- await new Promise(
219
- (
220
- resolve, // @ts-expect-error TS2531
221
- ) => { setTimeout(resolve, response.headers.get('X-RateLimit-Reset') * 1000 || 500) },
222
- );
223
-
224
- return request<I, O, A>(path, body, extraHeaders, extraOptions);
225
- }
226
-
227
- throw new HTTPError(response);
228
- }
229
- }
230
-
231
- function getRequestInit<I extends ApiCommon, O extends Record<string, unknown>>(
232
- body?: XMLHttpRequestBodyInit | Record<string | number, unknown> | null,
233
- extraHeaders?: Record<string, string> | null,
234
- extraOptions?: ExtraOptions<I, O>,
235
- ): RequestInit {
236
- const authorization = token ? { Authorization: token.toString() } : null;
237
- let contentType = null as { 'Content-Type': string } | null;
238
-
239
- if (body !== undefined) {
240
- switch (true) {
241
- case typeof body === 'string':
242
- contentType = { 'Content-Type': 'text/plain' };
243
- break;
244
- case body instanceof FormData: // Autofilled with 'multipart/form-data'
245
- case body instanceof URLSearchParams: // Autofilled with 'application/x-www-form-urlencoded'
246
- break;
247
- case body instanceof Blob: // File is instance of Blob too
248
- contentType = { 'Content-Type': body.type || 'application/octet-stream' };
249
- break;
250
- case body instanceof ArrayBuffer:
251
- case ArrayBuffer.isView(body):
252
- contentType = { 'Content-Type': 'application/octet-stream' };
253
- break;
254
- case body === null && !extraOptions?.sendNull:
255
- break;
256
- default:
257
- contentType = { 'Content-Type': 'application/json' };
258
- body = JSON.stringify(body);
259
- }
260
- }
261
-
262
- const headers = { Accept: 'application/json', ...authorization, ...contentType, ...extraHeaders };
263
- const method = extraOptions?.method ?? (body != null ? 'POST' : 'GET'); // don't touch `!=` please
264
-
265
- return { body, headers, method } as RequestInit;
266
- }
267
-
268
- interface Context<I extends ApiCommon, O extends Record<string, unknown>> {
269
- keysToRemove: Set<string>;
270
- keysToAdd: string[];
271
- revivers: Revivers<I, O>;
272
- }
273
-
274
- export function getContext<I extends ApiCommon, O extends Record<string, unknown>>(
275
- revivers: Revivers<I, O>,
276
- keys = Object.keys(revivers),
277
- ): Context<I, O> {
278
- return {
279
- keysToRemove: new Set(keys.filter(isUndefined, revivers).concat(keysToRemove)),
280
- keysToAdd: keys.filter(isReviver, revivers),
281
- revivers,
282
- };
283
- }
284
-
285
- export function processData<I extends ApiCommon, O extends Record<string, unknown>>(
286
- this: Context<I, O>,
287
- data: Exclude<Flatten<Exclude<Exclude<I, ApiError>['data'], string | undefined>>, string>,
288
- ): O { /* eslint-disable @typescript-eslint/prefer-for-of */
289
- const result = {} as Record<string, unknown>;
290
-
291
- const keys = Object.keys(data).filter(isPreserved, this.keysToRemove);
292
-
293
- for (let i = 0; i < keys.length; ++i) {
294
- result[toCamelCase(keys[i])] = data[keys[i]];
295
- }
296
-
297
- if (this.keysToAdd.length) {
298
- const keys = this.keysToAdd;
299
-
300
- for (let i = 0; i < keys.length; ++i) {
301
- result[keys[i]] = this.revivers[keys[i]]!(data);
302
- }
303
- }
304
-
305
- return result as O;
306
- } /* eslint-enable @typescript-eslint/prefer-for-of */
307
-
308
- export function toApiType<T extends Record<string, unknown>>(body: T): SnakeCasedProperties<T> {
309
- return Object.fromEntries(
310
- Object.entries(body).map(([k, v]) => [toSnakeCase(k), v]),
311
- ) as SnakeCasedProperties<T>;
312
- }
313
-
314
- export function toAppType<T extends Record<string, unknown>>(body: T): CamelCasedProperties<T> {
315
- return Object.fromEntries(
316
- Object.entries(body).map(([k, v]) => [toCamelCase(k), v]),
317
- ) as CamelCasedProperties<T>;
318
- }
319
-
320
- export function ensureArray<T extends Record<string, unknown>>(data: T | T[]): T[] {
321
- return Array.isArray(data) ? data : [data];
322
- }
323
-
324
- function toCamelCase(key: string): string {
325
- return key.replace(/_+(.)/g, (_, c: string) => c.toUpperCase());
326
- }
327
-
328
- function toSnakeCase(key: string): string {
329
- return key.replace(/[A-Z]/g, '_$&').toLowerCase();
330
- }
331
-
332
- function isUndefined(this: Record<string, unknown>, key: string): boolean {
333
- return this[key] === undefined;
334
- }
335
-
336
- function isReviver(this: Record<string, unknown>, key: string): boolean {
337
- return this[key] !== undefined;
338
- }
339
-
340
- function isPreserved(this: Set<string>, key: string): boolean {
341
- return !this.has(key);
342
- }
1
+ import type { CamelCasedProperties, SnakeCasedProperties } from 'type-fest';
2
+ import { apiHost, authenticate, token } from './oauth.js';
3
+
4
+ export type Flatten<Type> = Type extends Array<infer Item> ? Item : Type;
5
+
6
+ export type ApiCommonData = {
7
+ created_at: string | null;
8
+ deleted_at: string | null;
9
+ updated_at: string | null;
10
+ };
11
+
12
+ export interface ApiSuccess {
13
+ success: true;
14
+ data?: Record<string, unknown> | Array<Record<string, unknown>> | string | string[];
15
+ }
16
+
17
+ export interface ApiError {
18
+ success: false;
19
+ error: {
20
+ type: string;
21
+ message: string;
22
+ validation_errors?: Record<string, string[]>;
23
+ // schema_errors?: Record<string, unknown>[];
24
+ };
25
+ }
26
+
27
+ export type ApiCommon = ApiSuccess | ApiError;
28
+
29
+ export const myUser = 'me';
30
+ export const myOrganisations = 'mine';
31
+ export const lastJobRevision = 'last';
32
+
33
+ export const defaultListHeader = { 'X-Per-Page': '50' };
34
+ export const deletedNoneParam = getSearchParams({ deleted: 'none' });
35
+
36
+ export const keysToRemove = ['created_at', 'deleted_at', 'updated_at'];
37
+ export const staticContext = { keysToRemove: new Set(keysToRemove), keysToAdd: [], revivers: {} };
38
+
39
+ // class AuthorizationError extends Error {}
40
+ export class NetworkError extends Error {}
41
+
42
+ export class HTTPError extends Error {
43
+ statusCode: number;
44
+ constructor(response: Response) {
45
+ super(response.statusText);
46
+ this.statusCode = response.status;
47
+ }
48
+ }
49
+
50
+ export class APIError extends Error {
51
+ code: string;
52
+ constructor(apiError: ApiError) {
53
+ super(apiError.error.message);
54
+ this.code = apiError.error.type;
55
+ }
56
+ }
57
+
58
+ export function getSearchParams(search: Record<string, unknown>): URLSearchParams {
59
+ return new URLSearchParams(
60
+ Object.entries(search).reduce<string[][]>((arr, [key, value]) => {
61
+ if (Array.isArray(value)) {
62
+ arr.push(...value.map(val => [`${key}[]`, String(val)]));
63
+ } else if (value !== null && typeof value === 'object') {
64
+ const params = getSearchParams(value as Record<string, unknown>);
65
+
66
+ arr.push(
67
+ ...Array.from(params.entries()).map(([subKey, val]) => [
68
+ ~subKey.indexOf('[')
69
+ ? `${key}[${subKey.slice(0, subKey.indexOf('['))}]${subKey.slice(subKey.indexOf('['))}`
70
+ : `${key}[${subKey}]`,
71
+ val,
72
+ ]),
73
+ );
74
+ } else {
75
+ arr.push([key, String(value)]);
76
+ }
77
+
78
+ return arr;
79
+ }, []),
80
+ );
81
+ }
82
+
83
+ export type Revivers<
84
+ I extends ApiCommon,
85
+ O extends Record<string, unknown>,
86
+ R = Exclude<Flatten<Exclude<Exclude<I, ApiError>['data'], string | undefined>>, string>,
87
+ > = {
88
+ [K in keyof O | keyof R]?: K extends keyof O & keyof R
89
+ ? ((data: R) => O[K]) | undefined
90
+ : K extends Exclude<keyof O, keyof R>
91
+ ? (data: R) => O[K]
92
+ : K extends Exclude<keyof R, keyof O>
93
+ ? undefined
94
+ : never;
95
+ };
96
+ export interface ExtraOptions<I extends ApiCommon, O extends Record<string, unknown> | string> {
97
+ method?: 'GET' | 'HEAD' | 'POST' | 'PUT' | 'DELETE' | 'PATCH'; // 'CONNECT' | 'OPTIONS' | 'TRACE'
98
+ revivers?: O extends Record<string, unknown> ? Revivers<I, O> : never;
99
+ sendNull?: boolean;
100
+ withMeta?: boolean;
101
+ }
102
+
103
+ export class APIMeta<
104
+ I extends ApiCommon,
105
+ O extends Record<string, unknown> | string,
106
+ A = Exclude<I, ApiError>['data'] extends unknown[] ? true : false,
107
+ > extends Error {
108
+ data: A extends true ? O[] : O;
109
+ status: number;
110
+ headers: Headers;
111
+
112
+ constructor(data: A extends true ? O[] : O, headers: Headers, status: number) {
113
+ super();
114
+
115
+ this.data = data;
116
+ this.status = status;
117
+ this.headers = headers;
118
+ }
119
+ }
120
+
121
+ export async function request<
122
+ I extends ApiCommon,
123
+ O extends Record<string, unknown> | string,
124
+ A = Exclude<I, ApiError>['data'] extends unknown[] ? true : false,
125
+ >(
126
+ path: string,
127
+ body?: XMLHttpRequestBodyInit | Record<string | number, unknown> | null,
128
+ extraHeaders?: Record<string, string> | null,
129
+ extraOptions?: ExtraOptions<I, O>,
130
+ ): Promise<A extends true ? O[] : O> {
131
+ type R = A extends true ? O[] : O;
132
+
133
+ const href = `${apiHost}${path}`;
134
+ const init = getRequestInit(body, extraHeaders, extraOptions);
135
+
136
+ const response = await fetch(href, init).catch((error: Error) => {
137
+ throw new NetworkError(error?.message ?? error);
138
+ });
139
+
140
+ if (response.ok) {
141
+ const json = (await response.json().catch(() => ({
142
+ success: false,
143
+ error: { type: 'SyntaxError', message: 'Malformed JSON response' },
144
+ }))) as I;
145
+
146
+ if (json.success) {
147
+ const { data } = json;
148
+
149
+ const withMeta = extraOptions?.withMeta;
150
+ const revivers = extraOptions?.revivers;
151
+ const keys = revivers ? Object.keys(revivers) : [];
152
+
153
+ const context =
154
+ revivers && keys.length
155
+ ? getContext(revivers as Revivers<I, Record<string, unknown>>, keys)
156
+ : staticContext;
157
+
158
+ let result: R;
159
+
160
+ if (Array.isArray(data)) {
161
+ let moreData = [] as unknown as R;
162
+ const headers = response.headers;
163
+ // @ts-expect-error TS2362, get valid number or 0
164
+ const total = headers.get('X-Paginate-Total') | 0;
165
+
166
+ if (!withMeta && ((data.length && total > data.length) || response.status === 206)) {
167
+ // @ts-expect-error TS2362, get valid number or 0
168
+ const offset = `${(headers.get('X-Paginate-Offset') | 0) + data.length}`;
169
+
170
+ moreData = await request<I, O, A>(
171
+ path, body, { ...extraHeaders, 'X-Offset': offset }, extraOptions,
172
+ );
173
+ }
174
+
175
+ if (data.length) {
176
+ result = (
177
+ typeof data[0] !== 'string'
178
+ ? (data as Array<Record<string, unknown>>).map(processData, context)
179
+ : data
180
+ ).concat(moreData) as R;
181
+ } else {
182
+ result = moreData;
183
+ }
184
+ } else {
185
+ if (data !== undefined) {
186
+ // @ts-expect-error TS2345, TODO: fix type error for the `context` object
187
+ result = (typeof data !== 'string' ? processData.call(context, data) : data) as R;
188
+ } else {
189
+ result = {} as R;
190
+ }
191
+ }
192
+
193
+ if (withMeta) {
194
+ throw new APIMeta<I, O, A>(result, response.headers, response.status);
195
+ }
196
+
197
+ return result;
198
+ } else {
199
+ throw new APIError(json);
200
+ }
201
+ } else {
202
+ // TODO: parse and process all typical errors
203
+ // eslint-disable-next-line default-case
204
+ switch (response.status) {
205
+ case 401:
206
+ await authenticate();
207
+ break; // NO-OP
208
+ case 403:
209
+ case 404:
210
+ case 406:
211
+ throw new APIError(
212
+ (await response.json().catch(() => ({
213
+ success: false,
214
+ error: { type: 'HttpException', message: response.statusText },
215
+ }))) as ApiError,
216
+ );
217
+ case 429:
218
+ await new Promise(
219
+ (
220
+ resolve, // @ts-expect-error TS2531
221
+ ) => { setTimeout(resolve, response.headers.get('X-RateLimit-Reset') * 1000 || 500) },
222
+ );
223
+
224
+ return request<I, O, A>(path, body, extraHeaders, extraOptions);
225
+ }
226
+
227
+ throw new HTTPError(response);
228
+ }
229
+ }
230
+
231
+ function getRequestInit<I extends ApiCommon, O extends Record<string, unknown>>(
232
+ body?: XMLHttpRequestBodyInit | Record<string | number, unknown> | null,
233
+ extraHeaders?: Record<string, string> | null,
234
+ extraOptions?: ExtraOptions<I, O>,
235
+ ): RequestInit {
236
+ const authorization = token ? { Authorization: token.toString() } : null;
237
+ let contentType = null as { 'Content-Type': string } | null;
238
+
239
+ if (body !== undefined) {
240
+ switch (true) {
241
+ case typeof body === 'string':
242
+ contentType = { 'Content-Type': 'text/plain' };
243
+ break;
244
+ case body instanceof FormData: // Autofilled with 'multipart/form-data'
245
+ case body instanceof URLSearchParams: // Autofilled with 'application/x-www-form-urlencoded'
246
+ break;
247
+ case body instanceof Blob: // File is instance of Blob too
248
+ contentType = { 'Content-Type': body.type || 'application/octet-stream' };
249
+ break;
250
+ case body instanceof ArrayBuffer:
251
+ case ArrayBuffer.isView(body):
252
+ contentType = { 'Content-Type': 'application/octet-stream' };
253
+ break;
254
+ case body === null && !extraOptions?.sendNull:
255
+ break;
256
+ default:
257
+ contentType = { 'Content-Type': 'application/json' };
258
+ body = JSON.stringify(body);
259
+ }
260
+ }
261
+
262
+ const headers = { Accept: 'application/json', ...authorization, ...contentType, ...extraHeaders };
263
+ const method = extraOptions?.method ?? (body != null ? 'POST' : 'GET'); // don't touch `!=` please
264
+
265
+ return { body, headers, method } as RequestInit;
266
+ }
267
+
268
+ interface Context<I extends ApiCommon, O extends Record<string, unknown>> {
269
+ keysToRemove: Set<string>;
270
+ keysToAdd: string[];
271
+ revivers: Revivers<I, O>;
272
+ }
273
+
274
+ export function getContext<I extends ApiCommon, O extends Record<string, unknown>>(
275
+ revivers: Revivers<I, O>,
276
+ keys = Object.keys(revivers),
277
+ ): Context<I, O> {
278
+ return {
279
+ keysToRemove: new Set(keys.filter(isUndefined, revivers).concat(keysToRemove)),
280
+ keysToAdd: keys.filter(isReviver, revivers),
281
+ revivers,
282
+ };
283
+ }
284
+
285
+ export function processData<I extends ApiCommon, O extends Record<string, unknown>>(
286
+ this: Context<I, O>,
287
+ data: Exclude<Flatten<Exclude<Exclude<I, ApiError>['data'], string | undefined>>, string>,
288
+ ): O { /* eslint-disable @typescript-eslint/prefer-for-of */
289
+ const result = {} as Record<string, unknown>;
290
+
291
+ const keys = Object.keys(data).filter(isPreserved, this.keysToRemove);
292
+
293
+ for (let i = 0; i < keys.length; ++i) {
294
+ result[toCamelCase(keys[i])] = data[keys[i]];
295
+ }
296
+
297
+ if (this.keysToAdd.length) {
298
+ const keys = this.keysToAdd;
299
+
300
+ for (let i = 0; i < keys.length; ++i) {
301
+ result[keys[i]] = this.revivers[keys[i]]!(data);
302
+ }
303
+ }
304
+
305
+ return result as O;
306
+ } /* eslint-enable @typescript-eslint/prefer-for-of */
307
+
308
+ export function toApiType<T extends Record<string, unknown>>(body: T): SnakeCasedProperties<T> {
309
+ return Object.fromEntries(
310
+ Object.entries(body).map(([k, v]) => [toSnakeCase(k), v]),
311
+ ) as SnakeCasedProperties<T>;
312
+ }
313
+
314
+ export function toAppType<T extends Record<string, unknown>>(body: T): CamelCasedProperties<T> {
315
+ return Object.fromEntries(
316
+ Object.entries(body).map(([k, v]) => [toCamelCase(k), v]),
317
+ ) as CamelCasedProperties<T>;
318
+ }
319
+
320
+ export function ensureArray<T extends Record<string, unknown>>(data: T | T[]): T[] {
321
+ return Array.isArray(data) ? data : [data];
322
+ }
323
+
324
+ function toCamelCase(key: string): string {
325
+ return key.replace(/_+(.)/g, (_, c: string) => c.toUpperCase());
326
+ }
327
+
328
+ function toSnakeCase(key: string): string {
329
+ return key.replace(/[A-Z]/g, '_$&').toLowerCase();
330
+ }
331
+
332
+ function isUndefined(this: Record<string, unknown>, key: string): boolean {
333
+ return this[key] === undefined;
334
+ }
335
+
336
+ function isReviver(this: Record<string, unknown>, key: string): boolean {
337
+ return this[key] !== undefined;
338
+ }
339
+
340
+ function isPreserved(this: Set<string>, key: string): boolean {
341
+ return !this.has(key);
342
+ }