Skip to content

Commit

Permalink
feat(rest-client): Allow for customising rest clients (#1780)
Browse files Browse the repository at this point in the history
  • Loading branch information
Mattchewone authored and daffl committed Jan 18, 2020
1 parent 9d767bf commit c5cfec7
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 16 deletions.
35 changes: 19 additions & 16 deletions packages/rest-client/lib/index.js
Original file line number Diff line number Diff line change
@@ -1,33 +1,36 @@
const jQuery = require('./jquery');
const Superagent = require('./superagent');
const Request = require('./request');
const Fetch = require('./fetch');
const Axios = require('./axios');
const Angular = require('./angular');
const jQueryClient = require('./jquery');
const SuperagentClient = require('./superagent');
const RequestClient = require('./request');
const FetchClient = require('./fetch');
const AxiosClient = require('./axios');
const AngularClient = require('./angular');
const Base = require('./base');
const AngularHttpClient = require('./angular-http-client');

const transports = {
jquery: jQuery,
superagent: Superagent,
request: Request,
fetch: Fetch,
axios: Axios,
angular: Angular,
jquery: jQueryClient,
superagent: SuperagentClient,
request: RequestClient,
fetch: FetchClient,
axios: AxiosClient,
angular: AngularClient,
angularHttpClient: AngularHttpClient
};

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

Object.keys(transports).forEach(key => {
const Service = transports[key];

result[key] = function (connection, options = {}) {
result[key] = function (connection, options = {}, Service = transports[key]) {
if (!connection) {
throw new Error(`${key} has to be provided to feathers-rest`);
}

if (typeof options === 'function') {
Service = options;
options = {};
}

const defaultService = function (name) {
return new Service({ base, name, connection, options });
};
Expand All @@ -51,5 +54,5 @@ function restClient (base = '') {
return result;
}

module.exports = restClient;
module.exports = Object.assign(restClient, { SuperagentClient, FetchClient, jQueryClient, RequestClient, AxiosClient, AngularClient, AngularHttpClient });
module.exports.default = restClient;
47 changes: 47 additions & 0 deletions packages/rest-client/test/index.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const fetch = require('node-fetch');
const feathers = require('@feathersjs/feathers');
const rest = require('../lib/index');
const { FetchClient } = require('../lib/index');
const assert = require('assert');

const init = require('../lib');
Expand Down Expand Up @@ -94,4 +95,50 @@ describe('REST client tests', function () {
assert.strictEqual(error.message, `An id must be provided to the 'todos.patch' method`);
});
});

it('uses a custom client', () => {
const app = feathers();
class MyFetchClient extends FetchClient {
find () {
return Promise.resolve({
connection: this.connection,
base: this.base,
message: 'Custom fetch client'
});
}
}

app.configure(rest('http://localhost:8889').fetch(fetch, {}, MyFetchClient));

return app.service('messages').find().then(data => {
assert.deepStrictEqual(data, {
connection: fetch,
base: 'http://localhost:8889/messages',
message: 'Custom fetch client'
});
});
});

it('uses a custom client as second arg', () => {
const app = feathers();
class MyFetchClient extends FetchClient {
find () {
return Promise.resolve({
connection: this.connection,
base: this.base,
message: 'Custom fetch client'
});
}
}

app.configure(rest('http://localhost:8889').fetch(fetch, MyFetchClient));

return app.service('messages').find().then(data => {
assert.deepStrictEqual(data, {
connection: fetch,
base: 'http://localhost:8889/messages',
message: 'Custom fetch client'
});
});
});
});

0 comments on commit c5cfec7

Please sign in to comment.