Skip to content

Commit

Permalink
feat: allow override for mock handlers to be a function (#1375)
Browse files Browse the repository at this point in the history
* feat: allow override for mock handlers to be a function

* fix: has -> is

---------

Co-authored-by: Alfred Jonsson <alfred.jonsson@decerno.se>
  • Loading branch information
AffeJonsson and Alfred Jonsson committed May 15, 2024
1 parent f7db4ca commit bc3d277
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions packages/mock/src/msw/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,19 +95,23 @@ const generateDefinition = (
: '';

const delay = getDelay(override, !isFunction(mock) ? mock : undefined);
const isHandlerOverridden = isReturnHttpResponse && !isTextPlain;
const infoParam = isHandlerOverridden ? 'info' : '';
const handlerImplementation = `
export const ${handlerName} = (${isReturnHttpResponse && !isTextPlain ? `overrideResponse?: ${returnType}` : ''}) => {
export const ${handlerName} = (${isHandlerOverridden ? `overrideResponse?: ${returnType} | ((${infoParam}: Parameters<Parameters<typeof http.${verb}>[1]>[0]) => ${returnType})` : ''}) => {
return http.${verb}('${route}', ${
delay === false
? '() => {'
: `async () => {
? `(${infoParam}) => {`
: `async (${infoParam}) => {
await delay(${isFunction(delay) ? `(${delay})()` : delay});`
}
return new HttpResponse(${
isReturnHttpResponse
? isTextPlain
? `${getResponseMockFunctionName}()`
: `JSON.stringify(overrideResponse !== undefined ? overrideResponse : ${getResponseMockFunctionName}())`
: `JSON.stringify(overrideResponse !== undefined
? (typeof overrideResponse === "function" ? overrideResponse(${infoParam}) : overrideResponse)
: ${getResponseMockFunctionName}())`
: null
},
{
Expand Down

0 comments on commit bc3d277

Please sign in to comment.