@priceos/react 0.0.2

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.cjs ADDED
@@ -0,0 +1,72 @@
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
+ useCustomer: () => useCustomer
24
+ });
25
+ module.exports = __toCommonJS(index_exports);
26
+ var import_react = require("react");
27
+ var normalizeBaseUrl = (baseUrl) => baseUrl.replace(/\/$/, "");
28
+ var buildUrl = (baseUrl) => {
29
+ const normalizedBase = normalizeBaseUrl(baseUrl);
30
+ return `${normalizedBase}/v1/customer`;
31
+ };
32
+ var requestCustomer = async (fetchFn, url) => {
33
+ const response = await fetchFn(url);
34
+ const text = await response.text();
35
+ const data = text ? JSON.parse(text) : null;
36
+ if (!response.ok) {
37
+ const message = data && typeof data === "object" && "error" in data ? String(data.error) : response.statusText;
38
+ throw new Error(message || "Request failed");
39
+ }
40
+ return data;
41
+ };
42
+ function useCustomer(options = {}) {
43
+ const enabled = options.enabled ?? true;
44
+ const baseUrl = options.baseUrl ?? "/api/priceos";
45
+ const fetchFn = options.fetch ?? fetch;
46
+ const [customer, setCustomer] = (0, import_react.useState)(null);
47
+ const [error, setError] = (0, import_react.useState)(null);
48
+ const [loading, setLoading] = (0, import_react.useState)(false);
49
+ const refetch = async () => {
50
+ setLoading(true);
51
+ setError(null);
52
+ try {
53
+ const result = await requestCustomer(fetchFn, buildUrl(baseUrl));
54
+ setCustomer(result);
55
+ } catch (err) {
56
+ setError(err instanceof Error ? err : new Error("Request failed"));
57
+ setCustomer(null);
58
+ } finally {
59
+ setLoading(false);
60
+ }
61
+ };
62
+ (0, import_react.useEffect)(() => {
63
+ if (!enabled) return;
64
+ void refetch();
65
+ }, [enabled, baseUrl, fetchFn]);
66
+ return { customer, error, loading, refetch };
67
+ }
68
+ // Annotate the CommonJS export names for ESM import in node:
69
+ 0 && (module.exports = {
70
+ useCustomer
71
+ });
72
+ //# sourceMappingURL=index.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/index.ts"],"sourcesContent":["import { useEffect, useState } from \"react\";\nimport type { GetCustomerResponse } from \"priceos\";\n\nexport type UseCustomerOptions = {\n baseUrl?: string;\n enabled?: boolean;\n fetch?: typeof fetch;\n};\n\nexport type UseCustomerResult = {\n customer: GetCustomerResponse | null;\n error: Error | null;\n loading: boolean;\n refetch: () => Promise<void>;\n};\n\nconst normalizeBaseUrl = (baseUrl: string) => baseUrl.replace(/\\/$/, \"\");\n\nconst buildUrl = (baseUrl: string) => {\n const normalizedBase = normalizeBaseUrl(baseUrl);\n return `${normalizedBase}/v1/customer`;\n};\n\nconst requestCustomer = async (\n fetchFn: typeof fetch,\n url: string\n): Promise<GetCustomerResponse | null> => {\n const response = await fetchFn(url);\n const text = await response.text();\n const data = text ? (JSON.parse(text) as unknown) : null;\n if (!response.ok) {\n const message =\n data && typeof data === \"object\" && \"error\" in data\n ? String((data as { error?: unknown }).error)\n : response.statusText;\n throw new Error(message || \"Request failed\");\n }\n return data as GetCustomerResponse | null;\n};\n\nexport function useCustomer(options: UseCustomerOptions = {}): UseCustomerResult {\n const enabled = options.enabled ?? true;\n const baseUrl = options.baseUrl ?? \"/api/priceos\";\n const fetchFn = options.fetch ?? fetch;\n const [customer, setCustomer] = useState<GetCustomerResponse | null>(null);\n const [error, setError] = useState<Error | null>(null);\n const [loading, setLoading] = useState(false);\n\n const refetch = async () => {\n setLoading(true);\n setError(null);\n try {\n const result = await requestCustomer(fetchFn, buildUrl(baseUrl));\n setCustomer(result);\n } catch (err) {\n setError(err instanceof Error ? err : new Error(\"Request failed\"));\n setCustomer(null);\n } finally {\n setLoading(false);\n }\n };\n\n useEffect(() => {\n if (!enabled) return;\n void refetch();\n }, [enabled, baseUrl, fetchFn]);\n\n return { customer, error, loading, refetch };\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,mBAAoC;AAgBpC,IAAM,mBAAmB,CAAC,YAAoB,QAAQ,QAAQ,OAAO,EAAE;AAEvE,IAAM,WAAW,CAAC,YAAoB;AACpC,QAAM,iBAAiB,iBAAiB,OAAO;AAC/C,SAAO,GAAG,cAAc;AAC1B;AAEA,IAAM,kBAAkB,OACtB,SACA,QACwC;AACxC,QAAM,WAAW,MAAM,QAAQ,GAAG;AAClC,QAAM,OAAO,MAAM,SAAS,KAAK;AACjC,QAAM,OAAO,OAAQ,KAAK,MAAM,IAAI,IAAgB;AACpD,MAAI,CAAC,SAAS,IAAI;AAChB,UAAM,UACJ,QAAQ,OAAO,SAAS,YAAY,WAAW,OAC3C,OAAQ,KAA6B,KAAK,IAC1C,SAAS;AACf,UAAM,IAAI,MAAM,WAAW,gBAAgB;AAAA,EAC7C;AACA,SAAO;AACT;AAEO,SAAS,YAAY,UAA8B,CAAC,GAAsB;AAC/E,QAAM,UAAU,QAAQ,WAAW;AACnC,QAAM,UAAU,QAAQ,WAAW;AACnC,QAAM,UAAU,QAAQ,SAAS;AACjC,QAAM,CAAC,UAAU,WAAW,QAAI,uBAAqC,IAAI;AACzE,QAAM,CAAC,OAAO,QAAQ,QAAI,uBAAuB,IAAI;AACrD,QAAM,CAAC,SAAS,UAAU,QAAI,uBAAS,KAAK;AAE5C,QAAM,UAAU,YAAY;AAC1B,eAAW,IAAI;AACf,aAAS,IAAI;AACb,QAAI;AACF,YAAM,SAAS,MAAM,gBAAgB,SAAS,SAAS,OAAO,CAAC;AAC/D,kBAAY,MAAM;AAAA,IACpB,SAAS,KAAK;AACZ,eAAS,eAAe,QAAQ,MAAM,IAAI,MAAM,gBAAgB,CAAC;AACjE,kBAAY,IAAI;AAAA,IAClB,UAAE;AACA,iBAAW,KAAK;AAAA,IAClB;AAAA,EACF;AAEA,8BAAU,MAAM;AACd,QAAI,CAAC,QAAS;AACd,SAAK,QAAQ;AAAA,EACf,GAAG,CAAC,SAAS,SAAS,OAAO,CAAC;AAE9B,SAAO,EAAE,UAAU,OAAO,SAAS,QAAQ;AAC7C;","names":[]}
@@ -0,0 +1,16 @@
1
+ import { GetCustomerResponse } from 'priceos';
2
+
3
+ type UseCustomerOptions = {
4
+ baseUrl?: string;
5
+ enabled?: boolean;
6
+ fetch?: typeof fetch;
7
+ };
8
+ type UseCustomerResult = {
9
+ customer: GetCustomerResponse | null;
10
+ error: Error | null;
11
+ loading: boolean;
12
+ refetch: () => Promise<void>;
13
+ };
14
+ declare function useCustomer(options?: UseCustomerOptions): UseCustomerResult;
15
+
16
+ export { type UseCustomerOptions, type UseCustomerResult, useCustomer };
@@ -0,0 +1,16 @@
1
+ import { GetCustomerResponse } from 'priceos';
2
+
3
+ type UseCustomerOptions = {
4
+ baseUrl?: string;
5
+ enabled?: boolean;
6
+ fetch?: typeof fetch;
7
+ };
8
+ type UseCustomerResult = {
9
+ customer: GetCustomerResponse | null;
10
+ error: Error | null;
11
+ loading: boolean;
12
+ refetch: () => Promise<void>;
13
+ };
14
+ declare function useCustomer(options?: UseCustomerOptions): UseCustomerResult;
15
+
16
+ export { type UseCustomerOptions, type UseCustomerResult, useCustomer };
package/dist/index.js ADDED
@@ -0,0 +1,47 @@
1
+ // src/index.ts
2
+ import { useEffect, useState } from "react";
3
+ var normalizeBaseUrl = (baseUrl) => baseUrl.replace(/\/$/, "");
4
+ var buildUrl = (baseUrl) => {
5
+ const normalizedBase = normalizeBaseUrl(baseUrl);
6
+ return `${normalizedBase}/v1/customer`;
7
+ };
8
+ var requestCustomer = async (fetchFn, url) => {
9
+ const response = await fetchFn(url);
10
+ const text = await response.text();
11
+ const data = text ? JSON.parse(text) : null;
12
+ if (!response.ok) {
13
+ const message = data && typeof data === "object" && "error" in data ? String(data.error) : response.statusText;
14
+ throw new Error(message || "Request failed");
15
+ }
16
+ return data;
17
+ };
18
+ function useCustomer(options = {}) {
19
+ const enabled = options.enabled ?? true;
20
+ const baseUrl = options.baseUrl ?? "/api/priceos";
21
+ const fetchFn = options.fetch ?? fetch;
22
+ const [customer, setCustomer] = useState(null);
23
+ const [error, setError] = useState(null);
24
+ const [loading, setLoading] = useState(false);
25
+ const refetch = async () => {
26
+ setLoading(true);
27
+ setError(null);
28
+ try {
29
+ const result = await requestCustomer(fetchFn, buildUrl(baseUrl));
30
+ setCustomer(result);
31
+ } catch (err) {
32
+ setError(err instanceof Error ? err : new Error("Request failed"));
33
+ setCustomer(null);
34
+ } finally {
35
+ setLoading(false);
36
+ }
37
+ };
38
+ useEffect(() => {
39
+ if (!enabled) return;
40
+ void refetch();
41
+ }, [enabled, baseUrl, fetchFn]);
42
+ return { customer, error, loading, refetch };
43
+ }
44
+ export {
45
+ useCustomer
46
+ };
47
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/index.ts"],"sourcesContent":["import { useEffect, useState } from \"react\";\nimport type { GetCustomerResponse } from \"priceos\";\n\nexport type UseCustomerOptions = {\n baseUrl?: string;\n enabled?: boolean;\n fetch?: typeof fetch;\n};\n\nexport type UseCustomerResult = {\n customer: GetCustomerResponse | null;\n error: Error | null;\n loading: boolean;\n refetch: () => Promise<void>;\n};\n\nconst normalizeBaseUrl = (baseUrl: string) => baseUrl.replace(/\\/$/, \"\");\n\nconst buildUrl = (baseUrl: string) => {\n const normalizedBase = normalizeBaseUrl(baseUrl);\n return `${normalizedBase}/v1/customer`;\n};\n\nconst requestCustomer = async (\n fetchFn: typeof fetch,\n url: string\n): Promise<GetCustomerResponse | null> => {\n const response = await fetchFn(url);\n const text = await response.text();\n const data = text ? (JSON.parse(text) as unknown) : null;\n if (!response.ok) {\n const message =\n data && typeof data === \"object\" && \"error\" in data\n ? String((data as { error?: unknown }).error)\n : response.statusText;\n throw new Error(message || \"Request failed\");\n }\n return data as GetCustomerResponse | null;\n};\n\nexport function useCustomer(options: UseCustomerOptions = {}): UseCustomerResult {\n const enabled = options.enabled ?? true;\n const baseUrl = options.baseUrl ?? \"/api/priceos\";\n const fetchFn = options.fetch ?? fetch;\n const [customer, setCustomer] = useState<GetCustomerResponse | null>(null);\n const [error, setError] = useState<Error | null>(null);\n const [loading, setLoading] = useState(false);\n\n const refetch = async () => {\n setLoading(true);\n setError(null);\n try {\n const result = await requestCustomer(fetchFn, buildUrl(baseUrl));\n setCustomer(result);\n } catch (err) {\n setError(err instanceof Error ? err : new Error(\"Request failed\"));\n setCustomer(null);\n } finally {\n setLoading(false);\n }\n };\n\n useEffect(() => {\n if (!enabled) return;\n void refetch();\n }, [enabled, baseUrl, fetchFn]);\n\n return { customer, error, loading, refetch };\n}\n"],"mappings":";AAAA,SAAS,WAAW,gBAAgB;AAgBpC,IAAM,mBAAmB,CAAC,YAAoB,QAAQ,QAAQ,OAAO,EAAE;AAEvE,IAAM,WAAW,CAAC,YAAoB;AACpC,QAAM,iBAAiB,iBAAiB,OAAO;AAC/C,SAAO,GAAG,cAAc;AAC1B;AAEA,IAAM,kBAAkB,OACtB,SACA,QACwC;AACxC,QAAM,WAAW,MAAM,QAAQ,GAAG;AAClC,QAAM,OAAO,MAAM,SAAS,KAAK;AACjC,QAAM,OAAO,OAAQ,KAAK,MAAM,IAAI,IAAgB;AACpD,MAAI,CAAC,SAAS,IAAI;AAChB,UAAM,UACJ,QAAQ,OAAO,SAAS,YAAY,WAAW,OAC3C,OAAQ,KAA6B,KAAK,IAC1C,SAAS;AACf,UAAM,IAAI,MAAM,WAAW,gBAAgB;AAAA,EAC7C;AACA,SAAO;AACT;AAEO,SAAS,YAAY,UAA8B,CAAC,GAAsB;AAC/E,QAAM,UAAU,QAAQ,WAAW;AACnC,QAAM,UAAU,QAAQ,WAAW;AACnC,QAAM,UAAU,QAAQ,SAAS;AACjC,QAAM,CAAC,UAAU,WAAW,IAAI,SAAqC,IAAI;AACzE,QAAM,CAAC,OAAO,QAAQ,IAAI,SAAuB,IAAI;AACrD,QAAM,CAAC,SAAS,UAAU,IAAI,SAAS,KAAK;AAE5C,QAAM,UAAU,YAAY;AAC1B,eAAW,IAAI;AACf,aAAS,IAAI;AACb,QAAI;AACF,YAAM,SAAS,MAAM,gBAAgB,SAAS,SAAS,OAAO,CAAC;AAC/D,kBAAY,MAAM;AAAA,IACpB,SAAS,KAAK;AACZ,eAAS,eAAe,QAAQ,MAAM,IAAI,MAAM,gBAAgB,CAAC;AACjE,kBAAY,IAAI;AAAA,IAClB,UAAE;AACA,iBAAW,KAAK;AAAA,IAClB;AAAA,EACF;AAEA,YAAU,MAAM;AACd,QAAI,CAAC,QAAS;AACd,SAAK,QAAQ;AAAA,EACf,GAAG,CAAC,SAAS,SAAS,OAAO,CAAC;AAE9B,SAAO,EAAE,UAAU,OAAO,SAAS,QAAQ;AAC7C;","names":[]}
package/package.json ADDED
@@ -0,0 +1,36 @@
1
+ {
2
+ "name": "@priceos/react",
3
+ "version": "0.0.2",
4
+ "private": false,
5
+ "type": "module",
6
+ "main": "src/index.ts",
7
+ "module": "src/index.ts",
8
+ "types": "src/index.ts",
9
+ "exports": {
10
+ ".": {
11
+ "types": "./src/index.ts",
12
+ "import": "./src/index.ts",
13
+ "require": "./src/index.ts"
14
+ }
15
+ },
16
+ "files": [
17
+ "dist"
18
+ ],
19
+ "scripts": {
20
+ "build": "tsup",
21
+ "dev": "tsup src/index.ts --format esm,cjs --dts --watch",
22
+ "typecheck": "tsc -p tsconfig.json --noEmit"
23
+ },
24
+ "dependencies": {
25
+ "priceos": "^0.0.5"
26
+ },
27
+ "peerDependencies": {
28
+ "react": ">=18"
29
+ },
30
+ "devDependencies": {
31
+ "@types/node": "^25.0.10",
32
+ "@types/react": "^19.0.0",
33
+ "tsup": "^8.0.0",
34
+ "typescript": "^5.0.0"
35
+ }
36
+ }
package/src/index.ts ADDED
@@ -0,0 +1,69 @@
1
+ import { useEffect, useState } from "react";
2
+ import type { GetCustomerResponse } from "priceos";
3
+
4
+ export type UseCustomerOptions = {
5
+ baseUrl?: string;
6
+ enabled?: boolean;
7
+ fetch?: typeof fetch;
8
+ };
9
+
10
+ export type UseCustomerResult = {
11
+ customer: GetCustomerResponse | null;
12
+ error: Error | null;
13
+ loading: boolean;
14
+ refetch: () => Promise<void>;
15
+ };
16
+
17
+ const normalizeBaseUrl = (baseUrl: string) => baseUrl.replace(/\/$/, "");
18
+
19
+ const buildUrl = (baseUrl: string) => {
20
+ const normalizedBase = normalizeBaseUrl(baseUrl);
21
+ return `${normalizedBase}/v1/customer`;
22
+ };
23
+
24
+ const requestCustomer = async (
25
+ fetchFn: typeof fetch,
26
+ url: string
27
+ ): Promise<GetCustomerResponse | null> => {
28
+ const response = await fetchFn(url);
29
+ const text = await response.text();
30
+ const data = text ? (JSON.parse(text) as unknown) : null;
31
+ if (!response.ok) {
32
+ const message =
33
+ data && typeof data === "object" && "error" in data
34
+ ? String((data as { error?: unknown }).error)
35
+ : response.statusText;
36
+ throw new Error(message || "Request failed");
37
+ }
38
+ return data as GetCustomerResponse | null;
39
+ };
40
+
41
+ export function useCustomer(options: UseCustomerOptions = {}): UseCustomerResult {
42
+ const enabled = options.enabled ?? true;
43
+ const baseUrl = options.baseUrl ?? "/api/priceos";
44
+ const fetchFn = options.fetch ?? fetch;
45
+ const [customer, setCustomer] = useState<GetCustomerResponse | null>(null);
46
+ const [error, setError] = useState<Error | null>(null);
47
+ const [loading, setLoading] = useState(false);
48
+
49
+ const refetch = async () => {
50
+ setLoading(true);
51
+ setError(null);
52
+ try {
53
+ const result = await requestCustomer(fetchFn, buildUrl(baseUrl));
54
+ setCustomer(result);
55
+ } catch (err) {
56
+ setError(err instanceof Error ? err : new Error("Request failed"));
57
+ setCustomer(null);
58
+ } finally {
59
+ setLoading(false);
60
+ }
61
+ };
62
+
63
+ useEffect(() => {
64
+ if (!enabled) return;
65
+ void refetch();
66
+ }, [enabled, baseUrl, fetchFn]);
67
+
68
+ return { customer, error, loading, refetch };
69
+ }