diff --git a/package.json b/package.json index 72d7ad0..a3b534e 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "fetches", - "version": "0.2.1", + "version": "0.2.2", "description": "Smart and Modern fetch wrapper.", "main": "./lib/module.js", "scripts": { diff --git a/src/client.js b/src/client.js index 5cf3b8b..93b07ca 100644 --- a/src/client.js +++ b/src/client.js @@ -43,6 +43,20 @@ export class Client { after() { return this.options.after } + + appendBeforeMiddleware(middleware) { + const options = deepmerge(this.options, { + before: [middleware], + }) + return new Client(this.getURI(), options) + } + + appendAfterMiddleware(middleware) { + const options = deepmerge(this.options, { + after: [middleware], + }) + return new Client(this.getURI(), options) + } } export const createClient = (uri, options) => new Client(uri, options) diff --git a/src/http.js b/src/http.js index c1173b7..6466399 100644 --- a/src/http.js +++ b/src/http.js @@ -3,13 +3,6 @@ import deepmerge from 'deepmerge' import { appendParams } from './utils/append-params' import { Client } from './client' -const isClientInstance = client => { - if (client instanceof Client) { - return true - } - return false -} - const getDefaultOptions = client => { switch (client.getRequestType()) { case 'json': @@ -79,10 +72,6 @@ const request = (client, method) => { } export const getHTTPMethods = client => { - if (!isClientInstance(client)) { - throw new Error('Please specify a Client to get the http methods.') - } - const clientRequest = request.bind(null, client) return { diff --git a/src/middlewares/json-response.js b/src/middlewares/json-response.js index 2bd4fd7..5d75cb2 100644 --- a/src/middlewares/json-response.js +++ b/src/middlewares/json-response.js @@ -2,7 +2,7 @@ const contentTypeIsJSON = header => header && header.includes('application/json' export default response => { if (contentTypeIsJSON(response.headers.get('content-type'))) { - return Promise.all([response.json(), response]) + return response.json() } return Promise.resolve(response) diff --git a/tests/http.test.js b/tests/http.test.js index 1b35695..3cd17e1 100644 --- a/tests/http.test.js +++ b/tests/http.test.js @@ -12,9 +12,4 @@ describe('HTTP Module', () => { const keys = Object.keys(getHTTPMethods(client)) expect(keys).toEqual(expect.arrayContaining(httpMethods)) }) - test('getHTTPMethods needs a client as parameter', () => { - expect(() => { - getHTTPMethods() - }).toThrow() - }) })