@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 +24 -23
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +2 -3
- package/dist/index.d.ts +2 -3
- package/dist/index.js +14 -23
- package/dist/index.js.map +1 -1
- package/package.json +10 -8
- package/src/index.ts +0 -69
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
|
|
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
|
|
44
|
-
const
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
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 = {
|
package/dist/index.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/index.ts"],"sourcesContent":["import {
|
|
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
|
-
|
|
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
|
-
|
|
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
|
|
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
|
|
20
|
-
const
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
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 {
|
|
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.
|
|
3
|
+
"version": "0.0.4",
|
|
4
4
|
"private": false,
|
|
5
5
|
"type": "module",
|
|
6
|
-
"main": "
|
|
7
|
-
"module": "
|
|
8
|
-
"types": "
|
|
6
|
+
"main": "dist/index.cjs",
|
|
7
|
+
"module": "dist/index.js",
|
|
8
|
+
"types": "dist/index.d.ts",
|
|
9
9
|
"exports": {
|
|
10
10
|
".": {
|
|
11
|
-
"types": "./
|
|
12
|
-
"import": "./
|
|
13
|
-
"require": "./
|
|
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
|
-
}
|