getta 1.0.19 → 1.0.20

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/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "getta",
3
3
  "description": "An isomorphic rest client based on the Fetch API.",
4
- "version": "1.0.19",
4
+ "version": "1.0.20",
5
5
  "author": "miami-man",
6
6
  "license": "MIT",
7
7
  "homepage": "https://github.com/badbatch/getta",
@@ -1,18 +1,11 @@
1
- import { omitBy } from 'lodash-es';
1
+ import { isFunction, omitBy } from 'lodash-es';
2
2
  import queryString from 'query-string';
3
- import { type PlainObject } from '#types.ts';
4
3
  import { type BuildEndpointOptions } from './types.ts';
5
4
 
6
5
  export const buildEndpoint = (
7
6
  basePath: string,
8
7
  path: string,
9
- {
10
- optionalPathTemplateRegExp,
11
- pathTemplateCallback,
12
- pathTemplateData,
13
- pathTemplateRegExp,
14
- queryParams = {},
15
- }: BuildEndpointOptions,
8
+ { optionalPathTemplateRegExp, pathTemplateCallback, pathTemplateData, pathTemplateRegExp }: BuildEndpointOptions,
16
9
  ) => {
17
10
  const pathJoiner = basePath.endsWith('/') || path.startsWith('/') ? '' : '/';
18
11
  let endpoint = `${basePath}${pathJoiner}${path}`;
@@ -27,7 +20,22 @@ export const buildEndpoint = (
27
20
  endpoint = endpoint.slice(0, Math.max(0, endpoint.length - 1));
28
21
  }
29
22
 
30
- const sanitisedSearchParams = omitBy<PlainObject>(queryParams, entry => entry === undefined);
23
+ return endpoint;
24
+ };
25
+
26
+ export const appendSearchParams = (
27
+ endpoint: string,
28
+ searchParams: Record<string, string> | ((endpoint: string) => Record<string, string>),
29
+ searchParamOverrides: Record<string, string> = {},
30
+ ) => {
31
+ const mergedSearchParams = {
32
+ ...(isFunction(searchParams) ? searchParams(endpoint) : searchParams),
33
+ ...searchParamOverrides,
34
+ };
35
+
36
+ // We have seen instances where value can be undefined
37
+ // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
38
+ const sanitisedSearchParams = omitBy<Record<string, string>>(mergedSearchParams, entry => entry === undefined);
31
39
 
32
40
  if (Object.keys(sanitisedSearchParams).length > 0) {
33
41
  const queryJoin = queryString.extract(endpoint) ? '&' : '?';
@@ -1,4 +1,4 @@
1
- import { type PathTemplateCallback, type RequestOptions } from '../../types.ts';
1
+ import { type PathTemplateCallback, type RequestOptions } from '#types.ts';
2
2
 
3
3
  export interface BuildEndpointOptions extends Omit<RequestOptions, 'headers'> {
4
4
  optionalPathTemplateRegExp: RegExp;
package/src/main.ts CHANGED
@@ -4,7 +4,7 @@ import { castArray, merge } from 'lodash-es';
4
4
  import { Md5 } from 'ts-md5';
5
5
  import { type SetRequired } from 'type-fest';
6
6
  import * as consts from './constants.ts';
7
- import { buildEndpoint } from './helpers/buildEndpoint/index.ts';
7
+ import { appendSearchParams, buildEndpoint } from './helpers/buildEndpoint/index.ts';
8
8
  import { defaultPathTemplateCallback } from './helpers/defaultPathTemplateCallback/index.ts';
9
9
  import { delay } from './helpers/delay/index.ts';
10
10
  import { getResponseGroup } from './helpers/getResponseGroup/index.ts';
@@ -44,7 +44,7 @@ export class Getta {
44
44
  private _pathTemplateCallback: PathTemplateCallback;
45
45
  private _pathTemplateRegExp: RegExp;
46
46
  private _performance: Performance;
47
- private _queryParams: PlainObject;
47
+ private _queryParams: Record<string, string> | ((endpoint: string) => Record<string, string>);
48
48
  private _rateLimit: boolean;
49
49
  private _rateLimitCount = 0;
50
50
  private _rateLimitedRequestQueue: RequestQueue = [];
@@ -189,14 +189,14 @@ export class Getta {
189
189
  { headers = {}, pathTemplateData, queryParams = {}, ...rest }: Omit<RequestOptions, 'method'>,
190
190
  context?: Context,
191
191
  ) {
192
- const endpoint = buildEndpoint(this._basePath, path, {
192
+ let endpoint = buildEndpoint(this._basePath, path, {
193
193
  optionalPathTemplateRegExp: this._optionalPathTemplateRegExp,
194
194
  pathTemplateCallback: this._pathTemplateCallback,
195
195
  pathTemplateData,
196
196
  pathTemplateRegExp: this._pathTemplateRegExp,
197
- queryParams: { ...this._queryParams, ...queryParams },
198
197
  });
199
198
 
199
+ endpoint = appendSearchParams(endpoint, this._queryParams, queryParams);
200
200
  const requestHash = Md5.hashStr(endpoint);
201
201
  const cacheability = await this._cacheEntryHas(requestHash);
202
202
 
@@ -366,14 +366,15 @@ export class Getta {
366
366
  { headers = {}, pathTemplateData, queryParams = {} }: Omit<RequestOptions, 'method'>,
367
367
  context?: Context,
368
368
  ) {
369
- const endpoint = buildEndpoint(this._basePath, path, {
369
+ let endpoint = buildEndpoint(this._basePath, path, {
370
370
  optionalPathTemplateRegExp: this._optionalPathTemplateRegExp,
371
371
  pathTemplateCallback: this._pathTemplateCallback,
372
372
  pathTemplateData,
373
373
  pathTemplateRegExp: this._pathTemplateRegExp,
374
- queryParams: { ...this._queryParams, ...queryParams },
375
374
  });
376
375
 
376
+ endpoint = appendSearchParams(endpoint, this._queryParams, queryParams);
377
+
377
378
  const requestHash = Md5.hashStr(endpoint);
378
379
  const cacheability = await this._cacheEntryHas(requestHash);
379
380
 
@@ -484,14 +485,15 @@ export class Getta {
484
485
  { body, headers, method, pathTemplateData, queryParams, ...rest }: SetRequired<RequestOptions, 'method'>,
485
486
  context?: Context,
486
487
  ) {
487
- const endpoint = buildEndpoint(this._basePath, path, {
488
+ let endpoint = buildEndpoint(this._basePath, path, {
488
489
  optionalPathTemplateRegExp: this._optionalPathTemplateRegExp,
489
490
  pathTemplateCallback: this._pathTemplateCallback,
490
491
  pathTemplateData,
491
492
  pathTemplateRegExp: this._pathTemplateRegExp,
492
- queryParams: { ...this._queryParams, ...queryParams },
493
493
  });
494
494
 
495
+ endpoint = appendSearchParams(endpoint, this._queryParams, queryParams);
496
+
495
497
  return this._fetch(
496
498
  endpoint,
497
499
  {
package/src/types.ts CHANGED
@@ -91,7 +91,7 @@ export interface ConstructorOptions {
91
91
  /**
92
92
  * Any query params to attach to every request.
93
93
  */
94
- queryParams?: PlainObject;
94
+ queryParams?: Record<string, string> | ((endpoint: string) => Record<string, string>);
95
95
  /**
96
96
  * Whether to enable the rate limit feature.
97
97
  */
@@ -153,7 +153,7 @@ export interface RequestOptions {
153
153
  /**
154
154
  * Any query params to attach to the request.
155
155
  */
156
- queryParams?: PlainObject;
156
+ queryParams?: Record<string, string>;
157
157
  }
158
158
 
159
159
  export type RequestQueue = [(value: FetchResponse) => void, string, FetchOptions, PlainObject][];