@feathersjs/rest-client 5.0.0-pre.3 → 5.0.0-pre.30
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 +128 -176
- package/LICENSE +1 -1
- package/README.md +2 -2
- package/lib/axios.d.ts +3 -3
- package/lib/axios.js +3 -2
- package/lib/axios.js.map +1 -1
- package/lib/base.d.ts +11 -8
- package/lib/base.js +13 -13
- package/lib/base.js.map +1 -1
- package/lib/fetch.d.ts +3 -3
- 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 +3 -3
- package/lib/superagent.js.map +1 -1
- package/package.json +27 -23
- package/src/axios.ts +24 -16
- package/src/base.ts +124 -88
- package/src/fetch.ts +32 -25
- package/src/index.ts +44 -48
- package/src/superagent.ts +19 -15
package/src/base.ts
CHANGED
|
@@ -1,142 +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
|
+
}
|
|
13
|
+
|
|
14
|
+
export interface RestClientParams extends Params {
|
|
15
|
+
connection?: any
|
|
12
16
|
}
|
|
13
17
|
|
|
14
18
|
interface RestClientSettings {
|
|
15
|
-
name: string
|
|
16
|
-
base: string
|
|
17
|
-
connection: any
|
|
18
|
-
options: any
|
|
19
|
+
name: string
|
|
20
|
+
base: string
|
|
21
|
+
connection: any
|
|
22
|
+
options: any
|
|
19
23
|
}
|
|
20
24
|
|
|
21
|
-
export abstract class Base<T = any, D = Partial<T
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
this.
|
|
31
|
-
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}`
|
|
32
38
|
}
|
|
33
39
|
|
|
34
|
-
makeUrl
|
|
35
|
-
let url = this.base
|
|
40
|
+
makeUrl(query: Query, id?: string | number | null) {
|
|
41
|
+
let url = this.base
|
|
36
42
|
|
|
37
|
-
query = query || {}
|
|
43
|
+
query = query || {}
|
|
38
44
|
|
|
39
45
|
if (typeof id !== 'undefined' && id !== null) {
|
|
40
|
-
url += `/${encodeURIComponent(id)}
|
|
46
|
+
url += `/${encodeURIComponent(id)}`
|
|
41
47
|
}
|
|
42
48
|
|
|
43
|
-
return url + this.getQuery(query)
|
|
49
|
+
return url + this.getQuery(query)
|
|
44
50
|
}
|
|
45
51
|
|
|
46
|
-
getQuery
|
|
52
|
+
getQuery(query: Query) {
|
|
47
53
|
if (Object.keys(query).length !== 0) {
|
|
48
|
-
const queryString = qs.stringify(query)
|
|
54
|
+
const queryString = qs.stringify(query)
|
|
49
55
|
|
|
50
|
-
return `?${queryString}
|
|
56
|
+
return `?${queryString}`
|
|
51
57
|
}
|
|
52
58
|
|
|
53
|
-
return ''
|
|
59
|
+
return ''
|
|
54
60
|
}
|
|
55
61
|
|
|
56
|
-
abstract request
|
|
62
|
+
abstract request(options: any, params: P): any
|
|
57
63
|
|
|
58
|
-
methods
|
|
59
|
-
names.forEach(method => {
|
|
64
|
+
methods(this: any, ...names: string[]) {
|
|
65
|
+
names.forEach((method) => {
|
|
60
66
|
this[method] = function (body: any, params: Params = {}) {
|
|
61
|
-
return this.request(
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
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)
|
|
70
82
|
}
|
|
71
|
-
})
|
|
83
|
+
})
|
|
72
84
|
|
|
73
|
-
return this
|
|
85
|
+
return this
|
|
74
86
|
}
|
|
75
87
|
|
|
76
|
-
find
|
|
77
|
-
return this.request(
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
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)
|
|
82
97
|
}
|
|
83
98
|
|
|
84
|
-
get
|
|
99
|
+
get(id: Id, params?: P) {
|
|
85
100
|
if (typeof id === 'undefined') {
|
|
86
|
-
return Promise.reject(new Error(
|
|
101
|
+
return Promise.reject(new Error("id for 'get' can not be undefined"))
|
|
87
102
|
}
|
|
88
103
|
|
|
89
|
-
return this.request(
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
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)
|
|
94
112
|
}
|
|
95
113
|
|
|
96
|
-
create
|
|
97
|
-
return this.request(
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
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)
|
|
103
124
|
}
|
|
104
125
|
|
|
105
|
-
update
|
|
126
|
+
update(id: NullableId, body: D, params?: P) {
|
|
106
127
|
if (typeof id === 'undefined') {
|
|
107
|
-
return Promise.reject(
|
|
128
|
+
return Promise.reject(
|
|
129
|
+
new Error("id for 'update' can not be undefined, only 'null' when updating multiple entries")
|
|
130
|
+
)
|
|
108
131
|
}
|
|
109
132
|
|
|
110
|
-
return this.request(
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
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)
|
|
116
142
|
}
|
|
117
143
|
|
|
118
|
-
patch
|
|
144
|
+
patch(id: NullableId, body: D, params?: P) {
|
|
119
145
|
if (typeof id === 'undefined') {
|
|
120
|
-
return Promise.reject(
|
|
146
|
+
return Promise.reject(
|
|
147
|
+
new Error("id for 'patch' can not be undefined, only 'null' when updating multiple entries")
|
|
148
|
+
)
|
|
121
149
|
}
|
|
122
150
|
|
|
123
|
-
return this.request(
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
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)
|
|
129
160
|
}
|
|
130
161
|
|
|
131
|
-
remove
|
|
162
|
+
remove(id: NullableId, params?: P) {
|
|
132
163
|
if (typeof id === 'undefined') {
|
|
133
|
-
return Promise.reject(
|
|
164
|
+
return Promise.reject(
|
|
165
|
+
new Error("id for 'remove' can not be undefined, only 'null' when removing multiple entries")
|
|
166
|
+
)
|
|
134
167
|
}
|
|
135
168
|
|
|
136
|
-
return this.request(
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
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)
|
|
141
177
|
}
|
|
142
178
|
}
|
package/src/fetch.ts
CHANGED
|
@@ -1,42 +1,49 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import { Base } from './base'
|
|
4
|
-
|
|
5
|
-
export class FetchClient extends Base {
|
|
6
|
-
request
|
|
7
|
-
const fetchOptions = Object.assign({}, options, params.connection)
|
|
8
|
-
|
|
9
|
-
fetchOptions.headers = Object.assign(
|
|
10
|
-
|
|
11
|
-
|
|
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
|
+
)
|
|
12
16
|
|
|
13
17
|
if (options.body) {
|
|
14
|
-
fetchOptions.body = JSON.stringify(options.body)
|
|
18
|
+
fetchOptions.body = JSON.stringify(options.body)
|
|
15
19
|
}
|
|
16
20
|
|
|
17
21
|
return this.connection(options.url, fetchOptions)
|
|
18
22
|
.then(this.checkStatus)
|
|
19
23
|
.then((response: any) => {
|
|
20
24
|
if (response.status === 204) {
|
|
21
|
-
return null
|
|
25
|
+
return null
|
|
22
26
|
}
|
|
23
27
|
|
|
24
|
-
return response.json()
|
|
25
|
-
})
|
|
28
|
+
return response.json()
|
|
29
|
+
})
|
|
26
30
|
}
|
|
27
31
|
|
|
28
|
-
checkStatus
|
|
32
|
+
checkStatus(response: any) {
|
|
29
33
|
if (response.ok) {
|
|
30
|
-
return response
|
|
34
|
+
return response
|
|
31
35
|
}
|
|
32
36
|
|
|
33
|
-
return response
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
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
|
+
})
|
|
41
48
|
}
|
|
42
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,35 +1,39 @@
|
|
|
1
|
-
import { Params } from '@feathersjs/feathers'
|
|
2
|
-
import { Base } from './base'
|
|
1
|
+
import { Params } from '@feathersjs/feathers'
|
|
2
|
+
import { Base, RestClientParams } from './base'
|
|
3
3
|
|
|
4
|
-
export class SuperagentClient extends Base
|
|
5
|
-
|
|
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) {
|
|
6
10
|
const superagent = this.connection(options.method, options.url)
|
|
7
11
|
.set(this.options.headers || {})
|
|
8
12
|
.set('Accept', 'application/json')
|
|
9
13
|
.set(params.connection || {})
|
|
10
14
|
.set(options.headers || {})
|
|
11
|
-
.type(options.type || 'json')
|
|
15
|
+
.type(options.type || 'json')
|
|
12
16
|
|
|
13
17
|
return new Promise((resolve, reject) => {
|
|
14
|
-
superagent.set(options.headers)
|
|
18
|
+
superagent.set(options.headers)
|
|
15
19
|
|
|
16
20
|
if (options.body) {
|
|
17
|
-
superagent.send(options.body)
|
|
21
|
+
superagent.send(options.body)
|
|
18
22
|
}
|
|
19
23
|
|
|
20
24
|
superagent.end(function (error: any, res: any) {
|
|
21
25
|
if (error) {
|
|
22
26
|
try {
|
|
23
|
-
const response = error.response
|
|
24
|
-
error = JSON.parse(error.response.text)
|
|
25
|
-
error.response = response
|
|
26
|
-
} catch (e) {}
|
|
27
|
+
const response = error.response
|
|
28
|
+
error = JSON.parse(error.response.text)
|
|
29
|
+
error.response = response
|
|
30
|
+
} catch (e: any) {}
|
|
27
31
|
|
|
28
|
-
return reject(error)
|
|
32
|
+
return reject(error)
|
|
29
33
|
}
|
|
30
34
|
|
|
31
|
-
resolve(res && res.body)
|
|
32
|
-
})
|
|
33
|
-
})
|
|
35
|
+
resolve(res && res.body)
|
|
36
|
+
})
|
|
37
|
+
})
|
|
34
38
|
}
|
|
35
39
|
}
|