@outfoxx/sunday 1.0.9 → 1.1.0-alpha.11

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.
Files changed (179) hide show
  1. package/LICENSE.txt +203 -0
  2. package/README.md +35 -2
  3. package/dist/any-type.d.ts +2 -0
  4. package/dist/any-type.js +13 -0
  5. package/dist/any-type.js.map +1 -1
  6. package/dist/class-type.d.ts +2 -2
  7. package/dist/class-type.js +13 -0
  8. package/dist/class-type.js.map +1 -1
  9. package/dist/date-time-types.d.ts +2 -7
  10. package/dist/date-time-types.js +15 -4
  11. package/dist/date-time-types.js.map +1 -1
  12. package/dist/event-parser.d.ts +17 -0
  13. package/dist/event-parser.js +153 -0
  14. package/dist/event-parser.js.map +1 -0
  15. package/dist/fetch-event-source.d.ts +12 -10
  16. package/dist/fetch-event-source.js +154 -138
  17. package/dist/fetch-event-source.js.map +1 -1
  18. package/dist/fetch-request-factory.d.ts +10 -5
  19. package/dist/fetch-request-factory.js +100 -71
  20. package/dist/fetch-request-factory.js.map +1 -1
  21. package/dist/fetch.d.ts +7 -1
  22. package/dist/fetch.js +74 -8
  23. package/dist/fetch.js.map +1 -1
  24. package/dist/header-parameters.d.ts +3 -0
  25. package/dist/header-parameters.js +53 -0
  26. package/dist/header-parameters.js.map +1 -0
  27. package/dist/index.d.ts +12 -12
  28. package/dist/index.js +25 -12
  29. package/dist/index.js.map +1 -1
  30. package/dist/logger.js +13 -0
  31. package/dist/logger.js.map +1 -1
  32. package/dist/{binary-decoder.d.ts → media-type-codecs/binary-decoder.d.ts} +2 -2
  33. package/dist/media-type-codecs/binary-decoder.js +29 -0
  34. package/dist/media-type-codecs/binary-decoder.js.map +1 -0
  35. package/dist/{binary-encoder.d.ts → media-type-codecs/binary-encoder.d.ts} +0 -0
  36. package/dist/media-type-codecs/binary-encoder.js +26 -0
  37. package/dist/media-type-codecs/binary-encoder.js.map +1 -0
  38. package/dist/media-type-codecs/cbor-decoder.d.ts +39 -0
  39. package/dist/media-type-codecs/cbor-decoder.js +426 -0
  40. package/dist/media-type-codecs/cbor-decoder.js.map +1 -0
  41. package/dist/media-type-codecs/cbor-encoder.d.ts +43 -0
  42. package/dist/media-type-codecs/cbor-encoder.js +239 -0
  43. package/dist/media-type-codecs/cbor-encoder.js.map +1 -0
  44. package/dist/media-type-codecs/cbor-tags.d.ts +4 -0
  45. package/dist/media-type-codecs/cbor-tags.js +18 -0
  46. package/dist/media-type-codecs/cbor-tags.js.map +1 -0
  47. package/dist/media-type-codecs/json-decoder.d.ts +39 -0
  48. package/dist/media-type-codecs/json-decoder.js +380 -0
  49. package/dist/media-type-codecs/json-decoder.js.map +1 -0
  50. package/dist/media-type-codecs/json-encoder.d.ts +44 -0
  51. package/dist/media-type-codecs/json-encoder.js +276 -0
  52. package/dist/media-type-codecs/json-encoder.js.map +1 -0
  53. package/dist/media-type-codecs/media-type-decoder.d.ts +11 -0
  54. package/dist/media-type-codecs/media-type-decoder.js +19 -0
  55. package/dist/media-type-codecs/media-type-decoder.js.map +1 -0
  56. package/dist/{media-type-decoders.d.ts → media-type-codecs/media-type-decoders.d.ts} +5 -4
  57. package/dist/media-type-codecs/media-type-decoders.js +53 -0
  58. package/dist/media-type-codecs/media-type-decoders.js.map +1 -0
  59. package/dist/{media-type-encoder.d.ts → media-type-codecs/media-type-encoder.d.ts} +5 -1
  60. package/dist/media-type-codecs/media-type-encoder.js +24 -0
  61. package/dist/media-type-codecs/media-type-encoder.js.map +1 -0
  62. package/dist/{media-type-encoders.d.ts → media-type-codecs/media-type-encoders.d.ts} +5 -4
  63. package/dist/media-type-codecs/media-type-encoders.js +55 -0
  64. package/dist/media-type-codecs/media-type-encoders.js.map +1 -0
  65. package/dist/{url-encoder.d.ts → media-type-codecs/www-form-url-encoder.d.ts} +8 -8
  66. package/dist/{url-encoder.js → media-type-codecs/www-form-url-encoder.js} +51 -37
  67. package/dist/media-type-codecs/www-form-url-encoder.js.map +1 -0
  68. package/dist/media-type.d.ts +91 -13
  69. package/dist/media-type.js +286 -15
  70. package/dist/media-type.js.map +1 -1
  71. package/dist/problem.d.ts +24 -9
  72. package/dist/problem.js +153 -6
  73. package/dist/problem.js.map +1 -1
  74. package/dist/request-factory.d.ts +10 -10
  75. package/dist/request-factory.js +13 -0
  76. package/dist/request-factory.js.map +1 -1
  77. package/dist/sunday-error.d.ts +11 -0
  78. package/dist/sunday-error.js +31 -0
  79. package/dist/sunday-error.js.map +1 -0
  80. package/dist/url-template.js +13 -0
  81. package/dist/url-template.js.map +1 -1
  82. package/dist/util/any.d.ts +2 -0
  83. package/dist/util/any.js +24 -0
  84. package/dist/util/any.js.map +1 -0
  85. package/dist/util/base64.js +13 -0
  86. package/dist/util/base64.js.map +1 -1
  87. package/dist/util/error.d.ts +2 -0
  88. package/dist/util/error.js +26 -0
  89. package/dist/util/error.js.map +1 -0
  90. package/dist/util/hex.js +16 -2
  91. package/dist/util/hex.js.map +1 -1
  92. package/dist/util/rxjs.d.ts +3 -0
  93. package/dist/util/rxjs.js +22 -5
  94. package/dist/util/rxjs.js.map +1 -1
  95. package/dist/util/stream-rxjs.js +13 -0
  96. package/dist/util/stream-rxjs.js.map +1 -1
  97. package/dist/util/temporal.d.ts +2 -0
  98. package/dist/util/temporal.js +31 -0
  99. package/dist/util/temporal.js.map +1 -0
  100. package/package.json +29 -22
  101. package/src/any-type.ts +18 -0
  102. package/src/class-type.ts +20 -2
  103. package/src/date-time-types.ts +35 -8
  104. package/src/event-parser.ts +204 -0
  105. package/src/fetch-event-source.ts +163 -159
  106. package/src/fetch-request-factory.ts +142 -100
  107. package/src/fetch.ts +79 -14
  108. package/src/header-parameters.ts +66 -0
  109. package/src/index.ts +26 -12
  110. package/src/logger.ts +14 -0
  111. package/src/media-type-codecs/binary-decoder.ts +38 -0
  112. package/src/media-type-codecs/binary-encoder.ts +33 -0
  113. package/src/media-type-codecs/cbor-decoder.ts +529 -0
  114. package/src/media-type-codecs/cbor-encoder.ts +321 -0
  115. package/src/media-type-codecs/cbor-tags.ts +18 -0
  116. package/src/media-type-codecs/json-decoder.ts +484 -0
  117. package/src/media-type-codecs/json-encoder.ts +342 -0
  118. package/src/media-type-codecs/media-type-decoder.ts +34 -0
  119. package/src/media-type-codecs/media-type-decoders.ts +81 -0
  120. package/src/media-type-codecs/media-type-encoder.ts +45 -0
  121. package/src/media-type-codecs/media-type-encoders.ts +83 -0
  122. package/src/media-type-codecs/www-form-url-encoder.ts +193 -0
  123. package/src/media-type.ts +340 -22
  124. package/src/problem.ts +158 -12
  125. package/src/request-factory.ts +35 -12
  126. package/src/sunday-error.ts +51 -0
  127. package/src/url-template.ts +14 -0
  128. package/src/util/any.ts +24 -0
  129. package/src/util/base64.ts +14 -0
  130. package/src/util/error.ts +28 -0
  131. package/src/util/hex.ts +17 -2
  132. package/src/util/rxjs.ts +30 -5
  133. package/src/util/stream-rxjs.ts +14 -0
  134. package/src/util/temporal.ts +32 -0
  135. package/dist/binary-decoder.js +0 -16
  136. package/dist/binary-decoder.js.map +0 -1
  137. package/dist/binary-encoder.js +0 -13
  138. package/dist/binary-encoder.js.map +0 -1
  139. package/dist/cbor-decoder.d.ts +0 -15
  140. package/dist/cbor-decoder.js +0 -126
  141. package/dist/cbor-decoder.js.map +0 -1
  142. package/dist/cbor-encoder.d.ts +0 -29
  143. package/dist/cbor-encoder.js +0 -81
  144. package/dist/cbor-encoder.js.map +0 -1
  145. package/dist/cbor-tags.d.ts +0 -3
  146. package/dist/cbor-tags.js +0 -4
  147. package/dist/cbor-tags.js.map +0 -1
  148. package/dist/http-error.d.ts +0 -10
  149. package/dist/http-error.js +0 -45
  150. package/dist/http-error.js.map +0 -1
  151. package/dist/json-decoder.d.ts +0 -31
  152. package/dist/json-decoder.js +0 -139
  153. package/dist/json-decoder.js.map +0 -1
  154. package/dist/json-encoder.d.ts +0 -35
  155. package/dist/json-encoder.js +0 -134
  156. package/dist/json-encoder.js.map +0 -1
  157. package/dist/media-type-decoder.d.ts +0 -4
  158. package/dist/media-type-decoder.js +0 -2
  159. package/dist/media-type-decoder.js.map +0 -1
  160. package/dist/media-type-decoders.js +0 -40
  161. package/dist/media-type-decoders.js.map +0 -1
  162. package/dist/media-type-encoder.js +0 -6
  163. package/dist/media-type-encoder.js.map +0 -1
  164. package/dist/media-type-encoders.js +0 -42
  165. package/dist/media-type-encoders.js.map +0 -1
  166. package/dist/url-encoder.js.map +0 -1
  167. package/src/binary-decoder.ts +0 -24
  168. package/src/binary-encoder.ts +0 -19
  169. package/src/cbor-decoder.ts +0 -148
  170. package/src/cbor-encoder.ts +0 -95
  171. package/src/cbor-tags.ts +0 -3
  172. package/src/http-error.ts +0 -55
  173. package/src/json-decoder.ts +0 -164
  174. package/src/json-encoder.ts +0 -162
  175. package/src/media-type-decoder.ts +0 -5
  176. package/src/media-type-decoders.ts +0 -59
  177. package/src/media-type-encoder.ts +0 -16
  178. package/src/media-type-encoders.ts +0 -61
  179. package/src/url-encoder.ts +0 -173
