@priceos/react 0.0.2 → 0.0.4

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 CHANGED
@@ -1,7 +1,9 @@
1
1
  "use strict";
2
+ var __create = Object.create;
2
3
  var __defProp = Object.defineProperty;
3
4
  var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
5
  var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
5
7
  var __hasOwnProp = Object.prototype.hasOwnProperty;
6
8
  var __export = (target, all) => {
7
9
  for (var name in all)
@@ -15,6 +17,14 @@ var __copyProps = (to, from, except, desc) => {
15
17
  }
16
18
  return to;
17
19
  };
20
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
+ // If the importer is in node compatibility mode or this is not an ESM
22
+ // file that has been converted to a CommonJS file using a Babel-
23
+ // compatible transform (i.e. "__esModule" has not been set), then set
24
+ // "default" to the CommonJS "module.exports" for node compatibility.
25
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
+ mod
27
+ ));
18
28
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
29
 
20
30
  // src/index.ts
@@ -23,7 +33,8 @@ __export(index_exports, {
23
33
  useCustomer: () => useCustomer
24
34
  });
25
35
  module.exports = __toCommonJS(index_exports);
26
- var import_react = require("react");
36
+ var import_swr = __toESM(require("swr"), 1);
37
+ var DEFAULT_BASE_URL = "/api/priceos";
27
38
  var normalizeBaseUrl = (baseUrl) => baseUrl.replace(/\/$/, "");
28
39
  var buildUrl = (baseUrl) => {
29
40
  const normalizedBase = normalizeBaseUrl(baseUrl);
@@ -40,30 +51,20 @@ var requestCustomer = async (fetchFn, url) => {
40
51
  return data;
41
52
  };
42
53
  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);
54
+ const fetcher = (url) => requestCustomer(fetch, url);
55
+ const { data, error, isLoading, mutate } = (0, import_swr.default)(
56
+ buildUrl(DEFAULT_BASE_URL),
57
+ fetcher,
58
+ options.swr
59
+ );
60
+ return {
61
+ customer: data ?? null,
62
+ error,
63
+ loading: isLoading,
64
+ refetch: async () => {
65
+ await mutate();
60
66
  }
61
67
  };
62
- (0, import_react.useEffect)(() => {
63
- if (!enabled) return;
64
- void refetch();
65
- }, [enabled, baseUrl, fetchFn]);
66
- return { customer, error, loading, refetch };
67
68
  }
68
69
  // Annotate the CommonJS export names for ESM import in node:
