Skip to content

Commit

Permalink
fix: handle URLSearchParams array values
Browse files Browse the repository at this point in the history
  • Loading branch information
agerard-godaddy committed Dec 6, 2024
1 parent f4395fa commit b38b319
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 1 deletion.
7 changes: 7 additions & 0 deletions packages/gasket-request/lib/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,13 @@ export type RequestLike = {
*/
async function objectFromCookieStore(cookieStore: CookieStore): Promise<Record<string, string>>;

/**
* Capture the search params as a key/value object
* Necessary to capture array values
* @see https://developer.mozilla.org/en-US/docs/Web/API/URLSearchParams
*/
async function objectFromSearchParams(searchParams: URLSearchParams): Record<string, string>;

/**
* Expected request shape for GasketActions
*/
Expand Down
15 changes: 14 additions & 1 deletion packages/gasket-request/lib/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,19 @@ async function objectFromCookieStore(cookieStore) {
}, {});
}

/**
* @type {import('./index.js').objectFromSearchParams}
*/
function objectFromSearchParams(searchParams) {
const entries = Object.fromEntries(searchParams);
const keys = Object.keys(entries);
return keys.reduce((acc, key) => {
const value = searchParams.getAll(key);
acc[key] = value.length === 1 ? value[0] : value;
return acc;
}, {});
}

/**
* @type {import('./index.js').makeGasketRequest}
*/
Expand Down Expand Up @@ -68,7 +81,7 @@ export async function makeGasketRequest(requestLike) {
return new GasketRequest(Object.seal({
headers: 'entries' in headers ? Object.fromEntries(headers.entries()) : headers,
cookies: 'getAll' in cookies ? await objectFromCookieStore(cookies) : cookies,
query: query instanceof URLSearchParams ? Object.fromEntries(query) : query,
query: query instanceof URLSearchParams ? objectFromSearchParams(query) : query,
path
}));
};
Expand Down
16 changes: 16 additions & 0 deletions packages/gasket-request/test/request.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,22 @@ describe('makeGasketRequest', () => {
expect(result.query).toEqual({ query1: 'value1', query2: 'value2' });
});

it('handles URLSearchParams array values', async () => {
const headers = new Map([['header1', 'value1'], ['header2', 'value2']]);
const query = new URLSearchParams({ query1: 'value1', query2: 'value2' });
query.append('query3', 'value3');
query.append('query3', 'value4');
const requestLike = { headers, cookies: {}, query };

const result = await makeGasketRequest(requestLike);

expect(result.query).toEqual({
query1: 'value1',
query2: 'value2',
query3: ['value3', 'value4']
});
});

it('handles no query', async () => {
const headers = new Map([['header1', 'value1'], ['header2', 'value2']]);
const requestLike = { headers, cookies: {} };
Expand Down

0 comments on commit b38b319

Please sign in to comment.