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!: Invoke responseMiddleware in error cases as well #372

Merged
merged 1 commit into from
Aug 9, 2022
Merged
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
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