getta 1.0.5 → 1.0.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.
Files changed (32) hide show
  1. package/dist/cjs/index.cjs +1 -664
  2. package/dist/cjs/index.cjs.map +1 -1
  3. package/dist/esm/index.mjs +1 -620
  4. package/dist/esm/index.mjs.map +1 -1
  5. package/dist/production.analysis.txt +82 -0
  6. package/dist/types/cjs/constants.d.cts +33 -33
  7. package/dist/types/cjs/constants.d.cts.map +1 -1
  8. package/dist/types/cjs/helpers/defaultPathTemplateCallback/index.d.cts +1 -1
  9. package/dist/types/cjs/helpers/defaultPathTemplateCallback/index.d.cts.map +1 -1
  10. package/dist/types/cjs/main.d.cts +1 -1
  11. package/dist/types/cjs/main.d.cts.map +1 -1
  12. package/dist/types/cjs/types.d.cts +2 -4
  13. package/dist/types/cjs/types.d.cts.map +1 -1
  14. package/dist/types/esm/constants.d.ts +33 -33
  15. package/dist/types/esm/constants.d.ts.map +1 -1
  16. package/dist/types/esm/helpers/defaultPathTemplateCallback/index.d.ts +1 -1
  17. package/dist/types/esm/helpers/defaultPathTemplateCallback/index.d.ts.map +1 -1
  18. package/dist/types/esm/main.d.ts +1 -1
  19. package/dist/types/esm/main.d.ts.map +1 -1
  20. package/dist/types/esm/types.d.ts +2 -4
  21. package/dist/types/esm/types.d.ts.map +1 -1
  22. package/dist/types/tsconfig.build.tsbuildinfo +1 -1
  23. package/package.json +46 -90
  24. package/shellScripts/installActivateMise.sh +12 -0
  25. package/src/__testUtils__/helpers/index.ts +1 -1
  26. package/src/constants.ts +27 -27
  27. package/src/helpers/buildEndpoint/index.ts +1 -1
  28. package/src/helpers/defaultPathTemplateCallback/index.test.ts +7 -1
  29. package/src/helpers/defaultPathTemplateCallback/index.ts +4 -4
  30. package/src/main.test.ts +27 -24
  31. package/src/main.ts +45 -35
  32. package/src/types.ts +6 -4
package/src/main.ts CHANGED
@@ -2,7 +2,7 @@ import { type CacheHeaders, type Core } from '@cachemap/core';
2
2
  import { type Cacheability } from 'cacheability';
3
3
  import { castArray, merge } from 'lodash-es';
4
4
  import { Md5 } from 'ts-md5';
5
- import type { SetRequired } from 'type-fest';
5
+ import { type SetRequired } from 'type-fest';
6
6
  import * as consts from './constants.ts';
7
7
  import { buildEndpoint } from './helpers/buildEndpoint/index.ts';
8
8
  import { defaultPathTemplateCallback } from './helpers/defaultPathTemplateCallback/index.ts';
