@priceos/next 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,137 @@
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
+ priceosHandler: () => priceosHandler,
24
+ priceosPagesHandler: () => priceosPagesHandler
25
+ });
26
+ module.exports = __toCommonJS(index_exports);
27
+ var import_priceos = require("priceos");
28
+ var DEFAULT_BASE_PATH = "/api/priceos";
29
+ var normalizePath = (value) => value.replace(/^\/+/, "");
30
+ var jsonResponse = (status, body) => new Response(JSON.stringify(body), {
31
+ status,
32
+ headers: { "content-type": "application/json" }
33
+ });
34
+ function priceosHandler(options = {}) {
35
+ const envApiKey = globalThis.process?.env?.PRICEOS_API_KEY;
36
+ const apiKey = options.apiKey ?? envApiKey;
37
+ const identify = options.identify;
38
+ const baseUrl = (options.baseUrl ?? "https://api.priceos.com").replace(/\/$/, "");
39
+ const basePath = (options.basePath ?? DEFAULT_BASE_PATH).replace(/\/+$/, "");
40
+ return async (request) => {
41
+ if (!apiKey) {
42
+ return new Response("Missing PRICEOS_API_KEY.", { status: 500 });
43
+ }
44
+ if (!identify) {
45
+ return jsonResponse(500, { error: "Missing identify function" });
46
+ }
47
+ let path = "";
48
+ const url = new URL(request.url);
49
+ const pathname = url.pathname;
50
+ if (pathname.startsWith(basePath)) {
51
+ path = normalizePath(pathname.slice(basePath.length));
52
+ }
53
+ if (path !== "v1/customer") {
54
+ return new Response("Not found.", { status: 404 });
55
+ }
56
+ const client = new import_priceos.PriceOS(apiKey, { baseUrl });
57
+ const method = request.method.toUpperCase();
58
+ if (method !== "GET") {
59
+ return new Response("Method not allowed.", { status: 405 });
60
+ }
61
+ if (url.searchParams.has("customerId")) {
62
+ return jsonResponse(400, { error: "customerId must be set via identify" });
63
+ }
64
+ let customerId;
65
+ try {
66
+ customerId = await identify(request);
67
+ } catch (error) {
68
+ return jsonResponse(500, { error: "Failed to identify customer" });
69
+ }
70
+ if (typeof customerId !== "string" || !customerId) {
71
+ return jsonResponse(401, { error: "Customer not identified" });
72
+ }
73
+ try {
74
+ const data = await client.getCustomer(customerId);
75
+ return jsonResponse(200, data);
76
+ } catch (error) {
77
+ if (error instanceof import_priceos.PriceOSError) {
78
+ return jsonResponse(error.status ?? 500, { error: error.message });
79
+ }
80
+ return jsonResponse(500, { error: "Request failed" });
81
+ }
82
+ };
83
+ }
84
+ var getHeaderValue = (headers, key) => {
85
+ const value = headers?.[key];
86
+ if (Array.isArray(value)) return value.join(",");
87
+ return value;
88
+ };
89
+ var toHeaders = (headers) => {
90
+ const result = new Headers();
91
+ if (!headers) return result;
92
+ for (const [key, value] of Object.entries(headers)) {
93
+ if (typeof value === "string") result.set(key, value);
94
+ if (Array.isArray(value)) result.set(key, value.join(","));
95
+ }
96
+ return result;
97
+ };
98
+ var buildRequest = (req) => {
99
+ const headers = req.headers ?? {};
100
+ const forwardedProto = getHeaderValue(headers, "x-forwarded-proto");
101
+ const protocol = forwardedProto?.split(",")[0] ?? (req.socket?.encrypted ? "https" : "http");
102
+ const host = getHeaderValue(headers, "x-forwarded-host") ?? getHeaderValue(headers, "host") ?? "localhost";
103
+ const url = new URL(req.url ?? "/", `${protocol}://${host}`);
104
+ return new Request(url, { method: req.method ?? "GET", headers: toHeaders(headers) });
105
+ };
106
+ var sendResponse = async (res, response) => {
107
+ if (typeof res.status === "function") {
108
+ res.status(response.status);
109
+ } else {
110
+ res.statusCode = response.status;
111
+ }
112
+ if (typeof res.setHeader === "function") {
113
+ response.headers.forEach((value, key) => {
114
+ res.setHeader?.(key, value);
115
+ });
116
+ }
117
+ const body = await response.text();
118
+ res.end?.(body || void 0);
119
+ };
120
+ function priceosPagesHandler(options = {}) {
121
+ return async (req, res) => {
122
+ const identify = options.identify;
123
+ const handler = priceosHandler({
124
+ ...options,
125
+ identify: identify ? () => identify(req) : void 0
126
+ });
127
+ const request = buildRequest(req);
128
+ const response = await handler(request);
129
+ await sendResponse(res, response);
130
+ };
131
+ }
132
+ // Annotate the CommonJS export names for ESM import in node:
133
+ 0 && (module.exports = {
134
+ priceosHandler,
135
+ priceosPagesHandler
136
+ });
137
+ //# sourceMappingURL=index.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/index.ts"],"sourcesContent":["import { PriceOS, PriceOSError } from \"priceos\";\n\nexport type PriceOSIdentify = (\n request: Request\n) => string | null | undefined | Promise<string | null | undefined>;\n\nexport type PriceOSPagesRequest = {\n method?: string;\n headers?: Record<string, string | string[] | undefined>;\n url?: string;\n socket?: { encrypted?: boolean };\n};\n\nexport type PriceOSPagesResponse = {\n status?: (code: number) => PriceOSPagesResponse;\n setHeader?: (name: string, value: string | string[]) => void;\n end?: (body?: string) => void;\n statusCode?: number;\n};\n\nexport type PriceOSPagesIdentify = (\n request: PriceOSPagesRequest\n) => string | null | undefined | Promise<string | null | undefined>;\n\nexport type PriceOSNextHandlerOptions = {\n apiKey?: string;\n baseUrl?: string;\n basePath?: string;\n identify?: PriceOSIdentify;\n};\n\nexport type PriceOSPagesHandlerOptions = Omit<PriceOSNextHandlerOptions, \"identify\"> & {\n identify?: PriceOSPagesIdentify;\n};\n\nconst DEFAULT_BASE_PATH = \"/api/priceos\";\n\nconst normalizePath = (value: string) => value.replace(/^\\/+/, \"\");\n\nconst jsonResponse = (status: number, body: unknown) =>\n new Response(JSON.stringify(body), {\n status,\n headers: { \"content-type\": \"application/json\" },\n });\n\nexport function priceosHandler(options: PriceOSNextHandlerOptions = {}) {\n const envApiKey = (globalThis as { process?: { env?: Record<string, string | undefined> } })\n .process?.env?.PRICEOS_API_KEY;\n const apiKey = options.apiKey ?? envApiKey;\n const identify = options.identify;\n const baseUrl = (options.baseUrl ?? \"https://api.priceos.com\").replace(/\\/$/, \"\");\n const basePath = (options.basePath ?? DEFAULT_BASE_PATH).replace(/\\/+$/, \"\");\n\n return async (request: Request) => {\n if (!apiKey) {\n return new Response(\"Missing PRICEOS_API_KEY.\", { status: 500 });\n }\n\n if (!identify) {\n return jsonResponse(500, { error: \"Missing identify function\" });\n }\n\n let path = \"\";\n const url = new URL(request.url);\n const pathname = url.pathname;\n if (pathname.startsWith(basePath)) {\n path = normalizePath(pathname.slice(basePath.length));\n }\n\n if (path !== \"v1/customer\") {\n return new Response(\"Not found.\", { status: 404 });\n }\n\n const client = new PriceOS(apiKey, { baseUrl });\n const method = request.method.toUpperCase();\n if (method !== \"GET\") {\n return new Response(\"Method not allowed.\", { status: 405 });\n }\n\n if (url.searchParams.has(\"customerId\")) {\n return jsonResponse(400, { error: \"customerId must be set via identify\" });\n }\n\n let customerId: string | null | undefined;\n try {\n customerId = await identify(request);\n } catch (error) {\n return jsonResponse(500, { error: \"Failed to identify customer\" });\n }\n\n if (typeof customerId !== \"string\" || !customerId) {\n return jsonResponse(401, { error: \"Customer not identified\" });\n }\n\n try {\n const data = await client.getCustomer(customerId);\n return jsonResponse(200, data);\n } catch (error) {\n if (error instanceof PriceOSError) {\n return jsonResponse(error.status ?? 500, { error: error.message });\n }\n return jsonResponse(500, { error: \"Request failed\" });\n }\n };\n}\n\nconst getHeaderValue = (\n headers: Record<string, string | string[] | undefined> | undefined,\n key: string\n) => {\n const value = headers?.[key];\n if (Array.isArray(value)) return value.join(\",\");\n return value;\n};\n\nconst toHeaders = (headers: Record<string, string | string[] | undefined> | undefined) => {\n const result = new Headers();\n if (!headers) return result;\n for (const [key, value] of Object.entries(headers)) {\n if (typeof value === \"string\") result.set(key, value);\n if (Array.isArray(value)) result.set(key, value.join(\",\"));\n }\n return result;\n};\n\nconst buildRequest = (req: PriceOSPagesRequest) => {\n const headers = req.headers ?? {};\n const forwardedProto = getHeaderValue(headers, \"x-forwarded-proto\");\n const protocol =\n forwardedProto?.split(\",\")[0] ?? (req.socket?.encrypted ? \"https\" : \"http\");\n const host =\n getHeaderValue(headers, \"x-forwarded-host\") ??\n getHeaderValue(headers, \"host\") ??\n \"localhost\";\n const url = new URL(req.url ?? \"/\", `${protocol}://${host}`);\n return new Request(url, { method: req.method ?? \"GET\", headers: toHeaders(headers) });\n};\n\nconst sendResponse = async (res: PriceOSPagesResponse, response: Response) => {\n if (typeof res.status === \"function\") {\n res.status(response.status);\n } else {\n res.statusCode = response.status;\n }\n\n if (typeof res.setHeader === \"function\") {\n response.headers.forEach((value, key) => {\n res.setHeader?.(key, value);\n });\n }\n\n const body = await response.text();\n res.end?.(body || undefined);\n};\n\nexport function priceosPagesHandler(options: PriceOSPagesHandlerOptions = {}) {\n return async (req: PriceOSPagesRequest, res: PriceOSPagesResponse) => {\n const identify = options.identify;\n const handler = priceosHandler({\n ...options,\n identify: identify ? () => identify(req) : undefined,\n });\n const request = buildRequest(req);\n const response = await handler(request);\n await sendResponse(res, response);\n };\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,qBAAsC;AAmCtC,IAAM,oBAAoB;AAE1B,IAAM,gBAAgB,CAAC,UAAkB,MAAM,QAAQ,QAAQ,EAAE;AAEjE,IAAM,eAAe,CAAC,QAAgB,SACpC,IAAI,SAAS,KAAK,UAAU,IAAI,GAAG;AAAA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AE,QAAQ,IAAI,UAAU,OAAO,SAAS,UAAU,OAAO,EAAE,CAAC;AACtF;AAEA,IAAM,eAAe,OAAO,KAA2B,aAAuB;AAC5E,MAAI,OAAO,IAAI,WAAW,YAAY;AACpC,QAAI,OAAO,SAAS,MAAM;AAAA,EAC5B,OAAO;AACL,QAAI,aAAa,SAAS;AAAA,EAC5B;AAEA,MAAI,OAAO,IAAI,cAAc,YAAY;AACvC,aAAS,QAAQ,QAAQ,CAAC,OAAO,QAAQ;AACvC,UAAI,YAAY,KAAK,KAAK;AAAA,IAC5B,CAAC;AAAA,EACH;AAEA,QAAM,OAAO,MAAM,SAAS,KAAK;AACjC,MAAI,MAAM,QAAQ,MAAS;AAC7B;AAEO,SAAS,oBAAoB,UAAsC,CAAC,GAAG;AAC5E,SAAO,OAAO,KAA0B,QAA8B;AACpE,UAAM,WAAW,QAAQ;AACzB,UAAM,UAAU,eAAe;AAAA,MAC7B,GAAG;AAAA,MACH,UAAU,WAAW,MAAM,SAAS,GAAG,IAAI;AAAA,IAC7C,CAAC;AACD,UAAM,UAAU,aAAa,GAAG;AAChC,UAAM,WAAW,MAAM,QAAQ,OAAO;AACtC,UAAM,aAAa,KAAK,QAAQ;AAAA,EAClC;AACF;","names":[]}
@@ -0,0 +1,29 @@
1
+ type PriceOSIdentify = (request: Request) => string | null | undefined | Promise<string | null | undefined>;
2
+ type PriceOSPagesRequest = {
3
+ method?: string;
4
+ headers?: Record<string, string | string[] | undefined>;
5
+ url?: string;
6
+ socket?: {
7
+ encrypted?: boolean;
8
+ };
9
+ };
10
+ type PriceOSPagesResponse = {
11
+ status?: (code: number) => PriceOSPagesResponse;
12
+ setHeader?: (name: string, value: string | string[]) => void;
13
+ end?: (body?: string) => void;
14
+ statusCode?: number;
15
+ };
16
+ type PriceOSPagesIdentify = (request: PriceOSPagesRequest) => string | null | undefined | Promise<string | null | undefined>;
17
+ type PriceOSNextHandlerOptions = {
18
+ apiKey?: string;
19
+ baseUrl?: string;
20
+ basePath?: string;
21
+ identify?: PriceOSIdentify;
22
+ };
23
+ type PriceOSPagesHandlerOptions = Omit<PriceOSNextHandlerOptions, "identify"> & {
24
+ identify?: PriceOSPagesIdentify;
25
+ };
26
+ declare function priceosHandler(options?: PriceOSNextHandlerOptions): (request: Request) => Promise<Response>;
27
+ declare function priceosPagesHandler(options?: PriceOSPagesHandlerOptions): (req: PriceOSPagesRequest, res: PriceOSPagesResponse) => Promise<void>;
28
+
29
+ export { type PriceOSIdentify, type PriceOSNextHandlerOptions, type PriceOSPagesHandlerOptions, type PriceOSPagesIdentify, type PriceOSPagesRequest, type PriceOSPagesResponse, priceosHandler, priceosPagesHandler };
@@ -0,0 +1,29 @@
1
+ type PriceOSIdentify = (request: Request) => string | null | undefined | Promise<string | null | undefined>;
2
+ type PriceOSPagesRequest = {
3
+ method?: string;
4
+ headers?: Record<string, string | string[] | undefined>;
5
+ url?: string;
6
+ socket?: {
7
+ encrypted?: boolean;
8
+ };
9
+ };
10
+ type PriceOSPagesResponse = {
11
+ status?: (code: number) => PriceOSPagesResponse;
12
+ setHeader?: (name: string, value: string | string[]) => void;
13
+ end?: (body?: string) => void;
14
+ statusCode?: number;
15
+ };
16
+ type PriceOSPagesIdentify = (request: PriceOSPagesRequest) => string | null | undefined | Promise<string | null | undefined>;
17
+ type PriceOSNextHandlerOptions = {
18
+ apiKey?: string;
19
+ baseUrl?: string;
20
+ basePath?: string;
21
+ identify?: PriceOSIdentify;
22
+ };
23
+ type PriceOSPagesHandlerOptions = Omit<PriceOSNextHandlerOptions, "identify"> & {
24
+ identify?: PriceOSPagesIdentify;
25
+ };
26
+ declare function priceosHandler(options?: PriceOSNextHandlerOptions): (request: Request) => Promise<Response>;
27
+ declare function priceosPagesHandler(options?: PriceOSPagesHandlerOptions): (req: PriceOSPagesRequest, res: PriceOSPagesResponse) => Promise<void>;
28
+
29
+ export { type PriceOSIdentify, type PriceOSNextHandlerOptions, type PriceOSPagesHandlerOptions, type PriceOSPagesIdentify, type PriceOSPagesRequest, type PriceOSPagesResponse, priceosHandler, priceosPagesHandler };
package/dist/index.js ADDED
@@ -0,0 +1,111 @@
1
+ // src/index.ts
2
+ import { PriceOS, PriceOSError } from "priceos";
3
+ var DEFAULT_BASE_PATH = "/api/priceos";
4
+ var normalizePath = (value) => value.replace(/^\/+/, "");
5
+ var jsonResponse = (status, body) => new Response(JSON.stringify(body), {
6
+ status,
7
+ headers: { "content-type": "application/json" }
8
+ });
9
+ function priceosHandler(options = {}) {
10
+ const envApiKey = globalThis.process?.env?.PRICEOS_API_KEY;
11
+ const apiKey = options.apiKey ?? envApiKey;
12
+ const identify = options.identify;
13
+ const baseUrl = (options.baseUrl ?? "https://api.priceos.com").replace(/\/$/, "");
14
+ const basePath = (options.basePath ?? DEFAULT_BASE_PATH).replace(/\/+$/, "");
15
+ return async (request) => {
16
+ if (!apiKey) {
17
+ return new Response("Missing PRICEOS_API_KEY.", { status: 500 });
18
+ }
19
+ if (!identify) {
20
+ return jsonResponse(500, { error: "Missing identify function" });
21
+ }
22
+ let path = "";
23
+ const url = new URL(request.url);
24
+ const pathname = url.pathname;
25
+ if (pathname.startsWith(basePath)) {
26
+ path = normalizePath(pathname.slice(basePath.length));
27
+ }
28
+ if (path !== "v1/customer") {
29
+ return new Response("Not found.", { status: 404 });
30
+ }
31
+ const client = new PriceOS(apiKey, { baseUrl });
32
+ const method = request.method.toUpperCase();
33
+ if (method !== "GET") {
34
+ return new Response("Method not allowed.", { status: 405 });
35
+ }
36
+ if (url.searchParams.has("customerId")) {
37
+ return jsonResponse(400, { error: "customerId must be set via identify" });
38
+ }
39
+ let customerId;
40
+ try {
41
+ customerId = await identify(request);
42
+ } catch (error) {
43
+ return jsonResponse(500, { error: "Failed to identify customer" });
44
+ }
45
+ if (typeof customerId !== "string" || !customerId) {
46
+ return jsonResponse(401, { error: "Customer not identified" });
47
+ }
48
+ try {
49
+ const data = await client.getCustomer(customerId);
50
+ return jsonResponse(200, data);
51
+ } catch (error) {
52
+ if (error instanceof PriceOSError) {
53
+ return jsonResponse(error.status ?? 500, { error: error.message });
54
+ }
55
+ return jsonResponse(500, { error: "Request failed" });
56
+ }
57
+ };
58
+ }
59
+ var getHeaderValue = (headers, key) => {
60
+ const value = headers?.[key];
61
+ if (Array.isArray(value)) return value.join(",");
62
+ return value;
63
+ };
64
+ var toHeaders = (headers) => {
65
+ const result = new Headers();
66
+ if (!headers) return result;
67
+ for (const [key, value] of Object.entries(headers)) {
68
+ if (typeof value === "string") result.set(key, value);
69
+ if (Array.isArray(value)) result.set(key, value.join(","));
70
+ }
71
+ return result;
72
+ };
73
+ var buildRequest = (req) => {
74
+ const headers = req.headers ?? {};
75
+ const forwardedProto = getHeaderValue(headers, "x-forwarded-proto");
76
+ const protocol = forwardedProto?.split(",")[0] ?? (req.socket?.encrypted ? "https" : "http");
77
+ const host = getHeaderValue(headers, "x-forwarded-host") ?? getHeaderValue(headers, "host") ?? "localhost";
78
+ const url = new URL(req.url ?? "/", `${protocol}://${host}`);
79
+ return new Request(url, { method: req.method ?? "GET", headers: toHeaders(headers) });
80
+ };
81
+ var sendResponse = async (res, response) => {
82
+ if (typeof res.status === "function") {
83
+ res.status(response.status);
84
+ } else {
85
+ res.statusCode = response.status;
86
+ }
87
+ if (typeof res.setHeader === "function") {
88
+ response.headers.forEach((value, key) => {
89
+ res.setHeader?.(key, value);
90
+ });
91
+ }
92
+ const body = await response.text();
93
+ res.end?.(body || void 0);
94
+ };
95
+ function priceosPagesHandler(options = {}) {
96
+ return async (req, res) => {
97
+ const identify = options.identify;
98
+ const handler = priceosHandler({
99
+ ...options,
100
+ identify: identify ? () => identify(req) : void 0
101
+ });
102
+ const request = buildRequest(req);
103
+ const response = await handler(request);
104
+ await sendResponse(res, response);
105
+ };
106
+ }
107
+ export {
108
+ priceosHandler,
109
+ priceosPagesHandler
110
+ };
111
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/index.ts"],"sourcesContent":["import { PriceOS, PriceOSError } from \"priceos\";\n\nexport type PriceOSIdentify = (\n request: Request\n) => string | null | undefined | Promise<string | null | undefined>;\n\nexport type PriceOSPagesRequest = {\n method?: string;\n headers?: Record<string, string | string[] | undefined>;\n url?: string;\n socket?: { encrypted?: boolean };\n};\n\nexport type PriceOSPagesResponse = {\n status?: (code: number) => PriceOSPagesResponse;\n setHeader?: (name: string, value: string | string[]) => void;\n end?: (body?: string) => void;\n statusCode?: number;\n};\n\nexport type PriceOSPagesIdentify = (\n request: PriceOSPagesRequest\n) => string | null | undefined | Promise<string | null | undefined>;\n\nexport type PriceOSNextHandlerOptions = {\n apiKey?: string;\n baseUrl?: string;\n basePath?: string;\n identify?: PriceOSIdentify;\n};\n\nexport type PriceOSPagesHandlerOptions = Omit<PriceOSNextHandlerOptions, \"identify\"> & {\n identify?: PriceOSPagesIdentify;\n};\n\nconst DEFAULT_BASE_PATH = \"/api/priceos\";\n\nconst normalizePath = (value: string) => value.replace(/^\\/+/, \"\");\n\nconst jsonResponse = (status: number, body: unknown) =>\n new Response(JSON.stringify(body), {\n status,\n headers: { \"content-type\": \"application/json\" },\n });\n\nexport function priceosHandler(options: PriceOSNextHandlerOptions = {}) {\n const envApiKey = (globalThis as { process?: { env?: Record<string, string | undefined> } })\n .process?.env?.PRICEOS_API_KEY;\n const apiKey = options.apiKey ?? envApiKey;\n const identify = options.identify;\n const baseUrl = (options.baseUrl ?? \"https://api.priceos.com\").replace(/\\/$/, \"\");\n const basePath = (options.basePath ?? DEFAULT_BASE_PATH).replace(/\\/+$/, \"\");\n\n return async (request: Request) => {\n if (!apiKey) {\n return new Response(\"Missing PRICEOS_API_KEY.\", { status: 500 });\n }\n\n if (!identify) {\n return jsonResponse(500, { error: \"Missing identify function\" });\n }\n\n let path = \"\";\n const url = new URL(request.url);\n const pathname = url.pathname;\n if (pathname.startsWith(basePath)) {\n path = normalizePath(pathname.slice(basePath.length));\n }\n\n if (path !== \"v1/customer\") {\n return new Response(\"Not found.\", { status: 404 });\n }\n\n const client = new PriceOS(apiKey, { baseUrl });\n const method = request.method.toUpperCase();\n if (method !== \"GET\") {\n return new Response(\"Method not allowed.\", { status: 405 });\n }\n\n if (url.searchParams.has(\"customerId\")) {\n return jsonResponse(400, { error: \"customerId must be set via identify\" });\n }\n\n let customerId: string | null | undefined;\n try {\n customerId = await identify(request);\n } catch (error) {\n return jsonResponse(500, { error: \"Failed to identify customer\" });\n }\n\n if (typeof customerId !== \"string\" || !customerId) {\n return jsonResponse(401, { error: \"Customer not identified\" });\n }\n\n try {\n const data = await client.getCustomer(customerId);\n return jsonResponse(200, data);\n } catch (error) {\n if (error instanceof PriceOSError) {\n return jsonResponse(error.status ?? 500, { error: error.message });\n }\n return jsonResponse(500, { error: \"Request failed\" });\n }\n };\n}\n\nconst getHeaderValue = (\n headers: Record<string, string | string[] | undefined> | undefined,\n key: string\n) => {\n const value = headers?.[key];\n if (Array.isArray(value)) return value.join(\",\");\n return value;\n};\n\nconst toHeaders = (headers: Record<string, string | string[] | undefined> | undefined) => {\n const result = new Headers();\n if (!headers) return result;\n for (const [key, value] of Object.entries(headers)) {\n if (typeof value === \"string\") result.set(key, value);\n if (Array.isArray(value)) result.set(key, value.join(\",\"));\n }\n return result;\n};\n\nconst buildRequest = (req: PriceOSPagesRequest) => {\n const headers = req.headers ?? {};\n const forwardedProto = getHeaderValue(headers, \"x-forwarded-proto\");\n const protocol =\n forwardedProto?.split(\",\")[0] ?? (req.socket?.encrypted ? \"https\" : \"http\");\n const host =\n getHeaderValue(headers, \"x-forwarded-host\") ??\n getHeaderValue(headers, \"host\") ??\n \"localhost\";\n const url = new URL(req.url ?? \"/\", `${protocol}://${host}`);\n return new Request(url, { method: req.method ?? \"GET\", headers: toHeaders(headers) });\n};\n\nconst sendResponse = async (res: PriceOSPagesResponse, response: Response) => {\n if (typeof res.status === \"function\") {\n res.status(response.status);\n } else {\n res.statusCode = response.status;\n }\n\n if (typeof res.setHeader === \"function\") {\n response.headers.forEach((value, key) => {\n res.setHeader?.(key, value);\n });\n }\n\n const body = await response.text();\n res.end?.(body || undefined);\n};\n\nexport function priceosPagesHandler(options: PriceOSPagesHandlerOptions = {}) {\n return async (req: PriceOSPagesRequest, res: PriceOSPagesResponse) => {\n const identify = options.identify;\n const handler = priceosHandler({\n ...options,\n identify: identify ? () => identify(req) : undefined,\n });\n const request = buildRequest(req);\n const response = await handler(request);\n await sendResponse(res, response);\n };\n}\n"],"mappings":";AAAA,SAAS,SAAS,oBAAoB;AAmCtC,IAAM,oBAAoB;AAE1B,IAAM,gBAAgB,CAAC,UAAkB,MAAM,QAAQ,QAAQ,EAAE;AAEjE,IAAM,eAAe,CAAC,QAAgB,SACpC,IAAI,SAAS,KAAK,UAAU,IAAI,GAAG;AAAA,EACjC;AAAA,EACA,SAAS,EAAE,gBAAgB,mBAAmB;AAChD,CAAC;AAEI,SAAS,eAAe,UAAqC,CAAC,GAAG;AACtE,QAAM,YAAa,WAChB,SAAS,KAAK;AACjB,QAAM,SAAS,QAAQ,UAAU;AACjC,QAAM,WAAW,QAAQ;AACzB,QAAM,WAAW,QAAQ,WAAW,2BAA2B,QAAQ,OAAO,EAAE;AAChF,QAAM,YAAY,QAAQ,YAAY,mBAAmB,QAAQ,QAAQ,EAAE;AAE3E,SAAO,OAAO,YAAqB;AACjC,QAAI,CAAC,QAAQ;AACX,aAAO,IAAI,SAAS,4BAA4B,EAAE,QAAQ,IAAI,CAAC;AAAA,IACjE;AAEA,QAAI,CAAC,UAAU;AACb,aAAO,aAAa,KAAK,EAAE,OAAO,4BAA4B,CAAC;AAAA,IACjE;AAEA,QAAI,OAAO;AACX,UAAM,MAAM,IAAI,IAAI,QAAQ,GAAG;AAC/B,UAAM,WAAW,IAAI;AACrB,QAAI,SAAS,WAAW,QAAQ,GAAG;AACjC,aAAO,cAAc,SAAS,MAAM,SAAS,MAAM,CAAC;AAAA,IACtD;AAEA,QAAI,SAAS,eAAe;AAC1B,aAAO,IAAI,SAAS,cAAc,EAAE,QAAQ,IAAI,CAAC;AA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DED
@@ -0,0 +1,32 @@
1
+ {
2
+ "name": "@priceos/next",
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
+ "devDependencies": {
28
+ "@types/node": "^25.0.10",
29
+ "tsup": "^8.0.0",
30
+ "typescript": "^5.0.0"
31
+ }
32
+ }
package/src/index.ts ADDED
@@ -0,0 +1,167 @@
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
+ }