From d40e569e2f68664f582f55209b6af92d5b33b5b2 Mon Sep 17 00:00:00 2001 From: Matt Chaffe Date: Fri, 10 Jan 2020 15:51:15 +0000 Subject: [PATCH 1/3] lint fixes --- packages/transport-commons/src/channels/index.ts | 4 ++-- packages/transport-commons/src/client.ts | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/transport-commons/src/channels/index.ts b/packages/transport-commons/src/channels/index.ts index 84d43bfbc1..2441a0437b 100644 --- a/packages/transport-commons/src/channels/index.ts +++ b/packages/transport-commons/src/channels/index.ts @@ -90,10 +90,10 @@ export function channels () { if (!result) { return; } - + const results = Array.isArray(result) ? compact(flattenDeep(result)) : [result]; const channel = new CombinedChannel(results); - + if (channel && channel.length > 0) { app.emit('publish', event, channel, hook, data); } else { diff --git a/packages/transport-commons/src/client.ts b/packages/transport-commons/src/client.ts index 6aec89790d..872aa060ab 100644 --- a/packages/transport-commons/src/client.ts +++ b/packages/transport-commons/src/client.ts @@ -131,7 +131,7 @@ export class Service { off (name: string, ...args: any[]) { if (typeof this.connection.off === 'function') { const result = this.connection.off(`${this.path} ${name}`, ...args); - + return result === this.connection ? this : result; } else if (args.length === 0) { // @ts-ignore From e77080e54ee0eb363bd51ed136c5dad3f4575edd Mon Sep 17 00:00:00 2001 From: Matt Chaffe Date: Fri, 10 Jan 2020 15:51:38 +0000 Subject: [PATCH 2/3] allow for clients to be extended --- packages/rest-client/lib/index.js | 30 ++++++++++++------------- packages/rest-client/test/index.test.js | 24 ++++++++++++++++++++ 2 files changed, 38 insertions(+), 16 deletions(-) diff --git a/packages/rest-client/lib/index.js b/packages/rest-client/lib/index.js index e486b54cf8..3adf3e0d81 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,9 +21,7 @@ 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`); } @@ -51,5 +49,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..38fb168aae 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,27 @@ 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' + }); + }); + }); }); From bf0f0ad35044258f1eafad153311258390149e7d Mon Sep 17 00:00:00 2001 From: Matt Chaffe Date: Fri, 10 Jan 2020 16:57:49 +0000 Subject: [PATCH 3/3] allow for custom client to be used without options --- packages/rest-client/lib/index.js | 5 +++++ packages/rest-client/test/index.test.js | 23 +++++++++++++++++++++++ 2 files changed, 28 insertions(+) diff --git a/packages/rest-client/lib/index.js b/packages/rest-client/lib/index.js index 3adf3e0d81..bbc1a75458 100644 --- a/packages/rest-client/lib/index.js +++ b/packages/rest-client/lib/index.js @@ -26,6 +26,11 @@ function restClient (base = '') { 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 }); }; diff --git a/packages/rest-client/test/index.test.js b/packages/rest-client/test/index.test.js index 38fb168aae..e1f6f00e1c 100644 --- a/packages/rest-client/test/index.test.js +++ b/packages/rest-client/test/index.test.js @@ -118,4 +118,27 @@ describe('REST client tests', function () { }); }); }); + + 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' + }); + }); + }); });