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

feat: Allow async requestMiddleware #379

Merged
merged 2 commits into from
Aug 27, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
10 changes: 5 additions & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ const post = async <V = Variables>({
variables?: V
headers?: Dom.RequestInit['headers']
operationName?: string
middleware?: (request: Dom.RequestInit) => Dom.RequestInit
middleware?: (request: Dom.RequestInit) => Dom.RequestInit | Promise<Dom.RequestInit>
}) => {
const body = createRequestBody(query, variables, operationName, fetchOptions.jsonSerializer)

Expand All @@ -172,7 +172,7 @@ const post = async <V = Variables>({
...fetchOptions,
}
if (middleware) {
options = middleware(options)
options = await Promise.resolve(middleware(options))
}
return await fetch(url, options)
}
Expand All @@ -197,7 +197,7 @@ const get = async <V = Variables>({
variables?: V
headers?: HeadersInit
operationName?: string
middleware?: (request: Dom.RequestInit) => Dom.RequestInit
middleware?: (request: Dom.RequestInit) => Dom.RequestInit | Promise<Dom.RequestInit>
}) => {
const queryParams = buildGetQueryParams<V>({
query,
Expand All @@ -212,7 +212,7 @@ const get = async <V = Variables>({
...fetchOptions,
}
if (middleware) {
options = middleware(options)
options = await Promise.resolve(middleware(options))
}
return await fetch(`${url}?${queryParams}`, options)
}
Expand Down Expand Up @@ -458,7 +458,7 @@ async function makeRequest<T = any, V = Variables>({
fetch: any
method: string
fetchOptions: Dom.RequestInit
middleware?: (request: Dom.RequestInit) => Dom.RequestInit
middleware?: (request: Dom.RequestInit) => Dom.RequestInit | Promise<Dom.RequestInit>
}): Promise<Response<T>> {
const fetcher = method.toUpperCase() === 'POST' ? post : get
const isBathchingQuery = Array.isArray(query)
Expand Down
2 changes: 1 addition & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ export interface Response<T> {

export type PatchedRequestInit = Omit<Dom.RequestInit, 'headers'> & {
headers?: MaybeFunction<Dom.RequestInit['headers']>
requestMiddleware?: (request: Dom.RequestInit) => Dom.RequestInit
requestMiddleware?: (request: Dom.RequestInit) => Dom.RequestInit | Promise<Dom.RequestInit>
responseMiddleware?: (response: Response<unknown> | Error) => void
}

Expand Down