@outfoxx/sunday 1.1.0-alpha.8 → 1.1.0-beta.1
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/LICENSE.txt +203 -0
- package/README.md +6 -7
- package/dist/any-type.js +13 -0
- package/dist/any-type.js.map +1 -1
- package/dist/class-type.js +13 -0
- package/dist/class-type.js.map +1 -1
- package/dist/date-time-types.js +13 -0
- package/dist/date-time-types.js.map +1 -1
- package/dist/event-parser.d.ts +1 -1
- package/dist/event-parser.js +17 -4
- package/dist/event-parser.js.map +1 -1
- package/dist/fetch-event-source.d.ts +5 -1
- package/dist/fetch-event-source.js +15 -3
- package/dist/fetch-event-source.js.map +1 -1
- package/dist/fetch-request-factory.d.ts +3 -0
- package/dist/fetch-request-factory.js +47 -9
- package/dist/fetch-request-factory.js.map +1 -1
- package/dist/fetch.js +24 -5
- package/dist/fetch.js.map +1 -1
- package/dist/header-parameters.js +13 -0
- package/dist/header-parameters.js.map +1 -1
- package/dist/index.js +13 -0
- package/dist/index.js.map +1 -1
- package/dist/logger.js +13 -0
- package/dist/logger.js.map +1 -1
- package/dist/media-type-codecs/any-text-decoder.d.ts +7 -0
- package/dist/media-type-codecs/any-text-decoder.js +29 -0
- package/dist/media-type-codecs/any-text-decoder.js.map +1 -0
- package/dist/media-type-codecs/any-text-encoder.d.ts +5 -0
- package/dist/media-type-codecs/any-text-encoder.js +23 -0
- package/dist/media-type-codecs/any-text-encoder.js.map +1 -0
- package/dist/media-type-codecs/binary-decoder.js +13 -0
- package/dist/media-type-codecs/binary-decoder.js.map +1 -1
- package/dist/media-type-codecs/binary-encoder.js +13 -0
- package/dist/media-type-codecs/binary-encoder.js.map +1 -1
- package/dist/media-type-codecs/cbor-decoder.js +13 -0
- package/dist/media-type-codecs/cbor-decoder.js.map +1 -1
- package/dist/media-type-codecs/cbor-encoder.js +13 -0
- package/dist/media-type-codecs/cbor-encoder.js.map +1 -1
- package/dist/media-type-codecs/cbor-tags.js +13 -0
- package/dist/media-type-codecs/cbor-tags.js.map +1 -1
- package/dist/media-type-codecs/json-decoder.js +13 -0
- package/dist/media-type-codecs/json-decoder.js.map +1 -1
- package/dist/media-type-codecs/json-encoder.js +13 -0
- package/dist/media-type-codecs/json-encoder.js.map +1 -1
- package/dist/media-type-codecs/media-type-decoder.js +13 -0
- package/dist/media-type-codecs/media-type-decoder.js.map +1 -1
- package/dist/media-type-codecs/media-type-decoders.js +21 -3
- package/dist/media-type-codecs/media-type-decoders.js.map +1 -1
- package/dist/media-type-codecs/media-type-encoder.js +13 -0
- package/dist/media-type-codecs/media-type-encoder.js.map +1 -1
- package/dist/media-type-codecs/media-type-encoders.js +21 -3
- package/dist/media-type-codecs/media-type-encoders.js.map +1 -1
- package/dist/media-type-codecs/www-form-url-encoder.js +56 -6
- package/dist/media-type-codecs/www-form-url-encoder.js.map +1 -1
- package/dist/media-type.d.ts +4 -2
- package/dist/media-type.js +34 -5
- package/dist/media-type.js.map +1 -1
- package/dist/problem.js +19 -2
- package/dist/problem.js.map +1 -1
- package/dist/request-factory.d.ts +6 -0
- package/dist/request-factory.js +13 -0
- package/dist/request-factory.js.map +1 -1
- package/dist/result-response.d.ts +4 -0
- package/dist/result-response.js +15 -0
- package/dist/result-response.js.map +1 -0
- package/dist/sunday-error.js +13 -0
- package/dist/sunday-error.js.map +1 -1
- package/dist/url-template.js +13 -0
- package/dist/url-template.js.map +1 -1
- package/dist/util/any.js +13 -0
- package/dist/util/any.js.map +1 -1
- package/dist/util/base64.js +13 -0
- package/dist/util/base64.js.map +1 -1
- package/dist/util/error.d.ts +2 -0
- package/dist/util/error.js +26 -0
- package/dist/util/error.js.map +1 -0
- package/dist/util/hex.js +13 -0
- package/dist/util/hex.js.map +1 -1
- package/dist/util/rxjs.js +14 -2
- package/dist/util/rxjs.js.map +1 -1
- package/dist/util/stream-rxjs.js +13 -0
- package/dist/util/stream-rxjs.js.map +1 -1
- package/dist/util/temporal.js +13 -0
- package/dist/util/temporal.js.map +1 -1
- package/package.json +33 -28
- package/src/any-type.ts +14 -0
- package/src/class-type.ts +16 -2
- package/src/date-time-types.ts +14 -0
- package/src/event-parser.ts +22 -10
- package/src/fetch-event-source.ts +39 -11
- package/src/fetch-request-factory.ts +92 -32
- package/src/fetch.ts +27 -8
- package/src/header-parameters.ts +16 -2
- package/src/index.ts +14 -0
- package/src/logger.ts +14 -0
- package/src/media-type-codecs/any-text-decoder.ts +34 -0
- package/src/media-type-codecs/any-text-encoder.ts +26 -0
- package/src/media-type-codecs/binary-decoder.ts +16 -2
- package/src/media-type-codecs/binary-encoder.ts +15 -1
- package/src/media-type-codecs/cbor-decoder.ts +30 -16
- package/src/media-type-codecs/cbor-encoder.ts +28 -14
- package/src/media-type-codecs/cbor-tags.ts +14 -0
- package/src/media-type-codecs/json-decoder.ts +31 -16
- package/src/media-type-codecs/json-encoder.ts +21 -7
- package/src/media-type-codecs/media-type-decoder.ts +16 -2
- package/src/media-type-codecs/media-type-decoders.ts +28 -8
- package/src/media-type-codecs/media-type-encoder.ts +19 -5
- package/src/media-type-codecs/media-type-encoders.ts +28 -8
- package/src/media-type-codecs/www-form-url-encoder.ts +81 -17
- package/src/media-type.ts +46 -12
- package/src/problem.ts +22 -4
- package/src/request-factory.ts +46 -7
- package/src/result-response.ts +18 -0
- package/src/sunday-error.ts +18 -4
- package/src/url-template.ts +15 -1
- package/src/util/any.ts +14 -0
- package/src/util/base64.ts +15 -1
- package/src/util/error.ts +28 -0
- package/src/util/hex.ts +14 -0
- package/src/util/rxjs.ts +18 -5
- package/src/util/stream-rxjs.ts +15 -1
- package/src/util/temporal.ts +14 -0
|
@@ -1,10 +1,23 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
// Copyright 2020 Outfox, Inc.
|
|
2
|
+
//
|
|
3
|
+
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
|
+
// you may not use this file except in compliance with the License.
|
|
5
|
+
// You may obtain a copy of the License at
|
|
6
|
+
//
|
|
7
|
+
// http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
+
//
|
|
9
|
+
// Unless required by applicable law or agreed to in writing, software
|
|
10
|
+
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
11
|
+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
|
+
// See the License for the specific language governing permissions and
|
|
13
|
+
// limitations under the License.
|
|
14
|
+
|
|
15
|
+
import { defer, map, Observable, of, switchMap } from 'rxjs';
|
|
3
16
|
import { AnyType } from './any-type';
|
|
4
17
|
import { ConstructableClassType } from './class-type';
|
|
5
18
|
import { validate } from './fetch';
|
|
6
19
|
import { FetchEventSource } from './fetch-event-source';
|
|
7
|
-
import {
|
|
20
|
+
import { HeaderParameters } from './header-parameters';
|
|
8
21
|
import { Logger } from './logger';
|
|
9
22
|
import { MediaType } from './media-type';
|
|
10
23
|
import { TextMediaTypeDecoder } from './media-type-codecs/media-type-decoder';
|
|
@@ -18,8 +31,10 @@ import {
|
|
|
18
31
|
RequestFactory,
|
|
19
32
|
RequestSpec,
|
|
20
33
|
} from './request-factory';
|
|
34
|
+
import { ResultResponse } from './result-response';
|
|
35
|
+
import { SundayError } from './sunday-error';
|
|
21
36
|
import { URLTemplate } from './url-template';
|
|
22
|
-
import {
|
|
37
|
+
import { errorToMessage } from './util/error';
|
|
23
38
|
|
|
24
39
|
export class FetchRequestFactory implements RequestFactory {
|
|
25
40
|
public baseUrl: URLTemplate;
|
|
@@ -36,7 +51,7 @@ export class FetchRequestFactory implements RequestFactory {
|
|
|
36
51
|
mediaTypeEncoders?: MediaTypeEncoders;
|
|
37
52
|
mediaTypeDecoders?: MediaTypeDecoders;
|
|
38
53
|
logger?: Logger;
|
|
39
|
-
}
|
|
54
|
+
},
|
|
40
55
|
) {
|
|
41
56
|
this.baseUrl =
|
|
42
57
|
typeof baseUrl === 'string' ? new URLTemplate(baseUrl) : baseUrl;
|
|
@@ -50,7 +65,7 @@ export class FetchRequestFactory implements RequestFactory {
|
|
|
50
65
|
|
|
51
66
|
registerProblem(
|
|
52
67
|
type: URL | string,
|
|
53
|
-
problemType: ConstructableClassType<Problem
|
|
68
|
+
problemType: ConstructableClassType<Problem>,
|
|
54
69
|
): void {
|
|
55
70
|
const typeStr = type instanceof URL ? type.toString() : type;
|
|
56
71
|
this.problemTypes.set(typeStr, problemType);
|
|
@@ -58,26 +73,26 @@ export class FetchRequestFactory implements RequestFactory {
|
|
|
58
73
|
|
|
59
74
|
request(
|
|
60
75
|
requestSpec: RequestSpec<unknown>,
|
|
61
|
-
requestInit?: RequestInit
|
|
76
|
+
requestInit?: RequestInit,
|
|
62
77
|
): Observable<Request> {
|
|
63
78
|
//
|
|
64
79
|
return defer(() => {
|
|
65
80
|
const url = this.baseUrl.complete(
|
|
66
81
|
requestSpec.pathTemplate,
|
|
67
|
-
requestSpec.pathParameters ?? {}
|
|
82
|
+
requestSpec.pathParameters ?? {},
|
|
68
83
|
);
|
|
69
84
|
|
|
70
85
|
if (requestSpec.queryParameters) {
|
|
71
86
|
const encoder = this.mediaTypeEncoders.find(
|
|
72
|
-
MediaType.WWWFormUrlEncoded
|
|
87
|
+
MediaType.WWWFormUrlEncoded,
|
|
73
88
|
);
|
|
74
89
|
if (!isURLQueryParamsEncoder(encoder)) {
|
|
75
90
|
throw Error(
|
|
76
|
-
`MediaTypeEncoder for ${MediaType.WWWFormUrlEncoded} must be an instance of URLQueryParamsEncoder
|
|
91
|
+
`MediaTypeEncoder for ${MediaType.WWWFormUrlEncoded} must be an instance of URLQueryParamsEncoder`,
|
|
77
92
|
);
|
|
78
93
|
}
|
|
79
94
|
url.search = `?${encoder.encodeQueryString(
|
|
80
|
-
requestSpec.queryParameters
|
|
95
|
+
requestSpec.queryParameters,
|
|
81
96
|
)}`;
|
|
82
97
|
}
|
|
83
98
|
|
|
@@ -86,12 +101,12 @@ export class FetchRequestFactory implements RequestFactory {
|
|
|
86
101
|
// Determine & add accept header
|
|
87
102
|
if (requestSpec.acceptTypes) {
|
|
88
103
|
const supportedAcceptTypes = requestSpec.acceptTypes.filter((type) =>
|
|
89
|
-
this.mediaTypeDecoders.supports(type)
|
|
104
|
+
this.mediaTypeDecoders.supports(type),
|
|
90
105
|
);
|
|
91
106
|
|
|
92
107
|
if (!supportedAcceptTypes.length) {
|
|
93
108
|
throw Error(
|
|
94
|
-
'None of the provided accept types has a reqistered decoder'
|
|
109
|
+
'None of the provided accept types has a reqistered decoder',
|
|
95
110
|
);
|
|
96
111
|
}
|
|
97
112
|
|
|
@@ -102,7 +117,7 @@ export class FetchRequestFactory implements RequestFactory {
|
|
|
102
117
|
|
|
103
118
|
// Determine content type
|
|
104
119
|
const contentType = requestSpec.contentTypes?.find((type) =>
|
|
105
|
-
this.mediaTypeEncoders.supports(type)
|
|
120
|
+
this.mediaTypeEncoders.supports(type),
|
|
106
121
|
);
|
|
107
122
|
|
|
108
123
|
// If matched, add content type (even if body is nil, to match any expected server requirements)
|
|
@@ -115,7 +130,7 @@ export class FetchRequestFactory implements RequestFactory {
|
|
|
115
130
|
if (requestSpec.body) {
|
|
116
131
|
if (!contentType) {
|
|
117
132
|
throw Error(
|
|
118
|
-
'None of the provided content types has a registered encoder'
|
|
133
|
+
'None of the provided content types has a registered encoder',
|
|
119
134
|
);
|
|
120
135
|
}
|
|
121
136
|
|
|
@@ -138,45 +153,92 @@ export class FetchRequestFactory implements RequestFactory {
|
|
|
138
153
|
|
|
139
154
|
response(
|
|
140
155
|
request: Request | RequestSpec<unknown>,
|
|
141
|
-
dataExpected?: boolean
|
|
156
|
+
dataExpected?: boolean,
|
|
142
157
|
): Observable<Response> {
|
|
143
158
|
const request$ =
|
|
144
159
|
request instanceof Request ? of(request) : this.request(request);
|
|
145
160
|
return request$.pipe(
|
|
146
161
|
switchMap((req) => fetch(req)),
|
|
147
162
|
switchMap((response) =>
|
|
148
|
-
validate(response, dataExpected ?? false, this.problemTypes)
|
|
149
|
-
)
|
|
163
|
+
validate(response, dataExpected ?? false, this.problemTypes),
|
|
164
|
+
),
|
|
150
165
|
);
|
|
151
166
|
}
|
|
152
167
|
|
|
168
|
+
resultResponse<B, R>(
|
|
169
|
+
requestSpec: RequestSpec<B>,
|
|
170
|
+
resultType: AnyType,
|
|
171
|
+
): Observable<ResultResponse<R>>;
|
|
172
|
+
resultResponse<B>(
|
|
173
|
+
requestSpec: RequestSpec<B>,
|
|
174
|
+
): Observable<ResultResponse<void>>;
|
|
175
|
+
resultResponse(
|
|
176
|
+
request: RequestSpec<unknown>,
|
|
177
|
+
responseType?: AnyType,
|
|
178
|
+
): Observable<ResultResponse<unknown>> {
|
|
179
|
+
const response$ = this.response(request, !!responseType);
|
|
180
|
+
|
|
181
|
+
if (!responseType) {
|
|
182
|
+
return response$.pipe(
|
|
183
|
+
map((response) => {
|
|
184
|
+
return {
|
|
185
|
+
result: undefined,
|
|
186
|
+
response,
|
|
187
|
+
};
|
|
188
|
+
}),
|
|
189
|
+
);
|
|
190
|
+
} else {
|
|
191
|
+
return response$.pipe(
|
|
192
|
+
switchMap(async (response) => {
|
|
193
|
+
try {
|
|
194
|
+
const contentType = MediaType.from(
|
|
195
|
+
response.headers.get('content-type'),
|
|
196
|
+
MediaType.OctetStream,
|
|
197
|
+
);
|
|
198
|
+
const decoder = this.mediaTypeDecoders.find(contentType);
|
|
199
|
+
const result = await decoder.decode(response, responseType);
|
|
200
|
+
return {
|
|
201
|
+
result,
|
|
202
|
+
response,
|
|
203
|
+
};
|
|
204
|
+
} catch (error) {
|
|
205
|
+
throw await SundayError.fromResponse(
|
|
206
|
+
errorToMessage(error, 'Response Decoding Failed'),
|
|
207
|
+
response,
|
|
208
|
+
);
|
|
209
|
+
}
|
|
210
|
+
}),
|
|
211
|
+
);
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
|
|
153
215
|
result<B, R>(requestSpec: RequestSpec<B>, resultType: AnyType): Observable<R>;
|
|
154
216
|
result<B>(requestSpec: RequestSpec<B>): Observable<void>;
|
|
155
217
|
result(
|
|
156
218
|
request: RequestSpec<unknown>,
|
|
157
|
-
responseType?: AnyType
|
|
219
|
+
responseType?: AnyType,
|
|
158
220
|
): Observable<unknown> {
|
|
159
221
|
const response$ = this.response(request, !!responseType);
|
|
160
222
|
|
|
161
223
|
if (!responseType) {
|
|
162
|
-
return response$.pipe(
|
|
224
|
+
return response$.pipe(map(() => undefined));
|
|
163
225
|
} else {
|
|
164
226
|
return response$.pipe(
|
|
165
227
|
switchMap(async (response) => {
|
|
166
228
|
try {
|
|
167
229
|
const contentType = MediaType.from(
|
|
168
230
|
response.headers.get('content-type'),
|
|
169
|
-
MediaType.OctetStream
|
|
231
|
+
MediaType.OctetStream,
|
|
170
232
|
);
|
|
171
233
|
const decoder = this.mediaTypeDecoders.find(contentType);
|
|
172
234
|
return await decoder.decode(response, responseType);
|
|
173
235
|
} catch (error) {
|
|
174
236
|
throw await SundayError.fromResponse(
|
|
175
|
-
error
|
|
176
|
-
response
|
|
237
|
+
errorToMessage(error, 'Response Decoding Failed'),
|
|
238
|
+
response,
|
|
177
239
|
);
|
|
178
240
|
}
|
|
179
|
-
})
|
|
241
|
+
}),
|
|
180
242
|
);
|
|
181
243
|
}
|
|
182
244
|
}
|
|
@@ -185,7 +247,7 @@ export class FetchRequestFactory implements RequestFactory {
|
|
|
185
247
|
//
|
|
186
248
|
const adapter = (
|
|
187
249
|
url: string,
|
|
188
|
-
requestInit: RequestInit
|
|
250
|
+
requestInit: RequestInit,
|
|
189
251
|
): Observable<Request> => {
|
|
190
252
|
const eventSourceSpec = Object.assign({}, requestSpec, {
|
|
191
253
|
pathTemplate: url,
|
|
@@ -193,12 +255,10 @@ export class FetchRequestFactory implements RequestFactory {
|
|
|
193
255
|
return this.request(eventSourceSpec, requestInit);
|
|
194
256
|
};
|
|
195
257
|
|
|
196
|
-
|
|
258
|
+
return new FetchEventSource(requestSpec.pathTemplate, {
|
|
197
259
|
logger: this.logger,
|
|
198
260
|
adapter,
|
|
199
261
|
});
|
|
200
|
-
|
|
201
|
-
return eventSource;
|
|
202
262
|
}
|
|
203
263
|
|
|
204
264
|
eventStream<E>(
|
|
@@ -208,13 +268,13 @@ export class FetchRequestFactory implements RequestFactory {
|
|
|
208
268
|
event: string | undefined,
|
|
209
269
|
id: string | undefined,
|
|
210
270
|
data: string,
|
|
211
|
-
logger?: Logger
|
|
212
|
-
) => E | undefined
|
|
271
|
+
logger?: Logger,
|
|
272
|
+
) => E | undefined,
|
|
213
273
|
): Observable<E> {
|
|
214
274
|
const eventSource = this.eventSource(requestSpec);
|
|
215
275
|
|
|
216
276
|
const jsonDecoder = this.mediaTypeDecoders.find(
|
|
217
|
-
MediaType.JSON
|
|
277
|
+
MediaType.JSON,
|
|
218
278
|
) as TextMediaTypeDecoder;
|
|
219
279
|
|
|
220
280
|
return new Observable((subscriber) => {
|
|
@@ -229,7 +289,7 @@ export class FetchRequestFactory implements RequestFactory {
|
|
|
229
289
|
event.type,
|
|
230
290
|
event.lastEventId,
|
|
231
291
|
event.data,
|
|
232
|
-
this.logger
|
|
292
|
+
this.logger,
|
|
233
293
|
);
|
|
234
294
|
if (!decodedEvent) {
|
|
235
295
|
return;
|
package/src/fetch.ts
CHANGED
|
@@ -1,27 +1,46 @@
|
|
|
1
|
+
// Copyright 2020 Outfox, Inc.
|
|
2
|
+
//
|
|
3
|
+
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
|
+
// you may not use this file except in compliance with the License.
|
|
5
|
+
// You may obtain a copy of the License at
|
|
6
|
+
//
|
|
7
|
+
// http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
+
//
|
|
9
|
+
// Unless required by applicable law or agreed to in writing, software
|
|
10
|
+
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
11
|
+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
|
+
// See the License for the specific language governing permissions and
|
|
13
|
+
// limitations under the License.
|
|
14
|
+
|
|
1
15
|
import { ConstructableClassType } from './class-type';
|
|
2
16
|
import { MediaType } from './media-type';
|
|
3
17
|
import { Problem } from './problem';
|
|
4
18
|
import { SundayError } from './sunday-error';
|
|
5
19
|
import { Base64 } from './util/base64';
|
|
20
|
+
import { errorToMessage } from './util/error';
|
|
6
21
|
|
|
7
22
|
export async function validate(
|
|
8
23
|
response: Response,
|
|
9
24
|
dataExpected: boolean,
|
|
10
|
-
problemTypes?: Map<string, ConstructableClassType<Problem
|
|
25
|
+
problemTypes?: Map<string, ConstructableClassType<Problem>>,
|
|
11
26
|
): Promise<Response> {
|
|
12
27
|
if (response.status < 200 || response.status >= 300) {
|
|
13
28
|
const mediaType = MediaType.from(
|
|
14
29
|
response.headers.get('content-type'),
|
|
15
|
-
MediaType.OctetStream
|
|
30
|
+
MediaType.OctetStream,
|
|
16
31
|
);
|
|
17
|
-
const
|
|
18
|
-
if (!
|
|
32
|
+
const isProblem = mediaType?.compatible(MediaType.Problem) ?? false;
|
|
33
|
+
if (!isProblem) {
|
|
19
34
|
throw await Problem.fromResponse(response);
|
|
20
35
|
}
|
|
21
36
|
|
|
22
37
|
const problemData = await response.json();
|
|
23
38
|
const problemType = problemTypes?.get(problemData.type) ?? Problem;
|
|
24
|
-
|
|
39
|
+
if (problemType.name === 'Problem') {
|
|
40
|
+
throw new Problem(problemData);
|
|
41
|
+
} else {
|
|
42
|
+
throw new problemType(problemData?.instance);
|
|
43
|
+
}
|
|
25
44
|
}
|
|
26
45
|
|
|
27
46
|
if (dataExpected && (response.status === 204 || response.status === 205)) {
|
|
@@ -45,7 +64,7 @@ export namespace ResponseExample {
|
|
|
45
64
|
|
|
46
65
|
export async function bodyExcerpt(
|
|
47
66
|
response: Response,
|
|
48
|
-
maxLength: number
|
|
67
|
+
maxLength: number,
|
|
49
68
|
): Promise<[string, unknown]> {
|
|
50
69
|
let body: unknown;
|
|
51
70
|
let bodyExcerpt: string;
|
|
@@ -70,9 +89,9 @@ export namespace ResponseExample {
|
|
|
70
89
|
bodyExcerpt = Base64.encode(dataSlice);
|
|
71
90
|
}
|
|
72
91
|
}
|
|
73
|
-
} catch (
|
|
92
|
+
} catch (error) {
|
|
74
93
|
// ignore errors
|
|
75
|
-
const message =
|
|
94
|
+
const message = errorToMessage(error);
|
|
76
95
|
body = bodyExcerpt = `<<error displaying response data: ${message}>>`;
|
|
77
96
|
}
|
|
78
97
|
return [bodyExcerpt, body];
|
package/src/header-parameters.ts
CHANGED
|
@@ -1,6 +1,20 @@
|
|
|
1
|
+
// Copyright 2020 Outfox, Inc.
|
|
2
|
+
//
|
|
3
|
+
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
|
+
// you may not use this file except in compliance with the License.
|
|
5
|
+
// You may obtain a copy of the License at
|
|
6
|
+
//
|
|
7
|
+
// http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
+
//
|
|
9
|
+
// Unless required by applicable law or agreed to in writing, software
|
|
10
|
+
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
11
|
+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
|
+
// See the License for the specific language governing permissions and
|
|
13
|
+
// limitations under the License.
|
|
14
|
+
|
|
1
15
|
export namespace HeaderParameters {
|
|
2
16
|
export function encode(
|
|
3
|
-
parameters?: Record<string, unknown
|
|
17
|
+
parameters?: Record<string, unknown>,
|
|
4
18
|
): [string, string][] {
|
|
5
19
|
if (parameters == null) {
|
|
6
20
|
return [];
|
|
@@ -44,7 +58,7 @@ export namespace HeaderParameters {
|
|
|
44
58
|
function validate(name: string, value: string): string {
|
|
45
59
|
if (!asciiRegex.test(value)) {
|
|
46
60
|
throw new Error(
|
|
47
|
-
`The encoded header value contains one or more invalid characters: header=${name}, value=${value}
|
|
61
|
+
`The encoded header value contains one or more invalid characters: header=${name}, value=${value}`,
|
|
48
62
|
);
|
|
49
63
|
}
|
|
50
64
|
return value;
|
package/src/index.ts
CHANGED
|
@@ -1,3 +1,17 @@
|
|
|
1
|
+
// Copyright 2020 Outfox, Inc.
|
|
2
|
+
//
|
|
3
|
+
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
|
+
// you may not use this file except in compliance with the License.
|
|
5
|
+
// You may obtain a copy of the License at
|
|
6
|
+
//
|
|
7
|
+
// http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
+
//
|
|
9
|
+
// Unless required by applicable law or agreed to in writing, software
|
|
10
|
+
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
11
|
+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
|
+
// See the License for the specific language governing permissions and
|
|
13
|
+
// limitations under the License.
|
|
14
|
+
|
|
1
15
|
export * from './any-type';
|
|
2
16
|
export * from './class-type';
|
|
3
17
|
export * from './request-factory';
|
package/src/logger.ts
CHANGED
|
@@ -1,3 +1,17 @@
|
|
|
1
|
+
// Copyright 2020 Outfox, Inc.
|
|
2
|
+
//
|
|
3
|
+
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
|
+
// you may not use this file except in compliance with the License.
|
|
5
|
+
// You may obtain a copy of the License at
|
|
6
|
+
//
|
|
7
|
+
// http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
+
//
|
|
9
|
+
// Unless required by applicable law or agreed to in writing, software
|
|
10
|
+
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
11
|
+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
|
+
// See the License for the specific language governing permissions and
|
|
13
|
+
// limitations under the License.
|
|
14
|
+
|
|
1
15
|
export interface Logger {
|
|
2
16
|
trace?(...data: unknown[]): void;
|
|
3
17
|
debug?(...data: unknown[]): void;
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
// Copyright 2020 Outfox, Inc.
|
|
2
|
+
//
|
|
3
|
+
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
|
+
// you may not use this file except in compliance with the License.
|
|
5
|
+
// You may obtain a copy of the License at
|
|
6
|
+
//
|
|
7
|
+
// http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
+
//
|
|
9
|
+
// Unless required by applicable law or agreed to in writing, software
|
|
10
|
+
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
11
|
+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
|
+
// See the License for the specific language governing permissions and
|
|
13
|
+
// limitations under the License.
|
|
14
|
+
|
|
15
|
+
import { AnyType } from '../any-type';
|
|
16
|
+
import { TextMediaTypeDecoder } from './media-type-decoder';
|
|
17
|
+
|
|
18
|
+
export class AnyTextDecoder implements TextMediaTypeDecoder {
|
|
19
|
+
static default = new AnyTextDecoder();
|
|
20
|
+
|
|
21
|
+
async decode<T>(response: Response, type: AnyType): Promise<T> {
|
|
22
|
+
if (type[0] != String) {
|
|
23
|
+
throw Error('Invalid type, expected String');
|
|
24
|
+
}
|
|
25
|
+
return (await response.text()) as unknown as T;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
decodeText<T>(text: string, type: AnyType): T {
|
|
29
|
+
if (type[0] != String) {
|
|
30
|
+
throw Error('Invalid type, expected String');
|
|
31
|
+
}
|
|
32
|
+
return text as unknown as T;
|
|
33
|
+
}
|
|
34
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
// Copyright 2020 Outfox, Inc.
|
|
2
|
+
//
|
|
3
|
+
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
|
+
// you may not use this file except in compliance with the License.
|
|
5
|
+
// You may obtain a copy of the License at
|
|
6
|
+
//
|
|
7
|
+
// http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
+
//
|
|
9
|
+
// Unless required by applicable law or agreed to in writing, software
|
|
10
|
+
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
11
|
+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
|
+
// See the License for the specific language governing permissions and
|
|
13
|
+
// limitations under the License.
|
|
14
|
+
|
|
15
|
+
import { MediaTypeEncoder } from './media-type-encoder';
|
|
16
|
+
|
|
17
|
+
export class AnyTextEncoder implements MediaTypeEncoder {
|
|
18
|
+
static default = new AnyTextEncoder();
|
|
19
|
+
|
|
20
|
+
encode(value: unknown): BodyInit {
|
|
21
|
+
if (typeof value != 'string') {
|
|
22
|
+
throw Error('Invalid value, expected string');
|
|
23
|
+
}
|
|
24
|
+
return value;
|
|
25
|
+
}
|
|
26
|
+
}
|
|
@@ -1,3 +1,17 @@
|
|
|
1
|
+
// Copyright 2020 Outfox, Inc.
|
|
2
|
+
//
|
|
3
|
+
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
|
+
// you may not use this file except in compliance with the License.
|
|
5
|
+
// You may obtain a copy of the License at
|
|
6
|
+
//
|
|
7
|
+
// http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
+
//
|
|
9
|
+
// Unless required by applicable law or agreed to in writing, software
|
|
10
|
+
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
11
|
+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
|
+
// See the License for the specific language governing permissions and
|
|
13
|
+
// limitations under the License.
|
|
14
|
+
|
|
1
15
|
import { MediaTypeDecoder } from './media-type-decoder';
|
|
2
16
|
import { AnyConstructableType } from '../any-type';
|
|
3
17
|
|
|
@@ -8,7 +22,7 @@ export class BinaryDecoder implements MediaTypeDecoder {
|
|
|
8
22
|
const arrayBuffer = await response.arrayBuffer();
|
|
9
23
|
|
|
10
24
|
if (type[0] === ArrayBuffer) {
|
|
11
|
-
return
|
|
25
|
+
return arrayBuffer as unknown as T;
|
|
12
26
|
} else if (
|
|
13
27
|
type[0] === Uint8Array ||
|
|
14
28
|
type[0] === Int8Array ||
|
|
@@ -18,7 +32,7 @@ export class BinaryDecoder implements MediaTypeDecoder {
|
|
|
18
32
|
}
|
|
19
33
|
|
|
20
34
|
throw Error(
|
|
21
|
-
'Invalid value, expected ArrayBuffer, (Int|Uint)Array or DataView'
|
|
35
|
+
'Invalid value, expected ArrayBuffer, (Int|Uint)Array or DataView',
|
|
22
36
|
);
|
|
23
37
|
}
|
|
24
38
|
}
|
|
@@ -1,3 +1,17 @@
|
|
|
1
|
+
// Copyright 2020 Outfox, Inc.
|
|
2
|
+
//
|
|
3
|
+
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
|
+
// you may not use this file except in compliance with the License.
|
|
5
|
+
// You may obtain a copy of the License at
|
|
6
|
+
//
|
|
7
|
+
// http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
+
//
|
|
9
|
+
// Unless required by applicable law or agreed to in writing, software
|
|
10
|
+
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
11
|
+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
|
+
// See the License for the specific language governing permissions and
|
|
13
|
+
// limitations under the License.
|
|
14
|
+
|
|
1
15
|
import { MediaTypeEncoder } from './media-type-encoder';
|
|
2
16
|
|
|
3
17
|
export class BinaryEncoder implements MediaTypeEncoder {
|
|
@@ -11,7 +25,7 @@ export class BinaryEncoder implements MediaTypeEncoder {
|
|
|
11
25
|
!(value instanceof ReadableStream)
|
|
12
26
|
) {
|
|
13
27
|
throw Error(
|
|
14
|
-
'Invalid value, expected BufferSource, Blob or ReadableStream'
|
|
28
|
+
'Invalid value, expected BufferSource, Blob or ReadableStream',
|
|
15
29
|
);
|
|
16
30
|
}
|
|
17
31
|
return value;
|