@feathersjs/rest-client 5.0.0-pre.22 → 5.0.0-pre.25
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/CHANGELOG.md +35 -0
- package/lib/axios.d.ts +2 -1
- package/lib/axios.js +3 -2
- package/lib/axios.js.map +1 -1
- package/lib/base.d.ts +8 -8
- package/lib/base.js +11 -11
- package/lib/base.js.map +1 -1
- package/lib/fetch.d.ts +2 -1
- package/lib/fetch.js +5 -2
- package/lib/fetch.js.map +1 -1
- package/lib/index.d.ts +7 -20
- package/lib/index.js +8 -1
- package/lib/index.js.map +1 -1
- package/lib/superagent.d.ts +2 -1
- package/lib/superagent.js.map +1 -1
- package/package.json +13 -13
- package/src/axios.ts +24 -15
- package/src/base.ts +121 -89
- package/src/fetch.ts +32 -24
- package/src/index.ts +44 -48
- package/src/superagent.ts +18 -13
package/CHANGELOG.md
CHANGED
|
@@ -3,6 +3,41 @@
|
|
|
3
3
|
All notable changes to this project will be documented in this file.
|
|
4
4
|
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
|
|
5
5
|
|
|
6
|
+
# [5.0.0-pre.25](https://github.com/feathersjs/feathers/compare/v5.0.0-pre.24...v5.0.0-pre.25) (2022-06-22)
|
|
7
|
+
|
|
8
|
+
**Note:** Version bump only for package @feathersjs/rest-client
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
# [5.0.0-pre.24](https://github.com/feathersjs/feathers/compare/v5.0.0-pre.23...v5.0.0-pre.24) (2022-06-21)
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
### Features
|
|
18
|
+
|
|
19
|
+
* **cli:** Add typed client to a generated app ([#2669](https://github.com/feathersjs/feathers/issues/2669)) ([5b801b5](https://github.com/feathersjs/feathers/commit/5b801b5017ddc3eaa95622b539f51d605916bc86))
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
# [5.0.0-pre.23](https://github.com/feathersjs/feathers/compare/v5.0.0-pre.22...v5.0.0-pre.23) (2022-06-06)
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
### Bug Fixes
|
|
29
|
+
|
|
30
|
+
* **express:** Ensure Express options can be set before configuring REST transport ([#2655](https://github.com/feathersjs/feathers/issues/2655)) ([c9b8f74](https://github.com/feathersjs/feathers/commit/c9b8f74a0196acb99be44ac5e0fff3f1128288cd))
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
### Features
|
|
34
|
+
|
|
35
|
+
* **client:** Improve client side custom method support ([#2654](https://github.com/feathersjs/feathers/issues/2654)) ([c138acf](https://github.com/feathersjs/feathers/commit/c138acf50affbe6b66177d084d3c7a3e9220f09f))
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
|
|
6
41
|
# [5.0.0-pre.22](https://github.com/feathersjs/feathers/compare/v5.0.0-pre.21...v5.0.0-pre.22) (2022-05-24)
|
|
7
42
|
|
|
8
43
|
**Note:** Version bump only for package @feathersjs/rest-client
|
package/lib/axios.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
+
import { Params } from '@feathersjs/feathers';
|
|
1
2
|
import { Base, RestClientParams } from './base';
|
|
2
|
-
export declare class AxiosClient extends Base {
|
|
3
|
+
export declare class AxiosClient<T = any, D = Partial<T>, P extends Params = RestClientParams> extends Base<T, D, P> {
|
|
3
4
|
request(options: any, params: RestClientParams): any;
|
|
4
5
|
}
|
package/lib/axios.js
CHANGED
|
@@ -12,11 +12,12 @@ class AxiosClient extends base_1.Base {
|
|
|
12
12
|
Accept: 'application/json'
|
|
13
13
|
}, this.options.headers, options.headers)
|
|
14
14
|
}, params.connection);
|
|
15
|
-
return this.connection
|
|
15
|
+
return this.connection
|
|
16
|
+
.request(config)
|
|
16
17
|
.then((res) => res.data)
|
|
17
18
|
.catch((error) => {
|
|
18
19
|
const response = error.response || error;
|
|
19
|
-
throw response instanceof Error ? response :
|
|
20
|
+
throw response instanceof Error ? response : response.data || response;
|
|
20
21
|
});
|
|
21
22
|
}
|
|
22
23
|
}
|
package/lib/axios.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"axios.js","sourceRoot":"","sources":["../src/axios.ts"],"names":[],"mappings":";;;
|
|
1
|
+
{"version":3,"file":"axios.js","sourceRoot":"","sources":["../src/axios.ts"],"names":[],"mappings":";;;AACA,iCAA+C;AAE/C,MAAa,WAA0E,SAAQ,WAAa;IAC1G,OAAO,CAAC,OAAY,EAAE,MAAwB;QAC5C,MAAM,MAAM,GAAG,MAAM,CAAC,MAAM,CAC1B;YACE,GAAG,EAAE,OAAO,CAAC,GAAG;YAChB,MAAM,EAAE,OAAO,CAAC,MAAM;YACtB,IAAI,EAAE,OAAO,CAAC,IAAI;YAClB,OAAO,EAAE,MAAM,CAAC,MAAM,CACpB;gBACE,MAAM,EAAE,kBAAkB;aAC3B,EACD,IAAI,CAAC,OAAO,CAAC,OAAO,EACpB,OAAO,CAAC,OAAO,CAChB;SACF,EACD,MAAM,CAAC,UAAU,CAClB,CAAA;QAED,OAAO,IAAI,CAAC,UAAU;aACnB,OAAO,CAAC,MAAM,CAAC;aACf,IAAI,CAAC,CAAC,GAAQ,EAAE,EAAE,CAAC,GAAG,CAAC,IAAI,CAAC;aAC5B,KAAK,CAAC,CAAC,KAAU,EAAE,EAAE;YACpB,MAAM,QAAQ,GAAG,KAAK,CAAC,QAAQ,IAAI,KAAK,CAAA;YAExC,MAAM,QAAQ,YAAY,KAAK,CAAC,CAAC,CAAC,QAAQ,CAAC,CAAC,CAAC,QAAQ,CAAC,IAAI,IAAI,QAAQ,CAAA;QACxE,CAAC,CAAC,CAAA;IACN,CAAC;CACF;AA3BD,kCA2BC"}
|
package/lib/base.d.ts
CHANGED
|
@@ -8,7 +8,7 @@ interface RestClientSettings {
|
|
|
8
8
|
connection: any;
|
|
9
9
|
options: any;
|
|
10
10
|
}
|
|
11
|
-
export declare abstract class Base<T = any, D = Partial<T
|
|
11
|
+
export declare abstract class Base<T = any, D = Partial<T>, P extends Params = RestClientParams> implements ServiceInterface<T, D, P> {
|
|
12
12
|
name: string;
|
|
13
13
|
base: string;
|
|
14
14
|
connection: any;
|
|
@@ -16,13 +16,13 @@ export declare abstract class Base<T = any, D = Partial<T>> implements ServiceIn
|
|
|
16
16
|
constructor(settings: RestClientSettings);
|
|
17
17
|
makeUrl(query: Query, id?: string | number | null): string;
|
|
18
18
|
getQuery(query: Query): string;
|
|
19
|
-
abstract request(options: any, params:
|
|
19
|
+
abstract request(options: any, params: P): any;
|
|
20
20
|
methods(this: any, ...names: string[]): any;
|
|
21
|
-
find(params?:
|
|
22
|
-
get(id: Id, params?:
|
|
23
|
-
create(body: D, params?:
|
|
24
|
-
update(id: NullableId, body: D, params?:
|
|
25
|
-
patch(id: NullableId, body: D, params?:
|
|
26
|
-
remove(id: NullableId, params?:
|
|
21
|
+
find(params?: P): any;
|
|
22
|
+
get(id: Id, params?: P): any;
|
|
23
|
+
create(body: D, params?: P): any;
|
|
24
|
+
update(id: NullableId, body: D, params?: P): any;
|
|
25
|
+
patch(id: NullableId, body: D, params?: P): any;
|
|
26
|
+
remove(id: NullableId, params?: P): any;
|
|
27
27
|
}
|
|
28
28
|
export {};
|
package/lib/base.js
CHANGED
|
@@ -36,7 +36,7 @@ class Base {
|
|
|
36
36
|
return '';
|
|
37
37
|
}
|
|
38
38
|
methods(...names) {
|
|
39
|
-
names.forEach(method => {
|
|
39
|
+
names.forEach((method) => {
|
|
40
40
|
this[method] = function (body, params = {}) {
|
|
41
41
|
return this.request({
|
|
42
42
|
body,
|
|
@@ -51,16 +51,16 @@ class Base {
|
|
|
51
51
|
});
|
|
52
52
|
return this;
|
|
53
53
|
}
|
|
54
|
-
find(params
|
|
54
|
+
find(params) {
|
|
55
55
|
return this.request({
|
|
56
56
|
url: this.makeUrl(params.query),
|
|
57
57
|
method: 'GET',
|
|
58
58
|
headers: Object.assign({}, params.headers)
|
|
59
59
|
}, params).catch(toError);
|
|
60
60
|
}
|
|
61
|
-
get(id, params
|
|
61
|
+
get(id, params) {
|
|
62
62
|
if (typeof id === 'undefined') {
|
|
63
|
-
return Promise.reject(new Error(
|
|
63
|
+
return Promise.reject(new Error("id for 'get' can not be undefined"));
|
|
64
64
|
}
|
|
65
65
|
return this.request({
|
|
66
66
|
url: this.makeUrl(params.query, id),
|
|
@@ -68,7 +68,7 @@ class Base {
|
|
|
68
68
|
headers: Object.assign({}, params.headers)
|
|
69
69
|
}, params).catch(toError);
|
|
70
70
|
}
|
|
71
|
-
create(body, params
|
|
71
|
+
create(body, params) {
|
|
72
72
|
return this.request({
|
|
73
73
|
url: this.makeUrl(params.query),
|
|
74
74
|
body,
|
|
@@ -76,9 +76,9 @@ class Base {
|
|
|
76
76
|
headers: Object.assign({ 'Content-Type': 'application/json' }, params.headers)
|
|
77
77
|
}, params).catch(toError);
|
|
78
78
|
}
|
|
79
|
-
update(id, body, params
|
|
79
|
+
update(id, body, params) {
|
|
80
80
|
if (typeof id === 'undefined') {
|
|
81
|
-
return Promise.reject(new Error(
|
|
81
|
+
return Promise.reject(new Error("id for 'update' can not be undefined, only 'null' when updating multiple entries"));
|
|
82
82
|
}
|
|
83
83
|
return this.request({
|
|
84
84
|
url: this.makeUrl(params.query, id),
|
|
@@ -87,9 +87,9 @@ class Base {
|
|
|
87
87
|
headers: Object.assign({ 'Content-Type': 'application/json' }, params.headers)
|
|
88
88
|
}, params).catch(toError);
|
|
89
89
|
}
|
|
90
|
-
patch(id, body, params
|
|
90
|
+
patch(id, body, params) {
|
|
91
91
|
if (typeof id === 'undefined') {
|
|
92
|
-
return Promise.reject(new Error(
|
|
92
|
+
return Promise.reject(new Error("id for 'patch' can not be undefined, only 'null' when updating multiple entries"));
|
|
93
93
|
}
|
|
94
94
|
return this.request({
|
|
95
95
|
url: this.makeUrl(params.query, id),
|
|
@@ -98,9 +98,9 @@ class Base {
|
|
|
98
98
|
headers: Object.assign({ 'Content-Type': 'application/json' }, params.headers)
|
|
99
99
|
}, params).catch(toError);
|
|
100
100
|
}
|
|
101
|
-
remove(id, params
|
|
101
|
+
remove(id, params) {
|
|
102
102
|
if (typeof id === 'undefined') {
|
|
103
|
-
return Promise.reject(new Error(
|
|
103
|
+
return Promise.reject(new Error("id for 'remove' can not be undefined, only 'null' when removing multiple entries"));
|
|
104
104
|
}
|
|
105
105
|
return this.request({
|
|
106
106
|
url: this.makeUrl(params.query, id),
|
package/lib/base.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"base.js","sourceRoot":"","sources":["../src/base.ts"],"names":[],"mappings":";;;;;;AAAA,
|
|
1
|
+
{"version":3,"file":"base.js","sourceRoot":"","sources":["../src/base.ts"],"names":[],"mappings":";;;;;;AAAA,4CAAmB;AAEnB,+CAAyD;AACzD,iDAAqD;AAErD,SAAS,OAAO,CAAC,KAA+B;IAC9C,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,CAAA;KACjF;IAED,MAAM,IAAA,gBAAO,EAAC,KAAK,CAAC,CAAA;AACtB,CAAC;AAaD,MAAsB,IAAI;IAQxB,YAAY,QAA4B;QACtC,IAAI,CAAC,IAAI,GAAG,IAAA,sBAAY,EAAC,QAAQ,CAAC,IAAI,CAAC,CAAA;QACvC,IAAI,CAAC,OAAO,GAAG,QAAQ,CAAC,OAAO,CAAA;QAC/B,IAAI,CAAC,UAAU,GAAG,QAAQ,CAAC,UAAU,CAAA;QACrC,IAAI,CAAC,IAAI,GAAG,GAAG,QAAQ,CAAC,IAAI,IAAI,IAAI,CAAC,IAAI,EAAE,CAAA;IAC7C,CAAC;IAED,OAAO,CAAC,KAAY,EAAE,EAA2B;QAC/C,IAAI,GAA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1,5 +1,6 @@
|
|
|
1
|
+
import { Params } from '@feathersjs/feathers';
|
|
1
2
|
import { Base, RestClientParams } from './base';
|
|
2
|
-
export declare class FetchClient extends Base {
|
|
3
|
+
export declare class FetchClient<T = any, D = Partial<T>, P extends Params = RestClientParams> extends Base<T, D, P> {
|
|
3
4
|
request(options: any, params: RestClientParams): any;
|
|
4
5
|
checkStatus(response: any): any;
|
|
5
6
|
}
|
package/lib/fetch.js
CHANGED
|
@@ -25,10 +25,13 @@ class FetchClient extends base_1.Base {
|
|
|
25
25
|
if (response.ok) {
|
|
26
26
|
return response;
|
|
27
27
|
}
|
|
28
|
-
return response
|
|
28
|
+
return response
|
|
29
|
+
.json()
|
|
30
|
+
.catch(() => {
|
|
29
31
|
const ErrorClass = errors_1.errors[response.status] || Error;
|
|
30
32
|
return new ErrorClass('JSON parsing error');
|
|
31
|
-
})
|
|
33
|
+
})
|
|
34
|
+
.then((error) => {
|
|
32
35
|
error.response = response;
|
|
33
36
|
throw error;
|
|
34
37
|
});
|
package/lib/fetch.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"fetch.js","sourceRoot":"","sources":["../src/fetch.ts"],"names":[],"mappings":";;;AAAA,+
|
|
1
|
+
{"version":3,"file":"fetch.js","sourceRoot":"","sources":["../src/fetch.ts"],"names":[],"mappings":";;;AAAA,+CAA2C;AAE3C,iCAA+C;AAE/C,MAAa,WAA0E,SAAQ,WAAa;IAC1G,OAAO,CAAC,OAAY,EAAE,MAAwB;QAC5C,MAAM,YAAY,GAAG,MAAM,CAAC,MAAM,CAAC,EAAE,EAAE,OAAO,EAAE,MAAM,CAAC,UAAU,CAAC,CAAA;QAElE,YAAY,CAAC,OAAO,GAAG,MAAM,CAAC,MAAM,CAClC;YACE,MAAM,EAAE,kBAAkB;SAC3B,EACD,IAAI,CAAC,OAAO,CAAC,OAAO,EACpB,YAAY,CAAC,OAAO,CACrB,CAAA;QAED,IAAI,OAAO,CAAC,IAAI,EAAE;YAChB,YAAY,CAAC,IAAI,GAAG,IAAI,CAAC,SAAS,CAAC,OAAO,CAAC,IAAI,CAAC,CAAA;SACjD;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,CAAA;aACZ;YAED,OAAO,QAAQ,CAAC,IAAI,EAAE,CAAA;QACxB,CAAC,CAAC,CAAA;IACN,CAAC;IAED,WAAW,CAAC,QAAa;QACvB,IAAI,QAAQ,CAAC,EAAE,EAAE;YACf,OAAO,QAAQ,CAAA;SAChB;QAED,OAAO,QAAQ;aACZ,IAAI,EAAE;aACN,KAAK,CAAC,GAAG,EAAE;YACV,MAAM,UAAU,GAAI,eAAc,CAAC,QAAQ,CAAC,MAAM,CAAC,IAAI,KAAK,CAAA;YAE5D,OAAO,IAAI,UAAU,CAAC,oBAAoB,CAAC,CAAA;QAC7C,CAAC,CAAC;aACD,IAAI,CAAC,CAAC,KAAU,EAAE,EAAE;YACnB,KAAK,CAAC,QAAQ,GAAG,QAAQ,CAAA;YACzB,MAAM,KAAK,CAAA;QACb,CAAC,CAAC,CAAA;IACN,CAAC;CACF;AA5CD,kCA4CC"}
|
package/lib/index.d.ts
CHANGED
|
@@ -1,27 +1,14 @@
|
|
|
1
|
+
import { TransportConnection } from '@feathersjs/feathers';
|
|
1
2
|
import { Base } from './base';
|
|
2
3
|
import { AxiosClient } from './axios';
|
|
3
4
|
import { FetchClient } from './fetch';
|
|
4
5
|
import { SuperagentClient } from './superagent';
|
|
5
6
|
export { AxiosClient, FetchClient, SuperagentClient };
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
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;
|
|
7
|
+
export declare type Handler<ServiceTypes> = (connection: any, options?: any, Service?: any) => TransportConnection<ServiceTypes>;
|
|
8
|
+
export interface Transport<ServiceTypes> {
|
|
9
|
+
superagent: Handler<ServiceTypes>;
|
|
10
|
+
fetch: Handler<ServiceTypes>;
|
|
11
|
+
axios: Handler<ServiceTypes>;
|
|
25
12
|
}
|
|
26
13
|
export declare type RestService<T = any, D = Partial<any>> = Base<T, D>;
|
|
27
|
-
export default function restClient(base?: string): Transport
|
|
14
|
+
export default function restClient<ServiceTypes = any>(base?: string): Transport<ServiceTypes>;
|
package/lib/index.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.SuperagentClient = exports.FetchClient = exports.AxiosClient = void 0;
|
|
4
|
+
const feathers_1 = require("@feathersjs/feathers");
|
|
4
5
|
const base_1 = require("./base");
|
|
5
6
|
const axios_1 = require("./axios");
|
|
6
7
|
Object.defineProperty(exports, "AxiosClient", { enumerable: true, get: function () { return axios_1.AxiosClient; } });
|
|
@@ -15,7 +16,7 @@ const transports = {
|
|
|
15
16
|
};
|
|
16
17
|
function restClient(base = '') {
|
|
17
18
|
const result = { Base: base_1.Base };
|
|
18
|
-
Object.keys(transports).forEach(key => {
|
|
19
|
+
Object.keys(transports).forEach((key) => {
|
|
19
20
|
result[key] = function (connection, options = {}, Service = transports[key]) {
|
|
20
21
|
if (!connection) {
|
|
21
22
|
throw new Error(`${key} has to be provided to feathers-rest`);
|
|
@@ -33,6 +34,12 @@ function restClient(base = '') {
|
|
|
33
34
|
}
|
|
34
35
|
app.rest = connection;
|
|
35
36
|
app.defaultService = defaultService;
|
|
37
|
+
app.mixins.unshift((service, _location, options) => {
|
|
38
|
+
if (options && options.methods && service instanceof base_1.Base) {
|
|
39
|
+
const customMethods = options.methods.filter((name) => !feathers_1.defaultServiceMethods.includes(name));
|
|
40
|
+
service.methods(...customMethods);
|
|
41
|
+
}
|
|
42
|
+
});
|
|
36
43
|
};
|
|
37
44
|
initialize.Service = Service;
|
|
38
45
|
initialize.service = defaultService;
|
package/lib/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;AAAA,
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;AAAA,mDAA8F;AAE9F,iCAA6B;AAC7B,mCAAqC;AAI5B,4FAJA,mBAAW,OAIA;AAHpB,mCAAqC;AAGf,4FAHb,mBAAW,OAGa;AAFjC,6CAA+C;AAEZ,iGAF1B,6BAAgB,OAE0B;AAEnD,MAAM,UAAU,GAAG;IACjB,UAAU,EAAE,6BAAgB;IAC5B,KAAK,EAAE,mBAAW;IAClB,KAAK,EAAE,mBAAW;CACnB,CAAA;AAgBD,SAAwB,UAAU,CAAqB,IAAI,GAAG,EAAE;IAC9D,MAAM,MAAM,GAAQ,EAAE,IAAI,EAAJ,WAAI,EAAE,CAAA;IAE5B,MAAM,CAAC,IAAI,CAAC,UAAU,CAAC,CAAC,OAAO,CAAC,CAAC,GAAG,EAAE,EAAE;QACtC,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,CAAA;aAC9D;YAED,IAAI,OAAO,OAAO,KAAK,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AA;CAC3D"}
|
package/lib/superagent.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
+
import { Params } from '@feathersjs/feathers';
|
|
1
2
|
import { Base, RestClientParams } from './base';
|
|
2
|
-
export declare class SuperagentClient extends Base {
|
|
3
|
+
export declare class SuperagentClient<T = any, D = Partial<T>, P extends Params = RestClientParams> extends Base<T, D, P> {
|
|
3
4
|
request(options: any, params: RestClientParams): Promise<unknown>;
|
|
4
5
|
}
|
package/lib/superagent.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"superagent.js","sourceRoot":"","sources":["../src/superagent.ts"],"names":[],"mappings":";;;
|
|
1
|
+
{"version":3,"file":"superagent.js","sourceRoot":"","sources":["../src/superagent.ts"],"names":[],"mappings":";;;AACA,iCAA+C;AAE/C,MAAa,gBAA+E,SAAQ,WAInG;IACC,OAAO,CAAC,OAAY,EAAE,MAAwB;QAC5C,MAAM,UAAU,GAAG,IAAI,CAAC,UAAU,CAAC,OAAO,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,CAAC;aAC5D,GAAG,CAAC,IAAI,CAAC,OAAO,CAAC,OAAO,IAAI,EAAE,CAAC;aAC/B,GAAG,CAAC,QAAQ,EAAE,kBAAkB,CAAC;aACjC,GAAG,CAAC,MAAM,CAAC,UAAU,IAAI,EAAE,CAAC;aAC5B,GAAG,CAAC,OAAO,CAAC,OAAO,IAAI,EAAE,CAAC;aAC1B,IAAI,CAAC,OAAO,CAAC,IAAI,IAAI,MAAM,CAAC,CAAA;QAE/B,OAAO,IAAI,OAAO,CAAC,CAAC,OAAO,EAAE,MAAM,EAAE,EAAE;YACrC,UAAU,CAAC,GAAG,CAAC,OAAO,CAAC,OAAO,CAAC,CAAA;YAE/B,IAAI,OAAO,CAAC,IAAI,EAAE;gBAChB,UAAU,CAAC,IAAI,CAAC,OAAO,CAAC,IAAI,CAAC,CAAA;aAC9B;YAED,UAAU,CAAC,GAAG,CAAC,UAAU,KAAU,EAAE,GAAQ;gBAC3C,IAAI,KAAK,EAAE;oBACT,IAAI;wBACF,MAAM,QAAQ,GAAG,KAAK,CAAC,QAAQ,CAAA;wBAC/B,KAAK,GAAG,IAAI,CAAC,KAAK,CAAC,KAAK,CAAC,QAAQ,CAAC,IAAI,CAAC,CAAA;wBACvC,KAAK,CAAC,QAAQ,GAAG,QAAQ,CAAA;qBAC1B;oBAAC,OAAO,CAAM,EAAE,GAAE;oBAEnB,OAAO,MAAM,CAAC,KAAK,CAAC,CAAA;iBACrB;gBAED,OAAO,CAAC,GAAG,IAAI,GAAG,CAAC,IAAI,CAAC,CAAA;YAC1B,CAAC,CAAC,CAAA;QACJ,CAAC,CAAC,CAAA;IACJ,CAAC;CACF;AAnCD,4CAmCC"}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@feathersjs/rest-client",
|
|
3
3
|
"description": "REST client services for different Ajax libraries",
|
|
4
|
-
"version": "5.0.0-pre.
|
|
4
|
+
"version": "5.0.0-pre.25",
|
|
5
5
|
"homepage": "https://feathersjs.com",
|
|
6
6
|
"main": "lib/",
|
|
7
7
|
"types": "lib/",
|
|
@@ -52,28 +52,28 @@
|
|
|
52
52
|
"access": "public"
|
|
53
53
|
},
|
|
54
54
|
"dependencies": {
|
|
55
|
-
"@feathersjs/commons": "^5.0.0-pre.
|
|
56
|
-
"@feathersjs/errors": "^5.0.0-pre.
|
|
57
|
-
"@
|
|
55
|
+
"@feathersjs/commons": "^5.0.0-pre.25",
|
|
56
|
+
"@feathersjs/errors": "^5.0.0-pre.25",
|
|
57
|
+
"@feathersjs/feathers": "^5.0.0-pre.25",
|
|
58
58
|
"@types/superagent": "^4.1.15",
|
|
59
59
|
"qs": "^6.10.3"
|
|
60
60
|
},
|
|
61
61
|
"devDependencies": {
|
|
62
|
-
"@feathersjs/express": "^5.0.0-pre.
|
|
63
|
-
"@feathersjs/
|
|
64
|
-
"@feathersjs/
|
|
65
|
-
"@feathersjs/tests": "^5.0.0-pre.22",
|
|
62
|
+
"@feathersjs/express": "^5.0.0-pre.25",
|
|
63
|
+
"@feathersjs/memory": "^5.0.0-pre.25",
|
|
64
|
+
"@feathersjs/tests": "^5.0.0-pre.25",
|
|
66
65
|
"@types/mocha": "^9.1.1",
|
|
67
|
-
"@types/node": "^17.0.
|
|
66
|
+
"@types/node": "^17.0.40",
|
|
67
|
+
"@types/node-fetch": "^2.6.1",
|
|
68
68
|
"@types/qs": "^6.9.7",
|
|
69
69
|
"axios": "^0.27.2",
|
|
70
70
|
"mocha": "^10.0.0",
|
|
71
71
|
"node-fetch": "^2.6.1",
|
|
72
72
|
"rxjs": "^7.5.5",
|
|
73
73
|
"shx": "^0.3.4",
|
|
74
|
-
"superagent": "^7.1.
|
|
75
|
-
"ts-node": "^10.
|
|
76
|
-
"typescript": "^4.
|
|
74
|
+
"superagent": "^7.1.6",
|
|
75
|
+
"ts-node": "^10.8.1",
|
|
76
|
+
"typescript": "^4.7.3"
|
|
77
77
|
},
|
|
78
|
-
"gitHead": "
|
|
78
|
+
"gitHead": "c0ab3b603920dff6e9c912b2767cf168094d45c8"
|
|
79
79
|
}
|
package/src/axios.ts
CHANGED
|
@@ -1,22 +1,31 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { Params } from '@feathersjs/feathers'
|
|
2
|
+
import { Base, RestClientParams } from './base'
|
|
2
3
|
|
|
3
|
-
export class AxiosClient extends Base {
|
|
4
|
-
request
|
|
5
|
-
const config = Object.assign(
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
4
|
+
export class AxiosClient<T = any, D = Partial<T>, P extends Params = RestClientParams> extends Base<T, D, P> {
|
|
5
|
+
request(options: any, params: RestClientParams) {
|
|
6
|
+
const config = Object.assign(
|
|
7
|
+
{
|
|
8
|
+
url: options.url,
|
|
9
|
+
method: options.method,
|
|
10
|
+
data: options.body,
|
|
11
|
+
headers: Object.assign(
|
|
12
|
+
{
|
|
13
|
+
Accept: 'application/json'
|
|
14
|
+
},
|
|
15
|
+
this.options.headers,
|
|
16
|
+
options.headers
|
|
17
|
+
)
|
|
18
|
+
},
|
|
19
|
+
params.connection
|
|
20
|
+
)
|
|
13
21
|
|
|
14
|
-
return this.connection
|
|
22
|
+
return this.connection
|
|
23
|
+
.request(config)
|
|
15
24
|
.then((res: any) => res.data)
|
|
16
25
|
.catch((error: any) => {
|
|
17
|
-
const response = error.response || error
|
|
26
|
+
const response = error.response || error
|
|
18
27
|
|
|
19
|
-
throw response instanceof Error ? response :
|
|
20
|
-
})
|
|
28
|
+
throw response instanceof Error ? response : response.data || response
|
|
29
|
+
})
|
|
21
30
|
}
|
|
22
31
|
}
|
package/src/base.ts
CHANGED
|
@@ -1,146 +1,178 @@
|
|
|
1
|
-
import qs
|
|
2
|
-
import { Params, Id, Query, NullableId, ServiceInterface } from '@feathersjs/feathers'
|
|
3
|
-
import { Unavailable, convert }
|
|
4
|
-
import { _, stripSlashes } from '@feathersjs/commons'
|
|
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
5
|
|
|
6
|
-
function toError
|
|
6
|
+
function toError(error: Error & { code: string }) {
|
|
7
7
|
if (error.code === 'ECONNREFUSED') {
|
|
8
|
-
throw new Unavailable(error.message, _.pick(error, 'address', 'port', 'config'))
|
|
8
|
+
throw new Unavailable(error.message, _.pick(error, 'address', 'port', 'config'))
|
|
9
9
|
}
|
|
10
10
|
|
|
11
|
-
throw convert(error)
|
|
11
|
+
throw convert(error)
|
|
12
12
|
}
|
|
13
13
|
|
|
14
14
|
export interface RestClientParams extends Params {
|
|
15
|
-
connection?: any
|
|
15
|
+
connection?: any
|
|
16
16
|
}
|
|
17
17
|
|
|
18
18
|
interface RestClientSettings {
|
|
19
|
-
name: string
|
|
20
|
-
base: string
|
|
21
|
-
connection: any
|
|
22
|
-
options: any
|
|
19
|
+
name: string
|
|
20
|
+
base: string
|
|
21
|
+
connection: any
|
|
22
|
+
options: any
|
|
23
23
|
}
|
|
24
24
|
|
|
25
|
-
export abstract class Base<T = any, D = Partial<T
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
this.
|
|
35
|
-
this.
|
|
25
|
+
export abstract class Base<T = any, D = Partial<T>, P extends Params = RestClientParams>
|
|
26
|
+
implements ServiceInterface<T, D, P>
|
|
27
|
+
{
|
|
28
|
+
name: string
|
|
29
|
+
base: string
|
|
30
|
+
connection: any
|
|
31
|
+
options: any
|
|
32
|
+
|
|
33
|
+
constructor(settings: RestClientSettings) {
|
|
34
|
+
this.name = stripSlashes(settings.name)
|
|
35
|
+
this.options = settings.options
|
|
36
|
+
this.connection = settings.connection
|
|
37
|
+
this.base = `${settings.base}/${this.name}`
|
|
36
38
|
}
|
|
37
39
|
|
|
38
|
-
makeUrl
|
|
39
|
-
let url = this.base
|
|
40
|
+
makeUrl(query: Query, id?: string | number | null) {
|
|
41
|
+
let url = this.base
|
|
40
42
|
|
|
41
|
-
query = query || {}
|
|
43
|
+
query = query || {}
|
|
42
44
|
|
|
43
45
|
if (typeof id !== 'undefined' && id !== null) {
|
|
44
|
-
url += `/${encodeURIComponent(id)}
|
|
46
|
+
url += `/${encodeURIComponent(id)}`
|
|
45
47
|
}
|
|
46
48
|
|
|
47
|
-
return url + this.getQuery(query)
|
|
49
|
+
return url + this.getQuery(query)
|
|
48
50
|
}
|
|
49
51
|
|
|
50
|
-
getQuery
|
|
52
|
+
getQuery(query: Query) {
|
|
51
53
|
if (Object.keys(query).length !== 0) {
|
|
52
|
-
const queryString = qs.stringify(query)
|
|
54
|
+
const queryString = qs.stringify(query)
|
|
53
55
|
|
|
54
|
-
return `?${queryString}
|
|
56
|
+
return `?${queryString}`
|
|
55
57
|
}
|
|
56
58
|
|
|
57
|
-
return ''
|
|
59
|
+
return ''
|
|
58
60
|
}
|
|
59
61
|
|
|
60
|
-
abstract request
|
|
62
|
+
abstract request(options: any, params: P): any
|
|
61
63
|
|
|
62
|
-
methods
|
|
63
|
-
names.forEach(method => {
|
|
64
|
+
methods(this: any, ...names: string[]) {
|
|
65
|
+
names.forEach((method) => {
|
|
64
66
|
this[method] = function (body: any, params: Params = {}) {
|
|
65
|
-
return this.request(
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
67
|
+
return this.request(
|
|
68
|
+
{
|
|
69
|
+
body,
|
|
70
|
+
url: this.makeUrl(params.query),
|
|
71
|
+
method: 'POST',
|
|
72
|
+
headers: Object.assign(
|
|
73
|
+
{
|
|
74
|
+
'Content-Type': 'application/json',
|
|
75
|
+
'X-Service-Method': method
|
|
76
|
+
},
|
|
77
|
+
params.headers
|
|
78
|
+
)
|
|
79
|
+
},
|
|
80
|
+
params
|
|
81
|
+
).catch(toError)
|
|
74
82
|
}
|
|
75
|
-
})
|
|
83
|
+
})
|
|
76
84
|
|
|
77
|
-
return this
|
|
85
|
+
return this
|
|
78
86
|
}
|
|
79
87
|
|
|
80
|
-
find
|
|
81
|
-
return this.request(
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
88
|
+
find(params?: P) {
|
|
89
|
+
return this.request(
|
|
90
|
+
{
|
|
91
|
+
url: this.makeUrl(params.query),
|
|
92
|
+
method: 'GET',
|
|
93
|
+
headers: Object.assign({}, params.headers)
|
|
94
|
+
},
|
|
95
|
+
params
|
|
96
|
+
).catch(toError)
|
|
86
97
|
}
|
|
87
98
|
|
|
88
|
-
get
|
|
99
|
+
get(id: Id, params?: P) {
|
|
89
100
|
if (typeof id === 'undefined') {
|
|
90
|
-
return Promise.reject(new Error(
|
|
101
|
+
return Promise.reject(new Error("id for 'get' can not be undefined"))
|
|
91
102
|
}
|
|
92
103
|
|
|
93
|
-
return this.request(
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
104
|
+
return this.request(
|
|
105
|
+
{
|
|
106
|
+
url: this.makeUrl(params.query, id),
|
|
107
|
+
method: 'GET',
|
|
108
|
+
headers: Object.assign({}, params.headers)
|
|
109
|
+
},
|
|
110
|
+
params
|
|
111
|
+
).catch(toError)
|
|
98
112
|
}
|
|
99
113
|
|
|
100
|
-
create
|
|
101
|
-
return this.request(
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
114
|
+
create(body: D, params?: P) {
|
|
115
|
+
return this.request(
|
|
116
|
+
{
|
|
117
|
+
url: this.makeUrl(params.query),
|
|
118
|
+
body,
|
|
119
|
+
method: 'POST',
|
|
120
|
+
headers: Object.assign({ 'Content-Type': 'application/json' }, params.headers)
|
|
121
|
+
},
|
|
122
|
+
params
|
|
123
|
+
).catch(toError)
|
|
107
124
|
}
|
|
108
125
|
|
|
109
|
-
update
|
|
126
|
+
update(id: NullableId, body: D, params?: P) {
|
|
110
127
|
if (typeof id === 'undefined') {
|
|
111
|
-
return Promise.reject(
|
|
128
|
+
return Promise.reject(
|
|
129
|
+
new Error("id for 'update' can not be undefined, only 'null' when updating multiple entries")
|
|
130
|
+
)
|
|
112
131
|
}
|
|
113
132
|
|
|
114
|
-
return this.request(
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
133
|
+
return this.request(
|
|
134
|
+
{
|
|
135
|
+
url: this.makeUrl(params.query, id),
|
|
136
|
+
body,
|
|
137
|
+
method: 'PUT',
|
|
138
|
+
headers: Object.assign({ 'Content-Type': 'application/json' }, params.headers)
|
|
139
|
+
},
|
|
140
|
+
params
|
|
141
|
+
).catch(toError)
|
|
120
142
|
}
|
|
121
143
|
|
|
122
|
-
patch
|
|
144
|
+
patch(id: NullableId, body: D, params?: P) {
|
|
123
145
|
if (typeof id === 'undefined') {
|
|
124
|
-
return Promise.reject(
|
|
146
|
+
return Promise.reject(
|
|
147
|
+
new Error("id for 'patch' can not be undefined, only 'null' when updating multiple entries")
|
|
148
|
+
)
|
|
125
149
|
}
|
|
126
150
|
|
|
127
|
-
return this.request(
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
151
|
+
return this.request(
|
|
152
|
+
{
|
|
153
|
+
url: this.makeUrl(params.query, id),
|
|
154
|
+
body,
|
|
155
|
+
method: 'PATCH',
|
|
156
|
+
headers: Object.assign({ 'Content-Type': 'application/json' }, params.headers)
|
|
157
|
+
},
|
|
158
|
+
params
|
|
159
|
+
).catch(toError)
|
|
133
160
|
}
|
|
134
161
|
|
|
135
|
-
remove
|
|
162
|
+
remove(id: NullableId, params?: P) {
|
|
136
163
|
if (typeof id === 'undefined') {
|
|
137
|
-
return Promise.reject(
|
|
164
|
+
return Promise.reject(
|
|
165
|
+
new Error("id for 'remove' can not be undefined, only 'null' when removing multiple entries")
|
|
166
|
+
)
|
|
138
167
|
}
|
|
139
168
|
|
|
140
|
-
return this.request(
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
169
|
+
return this.request(
|
|
170
|
+
{
|
|
171
|
+
url: this.makeUrl(params.query, id),
|
|
172
|
+
method: 'DELETE',
|
|
173
|
+
headers: Object.assign({}, params.headers)
|
|
174
|
+
},
|
|
175
|
+
params
|
|
176
|
+
).catch(toError)
|
|
145
177
|
}
|
|
146
178
|
}
|
package/src/fetch.ts
CHANGED
|
@@ -1,41 +1,49 @@
|
|
|
1
|
-
import { errors } from '@feathersjs/errors'
|
|
2
|
-
import {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
1
|
+
import { errors } from '@feathersjs/errors'
|
|
2
|
+
import { Params } from '@feathersjs/feathers'
|
|
3
|
+
import { Base, RestClientParams } from './base'
|
|
4
|
+
|
|
5
|
+
export class FetchClient<T = any, D = Partial<T>, P extends Params = RestClientParams> extends Base<T, D, P> {
|
|
6
|
+
request(options: any, params: RestClientParams) {
|
|
7
|
+
const fetchOptions = Object.assign({}, options, params.connection)
|
|
8
|
+
|
|
9
|
+
fetchOptions.headers = Object.assign(
|
|
10
|
+
{
|
|
11
|
+
Accept: 'application/json'
|
|
12
|
+
},
|
|
13
|
+
this.options.headers,
|
|
14
|
+
fetchOptions.headers
|
|
15
|
+
)
|
|
11
16
|
|
|
12
17
|
if (options.body) {
|
|
13
|
-
fetchOptions.body = JSON.stringify(options.body)
|
|
18
|
+
fetchOptions.body = JSON.stringify(options.body)
|
|
14
19
|
}
|
|
15
20
|
|
|
16
21
|
return this.connection(options.url, fetchOptions)
|
|
17
22
|
.then(this.checkStatus)
|
|
18
23
|
.then((response: any) => {
|
|
19
24
|
if (response.status === 204) {
|
|
20
|
-
return null
|
|
25
|
+
return null
|
|
21
26
|
}
|
|
22
27
|
|
|
23
|
-
return response.json()
|
|
24
|
-
})
|
|
28
|
+
return response.json()
|
|
29
|
+
})
|
|
25
30
|
}
|
|
26
31
|
|
|
27
|
-
checkStatus
|
|
32
|
+
checkStatus(response: any) {
|
|
28
33
|
if (response.ok) {
|
|
29
|
-
return response
|
|
34
|
+
return response
|
|
30
35
|
}
|
|
31
36
|
|
|
32
|
-
return response
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
37
|
+
return response
|
|
38
|
+
.json()
|
|
39
|
+
.catch(() => {
|
|
40
|
+
const ErrorClass = (errors as any)[response.status] || Error
|
|
41
|
+
|
|
42
|
+
return new ErrorClass('JSON parsing error')
|
|
43
|
+
})
|
|
44
|
+
.then((error: any) => {
|
|
45
|
+
error.response = response
|
|
46
|
+
throw error
|
|
47
|
+
})
|
|
40
48
|
}
|
|
41
49
|
}
|
package/src/index.ts
CHANGED
|
@@ -1,80 +1,76 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { AxiosClient } from './axios';
|
|
3
|
-
import { FetchClient } from './fetch';
|
|
4
|
-
import { SuperagentClient } from './superagent';
|
|
1
|
+
import { Application, TransportConnection, defaultServiceMethods } from '@feathersjs/feathers'
|
|
5
2
|
|
|
6
|
-
|
|
3
|
+
import { Base } from './base'
|
|
4
|
+
import { AxiosClient } from './axios'
|
|
5
|
+
import { FetchClient } from './fetch'
|
|
6
|
+
import { SuperagentClient } from './superagent'
|
|
7
|
+
|
|
8
|
+
export { AxiosClient, FetchClient, SuperagentClient }
|
|
7
9
|
|
|
8
10
|
const transports = {
|
|
9
11
|
superagent: SuperagentClient,
|
|
10
12
|
fetch: FetchClient,
|
|
11
13
|
axios: AxiosClient
|
|
12
|
-
};
|
|
13
|
-
|
|
14
|
-
interface HandlerResult extends Function {
|
|
15
|
-
/**
|
|
16
|
-
* initialize service
|
|
17
|
-
*/
|
|
18
|
-
(): void;
|
|
19
|
-
|
|
20
|
-
/**
|
|
21
|
-
* Transport Service
|
|
22
|
-
*/
|
|
23
|
-
Service: any;
|
|
24
|
-
|
|
25
|
-
/**
|
|
26
|
-
* default Service
|
|
27
|
-
*/
|
|
28
|
-
service: any;
|
|
29
14
|
}
|
|
30
15
|
|
|
31
|
-
export type Handler = (
|
|
16
|
+
export type Handler<ServiceTypes> = (
|
|
17
|
+
connection: any,
|
|
18
|
+
options?: any,
|
|
19
|
+
Service?: any
|
|
20
|
+
) => TransportConnection<ServiceTypes>
|
|
32
21
|
|
|
33
|
-
export interface Transport {
|
|
34
|
-
superagent: Handler
|
|
35
|
-
fetch: Handler
|
|
36
|
-
axios: Handler
|
|
22
|
+
export interface Transport<ServiceTypes> {
|
|
23
|
+
superagent: Handler<ServiceTypes>
|
|
24
|
+
fetch: Handler<ServiceTypes>
|
|
25
|
+
axios: Handler<ServiceTypes>
|
|
37
26
|
}
|
|
38
27
|
|
|
39
|
-
export type RestService<T = any, D = Partial<any>> = Base<T, D
|
|
28
|
+
export type RestService<T = any, D = Partial<any>> = Base<T, D>
|
|
40
29
|
|
|
41
|
-
export default function restClient (base = '') {
|
|
42
|
-
const result: any = { Base }
|
|
30
|
+
export default function restClient<ServiceTypes = any>(base = '') {
|
|
31
|
+
const result: any = { Base }
|
|
43
32
|
|
|
44
|
-
Object.keys(transports).forEach(key => {
|
|
33
|
+
Object.keys(transports).forEach((key) => {
|
|
45
34
|
result[key] = function (connection: any, options: any = {}, Service: Base = (transports as any)[key]) {
|
|
46
35
|
if (!connection) {
|
|
47
|
-
throw new Error(`${key} has to be provided to feathers-rest`)
|
|
36
|
+
throw new Error(`${key} has to be provided to feathers-rest`)
|
|
48
37
|
}
|
|
49
38
|
|
|
50
39
|
if (typeof options === 'function') {
|
|
51
|
-
Service = options
|
|
52
|
-
options = {}
|
|
40
|
+
Service = options
|
|
41
|
+
options = {}
|
|
53
42
|
}
|
|
54
43
|
|
|
55
44
|
const defaultService = function (name: string) {
|
|
56
|
-
return new (Service as any)({ base, name, connection, options })
|
|
57
|
-
}
|
|
45
|
+
return new (Service as any)({ base, name, connection, options })
|
|
46
|
+
}
|
|
58
47
|
|
|
59
|
-
const initialize = (app: any) => {
|
|
48
|
+
const initialize = (app: Application & { rest: any }) => {
|
|
60
49
|
if (app.rest !== undefined) {
|
|
61
|
-
throw new Error('Only one default client provider can be configured')
|
|
50
|
+
throw new Error('Only one default client provider can be configured')
|
|
62
51
|
}
|
|
63
52
|
|
|
64
|
-
app.rest = connection
|
|
65
|
-
app.defaultService = defaultService
|
|
66
|
-
|
|
53
|
+
app.rest = connection
|
|
54
|
+
app.defaultService = defaultService
|
|
55
|
+
app.mixins.unshift((service, _location, options) => {
|
|
56
|
+
if (options && options.methods && service instanceof Base) {
|
|
57
|
+
const customMethods = options.methods.filter((name) => !defaultServiceMethods.includes(name))
|
|
58
|
+
|
|
59
|
+
service.methods(...customMethods)
|
|
60
|
+
}
|
|
61
|
+
})
|
|
62
|
+
}
|
|
67
63
|
|
|
68
|
-
initialize.Service = Service
|
|
69
|
-
initialize.service = defaultService
|
|
64
|
+
initialize.Service = Service
|
|
65
|
+
initialize.service = defaultService
|
|
70
66
|
|
|
71
|
-
return initialize
|
|
72
|
-
}
|
|
73
|
-
})
|
|
67
|
+
return initialize
|
|
68
|
+
}
|
|
69
|
+
})
|
|
74
70
|
|
|
75
|
-
return result as Transport
|
|
71
|
+
return result as Transport<ServiceTypes>
|
|
76
72
|
}
|
|
77
73
|
|
|
78
74
|
if (typeof module !== 'undefined') {
|
|
79
|
-
module.exports = Object.assign(restClient, module.exports)
|
|
75
|
+
module.exports = Object.assign(restClient, module.exports)
|
|
80
76
|
}
|
package/src/superagent.ts
CHANGED
|
@@ -1,34 +1,39 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { Params } from '@feathersjs/feathers'
|
|
2
|
+
import { Base, RestClientParams } from './base'
|
|
2
3
|
|
|
3
|
-
export class SuperagentClient extends Base
|
|
4
|
-
|
|
4
|
+
export class SuperagentClient<T = any, D = Partial<T>, P extends Params = RestClientParams> extends Base<
|
|
5
|
+
T,
|
|
6
|
+
D,
|
|
7
|
+
P
|
|
8
|
+
> {
|
|
9
|
+
request(options: any, params: RestClientParams) {
|
|
5
10
|
const superagent = this.connection(options.method, options.url)
|
|
6
11
|
.set(this.options.headers || {})
|
|
7
12
|
.set('Accept', 'application/json')
|
|
8
13
|
.set(params.connection || {})
|
|
9
14
|
.set(options.headers || {})
|
|
10
|
-
.type(options.type || 'json')
|
|
15
|
+
.type(options.type || 'json')
|
|
11
16
|
|
|
12
17
|
return new Promise((resolve, reject) => {
|
|
13
|
-
superagent.set(options.headers)
|
|
18
|
+
superagent.set(options.headers)
|
|
14
19
|
|
|
15
20
|
if (options.body) {
|
|
16
|
-
superagent.send(options.body)
|
|
21
|
+
superagent.send(options.body)
|
|
17
22
|
}
|
|
18
23
|
|
|
19
24
|
superagent.end(function (error: any, res: any) {
|
|
20
25
|
if (error) {
|
|
21
26
|
try {
|
|
22
|
-
const response = error.response
|
|
23
|
-
error = JSON.parse(error.response.text)
|
|
24
|
-
error.response = response
|
|
27
|
+
const response = error.response
|
|
28
|
+
error = JSON.parse(error.response.text)
|
|
29
|
+
error.response = response
|
|
25
30
|
} catch (e: any) {}
|
|
26
31
|
|
|
27
|
-
return reject(error)
|
|
32
|
+
return reject(error)
|
|
28
33
|
}
|
|
29
34
|
|
|
30
|
-
resolve(res && res.body)
|
|
31
|
-
})
|
|
32
|
-
})
|
|
35
|
+
resolve(res && res.body)
|
|
36
|
+
})
|
|
37
|
+
})
|
|
33
38
|
}
|
|
34
39
|
}
|