-
Notifications
You must be signed in to change notification settings - Fork 759
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
package pnpm-lock fetch mock Config Root APIConfig Backend BackendLis…
…t AppConfigSideEffect custom utils
- Loading branch information
Showing
14 changed files
with
223 additions
and
103 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
export function req(url: string, init: RequestInit) { | ||
if (import.meta.env.DEV) { | ||
return import('./mock').then((mod) => mod.mock(url, init)); | ||
} | ||
return fetch(url, init); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
const MOCK_HANDLERS = [ | ||
{ | ||
key: 'GET/', | ||
enabled: false, | ||
handler: (_u: string, _i: RequestInit) => { | ||
// throw new Error(); | ||
return deserializeError(); | ||
// return json({ hello: 'clash' }); | ||
}, | ||
}, | ||
{ | ||
key: 'GET/configs', | ||
enabled: false, | ||
handler: (_u: string, _i: RequestInit) => | ||
json({ | ||
port: 0, | ||
'socks-port': 7891, | ||
'redir-port': 0, | ||
'tproxy-port': 0, | ||
'mixed-port': 7890, | ||
'allow-lan': true, | ||
'bind-address': '*', | ||
mode: 'rule', | ||
'log-level': 'info', | ||
authentication: [], | ||
ipv6: false, | ||
}), | ||
}, | ||
]; | ||
|
||
export async function mock(url: string, init: RequestInit) { | ||
const method = init.method || 'GET'; | ||
const pathname = new URL(url).pathname; | ||
const key = `${method}${pathname}`; | ||
const item = MOCK_HANDLERS.find((h) => { | ||
if (h.enabled && h.key === key) return h; | ||
}); | ||
if (item) { | ||
console.warn('Using mocked API', key); | ||
return (await item?.handler(url, init)) as Response; | ||
} | ||
return fetch(url, init); | ||
} | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
async function json<T = any>(data: T) { | ||
await sleep(1); | ||
return { | ||
ok: true, | ||
json: async () => { | ||
await sleep(16); | ||
return data; | ||
}, | ||
}; | ||
} | ||
|
||
async function deserializeError() { | ||
await sleep(1); | ||
return { | ||
ok: true, | ||
json: async () => { | ||
await sleep(16); | ||
throw new Error(); | ||
}, | ||
}; | ||
} | ||
|
||
function sleep(ms: number) { | ||
return new Promise((resolve) => setTimeout(resolve, ms)); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.