package/src/problem.ts CHANGED
@@ -1,25 +1,171 @@
1
- export interface Problem {
2
- type: string;
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 { ResponseExample } from './fetch';
16
+ import {
17
+ JsonAnyGetter,
18
+ JsonAnySetter,
19
+ JsonClassType,
20
+ JsonCreator,
21
+ JsonCreatorMode,
22
+ JsonIgnore,
23
+ JsonProperty,
24
+ } from '@outfoxx/jackson-js';
25
+
26
+ export interface ProblemSpec {
27
+ type: URL | string;
28
+ title: string;
3
29
  status: number;
30
+ detail?: string;
31
+ instance?: URL | string;
32
+ [key: string]: unknown;
33
+ }
34
+
35
+ export interface Problem {
36
+ type: URL;
4
37
  title: string;
5
- detail: string;
6
- instance: string;
38
+ status: number;
39
+ detail?: string;
40
+ instance?: URL;
7
41
  [key: string]: unknown;
8
42
  }
9
43
 
44
+ @JsonCreator({ mode: JsonCreatorMode.DELEGATING })
10
45
  export class Problem extends Error implements Problem {
11
- public type: string;
12
- public status: number;
46
+ @JsonProperty()
47
+ @JsonClassType({ type: () => [URL] })
48
+ public type: URL;
49
+
50
+ @JsonProperty()
51
+ @JsonClassType({ type: () => [String] })
13
52
  public title: string;
14
- public detail: string;
15
- public instance: string;
16
53
 
17
- constructor(json: Record<string, unknown>) {
18
- super(`${json.status} ${json.type} - ${json.title}`);
19
- this.type = json.type as string;
54
+ @JsonProperty()
55
+ @JsonClassType({ type: () => [Number] })
56
+ public status: number;
57
+
58
+ @JsonProperty()
59
+ @JsonClassType({ type: () => [String] })
60
+ public detail?: string;
61
+
62
+ @JsonProperty()
63
+ @JsonClassType({ type: () => [URL] })
64
+ public instance?: URL;
65
+
66
+ @JsonIgnore()
67
+ private _parameters?: Record<string, unknown>;
68
+
69
+ @JsonClassType({ type: () => [Object] })
70
+ @JsonAnyGetter()
71
+ public get parameters(): Record<string, unknown> | undefined {
72
+ return this._parameters;
73
+ }
74
+
75
+ @JsonAnySetter()
76
+ private setParameter(key: string, value: unknown) {
77
+ this._parameters = this._parameters ?? {};
78
+ this._parameters[key] = value;
79
+ }
80
+
81
+ constructor(spec: ProblemSpec) {
82
+ super(`${spec.status.toString()} ${spec.type} - ${spec.title}`);
83
+
84
+ const src = spec as unknown as Record<string, unknown>;
85
+
86
+ const json = Object.assign({}, src);
87
+
88
+ this.type = Problem.parseURL(json.type) ?? new URL('about:blank');
89
+ delete json.type;
90
+
20
91
  this.status = json.status as number;
92
+ delete json.status;
93
+
21
94
  this.title = json.title as string;
95
+ delete json.title;
96
+
22
97
  this.detail = json.detail as string;
23
- this.instance = json.instance as string;
98
+ delete json.detail;
99
+
100
+ this.instance = Problem.parseURL(json.instance);
101
+ delete json.instance;
102
+
103
+ if (Object.keys(json).length != 0) {
104
+ this._parameters = json;
105
+ }
106
+ }
107
+
108
+ toString(): string {
109
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
110
+ const self = this as Record<string, any>;
111
+ const url = self.request ? self.request?.url : undefined;
112
+ const response = self.response?.example;
113
+ return JSON.stringify({
114
+ type: this.type,
115
+ status: this.status,
116
+ title: this.title,
117
+ detail: this.detail,
118
+ instance: this.instance,
119
+ url,
120
+ response,
121
+ });
122
+ }
123
+
124
+ static fromStatus(status: number, title: string): Problem {
125
+ return new Problem({
126
+ type: 'about:blank',
127
+ title,
128
+ status,
129
+ });
130
+ }
131
+
132
+ static async fromResponse(response: Response): Promise<Problem> {
133
+ const [bodyExcerpt, body] = await ResponseExample.bodyExcerpt(
134
+ response,
135
+ 256
136
+ );
137
+
138
+ return new Problem({
139
+ type: 'about:blank',
140
+ title: response.statusText,
141
+ status: response.status,
142
+ request: {
143
+ url: response.url,
144
+ },
145
+ response: {
146
+ headers: response.headers,
147
+ ok: response.ok,
148
+ redirected: response.redirected,
149
+ status: response.status,
150
+ statusText: response.statusText,
151
+ type: response.type,
152
+ url: response.url,
153
+ body,
154
+ example: ResponseExample.build(response, bodyExcerpt),
155
+ },
156
+ });
157
+ }
158
+
159
+ private static parseURL(value: unknown | undefined | null): URL | undefined {
160
+ if (value == null) {
161
+ return undefined;
162
+ }
163
+ if (value instanceof URL) {
164
+ return value;
165
+ }
166
+ if (typeof value == 'string') {
167
+ return new URL(value);
168
+ }
169
+ return new URL(`${value}`);
24
170
  }
25
171
  }
@@ -1,13 +1,34 @@
1
- import { ClassList } from '@outfoxx/jackson-js/dist/@types';
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
+
2
15
  import { Observable } from 'rxjs';
3
16
  import { AnyType } from './any-type';
4
- import { ClassType } from './class-type';
17
+ import { ClassType, ConstructableClassType } from './class-type';
5
18
  import { MediaType } from './media-type';
19
+ import { TextMediaTypeDecoder } from './media-type-codecs/media-type-decoder';
20
+ import { Problem } from './problem';
6
21
  import { URLTemplate } from './url-template';
22
+ import { Logger } from './logger';
7
23
 
8
24
  export interface RequestFactory {
9
25
  readonly baseUrl: URLTemplate;
10
26
 
27
+ registerProblem(
28
+ type: URL | string,
29
+ problemType: ConstructableClassType<Problem>
30
+ ): void;
31
+
11
32
  request(requestSpec: RequestSpec<unknown>): Observable<Request>;
12
33
 
13
34
  response(request: Request, dataExpected?: boolean): Observable<Response>;
@@ -36,11 +57,17 @@ export interface RequestFactory {
36
57
 
37
58
  result<B>(requestSpec: RequestSpec<B>): Observable<void>;
38
59
 
39
- events(requestSpec: RequestSpec<void>): ExtEventSource;
60
+ eventSource(requestSpec: RequestSpec<void>): ExtEventSource;
40
61
 
41
- events<E>(
62
+ eventStream<E>(
42
63
  requestSpec: RequestSpec<void>,
43
- eventTypes: EventTypes<E>
64
+ decoder: (
65
+ decoder: TextMediaTypeDecoder,
66
+ event: string | undefined,
67
+ id: string | undefined,
68
+ data: string,
69
+ logger?: Logger
70
+ ) => E | undefined
44
71
  ): Observable<E>;
45
72
  }
46
73
 
@@ -64,13 +91,9 @@ export interface RequestSpec<B> {
64
91
  queryParameters?: Record<string, unknown>;
65
92
  body?: B;
66
93
  bodyType?: AnyType;
67
- contentTypes?: (MediaType | string)[];
68
- acceptTypes?: (MediaType | string)[];
69
- headers?: HeadersInit;
70
- }
71
-
72
- export interface EventTypes<E> {
73
- [key: string]: ClassList<ClassType<E>>;
94
+ contentTypes?: MediaType[];
95
+ acceptTypes?: MediaType[];
96
+ headers?: Record<string, unknown>;
74
97
  }
75
98
 
76
99
  export type RequestAdapter = (request: Request) => Observable<Request>;
@@ -0,0 +1,51 @@
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 { ResponseExample } from './fetch';
16
+
17
+ export class SundayError extends Error {
18
+ constructor(
19
+ message: string,
20
+ public url: string,
21
+ public httpVersion: string,
22
+ public status: number,
23
+ public statusText: string,
24
+ public headers: Headers,
25
+ public body: unknown | undefined,
26
+ public responseExample: string
27
+ ) {
28
+ super(message);
29
+ }
30
+
31
+ static async fromResponse(
32
+ message: string,
33
+ response: Response
34
+ ): Promise<SundayError> {
35
+ const [bodyExcerpt, body] = await ResponseExample.bodyExcerpt(
36
+ response,
37
+ 256
38
+ );
39
+
40
+ return new SundayError(
41
+ message,
42
+ response.url,
43
+ '?.?',
44
+ response.status,
45
+ response.statusText,
46
+ response.headers,
47
+ body,
48
+ ResponseExample.build(response, bodyExcerpt)
49
+ );
50
+ }
51
+ }
@@ -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 { URI } from 'uri-template-lite';
2
16
 
3
17
  export class URLTemplate {
@@ -0,0 +1,24 @@
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
+ export function unknownGet<T = unknown>(obj: unknown, key: string): T {
16
+ return (obj as Record<string, unknown>)?.[key] as T;
17
+ }
18
+
19
+ export function unknownSet<T>(obj: unknown, key: string, value: T): void {
20
+ if (obj == null) {
21
+ return;
22
+ }
23
+ (obj as Record<string, unknown>)[key] = value;
24
+ }
@@ -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
  const chars =
2
16
  'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
3
17
 
@@ -0,0 +1,28 @@
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
+ export function isError(value: unknown): value is Error {
16
+ return value instanceof Error;
17
+ }
18
+
19
+ export function errorToMessage(value: unknown, defMsg?: string): string {
20
+ return isError(value) ? value.message : defMsg ?? `${fmtMsg(value)}`;
21
+ }
22
+
23
+ function fmtMsg(value: unknown): string {
24
+ if (value instanceof Object) {
25
+ return JSON.stringify(value);
26
+ }
27
+ return `${value}`;
28
+ }
package/src/util/hex.ts CHANGED
@@ -1,8 +1,23 @@
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 Hex {
2
16
  export function decode(hex: string): ArrayBuffer {
17
+ hex = hex.replace(/^0x/, '').replace(/\s/g, '');
3
18
  const values = hex.match(/[\da-f]{2}/gi);
4
- if (!values) {
5
- throw Error('Invalid hex string');
19
+ if (!values || values.length != hex.length / 2) {
20
+ throw Error(`Invalid hex string`);
6
21
  }
7
22
  return new Uint8Array(values.map((b) => parseInt(b, 16))).buffer;
8
23
  }
package/src/util/rxjs.ts CHANGED
@@ -1,17 +1,42 @@
1
- import { from, Observable, throwError } from 'rxjs';
2
- import { catchError } from 'rxjs/operators';
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 { catchError, from, Observable, throwError } from 'rxjs';
16
+ import { ClassType } from '../class-type';
3
17
  import { Problem } from '../problem';
4
18
 
5
19
  export function nullifyNotFound<T>(): (
6
20
  source: Observable<T>
7
21
  ) => Observable<T | null> {
22
+ return nullifyResponse([404], []);
23
+ }
24
+
25
+ export function nullifyResponse<T>(
26
+ statuses: number[],
27
+ problemTypes: ClassType<Problem>[]
28
+ ): (source: Observable<T>) => Observable<T | null> {
8
29
  return function <T>(source: Observable<T>): Observable<T | null> {
9
30
  return source.pipe(
10
31
  catchError((error) => {
11
- if (!(error instanceof Problem) || error.status !== 404) {
12
- return throwError(error);
32
+ const errorType = error.constructor as ClassType<Problem>;
33
+ if (
34
+ error instanceof Problem &&
35
+ (statuses.includes(error.status) || problemTypes.includes(errorType))
36
+ ) {
37
+ return from([null]);
13
38
  }
14
- return from([null]);
39
+ return throwError(error);
15
40
  })
16
41
  );
17
42
  };
@@ -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 { Observable } from 'rxjs';
2
16
 
3
17
  export function fromStream(
@@ -0,0 +1,32 @@
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
+ export function secondsToNumber(seconds: number, nanos: number): number {
16
+ if (nanos == 0) {
17
+ return seconds;
18
+ }
19
+ const nanoStr = nanos.toString();
20
+ return parseFloat(`${seconds}.${'0'.repeat(9 - nanoStr.length)}${nanos}`);
21
+ }
22
+
23
+ export function encodeSeconds(seconds: number, fraction: number): unknown[] {
24
+ const result = [];
25
+ if (seconds != 0 || fraction != 0) {
26
+ result.push(seconds);
27
+ if (fraction != 0) {
28
+ result.push(fraction);
29
+ }
30
+ }
31
+ return result;
32
+ }
@@ -1,16 +0,0 @@
1
- export class BinaryDecoder {
2
- async decode(response, type) {
3
- const arrayBuffer = await response.arrayBuffer();
4
- if (type[0] === ArrayBuffer) {
5
- return arrayBuffer;
6
- }
7
- else if (type[0] === Uint8Array ||
8
- type[0] === Int8Array ||
9
- type[0] === DataView) {
10
- return new type[0](arrayBuffer);
11
- }
12
- throw Error('Invalid value, expected ArrayBuffer, (Int|Uint)Array or DataView');
13
- }
14
- }
15
- BinaryDecoder.default = new BinaryDecoder();
16
- //# sourceMappingURL=binary-decoder.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"binary-decoder.js","sourceRoot":"","sources":["../src/binary-decoder.ts"],"names":[],"mappings":"AAGA,MAAM,OAAO,aAAa;IAGxB,KAAK,CAAC,MAAM,CAAI,QAAkB,EAAE,IAAa;QAC/C,MAAM,WAAW,GAAG,MAAM,QAAQ,CAAC,WAAW,EAAE,CAAC;QAEjD,IAAI,IAAI,CAAC,CAAC,CAAC,KAAK,WAAW,EAAE;YAC3B,OAAQ,WAA4B,CAAC;SACtC;aAAM,IACL,IAAI,CAAC,CAAC,CAAC,KAAK,UAAU;YACtB,IAAI,CAAC,CAAC,CAAC,KAAK,SAAS;YACrB,IAAI,CAAC,CAAC,CAAC,KAAK,QAAQ,EACpB;YACA,OAAO,IAAI,IAAI,CAAC,CAAC,CAAC,CAAC,WAAW,CAAM,CAAC;SACtC;QAED,MAAM,KAAK,CACT,kEAAkE,CACnE,CAAC;IACJ,CAAC;;AAlBM,qBAAO,GAAG,IAAI,aAAa,EAAE,CAAC"}
@@ -1,13 +0,0 @@
1
- export class BinaryEncoder {
2
- encode(value) {
3
- if (!ArrayBuffer.isView(value) &&
4
- !(value instanceof ArrayBuffer) &&
5
- !(value instanceof Blob) &&
6
- !(value instanceof ReadableStream)) {
7
- throw Error('Invalid value, expected BufferSource, Blob or ReadableStream');
8
- }
9
- return value;
10
- }
11
- }
12
- BinaryEncoder.default = new BinaryEncoder();
13
- //# sourceMappingURL=binary-encoder.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"binary-encoder.js","sourceRoot":"","sources":["../src/binary-encoder.ts"],"names":[],"mappings":"AAEA,MAAM,OAAO,aAAa;IAGxB,MAAM,CAAC,KAAc;QACnB,IACE,CAAC,WAAW,CAAC,MAAM,CAAC,KAAK,CAAC;YAC1B,CAAC,CAAC,KAAK,YAAY,WAAW,CAAC;YAC/B,CAAC,CAAC,KAAK,YAAY,IAAI,CAAC;YACxB,CAAC,CAAC,KAAK,YAAY,cAAc,CAAC,EAClC;YACA,MAAM,KAAK,CACT,8DAA8D,CAC/D,CAAC;SACH;QACD,OAAO,KAAK,CAAC;IACf,CAAC;;AAdM,qBAAO,GAAG,IAAI,aAAa,EAAE,CAAC"}
@@ -1,15 +0,0 @@
1
- import { AnyType } from './any-type';
2
- import { MediaTypeDecoder } from './media-type-decoder';
3
- export declare class CBORDecoder implements MediaTypeDecoder {
4
- static get default(): CBORDecoder;
5
- private readonly customDeserializers;
6
- private readonly parser;
7
- constructor();
8
- decode<T>(response: Response, type: AnyType): Promise<T>;
9
- decodeData<T>(buffer: ArrayBuffer, type: AnyType): T;
10
- private static untag;
11
- private dateTimeDeserializer;
12
- private dateDeserializer;
13
- private urlDeserializer;
14
- private arrayBufferDeserializer;
15
- }