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

Enable chaining for middlewares and afterwares in the network interface - Issue 564 #860

Merged
merged 4 commits into from Nov 2, 2016
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
1 change: 1 addition & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ Marc-Andre Giroux <[email protected]>
Martijn Walraven <[email protected]>
Maxime Quandalle <[email protected]>
Oleksandr Stubailo <[email protected]>
Olivier Ricordeau <[email protected]>
Pavol Fulop <[email protected]>
Pavol Fulop <[email protected]>
Robin Ricard <[email protected]>
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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**: 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.
Expand Down
10 changes: 6 additions & 4 deletions src/transport/networkInterface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -196,24 +196,26 @@ 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);
} else {
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);
} else {
throw new Error('Afterware must implement the applyAfterware function');
}
});
return this;
}
}

Expand Down
58 changes: 58 additions & 0 deletions test/networkInterface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,41 @@ 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,
);
});

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', () => {
Expand Down Expand Up @@ -348,6 +383,29 @@ 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]);
});

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', () => {
Expand Down