Skip to content

Commit

Permalink
feat!: Invoke responseMiddleware in error cases as well (#372)
Browse files Browse the repository at this point in the history
BREAKING CHANGE:

Middleware will get either the response or error now, whereas before it would only get the response and never run in the error case.
  • Loading branch information
tobilen authored Aug 9, 2022
1 parent 3662f38 commit 2f221a4
Show file tree
Hide file tree
Showing 3 changed files with 115 additions and 43 deletions.
51 changes: 36 additions & 15 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -267,12 +267,19 @@ export class GraphQLClient {
method,
fetchOptions,
middleware: requestMiddleware,
}).then((response) => {
if (responseMiddleware) {
responseMiddleware(response)
}
return response
})
.then((response) => {
if (responseMiddleware) {
responseMiddleware(response)
}
return response
})
.catch((error) => {
if (responseMiddleware) {
responseMiddleware(error)
}
throw error
})
}

/**
Expand Down Expand Up @@ -326,12 +333,19 @@ export class GraphQLClient {
method,
fetchOptions,
middleware: requestMiddleware,
}).then((response) => {
if (responseMiddleware) {
responseMiddleware(response)
}
return response.data
})
.then((response) => {
if (responseMiddleware) {
responseMiddleware(response)
}
return response.data
})
.catch((error) => {
if (responseMiddleware) {
responseMiddleware(error)
}
throw error
})
}

/**
Expand Down Expand Up @@ -379,12 +393,19 @@ export class GraphQLClient {
method,
fetchOptions,
middleware: requestMiddleware,
}).then((response) => {
if (responseMiddleware) {
responseMiddleware(response)
}
return response.data
})
.then((response) => {
if (responseMiddleware) {
responseMiddleware(response)
}
return response.data
})
.catch((error) => {
if (responseMiddleware) {
responseMiddleware(error)
}
throw error
})
}

setHeaders(headers: Dom.RequestInit['headers']): GraphQLClient {
Expand Down
2 changes: 1 addition & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ export interface Response<T> {
export type PatchedRequestInit = Omit<Dom.RequestInit, 'headers'> & {
headers?: MaybeFunction<Dom.RequestInit['headers']>
requestMiddleware?: (request: Dom.RequestInit) => Dom.RequestInit
responseMiddleware?: (response: Response<unknown>) => void
responseMiddleware?: (response: Response<unknown> | Error) => void
}

export type BatchRequestDocument<V = Variables> = {
Expand Down
105 changes: 78 additions & 27 deletions tests/general.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,40 +122,91 @@ describe('middleware', () => {
let requestMiddleware: jest.Mock
let responseMiddleware: jest.Mock

beforeEach(() => {
ctx.res({
body: {
data: {
result: 123,
describe('successful requests', () => {
beforeEach(() => {
ctx.res({
body: {
data: {
result: 123,
},
},
},
})

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

requestMiddleware = jest.fn((req) => ({ ...req }))
responseMiddleware = jest.fn()
client = new GraphQLClient(ctx.url, { requestMiddleware, responseMiddleware })
})
it('request', async () => {
const requestPromise = client.request<{ result: number }>(`x`)
expect(requestMiddleware).toBeCalledTimes(1)
const res = await requestPromise
expect(responseMiddleware).toBeCalledTimes(1)
expect(res.result).toBe(123)
})

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

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

it('batchRequests', async () => {
const requestPromise = client.batchRequests<{ result: number }>([{ document: `x` }])
expect(requestMiddleware).toBeCalledTimes(1)
await requestPromise
expect(responseMiddleware).toBeCalledTimes(1)
describe('failed requests', () => {
beforeEach(() => {
ctx.res({
body: {
errors: {
message: 'Syntax Error GraphQL request (1:1) Unexpected Name "x"\n\n1: x\n ^\n',
locations: [
{
line: 1,
column: 1,
},
],
},
},
})

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

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

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

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

Expand Down

0 comments on commit 2f221a4

Please sign in to comment.