diff --git a/packages/rest-client/lib/index.js b/packages/rest-client/lib/index.js index e486b54cf8..bbc1a75458 100644 --- a/packages/rest-client/lib/index.js +++ b/packages/rest-client/lib/index.js @@ -1,19 +1,19 @@ -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 }; @@ -21,13 +21,16 @@ 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 }); }; @@ -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; diff --git a/packages/rest-client/test/index.test.js b/packages/rest-client/test/index.test.js index dd4eb06a2d..e1f6f00e1c 100644 --- a/packages/rest-client/test/index.test.js +++ b/packages/rest-client/test/index.test.js @@ -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'); @@ -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' + }); + }); + }); });