Skip to content

Commit

Permalink
refactor(fetch): update query parameters handling
Browse files Browse the repository at this point in the history
BREAKING CHANGE: queryParametters renamed to queryParams
  • Loading branch information
alimd committed Sep 15, 2024
1 parent ae8fe24 commit 939b3d5
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 9 deletions.
8 changes: 4 additions & 4 deletions packages/fetch/src/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,12 @@ export function processOptions_(options: FetchOptions): Required<FetchOptions> {
options.removeDuplicate = cacheSupported ? 'until_load' : 'always';
}

if (options.url.lastIndexOf('?') === -1 && options.queryParameters != null) {
const queryParameters = options.queryParameters;
if (options.url.lastIndexOf('?') === -1 && options.queryParams != null) {
const queryParams = options.queryParams;
// prettier-ignore
const queryArray = Object
.keys(queryParameters)
.map((key) => `${key}=${String(queryParameters[key])}`);
.keys(queryParams)
.map((key) => `${key}=${String(queryParams[key])}`);

if (queryArray.length > 0) {
options.url += '?' + queryArray.join('&');
Expand Down
2 changes: 1 addition & 1 deletion packages/fetch/src/main.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ describe('fetch with search params', () => {
*/
const options = {
url: 'http://httpbin.org/get',
queryParameters: {
queryParams: {
a: 2,
},
cacheStrategy: 'network_only',
Expand Down
8 changes: 4 additions & 4 deletions packages/fetch/src/type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ import type {Dictionary, Json, JsonObject} from '@alwatr/type-helper';
/**
* Represents the available HTTP methods.
*/
export type Methods = 'GET' | 'HEAD' | 'POST' | 'PUT' | 'DELETE' | 'CONNECT' | 'TRACE' | 'OPTIONS' | 'PATCH';
export type HttpMethod = 'GET' | 'POST' | 'PUT' | 'DELETE' | 'PATCH' | 'OPTIONS' | 'HEAD' | 'CONNECT' | 'TRACE';

/**
* Represents a dictionary of query parameters.
* The keys are strings and the values can be strings, numbers, or booleans.
*/
export type QueryParameters = Dictionary<string | number | boolean>;
export type QueryParams = Dictionary<string | number | boolean>;

/**
* Represents the cache strategy for fetching data.
Expand Down Expand Up @@ -46,7 +46,7 @@ export interface FetchOptions extends RequestInit {
*
* @default 'GET'
*/
method?: Methods;
method?: HttpMethod;

/**
* A Headers object to set the request's headers.
Expand Down Expand Up @@ -121,7 +121,7 @@ export interface FetchOptions extends RequestInit {
/**
* URL query parameters as a JavaScript object.
*/
queryParameters?: QueryParameters;
queryParams?: QueryParams;

/**
* Add token to the Authentication bearer header.
Expand Down

0 comments on commit 939b3d5

Please sign in to comment.