Skip to content

Commit

Permalink
Fix support of relative urls in createJsonQuery and `createJsonMuta…
Browse files Browse the repository at this point in the history
…tion`
  • Loading branch information
igorkamyshev committed Aug 5, 2024
1 parent 56db427 commit df94492
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/light-jars-destroy.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@farfetched/core": patch
---

Fix support of relative urls in `createJsonQuery` and `createJsonMutation`
54 changes: 54 additions & 0 deletions packages/core/src/__tests__/relative_urls_in_jsdom.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/**
* @vitest-environment jsdom
*/
import { describe, test, expect } from 'vitest';
import { allSettled, fork } from 'effector';

import { createJsonQuery } from '../query/create_json_query';
import { unknownContract } from '../contract/unknown_contract';
import { fetchFx } from '../fetch/fetch';

describe('relative paths in createJsonQuery, issue #493', () => {
test('does not throw error for valid relative path', async () => {
const query = createJsonQuery({
request: { url: '/api', method: 'GET' },
response: { contract: unknownContract },
});

const scope = fork({
// We have to to mock fetchFx, because URL validation embed in createJsonQuery.__.executeFx
handlers: [[fetchFx, () => new Response(JSON.stringify('DATA'))]],
});

await allSettled(query.start, { scope });

expect(scope.getState(query.$error)).toBe(null);
expect(scope.getState(query.$data)).toBe('DATA');
});

test('does throw error for invalid relative path', async () => {
const query = createJsonQuery({
request: { url: 'api **** jkjj', method: 'GET' },
response: { contract: unknownContract },
});

const scope = fork({
// We have to to mock fetchFx, because URL validation embed in createJsonQuery.__.executeFx
handlers: [[fetchFx, () => new Response(JSON.stringify('DATA'))]],
});

await allSettled(query.start, { scope });

expect(scope.getState(query.$error)).toMatchInlineSnapshot(`
{
"errorType": "CONFIGURATION",
"explanation": "Operation is misconfigured",
"reason": "Invalid URL",
"validationErrors": [
""api **** jkjj" is not valid URL",
],
}
`);
expect(scope.getState(query.$data)).toBe(null);
});
});
7 changes: 6 additions & 1 deletion packages/core/src/fetch/lib.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,11 @@ export function formatUrl(
url: string,
queryRecord: FetchApiRecord | string
): URL {
let urlBase: string | undefined;
if (url.startsWith('/')) {
urlBase = window.location.origin;
}

let urlString: string;
let queryString: string;

Expand All @@ -89,7 +94,7 @@ export function formatUrl(
}

try {
return new URL(urlString);
return new URL(urlString, urlBase);
} catch (e) {
throw configurationError({
reason: 'Invalid URL',
Expand Down

0 comments on commit df94492

Please sign in to comment.