Skip to content

Commit 8f4ba34

Browse files
authored
Merge pull request #21 from leandrohlsilva/feature/response
Return response as third parameter if handler is set
2 parents b05aa76 + 7115895 commit 8f4ba34

3 files changed

Lines changed: 26 additions & 5 deletions

File tree

lib/index.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -229,8 +229,8 @@ class Api {
229229
this.resetRequest()
230230
}
231231

232-
const { json, httpStatus } = result
233-
return fn ? fn(json, httpStatus) : json
232+
const { json, httpStatus, response } = result
233+
return fn ? fn(json, httpStatus, response) : json
234234
}
235235

236236
/**
@@ -277,7 +277,7 @@ class Api {
277277
}
278278
}
279279

280-
return { httpStatus: r.status, json }
280+
return { httpStatus: r.status, json, response: r }
281281
}
282282

283283
let content = ''

lib/index.spec.js

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,26 @@ describe('util/api', () => {
281281
expect(code).to.equal(httpStatusCode)
282282
})
283283

284+
it('passes the whole response as third parameter', async () => {
285+
286+
const stubbedResponse = {
287+
status: 202,
288+
json: stub().resolves({ foo: 'bar' }),
289+
statusText: 'Accepted',
290+
headers: new Map([ ['john', 'doe'] ])
291+
}
292+
clientStub.resolves(stubbedResponse)
293+
294+
const response = await api
295+
.context({ fetch: clientStub })
296+
.endpoint('some/url')
297+
.get((json, statusCode, response) => {
298+
return response
299+
})
300+
301+
expect(response.headers).to.equal(stubbedResponse.headers)
302+
})
303+
284304
it('parses 3xx as successful', async () => {
285305
const httpStatusCode = 304
286306

@@ -598,7 +618,7 @@ describe('util/api', () => {
598618
})
599619

600620
it('has cleared method', () => {
601-
expect(api.config.method).to.equal('get')
621+
expect(api.config.method).to.equal('GET')
602622
})
603623

604624
it('has not cleared client', () => {

lib/types.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ export type ErrorHandlers = {
8282
/**
8383
* Function to transform API response
8484
*/
85-
export type ResponseTransformer<T = any> = (json: any, httpStatus: number) => T;
85+
export type ResponseTransformer<T = any> = (json: any, httpStatus: number, response: Response) => T;
8686

8787
/**
8888
* Fetch client interface
@@ -110,4 +110,5 @@ export type Response = {
110110
export type QueryResult = {
111111
httpStatus: number;
112112
json?: any;
113+
response: Response;
113114
};

0 commit comments

Comments
 (0)