@jpmorganchase/mosaic-from-http-request 0.1.0-beta.94 → 0.1.0-beta.96

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.
@@ -2,14 +2,31 @@ import 'event-target-polyfill';
2
2
  import 'yet-another-abortcontroller-polyfill';
3
3
  import { Observable } from 'rxjs';
4
4
  import { Request } from 'undici';
5
- export type ErrorResponse = {
6
- error: boolean;
5
+ /**
6
+ * Error raised when there an error is thrown
7
+ */
8
+ export interface FromHttpRequestThrownError {
9
+ error: true;
10
+ kind: 'thrown';
11
+ message: string;
12
+ }
13
+ /**
14
+ * Error raised when there is a network/http error
15
+ */
16
+ export interface FromHttpRequestHttpError {
17
+ error: true;
18
+ kind: 'http';
7
19
  message: string;
8
- };
9
- export declare function isErrorResponse<TData>(response: TData | ErrorResponse): response is ErrorResponse;
20
+ status: number;
21
+ statusText: string;
22
+ headers: Record<string, string>;
23
+ }
24
+ export type FromHttpRequestError = FromHttpRequestThrownError | FromHttpRequestHttpError;
25
+ export declare function isFromHttpRequestError(response: any): response is FromHttpRequestError;
26
+ export declare function isFromHttpRequestThrownError(response: any): response is FromHttpRequestThrownError;
10
27
  /**
11
28
  *
12
29
  * @param input The resource you would like to fetch. Can be a url or a request object.
13
30
  * @returns Observable of JSON or ErrorResponse
14
31
  */
15
- export declare function fromHttpRequest<TData>(input: string | Request): Observable<TData | ErrorResponse>;
32
+ export declare function fromHttpRequest<TData>(input: string | Request): Observable<TData | FromHttpRequestError>;
@@ -1,11 +1,15 @@
1
1
  import 'event-target-polyfill';
2
2
  import 'yet-another-abortcontroller-polyfill';
3
- import { of } from 'rxjs';
3
+ import { from, of } from 'rxjs';
4
4
  import { catchError, switchMap } from 'rxjs/operators';
5
5
  import contentTypeParser from 'fast-content-type-parse';
6
6
  import { fromFetch } from './fromFetch.js';
7
- export function isErrorResponse(response) {
8
- return response.error !== undefined;
7
+ export function isFromHttpRequestError(response) {
8
+ return (!!response.error && response.kind === 'http');
9
+ }
10
+ export function isFromHttpRequestThrownError(response) {
11
+ return (!!response.error &&
12
+ response.kind === 'thrown');
9
13
  }
10
14
  /**
11
15
  *
@@ -17,19 +21,31 @@ export function fromHttpRequest(input) {
17
21
  if (response.ok) {
18
22
  const contentTypeHeader = response.headers.get('content-type') || '';
19
23
  const contentType = contentTypeParser.safeParse(contentTypeHeader).type;
20
- if (contentType.includes('json')) {
21
- return response.json();
22
- }
23
24
  if (contentType.includes('text')) {
24
- return response.text();
25
+ return from(response.text());
25
26
  }
26
27
  // default to json
27
- return response.json();
28
+ return from(response.json());
28
29
  }
29
- return of({ error: true, message: `Error ${response.status}` });
30
- }), catchError(err => {
31
- console.error(err);
32
- return of({ error: true, message: err.message });
30
+ const headersObj = {};
31
+ response.headers.forEach((value, key) => {
32
+ headersObj[key] = value;
33
+ });
34
+ return of({
35
+ error: true,
36
+ kind: 'http',
37
+ message: `A non-OK HTTP response was received from the server: ${response.status}/${response.statusText}`,
38
+ status: response.status,
39
+ statusText: response.statusText,
40
+ headers: headersObj
41
+ });
42
+ }), catchError(error => {
43
+ // Network or thrown error
44
+ return of({
45
+ error: true,
46
+ kind: 'thrown',
47
+ message: error.message
48
+ });
33
49
  }));
34
50
  }
35
51
  //# sourceMappingURL=fromHttpRequest.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"fromHttpRequest.js","sourceRoot":"","sources":["../src/fromHttpRequest.ts"],"names":[],"mappings":"AAAA,OAAO,uBAAuB,CAAC;AAC/B,OAAO,sCAAsC,CAAC;AAC9C,OAAO,EAAc,EAAE,EAAE,MAAM,MAAM,CAAC;AACtC,OAAO,EAAE,UAAU,EAAE,SAAS,EAAE,MAAM,gBAAgB,CAAC;AACvD,OAAO,iBAAiB,MAAM,yBAAyB,CAAC;AAGxD,OAAO,EAAE,SAAS,EAAE,MAAM,gBAAgB,CAAC;AAI3C,MAAM,UAAU,eAAe,CAAQ,QAA+B;IACpE,OAAQ,QAA0B,CAAC,KAAK,KAAK,SA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,CAAC,CAAC,CAAC;QACH,OAAO,EAAE,CAA2B;YAClC,KAAK,EAAE,IAAI;YACX,IAAI,EAAE,MAAM;YACZ,OAAO,EAAE,wDAAwD,QAAQ,CAAC,MAAM,IAAI,QAAQ,CAAC,UAAU,EAAE;YACzG,MAAM,EAAE,QAAQ,CAAC,MAAM;YACvB,UAAU,EAAE,QAAQ,CAAC,UAAU;YAC/B,OAAO,EAAE,UAAU;SACpB,CAAC,CAAC;IACL,CAAC,CAAC,EACF,UAAU,CAAC,KAAK,CAAC,EAAE;QACjB,0BAA0B;QAC1B,OAAO,EAAE,CAA6B;YACpC,KAAK,EAAE,IAAI;YACX,IAAI,EAAE,QAAQ;YACd,OAAO,EAAE,KAAK,CAAC,OAAO;SACvB,CAAC,CAAC;IACL,CAAC,CAAC,CACH,CAAC;AACJ,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jpmorganchase/mosaic-from-http-request",
3
- "version": "0.1.0-beta.94",
3
+ "version": "0.1.0-beta.96",
4
4
  "author": "",
5
5
  "description": "Utility package to turn HTTP fetch requests to observables. This package can be deleted when node 18 is used.",
6
6
  "license": "Apache-2.0",
@@ -41,4 +41,4 @@
41
41
  "devDependencies": {
42
42
  "msw": "^2.0.0"
43
43
  }
44
- }
44
+ }