From 5019205c3af21b9b56955e75138456642e834f36 Mon Sep 17 00:00:00 2001 From: Olivier Ricordeau Date: Mon, 31 Oct 2016 04:35:39 +0100 Subject: [PATCH 1/4] Enable chaining of middlewares and afterwares in the network interface --- src/transport/networkInterface.ts | 10 +++++---- test/networkInterface.ts | 35 +++++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+), 4 deletions(-) diff --git a/src/transport/networkInterface.ts b/src/transport/networkInterface.ts index 9785a921b14..19f3d7ca8ab 100644 --- a/src/transport/networkInterface.ts +++ b/src/transport/networkInterface.ts @@ -62,8 +62,8 @@ export interface HTTPNetworkInterface extends NetworkInterface { _opts: RequestInit; _middlewares: MiddlewareInterface[]; _afterwares: AfterwareInterface[]; - use(middlewares: MiddlewareInterface[]): any; - useAfter(afterwares: AfterwareInterface[]): any; + use(middlewares: MiddlewareInterface[]): HTTPNetworkInterface; + useAfter(afterwares: AfterwareInterface[]): HTTPNetworkInterface; } export interface RequestAndOptions { @@ -196,7 +196,7 @@ export class HTTPFetchNetworkInterface implements NetworkInterface { }); }; - public use(middlewares: MiddlewareInterface[]) { + public use(middlewares: MiddlewareInterface[]): HTTPNetworkInterface { middlewares.map((middleware) => { if (typeof middleware.applyMiddleware === 'function') { this._middlewares.push(middleware); @@ -204,9 +204,10 @@ export class HTTPFetchNetworkInterface implements NetworkInterface { throw new Error('Middleware must implement the applyMiddleware function'); } }); + return this; } - public useAfter(afterwares: AfterwareInterface[]) { + public useAfter(afterwares: AfterwareInterface[]): HTTPNetworkInterface { afterwares.map(afterware => { if (typeof afterware.applyAfterware === 'function') { this._afterwares.push(afterware); @@ -214,6 +215,7 @@ export class HTTPFetchNetworkInterface implements NetworkInterface { throw new Error('Afterware must implement the applyAfterware function'); } }); + return this; } } diff --git a/test/networkInterface.ts b/test/networkInterface.ts index 78c82927efc..3d2524f2468 100644 --- a/test/networkInterface.ts +++ b/test/networkInterface.ts @@ -310,6 +310,30 @@ describe('network interface', () => { complexResult, ); }); + + it('should chain use() calls', () => { + const testWare1 = TestWare([ + { key: 'personNum', val: 1 }, + ]); + const testWare2 = TestWare([ + { key: 'filmNum', val: 1 }, + ]); + + const swapi = createNetworkInterface({ uri: swapiUrl }); + swapi.use([testWare1]) + .use([testWare2]); + const simpleRequest = { + query: complexQueryWithTwoVars, + variables: {}, + debugName: 'People query', + }; + + return assert.eventually.deepEqual( + swapi.query(simpleRequest), + complexResult, + ); + }); + }); describe('afterware', () => { @@ -348,6 +372,17 @@ describe('network interface', () => { assert.deepEqual(networkInterface._afterwares, [testWare1, testWare2]); }); + + it('should chain useAfter() calls', () => { + const testWare1 = TestAfterWare(); + const testWare2 = TestAfterWare(); + + const networkInterface = createNetworkInterface({ uri: '/graphql' }); + networkInterface.useAfter([testWare1]) + .useAfter([testWare2]); + + assert.deepEqual(networkInterface._afterwares, [testWare1, testWare2]); + }); }); describe('making a request', () => { From 35f4462059e9f5adc095bb74a8bf41800f7e9868 Mon Sep 17 00:00:00 2001 From: Olivier Ricordeau Date: Mon, 31 Oct 2016 04:56:59 +0100 Subject: [PATCH 2/4] Updated AUTHORS and CHANGELOG.md --- AUTHORS | 1 + CHANGELOG.md | 1 + 2 files changed, 2 insertions(+) diff --git a/AUTHORS b/AUTHORS index 82cb1502069..837ab981e9a 100644 --- a/AUTHORS +++ b/AUTHORS @@ -22,6 +22,7 @@ Marc-Andre Giroux Martijn Walraven Maxime Quandalle Oleksandr Stubailo +Olivier Ricordeau Pavol Fulop Pavol Fulop Robin Ricard diff --git a/CHANGELOG.md b/CHANGELOG.md index 5e33fa6186d..a456619e138 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ Expect active development and potentially significant breaking changes in the `0.x` track. We'll try to be diligent about releasing a `1.0` version in a timely fashion (ideally within 3 to 6 months), to signal the start of a more stable API. ### vNext +- **new Feature**: In network interface, enable chaining the `use` and `useAfter` functions, respectively. [Issue #564](https://github.com/apollostack/apollo-client/issues/564) ### v0.5.0 - Add a `createdBatchingNetworkInterface` function and export it. From fa80bef6fbccb9051f184cf2ccefaa9cc57c6cca Mon Sep 17 00:00:00 2001 From: Olivier Ricordeau Date: Mon, 31 Oct 2016 05:13:28 +0100 Subject: [PATCH 3/4] Added a reference to the PR in CHANGELOG.md --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a456619e138..ac680e4985d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,7 +3,7 @@ Expect active development and potentially significant breaking changes in the `0.x` track. We'll try to be diligent about releasing a `1.0` version in a timely fashion (ideally within 3 to 6 months), to signal the start of a more stable API. ### vNext -- **new Feature**: In network interface, enable chaining the `use` and `useAfter` functions, respectively. [Issue #564](https://github.com/apollostack/apollo-client/issues/564) +- **new Feature**: In network interface, enable chaining the `use` and `useAfter` functions, respectively. [PR #860](https://github.com/apollostack/apollo-client/pull/860) [Issue #564](https://github.com/apollostack/apollo-client/issues/564) ### v0.5.0 - Add a `createdBatchingNetworkInterface` function and export it. From 72d3a46bfb247711881a279b0b50a4752237b4db Mon Sep 17 00:00:00 2001 From: Olivier Ricordeau Date: Tue, 1 Nov 2016 17:16:17 +0100 Subject: [PATCH 4/4] Updated CHANGELOG.md with correct description + Added two new tests for use().useAfter() and useAfter().use() --- CHANGELOG.md | 2 +- test/networkInterface.ts | 23 +++++++++++++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ac680e4985d..4ed009cba21 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,7 +3,7 @@ Expect active development and potentially significant breaking changes in the `0.x` track. We'll try to be diligent about releasing a `1.0` version in a timely fashion (ideally within 3 to 6 months), to signal the start of a more stable API. ### vNext -- **new Feature**: In network interface, enable chaining the `use` and `useAfter` functions, respectively. [PR #860](https://github.com/apollostack/apollo-client/pull/860) [Issue #564](https://github.com/apollostack/apollo-client/issues/564) +- **new Feature**: Enable chaining of `use` and `useAfter` function calls in network interface. [PR #860](https://github.com/apollostack/apollo-client/pull/860) [Issue #564](https://github.com/apollostack/apollo-client/issues/564) ### v0.5.0 - Add a `createdBatchingNetworkInterface` function and export it. diff --git a/test/networkInterface.ts b/test/networkInterface.ts index 3d2524f2468..119aa51c21e 100644 --- a/test/networkInterface.ts +++ b/test/networkInterface.ts @@ -334,6 +334,17 @@ describe('network interface', () => { ); }); + it('should chain use() and useAfter() calls', () => { + const testWare1 = TestWare(); + const testWare2 = TestAfterWare(); + + const networkInterface = createNetworkInterface({ uri: swapiUrl }); + networkInterface.use([testWare1]) + .useAfter([testWare2]); + assert.deepEqual(networkInterface._middlewares, [testWare1]); + assert.deepEqual(networkInterface._afterwares, [testWare2]); + }); + }); describe('afterware', () => { @@ -383,6 +394,18 @@ describe('network interface', () => { assert.deepEqual(networkInterface._afterwares, [testWare1, testWare2]); }); + + it('should chain useAfter() and use() calls', () => { + const testWare1 = TestAfterWare(); + const testWare2 = TestWare(); + + const networkInterface = createNetworkInterface({ uri: swapiUrl }); + networkInterface.useAfter([testWare1]) + .use([testWare2]); + assert.deepEqual(networkInterface._middlewares, [testWare2]); + assert.deepEqual(networkInterface._afterwares, [testWare1]); + }); + }); describe('making a request', () => {