@priceos/react 0.0.2 → 0.0.3
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/package.json +7 -7
- package/src/index.ts +0 -69
package/package.json
CHANGED
|
@@ -1,16 +1,16 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@priceos/react",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.3",
|
|
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": [
|
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
|
-
}
|