getta 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/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.21",
5
5
  "author": "miami-man",
6
6
  "license": "MIT",
7
7
  "homepage": "https://github.com/badbatch/getta",
@@ -1,18 +1,12 @@
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';
3
+ import { type SearchParams } from '#types.js';
4
4
  import { type BuildEndpointOptions } from './types.ts';
5
5
 
6
6
  export const buildEndpoint = (
7
7
  basePath: string,
8
8
  path: string,
9
- {
10
- optionalPathTemplateRegExp,
11
- pathTemplateCallback,
12
- pathTemplateData,
13
- pathTemplateRegExp,
14
- queryParams = {},
15
- }: BuildEndpointOptions,
9
+ { optionalPathTemplateRegExp, pathTemplateCallback, pathTemplateData, pathTemplateRegExp }: BuildEndpointOptions,
16
10
  ) => {
17
11
  const pathJoiner = basePath.endsWith('/') || path.startsWith('/') ? '' : '/';
18
12
  let endpoint = `${basePath}${pathJoiner}${path}`;
@@ -27,7 +21,22 @@ export const buildEndpoint = (
27
21
  endpoint = endpoint.slice(0, Math.max(0, endpoint.length - 1));
28
22
  }
29
23
 
30
- const sanitisedSearchParams = omitBy<PlainObject>(queryParams, entry => entry === undefined);
24
+ return endpoint;
25
+ };
26
+
27
+ export const appendSearchParams = (
28
+ endpoint: string,
29
+ searchParams: SearchParams,
30
+ extraSearchParam: Record<string, string> = {},
31
+ ) => {
32
+ const mergedSearchParams = {
33
+ ...(isFunction(searchParams) ? searchParams(endpoint, extraSearchParam) : searchParams),
34
+ ...extraSearchParam,
35
+ };
36
+
37
+ // We have seen instances where value can be undefined
38
+ // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
39
+ const sanitisedSearchParams = omitBy<Record<string, string>>(mergedSearchParams, entry => entry === undefined);
31
40
 
32
41
  if (Object.keys(sanitisedSearchParams).length > 0) {
33
42
  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';
@@ -25,6 +25,7 @@ import {
25
25
  type RequestOptions,
26
26
  type RequestQueue,
27
27
  type RequestTracker,
28
+ type SearchParams,
28
29
  type ShortcutProperties,
29
30
  type Shortcuts,
30
31
  type StreamReader,
@@ -44,7 +45,7 @@ export class Getta {
44
45
  private _pathTemplateCallback: PathTemplateCallback;
45
46
  private _pathTemplateRegExp: RegExp;
46
47
  private _performance: Performance;
47
- private _queryParams: PlainObject;
48
+ private _queryParams: SearchParams;
48
49
  private _rateLimit: boolean;
49
50
  private _rateLimitCount = 0;
50
51
  private _rateLimitedRequestQueue: RequestQueue = [];
@@ -189,14 +190,14 @@ export class Getta {
189
190
  { headers = {}, pathTemplateData, queryParams = {}, ...rest }: Omit<RequestOptions, 'method'>,
190
191
  context?: Context,
191
192
  ) {
192
- const endpoint = buildEndpoint(this._basePath, path, {
193
+ let endpoint = buildEndpoint(this._basePath, path, {
193
194
  optionalPathTemplateRegExp: this._optionalPathTemplateRegExp,
194
195
  pathTemplateCallback: this._pathTemplateCallback,
195
196
  pathTemplateData,
196
197
  pathTemplateRegExp: this._pathTemplateRegExp,
197
- queryParams: { ...this._queryParams, ...queryParams },
198
198
  });
199
199
 
200
+ endpoint = appendSearchParams(endpoint, this._queryParams, queryParams);
200
201
  const requestHash = Md5.hashStr(endpoint);
201
202
  const cacheability = await this._cacheEntryHas(requestHash);
202
203
 
@@ -366,14 +367,15 @@ export class Getta {
366
367
  { headers = {}, pathTemplateData, queryParams = {} }: Omit<RequestOptions, 'method'>,
367
368
  context?: Context,
368
369
  ) {
369
- const endpoint = buildEndpoint(this._basePath, path, {
370
+ let endpoint = buildEndpoint(this._basePath, path, {
370
371
  optionalPathTemplateRegExp: this._optionalPathTemplateRegExp,
371
372
  pathTemplateCallback: this._pathTemplateCallback,
372
373
  pathTemplateData,
373
374
  pathTemplateRegExp: this._pathTemplateRegExp,
374
- queryParams: { ...this._queryParams, ...queryParams },
375
375
  });
376
376
 
377
+ endpoint = appendSearchParams(endpoint, this._queryParams, queryParams);
378
+
377
379
  const requestHash = Md5.hashStr(endpoint);
378
380
  const cacheability = await this._cacheEntryHas(requestHash);
379
381
 
@@ -484,14 +486,15 @@ export class Getta {
484
486
  { body, headers, method, pathTemplateData, queryParams, ...rest }: SetRequired<RequestOptions, 'method'>,
485
487
  context?: Context,
486
488
  ) {
487
- const endpoint = buildEndpoint(this._basePath, path, {
489
+ let endpoint = buildEndpoint(this._basePath, path, {
488
490
  optionalPathTemplateRegExp: this._optionalPathTemplateRegExp,
489
491
  pathTemplateCallback: this._pathTemplateCallback,
490
492
  pathTemplateData,
491
493
  pathTemplateRegExp: this._pathTemplateRegExp,
492
- queryParams: { ...this._queryParams, ...queryParams },
493
494
  });
494
495
 
496
+ endpoint = appendSearchParams(endpoint, this._queryParams, queryParams);
497
+
495
498
  return this._fetch(
496
499
  endpoint,
497
500
  {
package/src/types.ts CHANGED
@@ -18,6 +18,10 @@ export type ShortcutProperties<T extends string | number> = Record<
18
18
  <Resource = PlainObject>(...args: any[]) => Promise<FetchResponse<Resource>>
19
19
  >;
20
20
 
21
+ export type SearchParams =
22
+ | Record<string, string>
23
+ | ((endpoint: string, extraSearchParams?: Record<string, string>) => Record<string, string>);
24
+
21
25
  export interface ConstructorOptions {
22
26
  /**
23
27
  * The base path of the url for all requests made from
@@ -91,7 +95,7 @@ export interface ConstructorOptions {
91
95
  /**
92
96
  * Any query params to attach to every request.
93
97
  */
94
- queryParams?: PlainObject;
98
+ queryParams?: SearchParams;
95
99
  /**
96
100
  * Whether to enable the rate limit feature.
97
101
  */
@@ -153,7 +157,7 @@ export interface RequestOptions {
153
157
  /**
154
158
  * Any query params to attach to the request.
155
159
  */
156
- queryParams?: PlainObject;
160
+ queryParams?: Record<string, string>;
157
161
  }
158
162
 
159
163
  export type RequestQueue = [(value: FetchResponse) => void, string, FetchOptions, PlainObject][];