Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP]: How about this? #93

Open
wants to merge 1 commit into
base: mocks-proposal
Choose a base branch
from
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
32 changes: 10 additions & 22 deletions src/private/runApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@ const getUrl = (options: ActionOptions): string => {
const shouldUseCache = ({ options, requestType }: Request): boolean => {
return Boolean(
requestType.kind === 'fetch' &&
Boolean(requestType.piece?.list?.length) &&
options.cache,
Boolean(requestType.piece?.list?.length) &&
options.cache,
)
}

Expand Down Expand Up @@ -107,22 +107,8 @@ const failureAction = (
}
}

async function mock(options: ActionOptions): Promise<Response> {
await sleep(options.mockTimeout || 0)
const [error, result] = await attempt(
() => options.mockResponse?.() || Promise.resolve(),
)
if (error) {
return {
ok: false,
text: () => Promise.resolve(error),
} as Response
}
return {
ok: true,
json: () => Promise.resolve(result),
} as Response
}
const mockOrFetch: (options: ActionOptions) => (url: string, conig: RequestInit) => Promise<Response> =
(options: ActionOptions) => options.mockFetch ?? fetch

async function runApi(
request: Request,
Expand All @@ -144,12 +130,14 @@ async function runApi(
: requestType.piece.list
return Promise.resolve(result)
}
const isMock = typeof options.mockResponse !== 'undefined'
const isMock = typeof options.mockFetch !== 'undefined'
options.debugRequests && requestLogger(url, method, {}, isMock)
requestAction(request)
const [error, response] = await attempt(() =>
options.mockResponse ? mock(options) : fetch(url, config),
)

if (options.mockTimeout) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that the mockTimeout use is now orthogonal to the use of mockFetch. So I could introduce a timer on real fetch calls, it could be useful for troubleshooting.

await sleep(options.mockTimeout)
}
const [error, response] = await attempt(() => mockOrFetch(options)(url, config))
options.debugRequests &&
responseLogger(url, method, response || error, isMock)
if (response?.ok) {
Expand Down
2 changes: 1 addition & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ type ProviderOptions = {
debugRequests?: boolean
handleResponseHeaders?: (t: Response) => void
headers?: ((t: HeadersObj) => HeadersObj) | HeadersObj
mockResponse?: () => Promise<any>
mockFetch?: typeof fetch
mockTimeout?: number
paramsParser?: (t: string) => string
paramsUnparser?: (t: string) => string
Expand Down