Skip to content

Commit

Permalink
Merge branch 'master' into expose-mutate-options
Browse files Browse the repository at this point in the history
  • Loading branch information
kamilkisiela authored Nov 2, 2016
2 parents 4c188ec + 0290b7d commit b85817a
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 4 deletions.
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)

- Create and expose the `MutateOptions` [PR #866](https://github.com/apollostack/apollo-client/pull/866)

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

0 comments on commit b85817a

Please sign in to comment.