Skip to content

Commit

Permalink
feat: Allow async requestMiddleware (#379)
Browse files Browse the repository at this point in the history
  • Loading branch information
shkreios authored Aug 27, 2022
1 parent d04de20 commit 6246ffe
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 6 deletions.
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
35 changes: 35 additions & 0 deletions tests/general.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,41 @@ describe('middleware', () => {
})
})

describe('async request middleware', () => {
beforeEach(() => {
ctx.res({
body: {
data: {
result: 123,
},
},
})

requestMiddleware = jest.fn(async (req) => ({ ...req }))
client = new GraphQLClient(ctx.url, {
requestMiddleware,
})
})

it('request', async () => {
const requestPromise = client.request<{ result: number }>(`x`)
expect(requestMiddleware).toBeCalledTimes(1)
await requestPromise
})

it('rawRequest', async () => {
const requestPromise = client.rawRequest<{ result: number }>(`x`)
expect(requestMiddleware).toBeCalledTimes(1)
await requestPromise
})

it('batchRequests', async () => {
const requestPromise = client.batchRequests<{ result: number }>([{ document: `x` }])
expect(requestMiddleware).toBeCalledTimes(1)
await requestPromise
})
})

describe('failed requests', () => {
beforeEach(() => {
ctx.res({
Expand Down

0 comments on commit 6246ffe

Please sign in to comment.