@nzyme/fetch-utils 0.8.0 → 0.9.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/dist/cjs/assertResponse.cjs +3 -0
- package/dist/cjs/assertResponse.cjs.map +1 -1
- package/dist/cjs/assertResponse.d.ts +3 -0
- package/dist/cjs/defineEndpoint.cjs +5 -1
- package/dist/cjs/defineEndpoint.cjs.map +1 -1
- package/dist/cjs/defineEndpoint.d.ts +17 -0
- package/dist/cjs/defineFetch.cjs +12 -0
- package/dist/cjs/defineFetch.cjs.map +1 -0
- package/dist/cjs/defineFetch.d.ts +20 -0
- package/dist/cjs/fetchEndpoint.cjs +10 -3
- package/dist/cjs/fetchEndpoint.cjs.map +1 -1
- package/dist/cjs/fetchEndpoint.d.ts +15 -2
- package/dist/cjs/index.cjs +1 -0
- package/dist/cjs/index.cjs.map +1 -1
- package/dist/cjs/index.d.ts +1 -0
- package/dist/cjs/jsonNullableResponse.cjs +18 -0
- package/dist/cjs/jsonNullableResponse.cjs.map +1 -0
- package/dist/cjs/jsonNullableResponse.d.ts +5 -0
- package/dist/cjs/jsonResponse.cjs +6 -1
- package/dist/cjs/jsonResponse.cjs.map +1 -1
- package/dist/cjs/jsonResponse.d.ts +3 -0
- package/dist/cjs/xmlResponse.cjs +5 -0
- package/dist/cjs/xmlResponse.cjs.map +1 -1
- package/dist/cjs/xmlResponse.d.ts +3 -0
- package/dist/esm/assertResponse.d.ts +3 -0
- package/dist/esm/assertResponse.js +3 -0
- package/dist/esm/assertResponse.js.map +1 -1
- package/dist/esm/defineEndpoint.d.ts +17 -0
- package/dist/esm/defineEndpoint.js +5 -1
- package/dist/esm/defineEndpoint.js.map +1 -1
- package/dist/esm/defineFetch.d.ts +20 -0
- package/dist/esm/defineFetch.js +9 -0
- package/dist/esm/defineFetch.js.map +1 -0
- package/dist/esm/fetchEndpoint.d.ts +15 -2
- package/dist/esm/fetchEndpoint.js +11 -4
- package/dist/esm/fetchEndpoint.js.map +1 -1
- package/dist/esm/index.d.ts +1 -0
- package/dist/esm/index.js +1 -0
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/jsonNullableResponse.d.ts +5 -0
- package/dist/esm/jsonNullableResponse.js +15 -0
- package/dist/esm/jsonNullableResponse.js.map +1 -0
- package/dist/esm/jsonResponse.d.ts +3 -0
- package/dist/esm/jsonResponse.js +6 -1
- package/dist/esm/jsonResponse.js.map +1 -1
- package/dist/esm/xmlResponse.d.ts +3 -0
- package/dist/esm/xmlResponse.js +5 -0
- package/dist/esm/xmlResponse.js.map +1 -1
- package/package.json +7 -7
|
@@ -2,6 +2,9 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.assertResponse = assertResponse;
|
|
4
4
|
const FetchError_js_1 = require("./FetchError.cjs");
|
|
5
|
+
/**
|
|
6
|
+
* Assert that a response is ok.
|
|
7
|
+
*/
|
|
5
8
|
async function assertResponse(response) {
|
|
6
9
|
if (!response.ok) {
|
|
7
10
|
const message = await response.text();
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"assertResponse.cjs","sourceRoot":"","sources":["../../src/assertResponse.ts"],"names":[],"mappings":";;
|
|
1
|
+
{"version":3,"file":"assertResponse.cjs","sourceRoot":"","sources":["../../src/assertResponse.ts"],"names":[],"mappings":";;AAKA,wCAKC;AAVD,mDAA6C;AAE7C;;GAEG;AACI,KAAK,UAAU,cAAc,CAAC,QAAkB;IACnD,IAAI,CAAC,QAAQ,CAAC,EAAE,EAAE,CAAC;QACf,MAAM,OAAO,GAAG,MAAM,QAAQ,CAAC,IAAI,EAAE,CAAC;QACtC,MAAM,IAAI,0BAAU,CAAC,QAAQ,EAAE,OAAO,CAAC,CAAC;IAC5C,CAAC;AACL,CAAC"}
|
|
@@ -1,7 +1,11 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.defineEndpoint = defineEndpoint;
|
|
4
|
-
|
|
4
|
+
/**
|
|
5
|
+
* Define a fetch endpoint.
|
|
6
|
+
*
|
|
7
|
+
* #__NO_SIDE_EFFECTS__
|
|
8
|
+
*/
|
|
5
9
|
function defineEndpoint(endpoint) {
|
|
6
10
|
return endpoint;
|
|
7
11
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"defineEndpoint.cjs","sourceRoot":"","sources":["../../src/defineEndpoint.ts"],"names":[],"mappings":";;
|
|
1
|
+
{"version":3,"file":"defineEndpoint.cjs","sourceRoot":"","sources":["../../src/defineEndpoint.ts"],"names":[],"mappings":";;AA4BA,wCAIC;AATD;;;;GAIG;AACH,SAAgB,cAAc,CAC1B,QAAoC;IAEpC,OAAO,QAAQ,CAAC;AACpB,CAAC"}
|
|
@@ -1,9 +1,26 @@
|
|
|
1
1
|
import type { FetchRequest } from './fetchRequest.js';
|
|
2
|
+
/**
|
|
3
|
+
* A fetch endpoint.
|
|
4
|
+
*/
|
|
2
5
|
export interface Endpoint<TParams, TResult> {
|
|
6
|
+
/**
|
|
7
|
+
* The request for the endpoint.
|
|
8
|
+
*/
|
|
3
9
|
request: (params: TParams) => FetchRequest;
|
|
10
|
+
/**
|
|
11
|
+
* The response for the endpoint.
|
|
12
|
+
*/
|
|
4
13
|
response?: (response: Response, params: TParams) => Promise<TResult>;
|
|
5
14
|
}
|
|
15
|
+
/**
|
|
16
|
+
* A function that takes a response and returns a promise of a result.
|
|
17
|
+
*/
|
|
6
18
|
export interface EndpointResponse<T> {
|
|
7
19
|
(response: Response): Promise<T>;
|
|
8
20
|
}
|
|
21
|
+
/**
|
|
22
|
+
* Define a fetch endpoint.
|
|
23
|
+
*
|
|
24
|
+
* #__NO_SIDE_EFFECTS__
|
|
25
|
+
*/
|
|
9
26
|
export declare function defineEndpoint<TParams = void, TResult = void>(endpoint: Endpoint<TParams, TResult>): Endpoint<TParams, TResult>;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.defineFetch = defineFetch;
|
|
4
|
+
/**
|
|
5
|
+
* Define a fetch function.
|
|
6
|
+
*
|
|
7
|
+
* #__NO_SIDE_EFFECTS__
|
|
8
|
+
*/
|
|
9
|
+
function defineFetch(fetch) {
|
|
10
|
+
return fetch;
|
|
11
|
+
}
|
|
12
|
+
//# sourceMappingURL=defineFetch.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"defineFetch.cjs","sourceRoot":"","sources":["../../src/defineFetch.ts"],"names":[],"mappings":";;AAqBA,kCAEC;AAPD;;;;GAIG;AACH,SAAgB,WAAW,CAAiC,KAAqC;IAC7F,OAAO,KAAK,CAAC;AACjB,CAAC"}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import type { FetchRequest } from './fetchRequest.js';
|
|
2
|
+
/**
|
|
3
|
+
* A fetch function.
|
|
4
|
+
*/
|
|
5
|
+
export interface FetchOptions<TParams, TResult> {
|
|
6
|
+
/**
|
|
7
|
+
* The request for the endpoint.
|
|
8
|
+
*/
|
|
9
|
+
request: (params: TParams) => FetchRequest;
|
|
10
|
+
/**
|
|
11
|
+
* The response for the endpoint.
|
|
12
|
+
*/
|
|
13
|
+
response?: (response: Response, params: TParams) => Promise<TResult>;
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* Define a fetch function.
|
|
17
|
+
*
|
|
18
|
+
* #__NO_SIDE_EFFECTS__
|
|
19
|
+
*/
|
|
20
|
+
export declare function defineFetch<TParams = void, TResult = void>(fetch: FetchOptions<TParams, TResult>): FetchOptions<TParams, TResult>;
|
|
@@ -2,7 +2,10 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.fetchEndpoint = fetchEndpoint;
|
|
4
4
|
const ufo_1 = require("ufo");
|
|
5
|
-
const
|
|
5
|
+
const assertResponse_js_1 = require("./assertResponse.cjs");
|
|
6
|
+
/**
|
|
7
|
+
*
|
|
8
|
+
*/
|
|
6
9
|
async function fetchEndpoint(endpoint, params) {
|
|
7
10
|
let request = endpoint.request(params.params);
|
|
8
11
|
request = {
|
|
@@ -13,11 +16,15 @@ async function fetchEndpoint(endpoint, params) {
|
|
|
13
16
|
...request.headers,
|
|
14
17
|
},
|
|
15
18
|
};
|
|
16
|
-
|
|
17
|
-
|
|
19
|
+
const url = request.query ? (0, ufo_1.withQuery)(request.url, request.query) : request.url;
|
|
20
|
+
// Clone the response to avoid "Other side closed error"
|
|
21
|
+
const response = (await fetch(url, request)).clone();
|
|
18
22
|
if (endpoint.response) {
|
|
19
23
|
return await endpoint.response(response, params.params);
|
|
20
24
|
}
|
|
25
|
+
else {
|
|
26
|
+
await (0, assertResponse_js_1.assertResponse)(response);
|
|
27
|
+
}
|
|
21
28
|
return undefined;
|
|
22
29
|
}
|
|
23
30
|
//# sourceMappingURL=fetchEndpoint.cjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"fetchEndpoint.cjs","sourceRoot":"","sources":["../../src/fetchEndpoint.ts"],"names":[],"mappings":";;
|
|
1
|
+
{"version":3,"file":"fetchEndpoint.cjs","sourceRoot":"","sources":["../../src/fetchEndpoint.ts"],"names":[],"mappings":";;AA0BA,sCA0BC;AApDD,6BAAyC;AAEzC,2DAAqD;AAqBrD;;GAEG;AACI,KAAK,UAAU,aAAa,CAC/B,QAAoC,EACpC,MAAoC;IAEpC,IAAI,OAAO,GAAG,QAAQ,CAAC,OAAO,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC;IAE9C,OAAO,GAAG;QACN,GAAG,OAAO;QACV,GAAG,EAAE,MAAM,CAAC,OAAO,CAAC,CAAC,CAAC,IAAA,aAAO,EAAC,MAAM,CAAC,OAAO,EAAE,OAAO,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,OAAO,CAAC,GAAG;QACxE,OAAO,EAAE;YACL,GAAG,MAAM,CAAC,OAAO;YACjB,GAAG,OAAO,CAAC,OAAO;SACrB;KACJ,CAAC;IAEF,MAAM,GAAG,GAAG,OAAO,CAAC,KAAK,CAAC,CAAC,CAAC,IAAA,eAAS,EAAC,OAAO,CAAC,GAAG,EAAE,OAAO,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,OAAO,CAAC,GAAG,CAAC;IAChF,wDAAwD;IACxD,MAAM,QAAQ,GAAG,CAAC,MAAM,KAAK,CAAC,GAAG,EAAE,OAAO,CAAC,CAAC,CAAC,KAAK,EAAE,CAAC;IAErD,IAAI,QAAQ,CAAC,QAAQ,EAAE,CAAC;QACpB,OAAO,MAAM,QAAQ,CAAC,QAAQ,CAAC,QAAQ,EAAE,MAAM,CAAC,MAAM,CAAC,CAAC;IAC5D,CAAC;SAAM,CAAC;QACJ,MAAM,IAAA,kCAAc,EAAC,QAAQ,CAAC,CAAC;IACnC,CAAC;IAED,OAAO,SAA+B,CAAC;AAC3C,CAAC"}
|
|
@@ -1,9 +1,22 @@
|
|
|
1
1
|
import type { Endpoint } from './defineEndpoint.js';
|
|
2
|
-
|
|
2
|
+
/**
|
|
3
|
+
* Parameters for a fetch endpoint.
|
|
4
|
+
*/
|
|
3
5
|
export interface FetchEndpointParams<TParams> {
|
|
6
|
+
/**
|
|
7
|
+
* The parameters for the endpoint.
|
|
8
|
+
*/
|
|
4
9
|
params: TParams;
|
|
10
|
+
/**
|
|
11
|
+
* The base URL for the endpoint.
|
|
12
|
+
*/
|
|
5
13
|
baseUrl?: string;
|
|
14
|
+
/**
|
|
15
|
+
* Additional headers for the endpoint.
|
|
16
|
+
*/
|
|
6
17
|
headers?: Record<string, string>;
|
|
7
|
-
onRequest?: (request: FetchRequest) => void;
|
|
8
18
|
}
|
|
19
|
+
/**
|
|
20
|
+
*
|
|
21
|
+
*/
|
|
9
22
|
export declare function fetchEndpoint<TParams, TResult>(endpoint: Endpoint<TParams, TResult>, params: FetchEndpointParams<TParams>): Promise<TResult>;
|
package/dist/cjs/index.cjs
CHANGED
|
@@ -9,6 +9,7 @@ tslib_1.__exportStar(require("./fetchJson.cjs"), exports);
|
|
|
9
9
|
tslib_1.__exportStar(require("./fetchRequest.cjs"), exports);
|
|
10
10
|
tslib_1.__exportStar(require("./FetchError.cjs"), exports);
|
|
11
11
|
tslib_1.__exportStar(require("./formUrlEncodedRequest.cjs"), exports);
|
|
12
|
+
tslib_1.__exportStar(require("./jsonNullableResponse.cjs"), exports);
|
|
12
13
|
tslib_1.__exportStar(require("./jsonRequest.cjs"), exports);
|
|
13
14
|
tslib_1.__exportStar(require("./jsonResponse.cjs"), exports);
|
|
14
15
|
tslib_1.__exportStar(require("./xmlRequest.cjs"), exports);
|
package/dist/cjs/index.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.cjs","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":";;;AAAA,8DAAoC;AACpC,8DAAoC;AACpC,iEAAuC;AACvC,6DAAmC;AACnC,yDAA+B;AAC/B,4DAAkC;AAClC,0DAAgC;AAChC,qEAA2C;AAC3C,2DAAiC;AACjC,4DAAkC;AAClC,0DAAgC;AAChC,2DAAiC;AAEjC,iEAAuC;AACvC,gEAAsC;AACtC,kEAAwC"}
|
|
1
|
+
{"version":3,"file":"index.cjs","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":";;;AAAA,8DAAoC;AACpC,8DAAoC;AACpC,iEAAuC;AACvC,6DAAmC;AACnC,yDAA+B;AAC/B,4DAAkC;AAClC,0DAAgC;AAChC,qEAA2C;AAC3C,oEAA0C;AAC1C,2DAAiC;AACjC,4DAAkC;AAClC,0DAAgC;AAChC,2DAAiC;AAEjC,iEAAuC;AACvC,gEAAsC;AACtC,kEAAwC"}
|
package/dist/cjs/index.d.ts
CHANGED
|
@@ -6,6 +6,7 @@ export * from './fetchJson.js';
|
|
|
6
6
|
export * from './fetchRequest.js';
|
|
7
7
|
export * from './FetchError.js';
|
|
8
8
|
export * from './formUrlEncodedRequest.js';
|
|
9
|
+
export * from './jsonNullableResponse.js';
|
|
9
10
|
export * from './jsonRequest.js';
|
|
10
11
|
export * from './jsonResponse.js';
|
|
11
12
|
export * from './xmlRequest.js';
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.jsonNullableResponse = jsonNullableResponse;
|
|
4
|
+
const FetchError_js_1 = require("./FetchError.cjs");
|
|
5
|
+
/**
|
|
6
|
+
* Parse a JSON response.
|
|
7
|
+
* Returns null if the response is 404.
|
|
8
|
+
*/
|
|
9
|
+
async function jsonNullableResponse(response) {
|
|
10
|
+
if (response.ok) {
|
|
11
|
+
return (await response.json());
|
|
12
|
+
}
|
|
13
|
+
if (response.status === 404) {
|
|
14
|
+
return null;
|
|
15
|
+
}
|
|
16
|
+
throw new FetchError_js_1.FetchError(response);
|
|
17
|
+
}
|
|
18
|
+
//# sourceMappingURL=jsonNullableResponse.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"jsonNullableResponse.cjs","sourceRoot":"","sources":["../../src/jsonNullableResponse.ts"],"names":[],"mappings":";;AAMA,oDAUC;AAhBD,mDAA6C;AAE7C;;;GAGG;AACI,KAAK,UAAU,oBAAoB,CAAI,QAAkB;IAC5D,IAAI,QAAQ,CAAC,EAAE,EAAE,CAAC;QACd,OAAO,CAAC,MAAM,QAAQ,CAAC,IAAI,EAAE,CAAM,CAAC;IACxC,CAAC;IAED,IAAI,QAAQ,CAAC,MAAM,KAAK,GAAG,EAAE,CAAC;QAC1B,OAAO,IAAI,CAAC;IAChB,CAAC;IAED,MAAM,IAAI,0BAAU,CAAC,QAAQ,CAAC,CAAC;AACnC,CAAC"}
|
|
@@ -1,7 +1,12 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.jsonResponse = jsonResponse;
|
|
4
|
-
|
|
4
|
+
const assertResponse_js_1 = require("./assertResponse.cjs");
|
|
5
|
+
/**
|
|
6
|
+
* Parse a JSON response.
|
|
7
|
+
*/
|
|
8
|
+
async function jsonResponse(response) {
|
|
9
|
+
await (0, assertResponse_js_1.assertResponse)(response);
|
|
5
10
|
return response.json();
|
|
6
11
|
}
|
|
7
12
|
//# sourceMappingURL=jsonResponse.cjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"jsonResponse.cjs","sourceRoot":"","sources":["../../src/jsonResponse.ts"],"names":[],"mappings":";;
|
|
1
|
+
{"version":3,"file":"jsonResponse.cjs","sourceRoot":"","sources":["../../src/jsonResponse.ts"],"names":[],"mappings":";;AAKA,oCAGC;AARD,2DAAqD;AAErD;;GAEG;AACI,KAAK,UAAU,YAAY,CAAI,QAAkB;IACpD,MAAM,IAAA,kCAAc,EAAC,QAAQ,CAAC,CAAC;IAC/B,OAAO,QAAQ,CAAC,IAAI,EAAgB,CAAC;AACzC,CAAC"}
|
package/dist/cjs/xmlResponse.cjs
CHANGED
|
@@ -2,7 +2,12 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.xmlResponse = xmlResponse;
|
|
4
4
|
const xml_utils_1 = require("@nzyme/xml-utils");
|
|
5
|
+
const assertResponse_js_1 = require("./assertResponse.cjs");
|
|
6
|
+
/**
|
|
7
|
+
* Parse an XML response.
|
|
8
|
+
*/
|
|
5
9
|
async function xmlResponse(response) {
|
|
10
|
+
await (0, assertResponse_js_1.assertResponse)(response);
|
|
6
11
|
const xml = await response.text();
|
|
7
12
|
return (0, xml_utils_1.xmlParse)(xml);
|
|
8
13
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"xmlResponse.cjs","sourceRoot":"","sources":["../../src/xmlResponse.ts"],"names":[],"mappings":";;
|
|
1
|
+
{"version":3,"file":"xmlResponse.cjs","sourceRoot":"","sources":["../../src/xmlResponse.ts"],"names":[],"mappings":";;AAOA,kCAKC;AAZD,gDAA4C;AAE5C,2DAAqD;AAErD;;GAEG;AACI,KAAK,UAAU,WAAW,CAAC,QAAkB;IAChD,MAAM,IAAA,kCAAc,EAAC,QAAQ,CAAC,CAAC;IAE/B,MAAM,GAAG,GAAG,MAAM,QAAQ,CAAC,IAAI,EAAE,CAAC;IAClC,OAAO,IAAA,oBAAQ,EAAC,GAAG,CAAC,CAAC;AACzB,CAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"assertResponse.js","sourceRoot":"","sources":["../../src/assertResponse.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,iBAAiB,CAAC;AAE7C,MAAM,CAAC,KAAK,UAAU,cAAc,CAAC,QAAkB;IACnD,IAAI,CAAC,QAAQ,CAAC,EAAE,EAAE,CAAC;QACf,MAAM,OAAO,GAAG,MAAM,QAAQ,CAAC,IAAI,EAAE,CAAC;QACtC,MAAM,IAAI,UAAU,CAAC,QAAQ,EAAE,OAAO,CAAC,CAAC;IAC5C,CAAC;AACL,CAAC"}
|
|
1
|
+
{"version":3,"file":"assertResponse.js","sourceRoot":"","sources":["../../src/assertResponse.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,iBAAiB,CAAC;AAE7C;;GAEG;AACH,MAAM,CAAC,KAAK,UAAU,cAAc,CAAC,QAAkB;IACnD,IAAI,CAAC,QAAQ,CAAC,EAAE,EAAE,CAAC;QACf,MAAM,OAAO,GAAG,MAAM,QAAQ,CAAC,IAAI,EAAE,CAAC;QACtC,MAAM,IAAI,UAAU,CAAC,QAAQ,EAAE,OAAO,CAAC,CAAC;IAC5C,CAAC;AACL,CAAC"}
|
|
@@ -1,9 +1,26 @@
|
|
|
1
1
|
import type { FetchRequest } from './fetchRequest.js';
|
|
2
|
+
/**
|
|
3
|
+
* A fetch endpoint.
|
|
4
|
+
*/
|
|
2
5
|
export interface Endpoint<TParams, TResult> {
|
|
6
|
+
/**
|
|
7
|
+
* The request for the endpoint.
|
|
8
|
+
*/
|
|
3
9
|
request: (params: TParams) => FetchRequest;
|
|
10
|
+
/**
|
|
11
|
+
* The response for the endpoint.
|
|
12
|
+
*/
|
|
4
13
|
response?: (response: Response, params: TParams) => Promise<TResult>;
|
|
5
14
|
}
|
|
15
|
+
/**
|
|
16
|
+
* A function that takes a response and returns a promise of a result.
|
|
17
|
+
*/
|
|
6
18
|
export interface EndpointResponse<T> {
|
|
7
19
|
(response: Response): Promise<T>;
|
|
8
20
|
}
|
|
21
|
+
/**
|
|
22
|
+
* Define a fetch endpoint.
|
|
23
|
+
*
|
|
24
|
+
* #__NO_SIDE_EFFECTS__
|
|
25
|
+
*/
|
|
9
26
|
export declare function defineEndpoint<TParams = void, TResult = void>(endpoint: Endpoint<TParams, TResult>): Endpoint<TParams, TResult>;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"defineEndpoint.js","sourceRoot":"","sources":["../../src/defineEndpoint.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"defineEndpoint.js","sourceRoot":"","sources":["../../src/defineEndpoint.ts"],"names":[],"mappings":"AAuBA;;;;GAIG;AACH,MAAM,UAAU,cAAc,CAC1B,QAAoC;IAEpC,OAAO,QAAQ,CAAC;AACpB,CAAC"}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import type { FetchRequest } from './fetchRequest.js';
|
|
2
|
+
/**
|
|
3
|
+
* A fetch function.
|
|
4
|
+
*/
|
|
5
|
+
export interface FetchOptions<TParams, TResult> {
|
|
6
|
+
/**
|
|
7
|
+
* The request for the endpoint.
|
|
8
|
+
*/
|
|
9
|
+
request: (params: TParams) => FetchRequest;
|
|
10
|
+
/**
|
|
11
|
+
* The response for the endpoint.
|
|
12
|
+
*/
|
|
13
|
+
response?: (response: Response, params: TParams) => Promise<TResult>;
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* Define a fetch function.
|
|
17
|
+
*
|
|
18
|
+
* #__NO_SIDE_EFFECTS__
|
|
19
|
+
*/
|
|
20
|
+
export declare function defineFetch<TParams = void, TResult = void>(fetch: FetchOptions<TParams, TResult>): FetchOptions<TParams, TResult>;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"defineFetch.js","sourceRoot":"","sources":["../../src/defineFetch.ts"],"names":[],"mappings":"AAgBA;;;;GAIG;AACH,MAAM,UAAU,WAAW,CAAiC,KAAqC;IAC7F,OAAO,KAAK,CAAC;AACjB,CAAC"}
|
|
@@ -1,9 +1,22 @@
|
|
|
1
1
|
import type { Endpoint } from './defineEndpoint.js';
|
|
2
|
-
|
|
2
|
+
/**
|
|
3
|
+
* Parameters for a fetch endpoint.
|
|
4
|
+
*/
|
|
3
5
|
export interface FetchEndpointParams<TParams> {
|
|
6
|
+
/**
|
|
7
|
+
* The parameters for the endpoint.
|
|
8
|
+
*/
|
|
4
9
|
params: TParams;
|
|
10
|
+
/**
|
|
11
|
+
* The base URL for the endpoint.
|
|
12
|
+
*/
|
|
5
13
|
baseUrl?: string;
|
|
14
|
+
/**
|
|
15
|
+
* Additional headers for the endpoint.
|
|
16
|
+
*/
|
|
6
17
|
headers?: Record<string, string>;
|
|
7
|
-
onRequest?: (request: FetchRequest) => void;
|
|
8
18
|
}
|
|
19
|
+
/**
|
|
20
|
+
*
|
|
21
|
+
*/
|
|
9
22
|
export declare function fetchEndpoint<TParams, TResult>(endpoint: Endpoint<TParams, TResult>, params: FetchEndpointParams<TParams>): Promise<TResult>;
|
|
@@ -1,5 +1,8 @@
|
|
|
1
|
-
import { joinURL } from 'ufo';
|
|
2
|
-
import {
|
|
1
|
+
import { joinURL, withQuery } from 'ufo';
|
|
2
|
+
import { assertResponse } from './assertResponse.js';
|
|
3
|
+
/**
|
|
4
|
+
*
|
|
5
|
+
*/
|
|
3
6
|
export async function fetchEndpoint(endpoint, params) {
|
|
4
7
|
let request = endpoint.request(params.params);
|
|
5
8
|
request = {
|
|
@@ -10,11 +13,15 @@ export async function fetchEndpoint(endpoint, params) {
|
|
|
10
13
|
...request.headers,
|
|
11
14
|
},
|
|
12
15
|
};
|
|
13
|
-
|
|
14
|
-
|
|
16
|
+
const url = request.query ? withQuery(request.url, request.query) : request.url;
|
|
17
|
+
// Clone the response to avoid "Other side closed error"
|
|
18
|
+
const response = (await fetch(url, request)).clone();
|
|
15
19
|
if (endpoint.response) {
|
|
16
20
|
return await endpoint.response(response, params.params);
|
|
17
21
|
}
|
|
22
|
+
else {
|
|
23
|
+
await assertResponse(response);
|
|
24
|
+
}
|
|
18
25
|
return undefined;
|
|
19
26
|
}
|
|
20
27
|
//# sourceMappingURL=fetchEndpoint.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"fetchEndpoint.js","sourceRoot":"","sources":["../../src/fetchEndpoint.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,KAAK,CAAC;
|
|
1
|
+
{"version":3,"file":"fetchEndpoint.js","sourceRoot":"","sources":["../../src/fetchEndpoint.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,SAAS,EAAE,MAAM,KAAK,CAAC;AAEzC,OAAO,EAAE,cAAc,EAAE,MAAM,qBAAqB,CAAC;AAqBrD;;GAEG;AACH,MAAM,CAAC,KAAK,UAAU,aAAa,CAC/B,QAAoC,EACpC,MAAoC;IAEpC,IAAI,OAAO,GAAG,QAAQ,CAAC,OAAO,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC;IAE9C,OAAO,GAAG;QACN,GAAG,OAAO;QACV,GAAG,EAAE,MAAM,CAAC,OAAO,CAAC,CAAC,CAAC,OAAO,CAAC,MAAM,CAAC,OAAO,EAAE,OAAO,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,OAAO,CAAC,GAAG;QACxE,OAAO,EAAE;YACL,GAAG,MAAM,CAAC,OAAO;YACjB,GAAG,OAAO,CAAC,OAAO;SACrB;KACJ,CAAC;IAEF,MAAM,GAAG,GAAG,OAAO,CAAC,KAAK,CAAC,CAAC,CAAC,SAAS,CAAC,OAAO,CAAC,GAAG,EAAE,OAAO,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,OAAO,CAAC,GAAG,CAAC;IAChF,wDAAwD;IACxD,MAAM,QAAQ,GAAG,CAAC,MAAM,KAAK,CAAC,GAAG,EAAE,OAAO,CAAC,CAAC,CAAC,KAAK,EAAE,CAAC;IAErD,IAAI,QAAQ,CAAC,QAAQ,EAAE,CAAC;QACpB,OAAO,MAAM,QAAQ,CAAC,QAAQ,CAAC,QAAQ,EAAE,MAAM,CAAC,MAAM,CAAC,CAAC;IAC5D,CAAC;SAAM,CAAC;QACJ,MAAM,cAAc,CAAC,QAAQ,CAAC,CAAC;IACnC,CAAC;IAED,OAAO,SAA+B,CAAC;AAC3C,CAAC"}
|
package/dist/esm/index.d.ts
CHANGED
|
@@ -6,6 +6,7 @@ export * from './fetchJson.js';
|
|
|
6
6
|
export * from './fetchRequest.js';
|
|
7
7
|
export * from './FetchError.js';
|
|
8
8
|
export * from './formUrlEncodedRequest.js';
|
|
9
|
+
export * from './jsonNullableResponse.js';
|
|
9
10
|
export * from './jsonRequest.js';
|
|
10
11
|
export * from './jsonResponse.js';
|
|
11
12
|
export * from './xmlRequest.js';
|
package/dist/esm/index.js
CHANGED
|
@@ -6,6 +6,7 @@ export * from './fetchJson.js';
|
|
|
6
6
|
export * from './fetchRequest.js';
|
|
7
7
|
export * from './FetchError.js';
|
|
8
8
|
export * from './formUrlEncodedRequest.js';
|
|
9
|
+
export * from './jsonNullableResponse.js';
|
|
9
10
|
export * from './jsonRequest.js';
|
|
10
11
|
export * from './jsonResponse.js';
|
|
11
12
|
export * from './xmlRequest.js';
|
package/dist/esm/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,qBAAqB,CAAC;AACpC,cAAc,qBAAqB,CAAC;AACpC,cAAc,wBAAwB,CAAC;AACvC,cAAc,oBAAoB,CAAC;AACnC,cAAc,gBAAgB,CAAC;AAC/B,cAAc,mBAAmB,CAAC;AAClC,cAAc,iBAAiB,CAAC;AAChC,cAAc,4BAA4B,CAAC;AAC3C,cAAc,kBAAkB,CAAC;AACjC,cAAc,mBAAmB,CAAC;AAClC,cAAc,iBAAiB,CAAC;AAChC,cAAc,kBAAkB,CAAC;AAEjC,cAAc,wBAAwB,CAAC;AACvC,cAAc,uBAAuB,CAAC;AACtC,cAAc,yBAAyB,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,qBAAqB,CAAC;AACpC,cAAc,qBAAqB,CAAC;AACpC,cAAc,wBAAwB,CAAC;AACvC,cAAc,oBAAoB,CAAC;AACnC,cAAc,gBAAgB,CAAC;AAC/B,cAAc,mBAAmB,CAAC;AAClC,cAAc,iBAAiB,CAAC;AAChC,cAAc,4BAA4B,CAAC;AAC3C,cAAc,2BAA2B,CAAC;AAC1C,cAAc,kBAAkB,CAAC;AACjC,cAAc,mBAAmB,CAAC;AAClC,cAAc,iBAAiB,CAAC;AAChC,cAAc,kBAAkB,CAAC;AAEjC,cAAc,wBAAwB,CAAC;AACvC,cAAc,uBAAuB,CAAC;AACtC,cAAc,yBAAyB,CAAC"}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { FetchError } from './FetchError.js';
|
|
2
|
+
/**
|
|
3
|
+
* Parse a JSON response.
|
|
4
|
+
* Returns null if the response is 404.
|
|
5
|
+
*/
|
|
6
|
+
export async function jsonNullableResponse(response) {
|
|
7
|
+
if (response.ok) {
|
|
8
|
+
return (await response.json());
|
|
9
|
+
}
|
|
10
|
+
if (response.status === 404) {
|
|
11
|
+
return null;
|
|
12
|
+
}
|
|
13
|
+
throw new FetchError(response);
|
|
14
|
+
}
|
|
15
|
+
//# sourceMappingURL=jsonNullableResponse.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"jsonNullableResponse.js","sourceRoot":"","sources":["../../src/jsonNullableResponse.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,iBAAiB,CAAC;AAE7C;;;GAGG;AACH,MAAM,CAAC,KAAK,UAAU,oBAAoB,CAAI,QAAkB;IAC5D,IAAI,QAAQ,CAAC,EAAE,EAAE,CAAC;QACd,OAAO,CAAC,MAAM,QAAQ,CAAC,IAAI,EAAE,CAAM,CAAC;IACxC,CAAC;IAED,IAAI,QAAQ,CAAC,MAAM,KAAK,GAAG,EAAE,CAAC;QAC1B,OAAO,IAAI,CAAC;IAChB,CAAC;IAED,MAAM,IAAI,UAAU,CAAC,QAAQ,CAAC,CAAC;AACnC,CAAC"}
|
package/dist/esm/jsonResponse.js
CHANGED
|
@@ -1,4 +1,9 @@
|
|
|
1
|
-
|
|
1
|
+
import { assertResponse } from './assertResponse.js';
|
|
2
|
+
/**
|
|
3
|
+
* Parse a JSON response.
|
|
4
|
+
*/
|
|
5
|
+
export async function jsonResponse(response) {
|
|
6
|
+
await assertResponse(response);
|
|
2
7
|
return response.json();
|
|
3
8
|
}
|
|
4
9
|
//# sourceMappingURL=jsonResponse.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"jsonResponse.js","sourceRoot":"","sources":["../../src/jsonResponse.ts"],"names":[],"mappings":"AAAA,MAAM,UAAU,YAAY,CAAI,QAAkB;
|
|
1
|
+
{"version":3,"file":"jsonResponse.js","sourceRoot":"","sources":["../../src/jsonResponse.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,MAAM,qBAAqB,CAAC;AAErD;;GAEG;AACH,MAAM,CAAC,KAAK,UAAU,YAAY,CAAI,QAAkB;IACpD,MAAM,cAAc,CAAC,QAAQ,CAAC,CAAC;IAC/B,OAAO,QAAQ,CAAC,IAAI,EAAgB,CAAC;AACzC,CAAC"}
|
package/dist/esm/xmlResponse.js
CHANGED
|
@@ -1,5 +1,10 @@
|
|
|
1
1
|
import { xmlParse } from '@nzyme/xml-utils';
|
|
2
|
+
import { assertResponse } from './assertResponse.js';
|
|
3
|
+
/**
|
|
4
|
+
* Parse an XML response.
|
|
5
|
+
*/
|
|
2
6
|
export async function xmlResponse(response) {
|
|
7
|
+
await assertResponse(response);
|
|
3
8
|
const xml = await response.text();
|
|
4
9
|
return xmlParse(xml);
|
|
5
10
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"xmlResponse.js","sourceRoot":"","sources":["../../src/xmlResponse.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,kBAAkB,CAAC;AAE5C,MAAM,CAAC,KAAK,UAAU,WAAW,CAAC,QAAkB;IAChD,MAAM,GAAG,GAAG,MAAM,QAAQ,CAAC,IAAI,EAAE,CAAC;IAClC,OAAO,QAAQ,CAAC,GAAG,CAAC,CAAC;AACzB,CAAC"}
|
|
1
|
+
{"version":3,"file":"xmlResponse.js","sourceRoot":"","sources":["../../src/xmlResponse.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,kBAAkB,CAAC;AAE5C,OAAO,EAAE,cAAc,EAAE,MAAM,qBAAqB,CAAC;AAErD;;GAEG;AACH,MAAM,CAAC,KAAK,UAAU,WAAW,CAAC,QAAkB;IAChD,MAAM,cAAc,CAAC,QAAQ,CAAC,CAAC;IAE/B,MAAM,GAAG,GAAG,MAAM,QAAQ,CAAC,IAAI,EAAE,CAAC;IAClC,OAAO,QAAQ,CAAC,GAAG,CAAC,CAAC;AACzB,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@nzyme/fetch-utils",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.9.0",
|
|
4
4
|
"sideEffects": false,
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"repository": {
|
|
@@ -41,16 +41,16 @@
|
|
|
41
41
|
"smoke:cjs": "node tests/smoke.test.cjs"
|
|
42
42
|
},
|
|
43
43
|
"dependencies": {
|
|
44
|
-
"@nzyme/ioc": "^0.
|
|
45
|
-
"@nzyme/types": "^0.
|
|
46
|
-
"@nzyme/xml-utils": "^0.
|
|
44
|
+
"@nzyme/ioc": "^0.9.0",
|
|
45
|
+
"@nzyme/types": "^0.9.0",
|
|
46
|
+
"@nzyme/xml-utils": "^0.9.0",
|
|
47
47
|
"tslib": "^2.8.1",
|
|
48
48
|
"ufo": "^1.5.4"
|
|
49
49
|
},
|
|
50
50
|
"devDependencies": {
|
|
51
|
-
"@nzyme/eslint": "^0.
|
|
52
|
-
"@nzyme/tsconfig": "^0.
|
|
53
|
-
"@types/node": "^22.13.
|
|
51
|
+
"@nzyme/eslint": "^0.9.0",
|
|
52
|
+
"@nzyme/tsconfig": "^0.9.0",
|
|
53
|
+
"@types/node": "^22.13.14",
|
|
54
54
|
"typescript": "^5.8.2"
|
|
55
55
|
}
|
|
56
56
|
}
|