@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.
Files changed (49) hide show
  1. package/dist/cjs/assertResponse.cjs +3 -0
  2. package/dist/cjs/assertResponse.cjs.map +1 -1
  3. package/dist/cjs/assertResponse.d.ts +3 -0
  4. package/dist/cjs/defineEndpoint.cjs +5 -1
  5. package/dist/cjs/defineEndpoint.cjs.map +1 -1
  6. package/dist/cjs/defineEndpoint.d.ts +17 -0
  7. package/dist/cjs/defineFetch.cjs +12 -0
  8. package/dist/cjs/defineFetch.cjs.map +1 -0
  9. package/dist/cjs/defineFetch.d.ts +20 -0
  10. package/dist/cjs/fetchEndpoint.cjs +10 -3
  11. package/dist/cjs/fetchEndpoint.cjs.map +1 -1
  12. package/dist/cjs/fetchEndpoint.d.ts +15 -2
  13. package/dist/cjs/index.cjs +1 -0
  14. package/dist/cjs/index.cjs.map +1 -1
  15. package/dist/cjs/index.d.ts +1 -0
  16. package/dist/cjs/jsonNullableResponse.cjs +18 -0
  17. package/dist/cjs/jsonNullableResponse.cjs.map +1 -0
  18. package/dist/cjs/jsonNullableResponse.d.ts +5 -0
  19. package/dist/cjs/jsonResponse.cjs +6 -1
  20. package/dist/cjs/jsonResponse.cjs.map +1 -1
  21. package/dist/cjs/jsonResponse.d.ts +3 -0
  22. package/dist/cjs/xmlResponse.cjs +5 -0
  23. package/dist/cjs/xmlResponse.cjs.map +1 -1
  24. package/dist/cjs/xmlResponse.d.ts +3 -0
  25. package/dist/esm/assertResponse.d.ts +3 -0
  26. package/dist/esm/assertResponse.js +3 -0
  27. package/dist/esm/assertResponse.js.map +1 -1
  28. package/dist/esm/defineEndpoint.d.ts +17 -0
  29. package/dist/esm/defineEndpoint.js +5 -1
  30. package/dist/esm/defineEndpoint.js.map +1 -1
  31. package/dist/esm/defineFetch.d.ts +20 -0
  32. package/dist/esm/defineFetch.js +9 -0
  33. package/dist/esm/defineFetch.js.map +1 -0
  34. package/dist/esm/fetchEndpoint.d.ts +15 -2
  35. package/dist/esm/fetchEndpoint.js +11 -4
  36. package/dist/esm/fetchEndpoint.js.map +1 -1
  37. package/dist/esm/index.d.ts +1 -0
  38. package/dist/esm/index.js +1 -0
  39. package/dist/esm/index.js.map +1 -1
  40. package/dist/esm/jsonNullableResponse.d.ts +5 -0
  41. package/dist/esm/jsonNullableResponse.js +15 -0
  42. package/dist/esm/jsonNullableResponse.js.map +1 -0
  43. package/dist/esm/jsonResponse.d.ts +3 -0
  44. package/dist/esm/jsonResponse.js +6 -1
  45. package/dist/esm/jsonResponse.js.map +1 -1
  46. package/dist/esm/xmlResponse.d.ts +3 -0
  47. package/dist/esm/xmlResponse.js +5 -0
  48. package/dist/esm/xmlResponse.js.map +1 -1
  49. 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":";;AAEA,wCAKC;AAPD,mDAA6C;AAEtC,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
