@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/src/index.ts ADDED
@@ -0,0 +1,80 @@
1
+ import { Base } from './base';
2
+ import { AxiosClient } from './axios';
3
+ import { FetchClient } from './fetch';
4
+ import { SuperagentClient } from './superagent';
5
+
6
+ export { AxiosClient, FetchClient, SuperagentClient };
7
+
8
+ const transports = {
9
+ superagent: SuperagentClient,
10
+ fetch: FetchClient,
11
+ 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
+ }
30
+
31
+ export type Handler = (connection: any, options?: any, Service?: any) => HandlerResult;
32
+
33
+ export interface Transport {
34
+ superagent: Handler;
35
+ fetch: Handler;
36
+ axios: Handler;
37
+ }
38
+
39
+ export type RestService<T = any, D = Partial<any>> = Base<T, D>;
40
+
41
+ export default function restClient (base = '') {
42
+ const result: any = { Base };
43
+
44
+ Object.keys(transports).forEach(key => {
45
+ result[key] = function (connection: any, options: any = {}, Service: Base = (transports as any)[key]) {
46
+ if (!connection) {
47
+ throw new Error(`${key} has to be provided to feathers-rest`);
48
+ }
49
+
50
+ if (typeof options === 'function') {
51
+ Service = options;
52
+ options = {};
53
+ }
54
+
55
+ const defaultService = function (name: string) {
56
+ return new (Service as any)({ base, name, connection, options });
57
+ };
58
+
59
+ const initialize = (app: any) => {
60
+ if (app.rest !== undefined) {
61
+ throw new Error('Only one default client provider can be configured');
62
+ }
63
+
64
+ app.rest = connection;
65
+ app.defaultService = defaultService;
66
+ };
67
+
68
+ initialize.Service = Service;
69
+ initialize.service = defaultService;
70
+
71
+ return initialize;
72
+ };
73
+ });
74
+
75
+ return result as Transport;
76
+ }
77
+
78
+ if (typeof module !== 'undefined') {
79
+ module.exports = Object.assign(restClient, module.exports);
80
+ }
@@ -0,0 +1,35 @@
1
+ import { Params } from '@feathersjs/feathers';
2
+ import { Base } from './base';
3
+
4
+ export class SuperagentClient extends Base {
5
+ request (options: any, params: Params) {
6
+ const superagent = this.connection(options.method, options.url)
7
+ .set(this.options.headers || {})
8
+ .set('Accept', 'application/json')
9
+ .set(params.connection || {})
10
+ .set(options.headers || {})
11
+ .type(options.type || 'json');
12
+
13
+ return new Promise((resolve, reject) => {
14
+ superagent.set(options.headers);
15
+
16
+ if (options.body) {
17
+ superagent.send(options.body);
18
+ }
19
+
20
+ superagent.end(function (error: any, res: any) {
21
+ if (error) {
22
+ try {
23
+ const response = error.response;
24
+ error = JSON.parse(error.response.text);
25
+ error.response = response;
26
+ } catch (e: any) {}
27
+
28
+ return reject(error);
29
+ }
30
+
31
+ resolve(res && res.body);
32
+ });
33
+ });
34
+ }
35
+ }