Skip to content

Commit c5cfec7

Browse files
Mattchewonedaffl
authored andcommitted
feat(rest-client): Allow for customising rest clients (#1780)
1 parent 9d767bf commit c5cfec7

2 files changed

Lines changed: 66 additions & 16 deletions

File tree

packages/rest-client/lib/index.js

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,36 @@
1-
const jQuery = require('./jquery');
2-
const Superagent = require('./superagent');
3-
const Request = require('./request');
4-
const Fetch = require('./fetch');
5-
const Axios = require('./axios');
6-
const Angular = require('./angular');
1+
const jQueryClient = require('./jquery');
2+
const SuperagentClient = require('./superagent');
3+
const RequestClient = require('./request');
4+
const FetchClient = require('./fetch');
5+
const AxiosClient = require('./axios');
6+
const AngularClient = require('./angular');
77
const Base = require('./base');
88
const AngularHttpClient = require('./angular-http-client');
99

1010
const transports = {
11-
jquery: jQuery,
12-
superagent: Superagent,
13-
request: Request,
14-
fetch: Fetch,
15-
axios: Axios,
16-
angular: Angular,
11+
jquery: jQueryClient,
12+
superagent: SuperagentClient,
13+
request: RequestClient,
14+
fetch: FetchClient,
15+
axios: AxiosClient,
16+
angular: AngularClient,
1717
angularHttpClient: AngularHttpClient
1818
};
1919

2020
function restClient (base = '') {
2121
const result = { Base };
2222

2323
Object.keys(transports).forEach(key => {
24-
const Service = transports[key];
25-
26-
result[key] = function (connection, options = {}) {
24+
result[key] = function (connection, options = {}, Service = transports[key]) {
2725
if (!connection) {
2826
throw new Error(`${key} has to be provided to feathers-rest`);
2927
}
3028

29+
if (typeof options === 'function') {
30+
Service = options;
31+
options = {};
32+
}
33+
3134
const defaultService = function (name) {
3235
return new Service({ base, name, connection, options });
3336
};
@@ -51,5 +54,5 @@ function restClient (base = '') {
5154
return result;
5255
}
5356

54-
module.exports = restClient;
57+
module.exports = Object.assign(restClient, { SuperagentClient, FetchClient, jQueryClient, RequestClient, AxiosClient, AngularClient, AngularHttpClient });
5558
module.exports.default = restClient;

packages/rest-client/test/index.test.js

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
const fetch = require('node-fetch');
22
const feathers = require('@feathersjs/feathers');
33
const rest = require('../lib/index');
4+
const { FetchClient } = require('../lib/index');
45
const assert = require('assert');
56

67
const init = require('../lib');
@@ -94,4 +95,50 @@ describe('REST client tests', function () {
9495
assert.strictEqual(error.message, `An id must be provided to the 'todos.patch' method`);
9596
});
9697
});
98+
99+
it('uses a custom client', () => {
100+
const app = feathers();
101+
class MyFetchClient extends FetchClient {
102+
find () {
103+
return Promise.resolve({
104+
connection: this.connection,
105+
base: this.base,
106+
message: 'Custom fetch client'
107+
});
108+
}
109+
}
110+
111+
app.configure(rest('http://localhost:8889').fetch(fetch, {}, MyFetchClient));
112+
113+
return app.service('messages').find().then(data => {
114+
assert.deepStrictEqual(data, {
115+
connection: fetch,
116+
base: 'http://localhost:8889/messages',
117+
message: 'Custom fetch client'
118+
});
119+
});
120+
});
121+
122+
it('uses a custom client as second arg', () => {
123+
const app = feathers();
124+
class MyFetchClient extends FetchClient {
125+
find () {
126+
return Promise.resolve({
127+
connection: this.connection,
128+
base: this.base,
129+
message: 'Custom fetch client'
130+
});
131+
}
132+
}
133+
134+
app.configure(rest('http://localhost:8889').fetch(fetch, MyFetchClient));
135+
136+
return app.service('messages').find().then(data => {
137+
assert.deepStrictEqual(data, {
138+
connection: fetch,
139+
base: 'http://localhost:8889/messages',
140+
message: 'Custom fetch client'
141+
});
142+
});
143+
});
97144
});

0 commit comments

Comments
 (0)