+ {"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 +1,4 @@
1
+ /**
2
+ * Assert that a response is ok.
3
+ */
1
4
  export declare function assertResponse(response: Response): Promise<void>;
@@ -1,7 +1,11 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.defineEndpoint = defineEndpoint;
4
- /*#__NO_SIDE_EFFECTS__*/
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":";;AAYA,wCAIC;AALD,wBAAwB;AACxB,SAAgB,cAAc,CAC1B,QAAoC;IAEpC,OAAO,QAAQ,CAAC;AACpB,CAAC"}
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 fetchRequest_js_1 = require("./fetchRequest.cjs");
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
- params.onRequest?.(request);
17
- const response = await (0, fetchRequest_js_1.fetchRequest)(request);
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":";;AAaA,sCAuBC;AApCD,6BAA8B;AAI9B,uDAAiD;AAS1C,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,CAAC,SAAS,EAAE,CAAC,OAAO,CAAC,CAAC;IAC5B,MAAM,QAAQ,GAAG,MAAM,IAAA,8BAAY,EAAC,OAAO,CAAC,CAAC;IAE7C,IAAI,QAAQ,CAAC,QAAQ,EAAE,CAAC;QACpB,OAAO,MAAM,QAAQ,CAAC,QAAQ,CAAC,QAAQ,EAAE,MAAM,CAAC,MAAM,CAAC,CAAC;IAC5D,CAAC;IAED,OAAO,SAA+B,CAAC;AAC3C,CAAC"}
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
- import type { FetchRequest } from './fetchRequest.js';
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>;
@@ -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);
@@ -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"}
@@ -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"}
@@ -0,0 +1,5 @@
1
+ /**
2
+ * Parse a JSON response.
3
+ * Returns null if the response is 404.
4
+ */
5
+ export declare function jsonNullableResponse<T>(response: Response): Promise<T | null>;
@@ -1,7 +1,12 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.jsonResponse = jsonResponse;
4
- function jsonResponse(response) {
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":";;AAAA,oCAEC;AAFD,SAAgB,YAAY,CAAI,QAAkB;IAC9C,OAAO,QAAQ,CAAC,IAAI,EAAgB,CAAC;AACzC,CAAC"}
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"}
@@ -1 +1,4 @@
1
+ /**
2
+ * Parse a JSON response.
3
+ */
1
4
  export declare function jsonResponse<T>(response: Response): Promise<T>;
@@ -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":";;AAEA,kCAGC;AALD,gDAA4C;AAErC,KAAK,UAAU,WAAW,CAAC,QAAkB;IAChD,MAAM,GAAG,GAAG,MAAM,QAAQ,CAAC,IAAI,EAAE,CAAC;IAClC,OAAO,IAAA,oBAAQ,EAAC,GAAG,CAAC,CAAC;AACzB,CAAC"}
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,4 @@
1
+ /**
2
+ * Parse an XML response.
3
+ */
1
4
  export declare function xmlResponse(response: Response): Promise<import("@nzyme/xml-utils").XmlElement>;
@@ -1 +1,4 @@
1
+ /**
2
+ * Assert that a response is ok.
3
+ */
1
4
  export declare function assertResponse(response: Response): Promise<void>;
@@ -1,4 +1,7 @@
1
1
  import { FetchError } from './FetchError.js';
2
+ /**
3
+ * Assert that a response is ok.
4
+ */
2
5
  export async function assertResponse(response) {
3
6
  if (!response.ok) {
4
7
  const message = await response.text();
@@ -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,4 +1,8 @@
1
- /*#__NO_SIDE_EFFECTS__*/
1
+ /**
2
+ * Define a fetch endpoint.
3
+ *
4
+ * #__NO_SIDE_EFFECTS__
5
+ */
2
6
  export function defineEndpoint(endpoint) {
3
7
  return endpoint;
4
8
  }
@@ -1 +1 @@
1
- {"version":3,"file":"defineEndpoint.js","sourceRoot":"","sources":["../../src/defineEndpoint.ts"],"names":[],"mappings":"AAWA,wBAAwB;AACxB,MAAM,UAAU,cAAc,CAC1B,QAAoC;IAEpC,OAAO,QAAQ,CAAC;AACpB,CAAC"}
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,9 @@
1
+ /**
2
+ * Define a fetch function.
3
+ *
4
+ * #__NO_SIDE_EFFECTS__
5
+ */
6
+ export function defineFetch(fetch) {
7
+ return fetch;
8
+ }
9
+ //# sourceMappingURL=defineFetch.js.map
@@ -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
- import type { FetchRequest } from './fetchRequest.js';
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 { fetchRequest } from './fetchRequest.js';
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
- params.onRequest?.(request);
14
- const response = await fetchRequest(request);
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;AAI9B,OAAO,EAAE,YAAY,EAAE,MAAM,mBAAmB,CAAC;AASjD,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,CAAC,SAAS,EAAE,CAAC,OAAO,CAAC,CAAC;IAC5B,MAAM,QAAQ,GAAG,MAAM,YAAY,CAAC,OAAO,CAAC,CAAC;IAE7C,IAAI,QAAQ,CAAC,QAAQ,EAAE,CAAC;QACpB,OAAO,MAAM,QAAQ,CAAC,QAAQ,CAAC,QAAQ,EAAE,MAAM,CAAC,MAAM,CAAC,CAAC;IAC5D,CAAC;IAED,OAAO,SAA+B,CAAC;AAC3C,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"}
@@ -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';
@@ -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,5 @@
1
+ /**
2
+ * Parse a JSON response.
3
+ * Returns null if the response is 404.
4
+ */
5
+ export declare function jsonNullableResponse<T>(response: Response): Promise<T | null>;
@@ -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"}
@@ -1 +1,4 @@
1
+ /**
2
+ * Parse a JSON response.
3
+ */
1
4
  export declare function jsonResponse<T>(response: Response): Promise<T>;
@@ -1,4 +1,9 @@
1
- export function jsonResponse(response) {
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;IAC9C,OAAO,QAAQ,CAAC,IAAI,EAAgB,CAAC;AACzC,CAAC"}
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"}
@@ -1 +1,4 @@
1
+ /**
2
+ * Parse an XML response.
3
+ */
1
4
  export declare function xmlResponse(response: Response): Promise<import("@nzyme/xml-utils").XmlElement>;
@@ -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.8.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.8.0",
45
- "@nzyme/types": "^0.8.0",
46
- "@nzyme/xml-utils": "^0.8.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.8.0",
52
- "@nzyme/tsconfig": "^0.8.0",
53
- "@types/node": "^22.13.10",
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
  }