@feathersjs/rest-client 5.0.0-pre.10

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/base.d.ts ADDED
@@ -0,0 +1,25 @@
1
+ import { Params, Id, Query, NullableId, ServiceInterface } from '@feathersjs/feathers';
2
+ interface RestClientSettings {
3
+ name: string;
4
+ base: string;
5
+ connection: any;
6
+ options: any;
7
+ }
8
+ export declare abstract class Base<T = any, D = Partial<T>> implements ServiceInterface<T, D> {
9
+ name: string;
10
+ base: string;
11
+ connection: any;
12
+ options: any;
13
+ constructor(settings: RestClientSettings);
14
+ makeUrl(query: Query, id?: string | number | null): string;
15
+ getQuery(query: Query): string;
16
+ abstract request(options: any, params: Params): any;
17
+ methods(this: any, ...names: string[]): any;
18
+ find(params?: Params): any;
19
+ get(id: Id, params?: Params): any;
20
+ create(body: D, params?: Params): any;
21
+ update(id: NullableId, body: D, params?: Params): any;
22
+ patch(id: NullableId, body: D, params?: Params): any;
23
+ remove(id: NullableId, params?: Params): any;
24
+ }
25
+ export {};
package/lib/base.js ADDED
@@ -0,0 +1,113 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.Base = void 0;
7
+ const qs_1 = __importDefault(require("qs"));
8
+ const errors_1 = require("@feathersjs/errors");
9
+ const commons_1 = require("@feathersjs/commons");
10
+ function toError(error) {
11
+ if (error.code === 'ECONNREFUSED') {
12
+ throw new errors_1.Unavailable(error.message, commons_1._.pick(error, 'address', 'port', 'config'));
13
+ }
14
+ throw (0, errors_1.convert)(error);
15
+ }
16
+ class Base {
17
+ constructor(settings) {
18
+ this.name = (0, commons_1.stripSlashes)(settings.name);
19
+ this.options = settings.options;
20
+ this.connection = settings.connection;
21
+ this.base = `${settings.base}/${this.name}`;
22
+ }
23
+ makeUrl(query, id) {
24
+ let url = this.base;
25
+ query = query || {};
26
+ if (typeof id !== 'undefined' && id !== null) {
27
+ url += `/${encodeURIComponent(id)}`;
28
+ }
29
+ return url + this.getQuery(query);
30
+ }
31
+ getQuery(query) {
32
+ if (Object.keys(query).length !== 0) {
33
+ const queryString = qs_1.default.stringify(query);
34
+ return `?${queryString}`;
35
+ }
36
+ return '';
37
+ }
38
+ methods(...names) {
39
+ names.forEach(method => {
40
+ this[method] = function (body, params = {}) {
41
+ return this.request({
42
+ body,
43
+ url: this.makeUrl(params.query),
44
+ method: 'POST',
45
+ headers: Object.assign({
46
+ 'Content-Type': 'application/json',
47
+ 'X-Service-Method': method
48
+ }, params.headers)
49
+ }, params).catch(toError);
50
+ };
51
+ });
52
+ return this;
53
+ }
54
+ find(params = {}) {
55
+ return this.request({
56
+ url: this.makeUrl(params.query),
57
+ method: 'GET',
58
+ headers: Object.assign({}, params.headers)
59
+ }, params).catch(toError);
60
+ }
61
+ get(id, params = {}) {
62
+ if (typeof id === 'undefined') {
63
+ return Promise.reject(new Error('id for \'get\' can not be undefined'));
64
+ }
65
+ return this.request({
66
+ url: this.makeUrl(params.query, id),
67
+ method: 'GET',
68
+ headers: Object.assign({}, params.headers)
69
+ }, params).catch(toError);
70
+ }
71
+ create(body, params = {}) {
72
+ return this.request({
73
+ url: this.makeUrl(params.query),
74
+ body,
75
+ method: 'POST',
76
+ headers: Object.assign({ 'Content-Type': 'application/json' }, params.headers)
77
+ }, params).catch(toError);
78
+ }
79
+ update(id, body, params = {}) {
80
+ if (typeof id === 'undefined') {
81
+ return Promise.reject(new Error('id for \'update\' can not be undefined, only \'null\' when updating multiple entries'));
82
+ }
83
+ return this.request({
84
+ url: this.makeUrl(params.query, id),
85
+ body,
86
+ method: 'PUT',
87
+ headers: Object.assign({ 'Content-Type': 'application/json' }, params.headers)
88
+ }, params).catch(toError);
89
+ }
90
+ patch(id, body, params = {}) {
91
+ if (typeof id === 'undefined') {
92
+ return Promise.reject(new Error('id for \'patch\' can not be undefined, only \'null\' when updating multiple entries'));
93
+ }
94
+ return this.request({
95
+ url: this.makeUrl(params.query, id),
96
+ body,
97
+ method: 'PATCH',
98
+ headers: Object.assign({ 'Content-Type': 'application/json' }, params.headers)
99
+ }, params).catch(toError);
100
+ }
101
+ remove(id, params = {}) {
102
+ if (typeof id === 'undefined') {
103
+ return Promise.reject(new Error('id for \'remove\' can not be undefined, only \'null\' when removing multiple entries'));
104
+ }
105
+ return this.request({
106
+ url: this.makeUrl(params.query, id),
107
+ method: 'DELETE',
108
+ headers: Object.assign({}, params.headers)
109
+ }, params).catch(toError);
110
+ }
111
+ }
112
+ exports.Base = Base;
113
+ //# sourceMappingURL=base.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"base.js","sourceRoot":"","sources":["../src/base.ts"],"names":[],"mappings":";;;;;;AAAA,4CAAqB;AAErB,+CAA2D;AAC3D,iDAAsD;AAEtD,SAAS,OAAO,CAAE,KAA+B;IAC/C,IAAI,KAAK,CAAC,IAAI,KAAK,cAAc,EAAE;QACjC,MAAM,IAAI,oBAAW,CAAC,KAAK,CAAC,OAAO,EAAE,WAAC,CAAC,IAAI,CAAC,KAAK,EAAE,SAAS,EAAE,MAAM,EAAE,QAAQ,CAAC,CAAC,CAAC;KAClF;IAED,MAAM,IAAA,gBAAO,EAAC,KAAK,CAAC,CAAC;AACvB,CAAC;AASD,MAAsB,IAAI;IAMxB,YAAa,QAA4B;QACvC,IAAI,CAAC,IAAI,GAAG,IAAA,sBAAY,EAAC,QAAQ,CAAC,IAAI,CAAC,CAAC;QACxC,IAAI,CAAC,OAAO,GAAG,QAAQ,CAAC,OAAO,CAAC;QAChC,IAAI,CAAC,UAAU,GAAG,QAAQ,CAAC,UAAU,CAAC;QACtC,IAAI,CAAC,IAAI,GAAG,GAAG,QAAQ,CAAC,IAAI,IAAI,IAAI,CAAC,IAAI,EAAE,CAAC;IAC9C,CAAC;IAED,OAAO,CAAE,KAAY,EAAE,EAAuB;QAC5C,IAAI,GAAG,GAAG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AC;IAC5B,CAAC;CACF;AAzHD,oBAyHC"}
package/lib/fetch.d.ts ADDED
@@ -0,0 +1,6 @@
1
+ import { Params } from '@feathersjs/feathers';
2
+ import { Base } from './base';
3
+ export declare class FetchClient extends Base {
4
+ request(options: any, params: Params): any;
5
+ checkStatus(response: any): any;
6
+ }
package/lib/fetch.js ADDED
@@ -0,0 +1,38 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.FetchClient = void 0;
4
+ const errors_1 = require("@feathersjs/errors");
5
+ const base_1 = require("./base");
6
+ class FetchClient extends base_1.Base {
7
+ request(options, params) {
8
+ const fetchOptions = Object.assign({}, options, params.connection);
9
+ fetchOptions.headers = Object.assign({
10
+ Accept: 'application/json'
11
+ }, this.options.headers, fetchOptions.headers);
12
+ if (options.body) {
13
+ fetchOptions.body = JSON.stringify(options.body);
14
+ }
15
+ return this.connection(options.url, fetchOptions)
16
+ .then(this.checkStatus)
17
+ .then((response) => {
18
+ if (response.status === 204) {
19
+ return null;
20
+ }
21
+ return response.json();
22
+ });
23
+ }
24
+ checkStatus(response) {
25
+ if (response.ok) {
26
+ return response;
27
+ }
28
+ return response.json().catch(() => {
29
+ const ErrorClass = errors_1.errors[response.status] || Error;
30
+ return new ErrorClass('JSON parsing error');
31
+ }).then((error) => {
32
+ error.response = response;
33
+ throw error;
34
+ });
35
+ }
36
+ }
37
+ exports.FetchClient = FetchClient;
38
+ //# sourceMappingURL=fetch.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"fetch.js","sourceRoot":"","sources":["../src/fetch.ts"],"names":[],"mappings":";;;AACA,+CAA4C;AAC5C,iCAA8B;AAE9B,MAAa,WAAY,SAAQ,WAAI;IACnC,OAAO,CAAE,OAAY,EAAE,MAAc;QACnC,MAAM,YAAY,GAAG,MAAM,CAAC,MAAM,CAAC,EAAE,EAAE,OAAO,EAAE,MAAM,CAAC,UAAU,CAAC,CAAC;QAEnE,YAAY,CAAC,OAAO,GAAG,MAAM,CAAC,MAAM,CAAC;YACnC,MAAM,EAAE,kBAAkB;SAC3B,EAAE,IAAI,CAAC,OAAO,CAAC,OAAO,EAAE,YAAY,CAAC,OAAO,CAAC,CAAC;QAE/C,IAAI,OAAO,CAAC,IAAI,EAAE;YAChB,YAAY,CAAC,IAAI,GAAG,IAAI,CAAC,SAAS,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC;SAClD;QAED,OAAO,IAAI,CAAC,UAAU,CAAC,OAAO,CAAC,GAAG,EAAE,YAAY,CAAC;aAC9C,IAAI,CAAC,IAAI,CAAC,WAAW,CAAC;aACtB,IAAI,CAAC,CAAC,QAAa,EAAE,EAAE;YACtB,IAAI,QAAQ,CAAC,MAAM,KAAK,GAAG,EAAE;gBAC3B,OAAO,IAAI,CAAC;aACb;YAED,OAAO,QAAQ,CAAC,IAAI,EAAE,CAAC;QACzB,CAAC,CAAC,CAAC;IACP,CAAC;IAED,WAAW,CAAE,QAAa;QACxB,IAAI,QAAQ,CAAC,EAAE,EAAE;YACf,OAAO,QAAQ,CAAC;SACjB;QAED,OAAO,QAAQ,CAAC,IAAI,EAAE,CAAC,KAAK,CAAC,GAAG,EAAE;YAChC,MAAM,UAAU,GAAI,eAAc,CAAC,QAAQ,CAAC,MAAM,CAAC,IAAI,KAAK,CAAC;YAE7D,OAAO,IAAI,UAAU,CAAC,oBAAoB,CAAC,CAAC;QAC9C,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,KAAU,EAAE,EAAE;YACrB,KAAK,CAAC,QAAQ,GAAG,QAAQ,CAAC;YAC1B,MAAM,KAAK,CAAC;QACd,CAAC,CAAC,CAAC;IACL,CAAC;CACF;AArCD,kCAqCC"}
package/lib/index.d.ts ADDED
@@ -0,0 +1,27 @@
1
+ import { Base } from './base';
2
+ import { AxiosClient } from './axios';
3
+ import { FetchClient } from './fetch';
4
+ import { SuperagentClient } from './superagent';
5
+ export { AxiosClient, FetchClient, SuperagentClient };
6
+ interface HandlerResult extends Function {
7
+ /**
8
+ * initialize service
9
+ */
10
+ (): void;
11
+ /**
12
+ * Transport Service
13
+ */
14
+ Service: any;
15
+ /**
16
+ * default Service
17
+ */
18
+ service: any;
19
+ }
20
+ export declare type Handler = (connection: any, options?: any, Service?: any) => HandlerResult;
21
+ export interface Transport {
22
+ superagent: Handler;
23
+ fetch: Handler;
24
+ axios: Handler;
25
+ }
26
+ export declare type RestService<T = any, D = Partial<any>> = Base<T, D>;
27
+ export default function restClient(base?: string): Transport;
package/lib/index.js ADDED
@@ -0,0 +1,48 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.SuperagentClient = exports.FetchClient = exports.AxiosClient = void 0;
4
+ const base_1 = require("./base");
5
+ const axios_1 = require("./axios");
6
+ Object.defineProperty(exports, "AxiosClient", { enumerable: true, get: function () { return axios_1.AxiosClient; } });
7
+ const fetch_1 = require("./fetch");
8
+ Object.defineProperty(exports, "FetchClient", { enumerable: true, get: function () { return fetch_1.FetchClient; } });
9
+ const superagent_1 = require("./superagent");
10
+ Object.defineProperty(exports, "SuperagentClient", { enumerable: true, get: function () { return superagent_1.SuperagentClient; } });
11
+ const transports = {
12
+ superagent: superagent_1.SuperagentClient,
13
+ fetch: fetch_1.FetchClient,
14
+ axios: axios_1.AxiosClient
15
+ };
16
+ function restClient(base = '') {
17
+ const result = { Base: base_1.Base };
18
+ Object.keys(transports).forEach(key => {
19
+ result[key] = function (connection, options = {}, Service = transports[key]) {
20
+ if (!connection) {
21
+ throw new Error(`${key} has to be provided to feathers-rest`);
22
+ }
23
+ if (typeof options === 'function') {
24
+ Service = options;
25
+ options = {};
26
+ }
27
+ const defaultService = function (name) {
28
+ return new Service({ base, name, connection, options });
29
+ };
30
+ const initialize = (app) => {
31
+ if (app.rest !== undefined) {
32
+ throw new Error('Only one default client provider can be configured');
33
+ }
34
+ app.rest = connection;
35
+ app.defaultService = defaultService;
36
+ };
37
+ initialize.Service = Service;
38
+ initialize.service = defaultService;
39
+ return initialize;
40
+ };
41
+ });
42
+ return result;
43
+ }
44
+ exports.default = restClient;
45
+ if (typeof module !== 'undefined') {
46
+ module.exports = Object.assign(restClient, module.exports);
47
+ }
48
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;AAAA,iCAA8B;AAC9B,mCAAsC;AAI7B,4FAJA,mBAAW,OAIA;AAHpB,mCAAsC;AAGhB,4FAHb,mBAAW,OAGa;AAFjC,6CAAgD;AAEb,iGAF1B,6BAAgB,OAE0B;AAEnD,MAAM,UAAU,GAAG;IACjB,UAAU,EAAE,6BAAgB;IAC5B,KAAK,EAAE,mBAAW;IAClB,KAAK,EAAE,mBAAW;CACnB,CAAC;AA6BF,SAAwB,UAAU,CAAE,IAAI,GAAG,EAAE;IAC3C,MAAM,MAAM,GAAQ,EAAE,IAAI,EAAJ,WAAI,EAAE,CAAC;IAE7B,MAAM,CAAC,IAAI,CAAC,UAAU,CAAC,CAAC,OAAO,CAAC,GAAG,CAAC,EAAE;QACpC,MAAM,CAAC,GAAG,CAAC,GAAG,UAAU,UAAe,EAAE,UAAe,EAAE,EAAE,UAAiB,UAAkB,CAAC,GAAG,CAAC;YAClG,IAAI,CAAC,UAAU,EAAE;gBACf,MAAM,IAAI,KAAK,CAAC,GAAG,GAAG,sCAAsC,CAAC,CAAC;aAC/D;YAED,IAAI,OAAO,OAAO,KAAK,UAAU,EAAE;gBACjC,OAAO,GAAG,OAAO,CAAC;gBAClB,OAAO,GAAG,EAAE,CAAC;aACd;YAED,MAAM,cAAc,GAAG,UAAU,IAAY;gBAC3C,OAAO,IAAK,OAAe,CAAC,EAAE,IAAI,EAAE,IAAI,EAAE,UAAU,EAAE,OAAO,EAAE,CAAC,CAAC;YACnE,CAAC,CAAC;YAEF,MAAM,UAAU,GAAG,CAAC,GAAQ,EAAE,EAAE;gBAC9B,IAAI,GAAG,CAAC,IAAI,KAAK,SAAS,EAAE;oBAC1B,MAAM,IAAI,KAAK,CAAC,oDAAoD,CAAC,CAAC;iBACvE;gBAED,GAAG,CAAC,IAAI,GAAG,UAAU,CAAC;gBACtB,GAAG,CAAC,cAAc,GAAG,cAAc,CAAC;YACtC,CAAC,CAAC;YAEF,UAAU,CAAC,OAAO,GAAG,OAAO,CAAC;YAC7B,UAAU,CAAC,OAAO,GAAG,cAAc,CAAC;YAEpC,OAAO,UAAU,CAAC;QACpB,CAAC,CAAC;IACJ,CAAC,CAAC,CAAC;IAEH,OAAO,MAAmB,CAAC;AAC7B,CAAC;AAnCD,6BAmCC;AAED,IAAI,OAAO,MAAM,KAAK,WAAW,EAAE;IACjC,MAAM,CAAC,OAAO,GAAG,MAAM,CAAC,MAAM,CAAC,UAAU,EAAE,MAAM,CAAC,OAAO,CAAC,CAAC;CAC5D"}
@@ -0,0 +1,5 @@
1
+ import { Params } from '@feathersjs/feathers';
2
+ import { Base } from './base';
3
+ export declare class SuperagentClient extends Base {
4
+ request(options: any, params: Params): Promise<unknown>;
5
+ }
@@ -0,0 +1,34 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.SuperagentClient = void 0;
4
+ const base_1 = require("./base");
5
+ class SuperagentClient extends base_1.Base {
6
+ request(options, params) {
7
+ const superagent = this.connection(options.method, options.url)
8
+ .set(this.options.headers || {})
9
+ .set('Accept', 'application/json')
10
+ .set(params.connection || {})
11
+ .set(options.headers || {})
12
+ .type(options.type || 'json');
13
+ return new Promise((resolve, reject) => {
14
+ superagent.set(options.headers);
15
+ if (options.body) {
16
+ superagent.send(options.body);
17
+ }
18
+ superagent.end(function (error, res) {
19
+ if (error) {
20
+ try {
21
+ const response = error.response;
22
+ error = JSON.parse(error.response.text);
23
+ error.response = response;
24
+ }
25
+ catch (e) { }
26
+ return reject(error);
27
+ }
28
+ resolve(res && res.body);
29
+ });
30
+ });
31
+ }
32
+ }
33
+ exports.SuperagentClient = SuperagentClient;
34
+ //# sourceMappingURL=superagent.js.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,CAAC;QACL,CAAC,CAAC,CAAC;IACL,CAAC;CACF;AA/BD,4CA+BC"}
package/package.json ADDED
@@ -0,0 +1,76 @@
1
+ {
2
+ "name": "@feathersjs/rest-client",
3
+ "description": "REST client services for different Ajax libraries",
4
+ "version": "5.0.0-pre.10",
5
+ "homepage": "https://feathersjs.com",
6
+ "main": "lib/index.js",
7
+ "keywords": [
8
+ "feathers",
9
+ "feathers-plugin"
10
+ ],
11
+ "license": "MIT",
12
+ "funding": {
13
+ "type": "github",
14
+ "url": "https://github.com/sponsors/daffl"
15
+ },
16
+ "repository": {
17
+ "type": "git",
18
+ "url": "git://github.com/feathersjs/feathers.git"
19
+ },
20
+ "author": {
21
+ "name": "Feathers contributors",
22
+ "email": "hello@feathersjs.com",
23
+ "url": "https://feathersjs.com"
24
+ },
25
+ "contributors": [],
26
+ "bugs": {
27
+ "url": "https://github.com/feathersjs/feathers/issues"
28
+ },
29
+ "engines": {
30
+ "node": ">= 12"
31
+ },
32
+ "files": [
33
+ "CHANGELOG.md",
34
+ "LICENSE",
35
+ "README.md",
36
+ "src/**",
37
+ "lib/**",
38
+ "*.d.ts",
39
+ "*.js"
40
+ ],
41
+ "scripts": {
42
+ "prepublish": "npm run compile",
43
+ "compile": "shx rm -rf lib/ && tsc",
44
+ "test": "mocha --config ../../.mocharc.json --recursive test/**.test.ts test/**/*.test.ts"
45
+ },
46
+ "directories": {
47
+ "lib": "lib"
48
+ },
49
+ "publishConfig": {
50
+ "access": "public"
51
+ },
52
+ "dependencies": {
53
+ "@feathersjs/commons": "^5.0.0-pre.10",
54
+ "@feathersjs/errors": "^5.0.0-pre.10",
55
+ "@types/node-fetch": "^3.0.2",
56
+ "@types/superagent": "^4.1.13",
57
+ "qs": "^6.10.1"
58
+ },
59
+ "devDependencies": {
60
+ "@feathersjs/express": "^5.0.0-pre.10",
61
+ "@feathersjs/feathers": "^5.0.0-pre.10",
62
+ "@feathersjs/memory": "^5.0.0-pre.10",
63
+ "@feathersjs/tests": "^5.0.0-pre.10",
64
+ "@types/mocha": "^9.0.0",
65
+ "@types/node": "^16.9.4",
66
+ "axios": "^0.21.4",
67
+ "mocha": "^9.1.1",
68
+ "node-fetch": "^2.6.1",
69
+ "rxjs": "^7.3.0",
70
+ "shx": "^0.3.3",
71
+ "superagent": "^6.1.0",
72
+ "ts-node": "^10.2.1",
73
+ "typescript": "^4.4.3"
74
+ },
75
+ "gitHead": "a9f7865cce8db2305b7c0d2ef4a165c2724034ef"
76
+ }
package/src/axios.ts ADDED
@@ -0,0 +1,23 @@
1
+ import { Params } from '@feathersjs/feathers';
2
+ import { Base } from './base';
3
+
4
+ export class AxiosClient extends Base {
5
+ request (options: any, params: Params) {
6
+ const config = Object.assign({
7
+ url: options.url,
8
+ method: options.method,
9
+ data: options.body,
10
+ headers: Object.assign({
11
+ Accept: 'application/json'
12
+ }, this.options.headers, options.headers)
13
+ }, params.connection);
14
+
15
+ return this.connection.request(config)
16
+ .then((res: any) => res.data)
17
+ .catch((error: any) => {
18
+ const response = error.response || error;
19
+
20
+ throw response instanceof Error ? response : (response.data || response);
21
+ });
22
+ }
23
+ }
package/src/base.ts ADDED
@@ -0,0 +1,142 @@
1
+ import qs from 'qs';
2
+ import { Params, Id, Query, NullableId, ServiceInterface } from '@feathersjs/feathers';
3
+ import { Unavailable, convert } from '@feathersjs/errors';
4
+ import { _, stripSlashes } from '@feathersjs/commons';
5
+
6
+ function toError (error: Error & { code: string }) {
7
+ if (error.code === 'ECONNREFUSED') {
8
+ throw new Unavailable(error.message, _.pick(error, 'address', 'port', 'config'));
9
+ }
10
+
11
+ throw convert(error);
12
+ }
13
+
14
+ interface RestClientSettings {
15
+ name: string;
16
+ base: string;
17
+ connection: any;
18
+ options: any;
19
+ }
20
+
21
+ export abstract class Base<T = any, D = Partial<T>> implements ServiceInterface<T, D> {
22
+ name: string;
23
+ base: string;
24
+ connection: any;
25
+ options: any;
26
+
27
+ constructor (settings: RestClientSettings) {
28
+ this.name = stripSlashes(settings.name);
29
+ this.options = settings.options;
30
+ this.connection = settings.connection;
31
+ this.base = `${settings.base}/${this.name}`;
32
+ }
33
+
34
+ makeUrl (query: Query, id?: string|number|null) {
35
+ let url = this.base;
36
+
37
+ query = query || {};
38
+
39
+ if (typeof id !== 'undefined' && id !== null) {
40
+ url += `/${encodeURIComponent(id)}`;
41
+ }
42
+
43
+ return url + this.getQuery(query);
44
+ }
45
+
46
+ getQuery (query: Query) {
47
+ if (Object.keys(query).length !== 0) {
48
+ const queryString = qs.stringify(query);
49
+
50
+ return `?${queryString}`;
51
+ }
52
+
53
+ return '';
54
+ }
55
+
56
+ abstract request (options: any, params: Params): any;
57
+
58
+ methods (this: any, ...names: string[]) {
59
+ names.forEach(method => {
60
+ this[method] = function (body: any, params: Params = {}) {
61
+ return this.request({
62
+ body,
63
+ url: this.makeUrl(params.query),
64
+ method: 'POST',
65
+ headers: Object.assign({
66
+ 'Content-Type': 'application/json',
67
+ 'X-Service-Method': method
68
+ }, params.headers)
69
+ }, params).catch(toError);
70
+ }
71
+ });
72
+
73
+ return this;
74
+ }
75
+
76
+ find (params: Params = {}) {
77
+ return this.request({
78
+ url: this.makeUrl(params.query),
79
+ method: 'GET',
80
+ headers: Object.assign({}, params.headers)
81
+ }, params).catch(toError);
82
+ }
83
+
84
+ get (id: Id, params: Params = {}) {
85
+ if (typeof id === 'undefined') {
86
+ return Promise.reject(new Error('id for \'get\' can not be undefined'));
87
+ }
88
+
89
+ return this.request({
90
+ url: this.makeUrl(params.query, id),
91
+ method: 'GET',
92
+ headers: Object.assign({}, params.headers)
93
+ }, params).catch(toError);
94
+ }
95
+
96
+ create (body: D, params: Params = {}) {
97
+ return this.request({
98
+ url: this.makeUrl(params.query),
99
+ body,
100
+ method: 'POST',
101
+ headers: Object.assign({ 'Content-Type': 'application/json' }, params.headers)
102
+ }, params).catch(toError);
103
+ }
104
+
105
+ update (id: NullableId, body: D, params: Params = {}) {
106
+ if (typeof id === 'undefined') {
107
+ return Promise.reject(new Error('id for \'update\' can not be undefined, only \'null\' when updating multiple entries'));
108
+ }
109
+
110
+ return this.request({
111
+ url: this.makeUrl(params.query, id),
112
+ body,
113
+ method: 'PUT',
114
+ headers: Object.assign({ 'Content-Type': 'application/json' }, params.headers)
115
+ }, params).catch(toError);
116
+ }
117
+
118
+ patch (id: NullableId, body: D, params: Params = {}) {
119
+ if (typeof id === 'undefined') {
120
+ return Promise.reject(new Error('id for \'patch\' can not be undefined, only \'null\' when updating multiple entries'));
121
+ }
122
+
123
+ return this.request({
124
+ url: this.makeUrl(params.query, id),
125
+ body,
126
+ method: 'PATCH',
127
+ headers: Object.assign({ 'Content-Type': 'application/json' }, params.headers)
128
+ }, params).catch(toError);
129
+ }
130
+
131
+ remove (id: NullableId, params: Params = {}) {
132
+ if (typeof id === 'undefined') {
133
+ return Promise.reject(new Error('id for \'remove\' can not be undefined, only \'null\' when removing multiple entries'));
134
+ }
135
+
136
+ return this.request({
137
+ url: this.makeUrl(params.query, id),
138
+ method: 'DELETE',
139
+ headers: Object.assign({}, params.headers)
140
+ }, params).catch(toError);
141
+ }
142
+ }
package/src/fetch.ts ADDED
@@ -0,0 +1,42 @@
1
+ import { Params } from '@feathersjs/feathers';
2
+ import { errors } from '@feathersjs/errors';
3
+ import { Base } from './base';
4
+
5
+ export class FetchClient extends Base {
6
+ request (options: any, params: Params) {
7
+ const fetchOptions = Object.assign({}, options, params.connection);
8
+
9
+ fetchOptions.headers = Object.assign({
10
+ Accept: 'application/json'
11
+ }, this.options.headers, fetchOptions.headers);
12
+
13
+ if (options.body) {
14
+ fetchOptions.body = JSON.stringify(options.body);
15
+ }
16
+
17
+ return this.connection(options.url, fetchOptions)
18
+ .then(this.checkStatus)
19
+ .then((response: any) => {
20
+ if (response.status === 204) {
21
+ return null;
22
+ }
23
+
24
+ return response.json();
25
+ });
26
+ }
27
+
28
+ checkStatus (response: any) {
29
+ if (response.ok) {
30
+ return response;
31
+ }
32
+
33
+ return response.json().catch(() => {
34
+ const ErrorClass = (errors as any)[response.status] || Error;
35
+
36
+ return new ErrorClass('JSON parsing error');
37
+ }).then((error: any) => {
38
+ error.response = response;
39
+ throw error;
40
+ });
41
+ }
42
+ }