@@ -107,7 +107,7 @@ export class Getta {
107
107
  public createShortcut(
108
108
  name: string,
109
109
  path: string,
110
- { method, ...otherOptions }: SetRequired<RequestOptions, 'method'>
110
+ { method, ...otherOptions }: SetRequired<RequestOptions, 'method'>,
111
111
  ) {
112
112
  if (!consts.FETCH_METHODS.includes(method)) {
113
113
  throw new Error(`${consts.INVALID_FETCH_METHOD_ERROR} ${method}`);
@@ -116,9 +116,11 @@ export class Getta {
116
116
  // @ts-expect-error No index signature with a parameter of type 'string'
117
117
  this[name] = async <Resource extends PlainObject>(
118
118
  { method: requestMethod, ...otherOptionOverrides }: RequestOptions = {},
119
- context?: Context
119
+ context?: Context,
120
120
  ) =>
121
121
  // @ts-expect-error Type 'undefined' is not assignable to type 'BodyInit'
122
+ // To generic and complex to type without casting.
123
+ // eslint-disable-next-line @typescript-eslint/consistent-type-assertions
122
124
  this[requestMethod ?? method](path, merge({}, otherOptions, otherOptionOverrides), context) as Promise<
123
125
  FetchResponse<Resource>
124
126
  >;
@@ -185,7 +187,7 @@ export class Getta {
185
187
  private async _delete(
186
188
  path: string,
187
189
  { headers = {}, pathTemplateData, queryParams = {}, ...rest }: Omit<RequestOptions, 'method'>,
188
- context?: Context
190
+ context?: Context,
189
191
  ) {
190
192
  const endpoint = buildEndpoint(this._basePath, path, {
191
193
  optionalPathTemplateRegExp: this._optionalPathTemplateRegExp,
@@ -209,7 +211,7 @@ export class Getta {
209
211
  method: consts.DELETE_METHOD,
210
212
  ...rest,
211
213
  },
212
- context
214
+ context,
213
215
  );
214
216
  }
215
217
 
@@ -222,7 +224,7 @@ export class Getta {
222
224
  return await new Promise<FetchResponse>((resolve, reject) => {
223
225
  void (async () => {
224
226
  const fetchTimer = setTimeout(() => {
225
- reject(new Error(`${consts.FETCH_TIMEOUT_ERROR} ${this._fetchTimeout}ms.`));
227
+ reject(new Error(`${consts.FETCH_TIMEOUT_ERROR} ${String(this._fetchTimeout)}ms.`));
226
228
  }, this._fetchTimeout);
227
229
 
228
230
  if (this._rateLimit) {
@@ -242,7 +244,9 @@ export class Getta {
242
244
  });
243
245
  }
244
246
 
245
- const res = await fetch(endpoint, rest);
247
+ // Casting as fetch response does not support generics.
248
+ // eslint-disable-next-line @typescript-eslint/consistent-type-assertions
249
+ const res = (await fetch(endpoint, rest)) as FetchResponse;
246
250
 
247
251
  clearTimeout(fetchTimer);
248
252
 
@@ -253,14 +257,16 @@ export class Getta {
253
257
  resolve(
254
258
  await this._fetchRedirectHandler(
255
259
  res,
260
+ // Has check above means this cannot be undefined.
261
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
256
262
  headers.get(consts.LOCATION_HEADER)!,
257
263
  {
258
264
  redirects,
259
265
  status,
260
266
  ...rest,
261
267
  },
262
- context
263
- )
268
+ context,
269
+ ),
264
270
  );
265
271
 
266
272
  return;
@@ -275,64 +281,66 @@ export class Getta {
275
281
  retries,
276
282
  ...rest,
277
283
  },
278
- context
279
- )
284
+ context,
285
+ ),
280
286
  );
281
287
 
282
288
  return;
283
289
  }
284
290
 
285
- const fetchRes = res as FetchResponse;
286
-
287
291
  try {
288
- Object.defineProperty(fetchRes, 'data', {
292
+ Object.defineProperty(res, 'data', {
289
293
  enumerable: true,
290
294
  // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
291
295
  value: body ? this._bodyParser(await res[this._streamReader]()) : undefined,
292
296
  writable: true,
293
297
  });
294
298
 
295
- this._logResponse(fetchRes, endpoint, options, context);
296
- resolve(fetchRes);
299
+ this._logResponse(res, endpoint, options, context);
300
+ resolve(res);
297
301
  } catch (error) {
298
- reject([error, new Error(`Unable to ${rest.method} ${endpoint} due to previous error`)]);
302
+ if (error instanceof Error) {
303
+ reject(error);
304
+ } else {
305
+ reject(new Error(`Unable to ${rest.method} ${endpoint} due to previous error`));
306
+ }
299
307
  }
300
308
  })();
301
309
  });
302
310
  } catch (error) {
303
- const fetchRes = { errors: castArray(error) };
304
- this._logResponse(fetchRes as FetchResponse, endpoint, options, context);
305
- return fetchRes as FetchResponse;
311
+ // Based on above code, error is gonna be a type of Error.
312
+ // eslint-disable-next-line @typescript-eslint/consistent-type-assertions
313
+ const res = { errors: castArray(error) } as FetchResponse;
314
+ this._logResponse(res, endpoint, options, context);
315
+ return res;
306
316
  }
307
317
  }
308
318
 
309
319
  private async _fetchRedirectHandler(
310
- res: Response,
320
+ res: FetchResponse,
311
321
  endpoint: string,
312
322
  options: FetchRedirectHandlerOptions,
313
- context: Context
323
+ context: Context,
314
324
  ): Promise<FetchResponse> {
315
325
  const { method, redirects = 1, status, ...rest } = options;
316
326
 
317
327
  if (redirects === this._maxRedirects) {
318
- const fetchRes = res as FetchResponse;
319
- fetchRes.errors = [new Error(`${consts.MAX_REDIRECTS_EXCEEDED_ERROR} ${this._maxRedirects}.`)];
320
- this._logResponse(fetchRes, endpoint, options, context);
321
- return fetchRes;
328
+ res.errors = [new Error(`${consts.MAX_REDIRECTS_EXCEEDED_ERROR} ${String(this._maxRedirects)}.`)];
329
+ this._logResponse(res, endpoint, options, context);
330
+ return res;
322
331
  }
323
332
 
324
333
  const redirectMethod = status === 303 ? consts.GET_METHOD : method;
325
334
  return this._fetch(endpoint, { method: redirectMethod, redirects: redirects + 1, ...rest });
326
335
  }
327
336
 
328
- private async _fetchRetryHandler(res: Response, endpoint: string, options: FetchOptions, context: Context) {
337
+ private async _fetchRetryHandler(res: FetchResponse, endpoint: string, options: FetchOptions, context: Context) {
329
338
  const { retries = 1, ...rest } = options;
330
339
 
331
340
  if (retries === this._maxRetries) {
332
- const fetchRes = res as FetchResponse;
333
- fetchRes.errors = [new Error(`${consts.MAX_RETRIES_EXCEEDED_ERROR} ${this._maxRetries}.`)];
334
- this._logResponse(fetchRes, endpoint, options, context);
335
- return fetchRes;
341
+ res.errors = [new Error(`${consts.MAX_RETRIES_EXCEEDED_ERROR} ${String(this._maxRetries)}.`)];
342
+ this._logResponse(res, endpoint, options, context);
343
+ return res;
336
344
  }
337
345
 
338
346
  await delay(this._requestRetryWait);
@@ -342,7 +350,7 @@ export class Getta {
342
350
  private async _get(
343
351
  path: string,
344
352
  { headers = {}, pathTemplateData, queryParams = {} }: Omit<RequestOptions, 'method'>,
345
- context?: Context
353
+ context?: Context,
346
354
  ) {
347
355
  const endpoint = buildEndpoint(this._basePath, path, {
348
356
  optionalPathTemplateRegExp: this._optionalPathTemplateRegExp,
@@ -376,7 +384,7 @@ export class Getta {
376
384
 
377
385
  return this._getResolve(
378
386
  requestHash,
379
- await this._fetch(endpoint, { headers: { ...this._headers, ...headers }, method: consts.GET_METHOD }, context)
387
+ await this._fetch(endpoint, { headers: { ...this._headers, ...headers }, method: consts.GET_METHOD }, context),
380
388
  );
381
389
  }
382
390
 
@@ -449,7 +457,7 @@ export class Getta {
449
457
  private async _request(
450
458
  path: string,
451
459
  { body, headers, method, pathTemplateData, queryParams, ...rest }: SetRequired<RequestOptions, 'method'>,
452
- context?: Context
460
+ context?: Context,
453
461
  ) {
454
462
  const endpoint = buildEndpoint(this._basePath, path, {
455
463
  optionalPathTemplateRegExp: this._optionalPathTemplateRegExp,
@@ -467,7 +475,7 @@ export class Getta {
467
475
  method,
468
476
  ...rest,
469
477
  },
470
- context
478
+ context,
471
479
  );
472
480
  }
473
481
 
@@ -520,6 +528,8 @@ export class Getta {
520
528
  }
521
529
 
522
530
  export const createRestClient = <N extends string>(options: ConstructorOptions, shortcuts?: Shortcuts) => {
531
+ // Typing proving too complex without casting.
532
+ // eslint-disable-next-line @typescript-eslint/consistent-type-assertions
523
533
  const getta = new Getta(options) as Getta & ShortcutProperties<N>;
524
534
 
525
535
  if (!shortcuts) {
package/src/types.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import { type Core } from '@cachemap/core';
2
- import type { SetRequired } from 'type-fest';
2
+ import { type SetRequired } from 'type-fest';
3
3
 
4
4
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
5
5
  export type PlainObject = Record<string, any>;
@@ -11,10 +11,12 @@ export type FetchMethod = 'get' | 'post' | 'put' | 'delete';
11
11
 
12
12
  export type StreamReader = 'arrayBuffer' | 'blob' | 'formData' | 'json' | 'text';
13
13
 
14
- export type ShortcutProperties<T extends string | number> = {
14
+ export type ShortcutProperties<T extends string | number> = Record<
15
+ T,
16
+ // Want to keep this as generic as possible.
15
17
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
16
- [K in T]: <Resource = PlainObject>(...args: any[]) => Promise<FetchResponse<Resource>>;
17
- };
18
+ <Resource = PlainObject>(...args: any[]) => Promise<FetchResponse<Resource>>
19
+ >;
18
20
 
19
21
  export interface ConstructorOptions {
20
22
  /**