69
70
  0 && (module.exports = {
@@ -1 +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":[]}
1
+ {"version":3,"sources":["../src/index.ts"],"sourcesContent":["import useSWR, { type SWRConfiguration } from \"swr\";\nimport type { GetCustomerResponse } from \"priceos\";\n\nexport type UseCustomerOptions = {\n swr?: SWRConfiguration<GetCustomerResponse | null, Error>;\n};\n\nexport type UseCustomerResult = {\n customer: GetCustomerResponse | null;\n error: Error | null;\n loading: boolean;\n refetch: () => Promise<void>;\n};\n\nconst DEFAULT_BASE_URL = \"/api/priceos\";\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 fetcher = (url: string) => requestCustomer(fetch, url);\n const { data, error, isLoading, mutate } = useSWR(\n buildUrl(DEFAULT_BASE_URL),\n fetcher,\n options.swr\n );\n\n return {\n customer: data ?? null,\n error,\n loading: isLoading,\n refetch: async () => {\n await mutate();\n },\n };\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,iBAA8C;AAc9C,IAAM,mBAAmB;AAEzB,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,CAAC,QAAgB,gBAAgB,OAAO,GAAG;AAC3D,QAAM,EAAE,MAAM,OAAO,WAAW,OAAO,QAAI,WAAAA;AAAA,IACzC,SAAS,gBAAgB;AAAA,IACzB;AAAA,IACA,QAAQ;AAAA,EACV;AAEA,SAAO;AAAA,IACL,UAAU,QAAQ;AAAA,IAClB;AAAA,IACA,SAAS;AAAA,IACT,SAAS,YAAY;AACnB,YAAM,OAAO;AAAA,IACf;AAAA,EACF;AACF;","names":["useSWR"]}
package/dist/index.d.cts CHANGED
@@ -1,9 +1,8 @@
1
+ import { SWRConfiguration } from 'swr';
1
2
  import { GetCustomerResponse } from 'priceos';
2
3
 
3
4
  type UseCustomerOptions = {
4
- baseUrl?: string;
5
- enabled?: boolean;
6
- fetch?: typeof fetch;
5
+ swr?: SWRConfiguration<GetCustomerResponse | null, Error>;
7
6
  };
8
7
  type UseCustomerResult = {
9
8
  customer: GetCustomerResponse | null;
package/dist/index.d.ts CHANGED
@@ -1,9 +1,8 @@
1
+ import { SWRConfiguration } from 'swr';
1
2
  import { GetCustomerResponse } from 'priceos';
2
3
 
3
4
  type UseCustomerOptions = {
4
- baseUrl?: string;
5
- enabled?: boolean;
6
- fetch?: typeof fetch;
5
+ swr?: SWRConfiguration<GetCustomerResponse | null, Error>;
7
6
  };
8
7
  type UseCustomerResult = {
9
8
  customer: GetCustomerResponse | null;
package/dist/index.js CHANGED
@@ -1,5 +1,6 @@
1
1
  // src/index.ts
2
- import { useEffect, useState } from "react";
2
+ import useSWR from "swr";
3
+ var DEFAULT_BASE_URL = "/api/priceos";
3
4
  var normalizeBaseUrl = (baseUrl) => baseUrl.replace(/\/$/, "");
4
5
  var buildUrl = (baseUrl) => {
5
6
  const normalizedBase = normalizeBaseUrl(baseUrl);
@@ -16,30 +17,20 @@ var requestCustomer = async (fetchFn, url) => {
16
17
  return data;
17
18
  };
18
19
  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);
20
+ const fetcher = (url) => requestCustomer(fetch, url);
21
+ const { data, error, isLoading, mutate } = useSWR(
22
+ buildUrl(DEFAULT_BASE_URL),
23
+ fetcher,
24
+ options.swr
25
+ );
26
+ return {
27
+ customer: data ?? null,
28
+ error,
29
+ loading: isLoading,
30
+ refetch: async () => {
31
+ await mutate();
36
32
  }
37
33
  };
38
- useEffect(() => {
39
- if (!enabled) return;
40
- void refetch();
41
- }, [enabled, baseUrl, fetchFn]);
42
- return { customer, error, loading, refetch };
43
34
  }
44
35
  export {
45
36
  useCustomer
package/dist/index.js.map CHANGED
@@ -1 +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":[]}
1
+ {"version":3,"sources":["../src/index.ts"],"sourcesContent":["import useSWR, { type SWRConfiguration } from \"swr\";\nimport type { GetCustomerResponse } from \"priceos\";\n\nexport type UseCustomerOptions = {\n swr?: SWRConfiguration<GetCustomerResponse | null, Error>;\n};\n\nexport type UseCustomerResult = {\n customer: GetCustomerResponse | null;\n error: Error | null;\n loading: boolean;\n refetch: () => Promise<void>;\n};\n\nconst DEFAULT_BASE_URL = \"/api/priceos\";\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 fetcher = (url: string) => requestCustomer(fetch, url);\n const { data, error, isLoading, mutate } = useSWR(\n buildUrl(DEFAULT_BASE_URL),\n fetcher,\n options.swr\n );\n\n return {\n customer: data ?? null,\n error,\n loading: isLoading,\n refetch: async () => {\n await mutate();\n },\n };\n}\n"],"mappings":";AAAA,OAAO,YAAuC;AAc9C,IAAM,mBAAmB;AAEzB,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,CAAC,QAAgB,gBAAgB,OAAO,GAAG;AAC3D,QAAM,EAAE,MAAM,OAAO,WAAW,OAAO,IAAI;AAAA,IACzC,SAAS,gBAAgB;AAAA,IACzB;AAAA,IACA,QAAQ;AAAA,EACV;AAEA,SAAO;AAAA,IACL,UAAU,QAAQ;AAAA,IAClB;AAAA,IACA,SAAS;AAAA,IACT,SAAS,YAAY;AACnB,YAAM,OAAO;AAAA,IACf;AAAA,EACF;AACF;","names":[]}
package/package.json CHANGED
@@ -1,16 +1,16 @@
1
1
  {
2
2
  "name": "@priceos/react",
3
- "version": "0.0.2",
3
+ "version": "0.0.4",
4
4
  "private": false,
5
5
  "type": "module",
6
- "main": "src/index.ts",
7
- "module": "src/index.ts",
8
- "types": "src/index.ts",
6
+ "main": "dist/index.cjs",
7
+ "module": "dist/index.js",
8
+ "types": "dist/index.d.ts",
9
9
  "exports": {
10
10
  ".": {
11
- "types": "./src/index.ts",
12
- "import": "./src/index.ts",
13
- "require": "./src/index.ts"
11
+ "types": "./dist/index.d.ts",
12
+ "import": "./dist/index.js",
13
+ "require": "./dist/index.cjs"
14
14
  }
15
15
  },
16
16
  "files": [
@@ -25,11 +25,13 @@
25
25
  "priceos": "^0.0.5"
26
26
  },
27
27
  "peerDependencies": {
28
- "react": ">=18"
28
+ "react": ">=18",
29
+ "swr": ">=2"
29
30
  },
30
31
  "devDependencies": {
31
32
  "@types/node": "^25.0.10",
32
33
  "@types/react": "^19.0.0",
34
+ "swr": "^2.3.2",
33
35
  "tsup": "^8.0.0",
34
36
  "typescript": "^5.0.0"
35
37
  }
package/src/index.ts DELETED
@@ -1,69 +0,0 @@
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
- }