@pagopa/io-wallet-utils 0.4.1

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/index.js ADDED
@@ -0,0 +1,79 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/index.ts
21
+ var index_exports = {};
22
+ __export(index_exports, {
23
+ JsonParseError: () => import_utils.JsonParseError,
24
+ UnexpectedStatusCodeError: () => UnexpectedStatusCodeError,
25
+ ValidationError: () => import_utils.ValidationError,
26
+ hasStatusOrThrow: () => hasStatusOrThrow,
27
+ parseRawHttpResponse: () => parseRawHttpResponse,
28
+ serializeAttrs: () => serializeAttrs
29
+ });
30
+ module.exports = __toCommonJS(index_exports);
31
+
32
+ // src/errors.ts
33
+ var serializeAttrs = (attrs) => Object.entries(attrs).filter(([, v]) => v !== void 0).map(([k, v]) => {
34
+ if (Array.isArray(v)) return [k, `(${v.join(", ")})`];
35
+ if (typeof v !== "string") return [k, JSON.stringify(v)];
36
+ return [k, v];
37
+ }).map((_) => _.join("=")).join(" ");
38
+ var UnexpectedStatusCodeError = class extends Error {
39
+ code = "ERR_UNEXPECTED_STATUS_CODE";
40
+ reason;
41
+ statusCode;
42
+ constructor({
43
+ message,
44
+ reason,
45
+ statusCode
46
+ }) {
47
+ super(serializeAttrs({ message, reason, statusCode }));
48
+ this.reason = reason;
49
+ this.statusCode = statusCode;
50
+ }
51
+ };
52
+
53
+ // src/fetcher.ts
54
+ var hasStatusOrThrow = (status, customError) => async (res) => {
55
+ if (res.status !== status) {
56
+ const ErrorClass = customError ?? UnexpectedStatusCodeError;
57
+ throw new ErrorClass({
58
+ message: `Http request failed. Expected ${status}, got ${res.status}, url: ${res.url}`,
59
+ reason: await parseRawHttpResponse(res),
60
+ // Pass the response body as reason so the original error can surface
61
+ statusCode: res.status
62
+ });
63
+ }
64
+ return res;
65
+ };
66
+ var parseRawHttpResponse = (response) => response.headers.get("content-type")?.includes("application/json") ? response.json() : response.text();
67
+
68
+ // src/index.ts
69
+ var import_utils = require("@openid4vc/utils");
70
+ // Annotate the CommonJS export names for ESM import in node:
71
+ 0 && (module.exports = {
72
+ JsonParseError,
73
+ UnexpectedStatusCodeError,
74
+ ValidationError,
75
+ hasStatusOrThrow,
76
+ parseRawHttpResponse,
77
+ serializeAttrs
78
+ });
79
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/index.ts","../src/errors.ts","../src/fetcher.ts"],"sourcesContent":["export * from \"./errors\";\nexport * from \"./fetcher\";\n\nexport { JsonParseError, ValidationError } from \"@openid4vc/utils\";\n","// An error reason that supports both a string and a generic JSON object\ntype GenericErrorReason = Record<string, unknown> | string;\n\n/**\n * utility to format a set of attributes into an error message string\n *\n * @example\n * // returns \"foo=value bar=(list, item)\"\n * serializeAttrs({ foo: \"value\", bar: [\"list\", \"item\"] })\n *\n * @param attrs A key value record set\n * @returns a human-readable serialization of the set\n */\nexport const serializeAttrs = (\n attrs: Record<string, GenericErrorReason | number | string[] | undefined>,\n): string =>\n Object.entries(attrs)\n .filter(([, v]) => v !== undefined)\n .map(([k, v]) => {\n if (Array.isArray(v)) return [k, `(${v.join(\", \")})`];\n if (typeof v !== \"string\") return [k, JSON.stringify(v)];\n return [k, v];\n })\n .map((_) => _.join(\"=\"))\n .join(\" \");\n\n/**\n * An error subclass thrown when an HTTP request has a status code different from the one expected.\n */\nexport class UnexpectedStatusCodeError extends Error {\n code = \"ERR_UNEXPECTED_STATUS_CODE\";\n reason: GenericErrorReason;\n statusCode: number;\n\n constructor({\n message,\n reason,\n statusCode,\n }: {\n message: string;\n reason: GenericErrorReason;\n statusCode: number;\n }) {\n super(serializeAttrs({ message, reason, statusCode }));\n this.reason = reason;\n this.statusCode = statusCode;\n }\n}\n","import { UnexpectedStatusCodeError } from \"./errors\";\n\n/**\n * Check if a response is in the expected status, otherwise throw an error\n * @param status - The expected status\n * @param customError - A custom error compatible with {@link UnexpectedStatusCodeError}\n * @throws UnexpectedStatusCodeError if the status is different from the one expected\n * @returns The given response object\n */\nexport const hasStatusOrThrow =\n (status: number, customError?: typeof UnexpectedStatusCodeError) =>\n async (res: Response): Promise<Response> => {\n if (res.status !== status) {\n const ErrorClass = customError ?? UnexpectedStatusCodeError;\n throw new ErrorClass({\n message: `Http request failed. Expected ${status}, got ${res.status}, url: ${res.url}`,\n reason: await parseRawHttpResponse(res), // Pass the response body as reason so the original error can surface\n statusCode: res.status,\n });\n }\n return res;\n };\n\n/**\n * Utility function to parse a raw HTTP response as JSON if supported, otherwise as text.\n */\nexport const parseRawHttpResponse = <T extends Record<string, unknown>>(\n response: Response,\n) =>\n response.headers.get(\"content-type\")?.includes(\"application/json\")\n ? (response.json() as Promise<T>)\n : response.text();\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACaO,IAAM,iBAAiB,CAC5B,UAEA,OAAO,QAAQ,KAAK,EACjB,OAAO,CAAC,CAAC,EAAE,CAAC,MAAM,MAAM,MAAS,EACjC,IAAI,CAAC,CAAC,GAAG,CAAC,MAAM;AACf,MAAI,MAAM,QAAQ,CAAC,EAAG,QAAO,CAAC,GAAG,IAAI,EAAE,KAAK,IAAI,CAAC,GAAG;AACpD,MAAI,OAAO,MAAM,SAAU,QAAO,CAAC,GAAG,KAAK,UAAU,CAAC,CAAC;AACvD,SAAO,CAAC,GAAG,CAAC;AACd,CAAC,EACA,IAAI,CAAC,MAAM,EAAE,KAAK,GAAG,CAAC,EACtB,KAAK,GAAG;AAKN,IAAM,4BAAN,cAAwC,MAAM;AAAA,EACnD,OAAO;AAAA,EACP;AAAA,EACA;AAAA,EAEA,YAAY;AAAA,IACV;AAAA,IACA;AAAA,IACA;AAAA,EACF,GAIG;AACD,UAAM,eAAe,EAAE,SAAS,QAAQ,WAAW,CAAC,CAAC;AACrD,SAAK,SAAS;AACd,SAAK,aAAa;AAAA,EACpB;AACF;;;ACtCO,IAAM,mBACX,CAAC,QAAgB,gBACjB,OAAO,QAAqC;AAC1C,MAAI,IAAI,WAAW,QAAQ;AACzB,UAAM,aAAa,eAAe;AAClC,UAAM,IAAI,WAAW;AAAA,MACnB,SAAS,iCAAiC,MAAM,SAAS,IAAI,MAAM,UAAU,IAAI,GAAG;AAAA,MACpF,QAAQ,MAAM,qBAAqB,GAAG;AAAA;AAAA,MACtC,YAAY,IAAI;AAAA,IAClB,CAAC;AAAA,EACH;AACA,SAAO;AACT;AAKK,IAAM,uBAAuB,CAClC,aAEA,SAAS,QAAQ,IAAI,cAAc,GAAG,SAAS,kBAAkB,IAC5D,SAAS,KAAK,IACf,SAAS,KAAK;;;AF5BpB,mBAAgD;","names":[]}
package/dist/index.mjs ADDED
@@ -0,0 +1,47 @@
1
+ // src/errors.ts
2
+ var serializeAttrs = (attrs) => Object.entries(attrs).filter(([, v]) => v !== void 0).map(([k, v]) => {
3
+ if (Array.isArray(v)) return [k, `(${v.join(", ")})`];
4
+ if (typeof v !== "string") return [k, JSON.stringify(v)];
5
+ return [k, v];
6
+ }).map((_) => _.join("=")).join(" ");
7
+ var UnexpectedStatusCodeError = class extends Error {
8
+ code = "ERR_UNEXPECTED_STATUS_CODE";
9
+ reason;
10
+ statusCode;
11
+ constructor({
12
+ message,
13
+ reason,
14
+ statusCode
15
+ }) {
16
+ super(serializeAttrs({ message, reason, statusCode }));
17
+ this.reason = reason;
18
+ this.statusCode = statusCode;
19
+ }
20
+ };
21
+
22
+ // src/fetcher.ts
23
+ var hasStatusOrThrow = (status, customError) => async (res) => {
24
+ if (res.status !== status) {
25
+ const ErrorClass = customError ?? UnexpectedStatusCodeError;
26
+ throw new ErrorClass({
27
+ message: `Http request failed. Expected ${status}, got ${res.status}, url: ${res.url}`,
28
+ reason: await parseRawHttpResponse(res),
29
+ // Pass the response body as reason so the original error can surface
30
+ statusCode: res.status
31
+ });
32
+ }
33
+ return res;
34
+ };
35
+ var parseRawHttpResponse = (response) => response.headers.get("content-type")?.includes("application/json") ? response.json() : response.text();
36
+
37
+ // src/index.ts
38
+ import { JsonParseError, ValidationError } from "@openid4vc/utils";
39
+ export {
40
+ JsonParseError,
41
+ UnexpectedStatusCodeError,
42
+ ValidationError,
43
+ hasStatusOrThrow,
44
+ parseRawHttpResponse,
45
+ serializeAttrs
46
+ };
47
+ //# sourceMappingURL=index.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/errors.ts","../src/fetcher.ts","../src/index.ts"],"sourcesContent":["// An error reason that supports both a string and a generic JSON object\ntype GenericErrorReason = Record<string, unknown> | string;\n\n/**\n * utility to format a set of attributes into an error message string\n *\n * @example\n * // returns \"foo=value bar=(list, item)\"\n * serializeAttrs({ foo: \"value\", bar: [\"list\", \"item\"] })\n *\n * @param attrs A key value record set\n * @returns a human-readable serialization of the set\n */\nexport const serializeAttrs = (\n attrs: Record<string, GenericErrorReason | number | string[] | undefined>,\n): string =>\n Object.entries(attrs)\n .filter(([, v]) => v !== undefined)\n .map(([k, v]) => {\n if (Array.isArray(v)) return [k, `(${v.join(\", \")})`];\n if (typeof v !== \"string\") return [k, JSON.stringify(v)];\n return [k, v];\n })\n .map((_) => _.join(\"=\"))\n .join(\" \");\n\n/**\n * An error subclass thrown when an HTTP request has a status code different from the one expected.\n */\nexport class UnexpectedStatusCodeError extends Error {\n code = \"ERR_UNEXPECTED_STATUS_CODE\";\n reason: GenericErrorReason;\n statusCode: number;\n\n constructor({\n message,\n reason,\n statusCode,\n }: {\n message: string;\n reason: GenericErrorReason;\n statusCode: number;\n }) {\n super(serializeAttrs({ message, reason, statusCode }));\n this.reason = reason;\n this.statusCode = statusCode;\n }\n}\n","import { UnexpectedStatusCodeError } from \"./errors\";\n\n/**\n * Check if a response is in the expected status, otherwise throw an error\n * @param status - The expected status\n * @param customError - A custom error compatible with {@link UnexpectedStatusCodeError}\n * @throws UnexpectedStatusCodeError if the status is different from the one expected\n * @returns The given response object\n */\nexport const hasStatusOrThrow =\n (status: number, customError?: typeof UnexpectedStatusCodeError) =>\n async (res: Response): Promise<Response> => {\n if (res.status !== status) {\n const ErrorClass = customError ?? UnexpectedStatusCodeError;\n throw new ErrorClass({\n message: `Http request failed. Expected ${status}, got ${res.status}, url: ${res.url}`,\n reason: await parseRawHttpResponse(res), // Pass the response body as reason so the original error can surface\n statusCode: res.status,\n });\n }\n return res;\n };\n\n/**\n * Utility function to parse a raw HTTP response as JSON if supported, otherwise as text.\n */\nexport const parseRawHttpResponse = <T extends Record<string, unknown>>(\n response: Response,\n) =>\n response.headers.get(\"content-type\")?.includes(\"application/json\")\n ? (response.json() as Promise<T>)\n : response.text();\n","export * from \"./errors\";\nexport * from \"./fetcher\";\n\nexport { JsonParseError, ValidationError } from \"@openid4vc/utils\";\n"],"mappings":";AAaO,IAAM,iBAAiB,CAC5B,UAEA,OAAO,QAAQ,KAAK,EACjB,OAAO,CAAC,CAAC,EAAE,CAAC,MAAM,MAAM,MAAS,EACjC,IAAI,CAAC,CAAC,GAAG,CAAC,MAAM;AACf,MAAI,MAAM,QAAQ,CAAC,EAAG,QAAO,CAAC,GAAG,IAAI,EAAE,KAAK,IAAI,CAAC,GAAG;AACpD,MAAI,OAAO,MAAM,SAAU,QAAO,CAAC,GAAG,KAAK,UAAU,CAAC,CAAC;AACvD,SAAO,CAAC,GAAG,CAAC;AACd,CAAC,EACA,IAAI,CAAC,MAAM,EAAE,KAAK,GAAG,CAAC,EACtB,KAAK,GAAG;AAKN,IAAM,4BAAN,cAAwC,MAAM;AAAA,EACnD,OAAO;AAAA,EACP;AAAA,EACA;AAAA,EAEA,YAAY;AAAA,IACV;AAAA,IACA;AAAA,IACA;AAAA,EACF,GAIG;AACD,UAAM,eAAe,EAAE,SAAS,QAAQ,WAAW,CAAC,CAAC;AACrD,SAAK,SAAS;AACd,SAAK,aAAa;AAAA,EACpB;AACF;;;ACtCO,IAAM,mBACX,CAAC,QAAgB,gBACjB,OAAO,QAAqC;AAC1C,MAAI,IAAI,WAAW,QAAQ;AACzB,UAAM,aAAa,eAAe;AAClC,UAAM,IAAI,WAAW;AAAA,MACnB,SAAS,iCAAiC,MAAM,SAAS,IAAI,MAAM,UAAU,IAAI,GAAG;AAAA,MACpF,QAAQ,MAAM,qBAAqB,GAAG;AAAA;AAAA,MACtC,YAAY,IAAI;AAAA,IAClB,CAAC;AAAA,EACH;AACA,SAAO;AACT;AAKK,IAAM,uBAAuB,CAClC,aAEA,SAAS,QAAQ,IAAI,cAAc,GAAG,SAAS,kBAAkB,IAC5D,SAAS,KAAK,IACf,SAAS,KAAK;;;AC5BpB,SAAS,gBAAgB,uBAAuB;","names":[]}
package/package.json ADDED
@@ -0,0 +1,35 @@
1
+ {
2
+ "name": "@pagopa/io-wallet-utils",
3
+ "version": "0.4.1",
4
+ "files": [
5
+ "dist"
6
+ ],
7
+ "license": "Apache-2.0",
8
+ "exports": {
9
+ ".": {
10
+ "import": "./dist/index.mjs",
11
+ "require": "./dist/index.js",
12
+ "types": "./dist/index.d.ts"
13
+ },
14
+ "./package.json": "./package.json"
15
+ },
16
+ "homepage": "https://github.com/pagopa/io-wallet-sdk/tree/main/packages/utils",
17
+ "repository": {
18
+ "type": "git",
19
+ "url": "https://github.com/pagopa/io-wallet-sdk",
20
+ "directory": "packages/utils"
21
+ },
22
+ "publishConfig": {
23
+ "access": "public"
24
+ },
25
+ "dependencies": {
26
+ "@openid4vc/utils": "0.3.0-alpha-20250714110838"
27
+ },
28
+ "scripts": {
29
+ "build": "tsup src/index.ts --format cjs,esm --dts --clean --sourcemap",
30
+ "test": "vitest"
31
+ },
32
+ "main": "./dist/index.js",
33
+ "module": "./dist/index.mjs",
34
+ "types": "./dist/index.d.ts"
35
+ }