@jpmorganchase/mosaic-from-http-request 0.0.0-snapshot-20240604074928
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/dist/fromFetch.d.ts +2 -0
- package/dist/fromFetch.js +12 -0
- package/dist/fromFetch.js.map +1 -0
- package/dist/fromHttpRequest.d.ts +14 -0
- package/dist/fromHttpRequest.js +35 -0
- package/dist/fromHttpRequest.js.map +1 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +2 -0
- package/dist/index.js.map +1 -0
- package/package.json +45 -0
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/* eslint-disable eslint-comments/no-unlimited-disable */
|
|
2
|
+
import * as nodeFetch from 'node-fetch';
|
|
3
|
+
import { fromFetch } from 'rxjs/fetch';
|
|
4
|
+
// eslint-disable-next-line
|
|
5
|
+
// @ts-ignore
|
|
6
|
+
globalThis.fetch = nodeFetch.default;
|
|
7
|
+
// eslint-disable-next-line
|
|
8
|
+
// @ts-ignore
|
|
9
|
+
globalThis.Request = nodeFetch.Request;
|
|
10
|
+
export { fromFetch };
|
|
11
|
+
/* eslint-enable eslint-comments/no-unlimited-disable */
|
|
12
|
+
//# sourceMappingURL=fromFetch.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"fromFetch.js","sourceRoot":"","sources":["../src/fromFetch.ts"],"names":[],"mappings":"AAAA,yDAAyD;AACzD,OAAO,KAAK,SAAS,MAAM,YAAY,CAAC;AACxC,OAAO,EAAE,SAAS,EAAE,MAAM,YAAY,CAAC;AAEvC,2BAA2B;AAC3B,aAAa;AACb,UAAU,CAAC,KAAK,GAAG,SAAS,CAAC,OAAO,CAAC;AACrC,2BAA2B;AAC3B,aAAa;AACb,UAAU,CAAC,OAAO,GAAG,SAAS,CAAC,OAAO,CAAC;AAEvC,OAAO,EAAE,SAAS,EAAE,CAAC;AACrB,wDAAwD"}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import 'event-target-polyfill';
|
|
2
|
+
import 'yet-another-abortcontroller-polyfill';
|
|
3
|
+
import { Observable } from 'rxjs';
|
|
4
|
+
export type ErrorResponse = {
|
|
5
|
+
error: boolean;
|
|
6
|
+
message: string;
|
|
7
|
+
};
|
|
8
|
+
export declare function isErrorResponse<TData>(response: TData | ErrorResponse): response is ErrorResponse;
|
|
9
|
+
/**
|
|
10
|
+
*
|
|
11
|
+
* @param input The resource you would like to fetch. Can be a url or a request object.
|
|
12
|
+
* @returns Observable of JSON or ErrorResponse
|
|
13
|
+
*/
|
|
14
|
+
export declare function fromHttpRequest<TData>(input: string | Request): Observable<TData | ErrorResponse>;
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import 'event-target-polyfill';
|
|
2
|
+
import 'yet-another-abortcontroller-polyfill';
|
|
3
|
+
import { of } from 'rxjs';
|
|
4
|
+
import { catchError, switchMap } from 'rxjs/operators';
|
|
5
|
+
import contentTypeParser from 'fast-content-type-parse';
|
|
6
|
+
import { fromFetch } from './fromFetch.js';
|
|
7
|
+
export function isErrorResponse(response) {
|
|
8
|
+
return response.error !== undefined;
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
*
|
|
12
|
+
* @param input The resource you would like to fetch. Can be a url or a request object.
|
|
13
|
+
* @returns Observable of JSON or ErrorResponse
|
|
14
|
+
*/
|
|
15
|
+
export function fromHttpRequest(input) {
|
|
16
|
+
return fromFetch(input).pipe(switchMap(response => {
|
|
17
|
+
if (response.ok) {
|
|
18
|
+
const contentTypeHeader = response.headers.get('content-type') || '';
|
|
19
|
+
const contentType = contentTypeParser.safeParse(contentTypeHeader).type;
|
|
20
|
+
if (contentType.includes('json')) {
|
|
21
|
+
return response.json();
|
|
22
|
+
}
|
|
23
|
+
if (contentType.includes('text')) {
|
|
24
|
+
return response.text();
|
|
25
|
+
}
|
|
26
|
+
// default to json
|
|
27
|
+
return response.json();
|
|
28
|
+
}
|
|
29
|
+
return of({ error: true, message: `Error ${response.status}` });
|
|
30
|
+
}), catchError(err => {
|
|
31
|
+
console.error(err);
|
|
32
|
+
return of({ error: true, message: err.message });
|
|
33
|
+
}));
|
|
34
|
+
}
|
|
35
|
+
//# sourceMappingURL=fromHttpRequest.js.map
|
|
@@ -0,0 +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;AAExD,OAAO,EAAE,SAAS,EAAE,MAAM,gBAAgB,CAAC;AAI3C,MAAM,UAAU,eAAe,CAAQ,QAA+B;IACpE,OAAQ,QAA0B,CAAC,KAAK,KAAK,SAAS,CAAC;AACzD,CAAC;AAED;;;;GAIG;AACH,MAAM,UAAU,eAAe,CAAQ,KAAuB;IAC5D,OAAO,SAAS,CAAC,KAAK,CAAC,CAAC,IAAI,CAC1B,SAAS,CAAC,QAAQ,CAAC,EAAE;QACnB,IAAI,QAAQ,CAAC,EAAE,EAAE;YACf,MAAM,iBAAiB,GAAG,QAAQ,CAAC,OAAO,CAAC,GAAG,CAAC,cAAc,CAAC,IAAI,EAAE,CAAC;YACrE,MAAM,WAAW,GAAG,iBAAiB,CAAC,SAAS,CAAC,iBAAiB,CAAC,CAAC,IAAI,CAAC;YAExE,IAAI,WAAW,CAAC,QAAQ,CAAC,MAAM,CAAC,EAAE;gBAChC,OAAO,QAAQ,CAAC,IAAI,EAAoB,CAAC;aAC1C;YAED,IAAI,WAAW,CAAC,QAAQ,CAAC,MAAM,CAAC,EAAE;gBAChC,OAAO,QAAQ,CAAC,IAAI,EAAoB,CAAC;aAC1C;YACD,kBAAkB;YAClB,OAAO,QAAQ,CAAC,IAAI,EAAoB,CAAC;SAC1C;QACD,OAAO,EAAE,CAAgB,EAAE,KAAK,EAAE,IAAI,EAAE,OAAO,EAAE,SAAS,QAAQ,CAAC,MAAM,EAAE,EAAE,CAAC,CAAC;IACjF,CAAC,CAAC,EACF,UAAU,CAAC,GAAG,CAAC,EAAE;QACf,OAAO,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC;QACnB,OAAO,EAAE,CAAgB,EAAE,KAAK,EAAE,IAAI,EAAE,OAAO,EAAE,GAAG,CAAC,OAAiB,EAAE,CAAC,CAAC;IAC5E,CAAC,CAAC,CACH,CAAC;AACJ,CAAC"}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './fromHttpRequest.js';
|
package/dist/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,sBAAsB,CAAC"}
|
package/package.json
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@jpmorganchase/mosaic-from-http-request",
|
|
3
|
+
"version": "0.0.0-snapshot-20240604074928",
|
|
4
|
+
"author": "",
|
|
5
|
+
"description": "Utility package to turn HTTP fetch requests to observables. This package can be deleted when node 18 is used.",
|
|
6
|
+
"license": "Apache-2.0",
|
|
7
|
+
"main": "./dist/index.js",
|
|
8
|
+
"type": "module",
|
|
9
|
+
"types": "./dist/index.d.ts",
|
|
10
|
+
"exports": {
|
|
11
|
+
".": {
|
|
12
|
+
"types": "./dist/index.d.ts",
|
|
13
|
+
"import": "./dist/index.js",
|
|
14
|
+
"node": "./dist/index.js",
|
|
15
|
+
"default": "./dist/index.js"
|
|
16
|
+
}
|
|
17
|
+
},
|
|
18
|
+
"engines": {
|
|
19
|
+
"node": ">=16.10.0 || >=18.0.0"
|
|
20
|
+
},
|
|
21
|
+
"files": [
|
|
22
|
+
"dist"
|
|
23
|
+
],
|
|
24
|
+
"scripts": {
|
|
25
|
+
"build": "tsc",
|
|
26
|
+
"clean": "rm -fr dist",
|
|
27
|
+
"lint": "eslint --ignore-pattern \"**/__tests__/**\""
|
|
28
|
+
},
|
|
29
|
+
"repository": {
|
|
30
|
+
"type": "git",
|
|
31
|
+
"url": "git@github.com:jpmorganchase/mosaic.git",
|
|
32
|
+
"directory": "packages/fromHttpRequest"
|
|
33
|
+
},
|
|
34
|
+
"dependencies": {
|
|
35
|
+
"event-target-polyfill": "^0.0.3",
|
|
36
|
+
"fast-content-type-parse": "^1.1.0",
|
|
37
|
+
"rxjs": "^7.5.5",
|
|
38
|
+
"node-fetch": "^2.6.2",
|
|
39
|
+
"yet-another-abortcontroller-polyfill": "0.0.4"
|
|
40
|
+
},
|
|
41
|
+
"devDependencies": {
|
|
42
|
+
"@types/node-fetch": "^2.6.2",
|
|
43
|
+
"msw": "^0.47.4"
|
|
44
|
+
}
|
|
45
|
+
}
|