Skip to content

Commit

Permalink
fix(client): adjust token parsing to follow query parameters
Browse files Browse the repository at this point in the history
Token parsing in API requests was reordered to occur after query parameters are set. This prevents the token from being overwritten when both are provided in the URL, ensuring that all parameters are correctly included in the request. This adjustment maintains the integrity of the URL's query string by appending the token after existing query parameters.
  • Loading branch information
shorwood committed Nov 29, 2024
1 parent d2ef75d commit 18662ed
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 2 deletions.
3 changes: 2 additions & 1 deletion packages/client/utils/parseRequest.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,13 +119,14 @@ describe('parseRequest', () => {
it('should set the token in the query parameters', () => {
const context = parseRequest('GET /users', {
baseUrl: 'https://api.example.com',
query: { page: 1 },
token: 'my-api-key',
tokenLocation: 'query',
tokenProperty: 'api_key',
})
expect(context).toStrictEqual({
init: { method: 'get' },
url: new URL('https://api.example.com/users?api_key=my-api-key'),
url: new URL('https://api.example.com/users?page=1&api_key=my-api-key'),
})
})

Expand Down
2 changes: 1 addition & 1 deletion packages/client/utils/parseRequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,12 +139,12 @@ export function parseRequest(route: string, options: FetchOptions = {}): Request
parseRequestUrl(context, route, { baseUrl, method })
parseRequestParameters(context, { parameters: parameters ?? dataObject })
parseRequestBasicAuth(context, { username, password })
parseRequestToken(context, { token, tokenLocation, tokenProperty })

// --- Depending on the method, parse the query, body, and headers.
const requestMethod = context.init?.method?.toLowerCase() ?? 'get'
const requestExpectsBody = ['post', 'put', 'patch'].includes(requestMethod)
parseRequestQuery(context, { queryArrayFormat, query: requestExpectsBody ? query : query ?? dataObject })
parseRequestToken(context, { token, tokenLocation, tokenProperty })
parseRequestBody(context, { body: requestExpectsBody ? body ?? dataObject : undefined })
parseRequestHeaders(context, { headers })

Expand Down

0 comments on commit 18662ed

Please sign in to comment.