@priceos/next 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/package.json +7 -7
- package/src/index.ts +0 -167
package/package.json
CHANGED
|
@@ -1,16 +1,16 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@priceos/next",
|
|
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": [
|
package/src/index.ts
DELETED
|
@@ -1,167 +0,0 @@
|
|
|
1
|
-
import { PriceOS, PriceOSError } from "priceos";
|
|
2
|
-
|
|
3
|
-
export type PriceOSIdentify = (
|
|
4
|
-
request: Request
|
|
5
|
-
) => string | null | undefined | Promise<string | null | undefined>;
|
|
6
|
-
|
|
7
|
-
export type PriceOSPagesRequest = {
|
|
8
|
-
method?: string;
|
|
9
|
-
headers?: Record<string, string | string[] | undefined>;
|
|
10
|
-
url?: string;
|
|
11
|
-
socket?: { encrypted?: boolean };
|
|
12
|
-
};
|
|
13
|
-
|
|
14
|
-
export type PriceOSPagesResponse = {
|
|
15
|
-
status?: (code: number) => PriceOSPagesResponse;
|
|
16
|
-
setHeader?: (name: string, value: string | string[]) => void;
|
|
17
|
-
end?: (body?: string) => void;
|
|
18
|
-
statusCode?: number;
|
|
19
|
-
};
|
|
20
|
-
|
|
21
|
-
export type PriceOSPagesIdentify = (
|
|
22
|
-
request: PriceOSPagesRequest
|
|
23
|
-
) => string | null | undefined | Promise<string | null | undefined>;
|
|
24
|
-
|
|
25
|
-
export type PriceOSNextHandlerOptions = {
|
|
26
|
-
apiKey?: string;
|
|
27
|
-
baseUrl?: string;
|
|
28
|
-
basePath?: string;
|
|
29
|
-
identify?: PriceOSIdentify;
|
|
30
|
-
};
|
|
31
|
-
|
|
32
|
-
export type PriceOSPagesHandlerOptions = Omit<PriceOSNextHandlerOptions, "identify"> & {
|
|
33
|
-
identify?: PriceOSPagesIdentify;
|
|
34
|
-
};
|
|
35
|
-
|
|
36
|
-
const DEFAULT_BASE_PATH = "/api/priceos";
|
|
37
|
-
|
|
38
|
-
const normalizePath = (value: string) => value.replace(/^\/+/, "");
|
|
39
|
-
|
|
40
|
-
const jsonResponse = (status: number, body: unknown) =>
|
|
41
|
-
new Response(JSON.stringify(body), {
|
|
42
|
-
status,
|
|
43
|
-
headers: { "content-type": "application/json" },
|
|
44
|
-
});
|
|
45
|
-
|
|
46
|
-
export function priceosHandler(options: PriceOSNextHandlerOptions = {}) {
|
|
47
|
-
const envApiKey = (globalThis as { process?: { env?: Record<string, string | undefined> } })
|
|
48
|
-
.process?.env?.PRICEOS_API_KEY;
|
|
49
|
-
const apiKey = options.apiKey ?? envApiKey;
|
|
50
|
-
const identify = options.identify;
|
|
51
|
-
const baseUrl = (options.baseUrl ?? "https://api.priceos.com").replace(/\/$/, "");
|
|
52
|
-
const basePath = (options.basePath ?? DEFAULT_BASE_PATH).replace(/\/+$/, "");
|
|
53
|
-
|
|
54
|
-
return async (request: Request) => {
|
|
55
|
-
if (!apiKey) {
|
|
56
|
-
return new Response("Missing PRICEOS_API_KEY.", { status: 500 });
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
if (!identify) {
|
|
60
|
-
return jsonResponse(500, { error: "Missing identify function" });
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
let path = "";
|
|
64
|
-
const url = new URL(request.url);
|
|
65
|
-
const pathname = url.pathname;
|
|
66
|
-
if (pathname.startsWith(basePath)) {
|
|
67
|
-
path = normalizePath(pathname.slice(basePath.length));
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
if (path !== "v1/customer") {
|
|
71
|
-
return new Response("Not found.", { status: 404 });
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
const client = new PriceOS(apiKey, { baseUrl });
|
|
75
|
-
const method = request.method.toUpperCase();
|
|
76
|
-
if (method !== "GET") {
|
|
77
|
-
return new Response("Method not allowed.", { status: 405 });
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
if (url.searchParams.has("customerId")) {
|
|
81
|
-
return jsonResponse(400, { error: "customerId must be set via identify" });
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
let customerId: string | null | undefined;
|
|
85
|
-
try {
|
|
86
|
-
customerId = await identify(request);
|
|
87
|
-
} catch (error) {
|
|
88
|
-
return jsonResponse(500, { error: "Failed to identify customer" });
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
if (typeof customerId !== "string" || !customerId) {
|
|
92
|
-
return jsonResponse(401, { error: "Customer not identified" });
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
try {
|
|
96
|
-
const data = await client.getCustomer(customerId);
|
|
97
|
-
return jsonResponse(200, data);
|
|
98
|
-
} catch (error) {
|
|
99
|
-
if (error instanceof PriceOSError) {
|
|
100
|
-
return jsonResponse(error.status ?? 500, { error: error.message });
|
|
101
|
-
}
|
|
102
|
-
return jsonResponse(500, { error: "Request failed" });
|
|
103
|
-
}
|
|
104
|
-
};
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
const getHeaderValue = (
|
|
108
|
-
headers: Record<string, string | string[] | undefined> | undefined,
|
|
109
|
-
key: string
|
|
110
|
-
) => {
|
|
111
|
-
const value = headers?.[key];
|
|
112
|
-
if (Array.isArray(value)) return value.join(",");
|
|
113
|
-
return value;
|
|
114
|
-
};
|
|
115
|
-
|
|
116
|
-
const toHeaders = (headers: Record<string, string | string[] | undefined> | undefined) => {
|
|
117
|
-
const result = new Headers();
|
|
118
|
-
if (!headers) return result;
|
|
119
|
-
for (const [key, value] of Object.entries(headers)) {
|
|
120
|
-
if (typeof value === "string") result.set(key, value);
|
|
121
|
-
if (Array.isArray(value)) result.set(key, value.join(","));
|
|
122
|
-
}
|
|
123
|
-
return result;
|
|
124
|
-
};
|
|
125
|
-
|
|
126
|
-
const buildRequest = (req: PriceOSPagesRequest) => {
|
|
127
|
-
const headers = req.headers ?? {};
|
|
128
|
-
const forwardedProto = getHeaderValue(headers, "x-forwarded-proto");
|
|
129
|
-
const protocol =
|
|
130
|
-
forwardedProto?.split(",")[0] ?? (req.socket?.encrypted ? "https" : "http");
|
|
131
|
-
const host =
|
|
132
|
-
getHeaderValue(headers, "x-forwarded-host") ??
|
|
133
|
-
getHeaderValue(headers, "host") ??
|
|
134
|
-
"localhost";
|
|
135
|
-
const url = new URL(req.url ?? "/", `${protocol}://${host}`);
|
|
136
|
-
return new Request(url, { method: req.method ?? "GET", headers: toHeaders(headers) });
|
|
137
|
-
};
|
|
138
|
-
|
|
139
|
-
const sendResponse = async (res: PriceOSPagesResponse, response: Response) => {
|
|
140
|
-
if (typeof res.status === "function") {
|
|
141
|
-
res.status(response.status);
|
|
142
|
-
} else {
|
|
143
|
-
res.statusCode = response.status;
|
|
144
|
-
}
|
|
145
|
-
|
|
146
|
-
if (typeof res.setHeader === "function") {
|
|
147
|
-
response.headers.forEach((value, key) => {
|
|
148
|
-
res.setHeader?.(key, value);
|
|
149
|
-
});
|
|
150
|
-
}
|
|
151
|
-
|
|
152
|
-
const body = await response.text();
|
|
153
|
-
res.end?.(body || undefined);
|
|
154
|
-
};
|
|
155
|
-
|
|
156
|
-
export function priceosPagesHandler(options: PriceOSPagesHandlerOptions = {}) {
|
|
157
|
-
return async (req: PriceOSPagesRequest, res: PriceOSPagesResponse) => {
|
|
158
|
-
const identify = options.identify;
|
|
159
|
-
const handler = priceosHandler({
|
|
160
|
-
...options,
|
|
161
|
-
identify: identify ? () => identify(req) : undefined,
|
|
162
|
-
});
|
|
163
|
-
const request = buildRequest(req);
|
|
164
|
-
const response = await handler(request);
|
|
165
|
-
await sendResponse(res, response);
|
|
166
|
-
};
|
|
167
|
-
}
|