Public Access
refactor(frontend): lint, one session guard, one request helper, one user context
This commit is contained in:
1 parent
111ab1e200
commit
d0ea9e900d
78 files changed
+2138
-624
No files matched your search
@@ -0,0 +1,20 @@
|
||||
import {vi} from 'vitest';
|
||||
|
||||
export const json = (status, body) => ({ok: status >= 200 && status < 300, status, json: async () => body});
|
||||
|
||||
/**
|
||||
* A fetch mock that answers by method and path, e.g. `'GET /api/users/me'`. The path starts at `/api/` (the base URL
|
||||
* is not set in tests) and leaves out the query. A value is an answer or `(url, init) => answer`. Any other request
|
||||
* throws, so a test cannot pass on a request it did not expect.
|
||||
* Sets `globalThis.fetch` and returns the mock.
|
||||
*/
|
||||
export function fetchRoutes(routes) {
|
||||
const mock = vi.fn(async (url, init) => {
|
||||
const key = `${init?.method ?? 'GET'} ${new URL(url, 'http://x').pathname.replace(/^.*?(?=\/api\/)/, '')}`;
|
||||
if (!(key in routes)) throw new Error(`no route ${key}`);
|
||||
const answer = routes[key];
|
||||
return typeof answer === 'function' ? answer(url, init) : answer;
|
||||
});
|
||||
globalThis.fetch = mock;
|
||||
return mock;
|
||||
}
|
||||
Reference in new issue
Block a user