Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion packages/ajax/src/Ajax.js
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,15 @@ export class Ajax {
* @returns {Promise<Response>}
*/
async fetch(info, init, parseErrorResponse = false) {
const request = /** @type {CacheRequest} */ (new Request(info, { ...init }));
const baseUrl = typeof info === 'string' ? info : /** @type {Request} */ (info).url;
const url = `${baseUrl}${init?.params ? `?${new URLSearchParams(init.params)}` : ''}`;

const request = /** @type {CacheRequest} */ (
typeof info === 'string'
? new Request(url, { ...init })
: new Request({ ...info, url }, { ...init })
);

request.cacheOptions = init?.cacheOptions;
request.params = init?.params;

Expand Down
9 changes: 9 additions & 0 deletions packages/ajax/test/Ajax.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,15 @@ describe('Ajax', () => {
expect(response.headers.get('X-Custom-Header')).to.equal('y-custom-value');
});

it('calls fetch with the given query params', async () => {
await ajax.fetch('/foo', { params: { query: 'param', intValue: 1 } });

expect(fetchStub).to.have.been.calledOnce;
const request = fetchStub.getCall(0).args[0];

expect(request.url).to.equal(`${window.location.origin}/foo?query=param&intValue=1`);
});

it('throws on 4xx responses', async () => {
fetchStub.returns(Promise.resolve(new Response('', { status: 400 })));

Expand Down