Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Allow for customising rest clients #1780

Merged
merged 4 commits into from
Jan 18, 2020
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
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'
});
});
});
});