@robosystems/client 0.3.42 → 0.5.0
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/artifacts/InvestorClient.js +7 -7
- package/artifacts/InvestorClient.ts +13 -13
- package/artifacts/LedgerClient.d.ts +2 -2
- package/artifacts/LedgerClient.js +48 -48
- package/artifacts/LedgerClient.ts +91 -91
- package/artifacts/QueryClient.js +2 -2
- package/artifacts/QueryClient.ts +5 -5
- package/artifacts/graphql/generated/graphql.d.ts +2 -0
- package/artifacts/graphql/generated/graphql.ts +2 -0
- package/client/client.gen.js +109 -126
- package/client/client.gen.ts +129 -139
- package/client/types.gen.d.ts +7 -4
- package/client/types.gen.ts +10 -5
- package/client/utils.gen.d.ts +7 -3
- package/client/utils.gen.js +6 -6
- package/client/utils.gen.ts +11 -11
- package/core/bodySerializer.gen.d.ts +4 -4
- package/core/bodySerializer.gen.ts +6 -8
- package/core/params.gen.js +5 -5
- package/core/params.gen.ts +5 -5
- package/core/serverSentEvents.gen.d.ts +1 -1
- package/core/serverSentEvents.gen.js +4 -6
- package/core/serverSentEvents.gen.ts +4 -5
- package/core/types.gen.d.ts +1 -1
- package/core/types.gen.ts +1 -1
- package/core/utils.gen.js +1 -1
- package/core/utils.gen.ts +1 -1
- package/index.ts +2 -2
- package/package.json +2 -2
- package/sdk/client/client.gen.js +109 -126
- package/sdk/client/client.gen.ts +129 -139
- package/sdk/client/types.gen.d.ts +7 -4
- package/sdk/client/types.gen.ts +10 -5
- package/sdk/client/utils.gen.d.ts +7 -3
- package/sdk/client/utils.gen.js +6 -6
- package/sdk/client/utils.gen.ts +11 -11
- package/sdk/core/bodySerializer.gen.d.ts +4 -4
- package/sdk/core/bodySerializer.gen.ts +6 -8
- package/sdk/core/params.gen.js +5 -5
- package/sdk/core/params.gen.ts +5 -5
- package/sdk/core/serverSentEvents.gen.d.ts +1 -1
- package/sdk/core/serverSentEvents.gen.js +4 -6
- package/sdk/core/serverSentEvents.gen.ts +4 -5
- package/sdk/core/types.gen.d.ts +1 -1
- package/sdk/core/types.gen.ts +1 -1
- package/sdk/core/utils.gen.js +1 -1
- package/sdk/core/utils.gen.ts +1 -1
- package/sdk/index.d.ts +2 -2
- package/sdk/index.js +70 -65
- package/sdk/index.ts +2 -2
- package/sdk/sdk.gen.d.ts +145 -101
- package/sdk/sdk.gen.js +297 -208
- package/sdk/sdk.gen.ts +230 -141
- package/sdk/types.gen.d.ts +1300 -772
- package/sdk/types.gen.ts +1342 -783
- package/sdk.gen.d.ts +145 -101
- package/sdk.gen.js +297 -208
- package/sdk.gen.ts +230 -141
- package/types.gen.d.ts +1300 -772
- package/types.gen.ts +1342 -783
package/client/client.gen.js
CHANGED
|
@@ -22,10 +22,7 @@ const createClient = (config = {}) => {
|
|
|
22
22
|
serializedBody: undefined,
|
|
23
23
|
};
|
|
24
24
|
if (opts.security) {
|
|
25
|
-
await (0, utils_gen_2.setAuthParams)(
|
|
26
|
-
...opts,
|
|
27
|
-
security: opts.security,
|
|
28
|
-
});
|
|
25
|
+
await (0, utils_gen_2.setAuthParams)(opts);
|
|
29
26
|
}
|
|
30
27
|
if (opts.requestValidator) {
|
|
31
28
|
await opts.requestValidator(opts);
|
|
@@ -37,153 +34,139 @@ const createClient = (config = {}) => {
|
|
|
37
34
|
if (opts.body === undefined || opts.serializedBody === '') {
|
|
38
35
|
opts.headers.delete('Content-Type');
|
|
39
36
|
}
|
|
40
|
-
const
|
|
41
|
-
|
|
37
|
+
const resolvedOpts = opts;
|
|
38
|
+
const url = (0, utils_gen_2.buildUrl)(resolvedOpts);
|
|
39
|
+
return { opts: resolvedOpts, url };
|
|
42
40
|
};
|
|
43
41
|
const request = async (options) => {
|
|
44
|
-
const
|
|
45
|
-
const
|
|
46
|
-
|
|
47
|
-
...opts,
|
|
48
|
-
body: (0, utils_gen_1.getValidRequestBody)(opts),
|
|
49
|
-
};
|
|
50
|
-
let request = new Request(url, requestInit);
|
|
51
|
-
for (const fn of interceptors.request.fns) {
|
|
52
|
-
if (fn) {
|
|
53
|
-
request = await fn(request, opts);
|
|
54
|
-
}
|
|
55
|
-
}
|
|
56
|
-
// fetch must be assigned here, otherwise it would throw the error:
|
|
57
|
-
// TypeError: Failed to execute 'fetch' on 'Window': Illegal invocation
|
|
58
|
-
const _fetch = opts.fetch;
|
|
42
|
+
const throwOnError = options.throwOnError ?? _config.throwOnError;
|
|
43
|
+
const responseStyle = options.responseStyle ?? _config.responseStyle;
|
|
44
|
+
let request;
|
|
59
45
|
let response;
|
|
60
46
|
try {
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
47
|
+
const { opts, url } = await beforeRequest(options);
|
|
48
|
+
const requestInit = {
|
|
49
|
+
redirect: 'follow',
|
|
50
|
+
...opts,
|
|
51
|
+
body: (0, utils_gen_1.getValidRequestBody)(opts),
|
|
52
|
+
};
|
|
53
|
+
request = new Request(url, requestInit);
|
|
54
|
+
for (const fn of interceptors.request.fns) {
|
|
67
55
|
if (fn) {
|
|
68
|
-
|
|
56
|
+
request = await fn(request, opts);
|
|
69
57
|
}
|
|
70
58
|
}
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
error: finalError,
|
|
80
|
-
request,
|
|
81
|
-
response: undefined,
|
|
82
|
-
};
|
|
83
|
-
}
|
|
84
|
-
for (const fn of interceptors.response.fns) {
|
|
85
|
-
if (fn) {
|
|
86
|
-
response = await fn(response, request, opts);
|
|
59
|
+
// fetch must be assigned here, otherwise it would throw the error:
|
|
60
|
+
// TypeError: Failed to execute 'fetch' on 'Window': Illegal invocation
|
|
61
|
+
const _fetch = opts.fetch;
|
|
62
|
+
response = await _fetch(request);
|
|
63
|
+
for (const fn of interceptors.response.fns) {
|
|
64
|
+
if (fn) {
|
|
65
|
+
response = await fn(response, request, opts);
|
|
66
|
+
}
|
|
87
67
|
}
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
68
|
+
const result = {
|
|
69
|
+
request,
|
|
70
|
+
response,
|
|
71
|
+
};
|
|
72
|
+
if (response.ok) {
|
|
73
|
+
const parseAs = (opts.parseAs === 'auto'
|
|
74
|
+
? (0, utils_gen_2.getParseAs)(response.headers.get('Content-Type'))
|
|
75
|
+
: opts.parseAs) ?? 'json';
|
|
76
|
+
if (response.status === 204 || response.headers.get('Content-Length') === '0') {
|
|
77
|
+
let emptyData;
|
|
78
|
+
switch (parseAs) {
|
|
79
|
+
case 'arrayBuffer':
|
|
80
|
+
case 'blob':
|
|
81
|
+
case 'text':
|
|
82
|
+
emptyData = await response[parseAs]();
|
|
83
|
+
break;
|
|
84
|
+
case 'formData':
|
|
85
|
+
emptyData = new FormData();
|
|
86
|
+
break;
|
|
87
|
+
case 'stream':
|
|
88
|
+
emptyData = response.body;
|
|
89
|
+
break;
|
|
90
|
+
case 'json':
|
|
91
|
+
default:
|
|
92
|
+
emptyData = {};
|
|
93
|
+
break;
|
|
94
|
+
}
|
|
95
|
+
return opts.responseStyle === 'data'
|
|
96
|
+
? emptyData
|
|
97
|
+
: {
|
|
98
|
+
data: emptyData,
|
|
99
|
+
...result,
|
|
100
|
+
};
|
|
101
|
+
}
|
|
102
|
+
let data;
|
|
99
103
|
switch (parseAs) {
|
|
100
104
|
case 'arrayBuffer':
|
|
101
105
|
case 'blob':
|
|
106
|
+
case 'formData':
|
|
102
107
|
case 'text':
|
|
103
|
-
|
|
108
|
+
data = await response[parseAs]();
|
|
104
109
|
break;
|
|
105
|
-
case '
|
|
106
|
-
|
|
110
|
+
case 'json': {
|
|
111
|
+
// Some servers return 200 with no Content-Length and empty body.
|
|
112
|
+
// response.json() would throw; read as text and parse if non-empty.
|
|
113
|
+
const text = await response.text();
|
|
114
|
+
data = text ? JSON.parse(text) : {};
|
|
107
115
|
break;
|
|
116
|
+
}
|
|
108
117
|
case 'stream':
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
118
|
+
return opts.responseStyle === 'data'
|
|
119
|
+
? response.body
|
|
120
|
+
: {
|
|
121
|
+
data: response.body,
|
|
122
|
+
...result,
|
|
123
|
+
};
|
|
124
|
+
}
|
|
125
|
+
if (parseAs === 'json') {
|
|
126
|
+
if (opts.responseValidator) {
|
|
127
|
+
await opts.responseValidator(data);
|
|
128
|
+
}
|
|
129
|
+
if (opts.responseTransformer) {
|
|
130
|
+
data = await opts.responseTransformer(data);
|
|
131
|
+
}
|
|
115
132
|
}
|
|
116
133
|
return opts.responseStyle === 'data'
|
|
117
|
-
?
|
|
134
|
+
? data
|
|
118
135
|
: {
|
|
119
|
-
data
|
|
136
|
+
data,
|
|
120
137
|
...result,
|
|
121
138
|
};
|
|
122
139
|
}
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
case 'formData':
|
|
128
|
-
case 'text':
|
|
129
|
-
data = await response[parseAs]();
|
|
130
|
-
break;
|
|
131
|
-
case 'json': {
|
|
132
|
-
// Some servers return 200 with no Content-Length and empty body.
|
|
133
|
-
// response.json() would throw; read as text and parse if non-empty.
|
|
134
|
-
const text = await response.text();
|
|
135
|
-
data = text ? JSON.parse(text) : {};
|
|
136
|
-
break;
|
|
137
|
-
}
|
|
138
|
-
case 'stream':
|
|
139
|
-
return opts.responseStyle === 'data'
|
|
140
|
-
? response.body
|
|
141
|
-
: {
|
|
142
|
-
data: response.body,
|
|
143
|
-
...result,
|
|
144
|
-
};
|
|
140
|
+
const textError = await response.text();
|
|
141
|
+
let jsonError;
|
|
142
|
+
try {
|
|
143
|
+
jsonError = JSON.parse(textError);
|
|
145
144
|
}
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
145
|
+
catch {
|
|
146
|
+
// noop
|
|
147
|
+
}
|
|
148
|
+
throw jsonError ?? textError;
|
|
149
|
+
}
|
|
150
|
+
catch (error) {
|
|
151
|
+
let finalError = error;
|
|
152
|
+
for (const fn of interceptors.error.fns) {
|
|
153
|
+
if (fn) {
|
|
154
|
+
finalError = await fn(finalError, response, request, options);
|
|
152
155
|
}
|
|
153
156
|
}
|
|
154
|
-
|
|
155
|
-
|
|
157
|
+
finalError = finalError || {};
|
|
158
|
+
if (throwOnError) {
|
|
159
|
+
throw finalError;
|
|
160
|
+
}
|
|
161
|
+
// TODO: we probably want to return error and improve types
|
|
162
|
+
return responseStyle === 'data'
|
|
163
|
+
? undefined
|
|
156
164
|
: {
|
|
157
|
-
|
|
158
|
-
|
|
165
|
+
error: finalError,
|
|
166
|
+
request,
|
|
167
|
+
response,
|
|
159
168
|
};
|
|
160
169
|
}
|
|
161
|
-
const textError = await response.text();
|
|
162
|
-
let jsonError;
|
|
163
|
-
try {
|
|
164
|
-
jsonError = JSON.parse(textError);
|
|
165
|
-
}
|
|
166
|
-
catch {
|
|
167
|
-
// noop
|
|
168
|
-
}
|
|
169
|
-
const error = jsonError ?? textError;
|
|
170
|
-
let finalError = error;
|
|
171
|
-
for (const fn of interceptors.error.fns) {
|
|
172
|
-
if (fn) {
|
|
173
|
-
finalError = (await fn(error, response, request, opts));
|
|
174
|
-
}
|
|
175
|
-
}
|
|
176
|
-
finalError = finalError || {};
|
|
177
|
-
if (opts.throwOnError) {
|
|
178
|
-
throw finalError;
|
|
179
|
-
}
|
|
180
|
-
// TODO: we probably want to return error and improve types
|
|
181
|
-
return opts.responseStyle === 'data'
|
|
182
|
-
? undefined
|
|
183
|
-
: {
|
|
184
|
-
error: finalError,
|
|
185
|
-
...result,
|
|
186
|
-
};
|
|
187
170
|
};
|
|
188
171
|
const makeMethodFn = (method) => (options) => request({ ...options, method });
|
|
189
172
|
const makeSseFn = (method) => async (options) => {
|
|
@@ -191,7 +174,6 @@ const createClient = (config = {}) => {
|
|
|
191
174
|
return (0, serverSentEvents_gen_1.createSseClient)({
|
|
192
175
|
...opts,
|
|
193
176
|
body: opts.body,
|
|
194
|
-
headers: opts.headers,
|
|
195
177
|
method,
|
|
196
178
|
onRequest: async (url, init) => {
|
|
197
179
|
let request = new Request(url, init);
|
|
@@ -206,8 +188,9 @@ const createClient = (config = {}) => {
|
|
|
206
188
|
url,
|
|
207
189
|
});
|
|
208
190
|
};
|
|
191
|
+
const _buildUrl = (options) => (0, utils_gen_2.buildUrl)({ ..._config, ...options });
|
|
209
192
|
return {
|
|
210
|
-
buildUrl:
|
|
193
|
+
buildUrl: _buildUrl,
|
|
211
194
|
connect: makeMethodFn('CONNECT'),
|
|
212
195
|
delete: makeMethodFn('DELETE'),
|
|
213
196
|
get: makeMethodFn('GET'),
|
package/client/client.gen.ts
CHANGED
|
@@ -31,20 +31,24 @@ export const createClient = (config: Config = {}): Client => {
|
|
|
31
31
|
|
|
32
32
|
const interceptors = createInterceptors<Request, Response, unknown, ResolvedRequestOptions>();
|
|
33
33
|
|
|
34
|
-
const beforeRequest = async
|
|
34
|
+
const beforeRequest = async <
|
|
35
|
+
TData = unknown,
|
|
36
|
+
TResponseStyle extends 'data' | 'fields' = 'fields',
|
|
37
|
+
ThrowOnError extends boolean = boolean,
|
|
38
|
+
Url extends string = string,
|
|
39
|
+
>(
|
|
40
|
+
options: RequestOptions<TData, TResponseStyle, ThrowOnError, Url>,
|
|
41
|
+
) => {
|
|
35
42
|
const opts = {
|
|
36
43
|
..._config,
|
|
37
44
|
...options,
|
|
38
45
|
fetch: options.fetch ?? _config.fetch ?? globalThis.fetch,
|
|
39
46
|
headers: mergeHeaders(_config.headers, options.headers),
|
|
40
|
-
serializedBody: undefined,
|
|
47
|
+
serializedBody: undefined as string | undefined,
|
|
41
48
|
};
|
|
42
49
|
|
|
43
50
|
if (opts.security) {
|
|
44
|
-
await setAuthParams(
|
|
45
|
-
...opts,
|
|
46
|
-
security: opts.security,
|
|
47
|
-
});
|
|
51
|
+
await setAuthParams(opts);
|
|
48
52
|
}
|
|
49
53
|
|
|
50
54
|
if (opts.requestValidator) {
|
|
@@ -52,7 +56,7 @@ export const createClient = (config: Config = {}): Client => {
|
|
|
52
56
|
}
|
|
53
57
|
|
|
54
58
|
if (opts.body !== undefined && opts.bodySerializer) {
|
|
55
|
-
opts.serializedBody = opts.bodySerializer(opts.body);
|
|
59
|
+
opts.serializedBody = opts.bodySerializer(opts.body) as string | undefined;
|
|
56
60
|
}
|
|
57
61
|
|
|
58
62
|
// remove Content-Type header if body is empty to avoid sending invalid requests
|
|
@@ -60,177 +64,162 @@ export const createClient = (config: Config = {}): Client => {
|
|
|
60
64
|
opts.headers.delete('Content-Type');
|
|
61
65
|
}
|
|
62
66
|
|
|
63
|
-
const
|
|
67
|
+
const resolvedOpts = opts as typeof opts &
|
|
68
|
+
ResolvedRequestOptions<TResponseStyle, ThrowOnError, Url>;
|
|
69
|
+
const url = buildUrl(resolvedOpts);
|
|
64
70
|
|
|
65
|
-
return { opts, url };
|
|
71
|
+
return { opts: resolvedOpts, url };
|
|
66
72
|
};
|
|
67
73
|
|
|
68
74
|
const request: Client['request'] = async (options) => {
|
|
69
|
-
const
|
|
70
|
-
const
|
|
71
|
-
redirect: 'follow',
|
|
72
|
-
...opts,
|
|
73
|
-
body: getValidRequestBody(opts),
|
|
74
|
-
};
|
|
75
|
-
|
|
76
|
-
let request = new Request(url, requestInit);
|
|
75
|
+
const throwOnError = options.throwOnError ?? _config.throwOnError;
|
|
76
|
+
const responseStyle = options.responseStyle ?? _config.responseStyle;
|
|
77
77
|
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
request = await fn(request, opts);
|
|
81
|
-
}
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
// fetch must be assigned here, otherwise it would throw the error:
|
|
85
|
-
// TypeError: Failed to execute 'fetch' on 'Window': Illegal invocation
|
|
86
|
-
const _fetch = opts.fetch!;
|
|
87
|
-
let response: Response;
|
|
78
|
+
let request: Request | undefined;
|
|
79
|
+
let response: Response | undefined;
|
|
88
80
|
|
|
89
81
|
try {
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
82
|
+
const { opts, url } = await beforeRequest(options);
|
|
83
|
+
const requestInit: ReqInit = {
|
|
84
|
+
redirect: 'follow',
|
|
85
|
+
...opts,
|
|
86
|
+
body: getValidRequestBody(opts),
|
|
87
|
+
};
|
|
94
88
|
|
|
95
|
-
|
|
89
|
+
request = new Request(url, requestInit);
|
|
90
|
+
|
|
91
|
+
for (const fn of interceptors.request.fns) {
|
|
96
92
|
if (fn) {
|
|
97
|
-
|
|
93
|
+
request = await fn(request, opts);
|
|
98
94
|
}
|
|
99
95
|
}
|
|
100
96
|
|
|
101
|
-
|
|
97
|
+
// fetch must be assigned here, otherwise it would throw the error:
|
|
98
|
+
// TypeError: Failed to execute 'fetch' on 'Window': Illegal invocation
|
|
99
|
+
const _fetch = opts.fetch!;
|
|
102
100
|
|
|
103
|
-
|
|
104
|
-
throw finalError;
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
// Return error response
|
|
108
|
-
return opts.responseStyle === 'data'
|
|
109
|
-
? undefined
|
|
110
|
-
: {
|
|
111
|
-
error: finalError,
|
|
112
|
-
request,
|
|
113
|
-
response: undefined as any,
|
|
114
|
-
};
|
|
115
|
-
}
|
|
101
|
+
response = await _fetch(request);
|
|
116
102
|
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
103
|
+
for (const fn of interceptors.response.fns) {
|
|
104
|
+
if (fn) {
|
|
105
|
+
response = await fn(response, request, opts);
|
|
106
|
+
}
|
|
120
107
|
}
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
const result = {
|
|
124
|
-
request,
|
|
125
|
-
response,
|
|
126
|
-
};
|
|
127
108
|
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
109
|
+
const result = {
|
|
110
|
+
request,
|
|
111
|
+
response,
|
|
112
|
+
};
|
|
113
|
+
|
|
114
|
+
if (response.ok) {
|
|
115
|
+
const parseAs =
|
|
116
|
+
(opts.parseAs === 'auto'
|
|
117
|
+
? getParseAs(response.headers.get('Content-Type'))
|
|
118
|
+
: opts.parseAs) ?? 'json';
|
|
119
|
+
|
|
120
|
+
if (response.status === 204 || response.headers.get('Content-Length') === '0') {
|
|
121
|
+
let emptyData: any;
|
|
122
|
+
switch (parseAs) {
|
|
123
|
+
case 'arrayBuffer':
|
|
124
|
+
case 'blob':
|
|
125
|
+
case 'text':
|
|
126
|
+
emptyData = await response[parseAs]();
|
|
127
|
+
break;
|
|
128
|
+
case 'formData':
|
|
129
|
+
emptyData = new FormData();
|
|
130
|
+
break;
|
|
131
|
+
case 'stream':
|
|
132
|
+
emptyData = response.body;
|
|
133
|
+
break;
|
|
134
|
+
case 'json':
|
|
135
|
+
default:
|
|
136
|
+
emptyData = {};
|
|
137
|
+
break;
|
|
138
|
+
}
|
|
139
|
+
return opts.responseStyle === 'data'
|
|
140
|
+
? emptyData
|
|
141
|
+
: {
|
|
142
|
+
data: emptyData,
|
|
143
|
+
...result,
|
|
144
|
+
};
|
|
145
|
+
}
|
|
133
146
|
|
|
134
|
-
|
|
135
|
-
let emptyData: any;
|
|
147
|
+
let data: any;
|
|
136
148
|
switch (parseAs) {
|
|
137
149
|
case 'arrayBuffer':
|
|
138
150
|
case 'blob':
|
|
151
|
+
case 'formData':
|
|
139
152
|
case 'text':
|
|
140
|
-
|
|
153
|
+
data = await response[parseAs]();
|
|
141
154
|
break;
|
|
142
|
-
case '
|
|
143
|
-
|
|
155
|
+
case 'json': {
|
|
156
|
+
// Some servers return 200 with no Content-Length and empty body.
|
|
157
|
+
// response.json() would throw; read as text and parse if non-empty.
|
|
158
|
+
const text = await response.text();
|
|
159
|
+
data = text ? JSON.parse(text) : {};
|
|
144
160
|
break;
|
|
161
|
+
}
|
|
145
162
|
case 'stream':
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
163
|
+
return opts.responseStyle === 'data'
|
|
164
|
+
? response.body
|
|
165
|
+
: {
|
|
166
|
+
data: response.body,
|
|
167
|
+
...result,
|
|
168
|
+
};
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
if (parseAs === 'json') {
|
|
172
|
+
if (opts.responseValidator) {
|
|
173
|
+
await opts.responseValidator(data);
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
if (opts.responseTransformer) {
|
|
177
|
+
data = await opts.responseTransformer(data);
|
|
178
|
+
}
|
|
152
179
|
}
|
|
180
|
+
|
|
153
181
|
return opts.responseStyle === 'data'
|
|
154
|
-
?
|
|
182
|
+
? data
|
|
155
183
|
: {
|
|
156
|
-
data
|
|
184
|
+
data,
|
|
157
185
|
...result,
|
|
158
186
|
};
|
|
159
187
|
}
|
|
160
188
|
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
break;
|
|
169
|
-
case 'json': {
|
|
170
|
-
// Some servers return 200 with no Content-Length and empty body.
|
|
171
|
-
// response.json() would throw; read as text and parse if non-empty.
|
|
172
|
-
const text = await response.text();
|
|
173
|
-
data = text ? JSON.parse(text) : {};
|
|
174
|
-
break;
|
|
175
|
-
}
|
|
176
|
-
case 'stream':
|
|
177
|
-
return opts.responseStyle === 'data'
|
|
178
|
-
? response.body
|
|
179
|
-
: {
|
|
180
|
-
data: response.body,
|
|
181
|
-
...result,
|
|
182
|
-
};
|
|
189
|
+
const textError = await response.text();
|
|
190
|
+
let jsonError: unknown;
|
|
191
|
+
|
|
192
|
+
try {
|
|
193
|
+
jsonError = JSON.parse(textError);
|
|
194
|
+
} catch {
|
|
195
|
+
// noop
|
|
183
196
|
}
|
|
184
197
|
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
}
|
|
198
|
+
throw jsonError ?? textError;
|
|
199
|
+
} catch (error) {
|
|
200
|
+
let finalError = error;
|
|
189
201
|
|
|
190
|
-
|
|
191
|
-
|
|
202
|
+
for (const fn of interceptors.error.fns) {
|
|
203
|
+
if (fn) {
|
|
204
|
+
finalError = await fn(finalError, response, request, options as ResolvedRequestOptions);
|
|
192
205
|
}
|
|
193
206
|
}
|
|
194
207
|
|
|
195
|
-
|
|
196
|
-
? data
|
|
197
|
-
: {
|
|
198
|
-
data,
|
|
199
|
-
...result,
|
|
200
|
-
};
|
|
201
|
-
}
|
|
208
|
+
finalError = finalError || {};
|
|
202
209
|
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
try {
|
|
207
|
-
jsonError = JSON.parse(textError);
|
|
208
|
-
} catch {
|
|
209
|
-
// noop
|
|
210
|
-
}
|
|
211
|
-
|
|
212
|
-
const error = jsonError ?? textError;
|
|
213
|
-
let finalError = error;
|
|
214
|
-
|
|
215
|
-
for (const fn of interceptors.error.fns) {
|
|
216
|
-
if (fn) {
|
|
217
|
-
finalError = (await fn(error, response, request, opts)) as string;
|
|
210
|
+
if (throwOnError) {
|
|
211
|
+
throw finalError;
|
|
218
212
|
}
|
|
219
|
-
}
|
|
220
|
-
|
|
221
|
-
finalError = finalError || ({} as string);
|
|
222
213
|
|
|
223
|
-
|
|
224
|
-
|
|
214
|
+
// TODO: we probably want to return error and improve types
|
|
215
|
+
return responseStyle === 'data'
|
|
216
|
+
? undefined
|
|
217
|
+
: {
|
|
218
|
+
error: finalError,
|
|
219
|
+
request,
|
|
220
|
+
response,
|
|
221
|
+
};
|
|
225
222
|
}
|
|
226
|
-
|
|
227
|
-
// TODO: we probably want to return error and improve types
|
|
228
|
-
return opts.responseStyle === 'data'
|
|
229
|
-
? undefined
|
|
230
|
-
: {
|
|
231
|
-
error: finalError,
|
|
232
|
-
...result,
|
|
233
|
-
};
|
|
234
223
|
};
|
|
235
224
|
|
|
236
225
|
const makeMethodFn = (method: Uppercase<HttpMethod>) => (options: RequestOptions) =>
|
|
@@ -241,7 +230,6 @@ export const createClient = (config: Config = {}): Client => {
|
|
|
241
230
|
return createSseClient({
|
|
242
231
|
...opts,
|
|
243
232
|
body: opts.body as BodyInit | null | undefined,
|
|
244
|
-
headers: opts.headers as unknown as Record<string, string>,
|
|
245
233
|
method,
|
|
246
234
|
onRequest: async (url, init) => {
|
|
247
235
|
let request = new Request(url, init);
|
|
@@ -257,8 +245,10 @@ export const createClient = (config: Config = {}): Client => {
|
|
|
257
245
|
});
|
|
258
246
|
};
|
|
259
247
|
|
|
248
|
+
const _buildUrl: Client['buildUrl'] = (options) => buildUrl({ ..._config, ...options });
|
|
249
|
+
|
|
260
250
|
return {
|
|
261
|
-
buildUrl,
|
|
251
|
+
buildUrl: _buildUrl,
|
|
262
252
|
connect: makeMethodFn('CONNECT'),
|
|
263
253
|
delete: makeMethodFn('DELETE'),
|
|
264
254
|
get: makeMethodFn('GET'),
|