@priceos/next 0.0.33 → 0.0.34

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
@@ -31,6 +31,11 @@ var jsonResponse = (status, body) => new Response(JSON.stringify(body), {
31
31
  status,
32
32
  headers: { "content-type": "application/json" }
33
33
  });
34
+ var normalizeCustomerId = (value) => {
35
+ if (typeof value !== "string") return null;
36
+ const normalized = value.trim();
37
+ return normalized.length ? normalized : null;
38
+ };
34
39
  function priceosHandler(options = {}) {
35
40
  const envApiKey = globalThis.process?.env?.PRICEOS_API_KEY;
36
41
  const apiKey = options.apiKey ?? envApiKey;
@@ -66,11 +71,12 @@ function priceosHandler(options = {}) {
66
71
  } catch (error) {
67
72
  return jsonResponse(500, { error: "Failed to resolve customerId" });
68
73
  }
69
- if (typeof customerId !== "string" || !customerId) {
74
+ const normalizedCustomerId = normalizeCustomerId(customerId);
75
+ if (!normalizedCustomerId) {
70
76
  return jsonResponse(401, { error: "Customer not identified" });
71
77
  }
72
78
  try {
73
- const data = await client.customers.get(customerId);
79
+ const data = await client.customers.get(normalizedCustomerId);
74
80
  return jsonResponse(200, data);
75
81
  } catch (error) {
76
82
  if (error instanceof import_priceos.PriceOSError) {
@@ -81,9 +87,17 @@ function priceosHandler(options = {}) {
81
87
  };
82
88
  }
83
89
  var getHeaderValue = (headers, key) => {
84
- const value = headers?.[key];
85
- if (Array.isArray(value)) return value.join(",");
86
- return value;
90
+ if (!headers) return void 0;
91
+ const value = headers[key] ?? headers[key.toLowerCase()] ?? headers[key.toUpperCase()];
92
+ if (value !== void 0) {
93
+ if (Array.isArray(value)) return value.join(",");
94
+ return value;
95
+ }
96
+ const entry = Object.entries(headers).find(([headerKey]) => headerKey.toLowerCase() === key.toLowerCase());
97
+ if (!entry) return void 0;
98
+ const [, entryValue] = entry;
99
+ if (Array.isArray(entryValue)) return entryValue.join(",");
100
+ return entryValue;
87
101
  };
88
102
  var toHeaders = (headers) => {
89
103
  const result = new Headers();
@@ -97,7 +111,7 @@ var toHeaders = (headers) => {
97
111
  var buildRequest = (req) => {
98
112
  const headers = req.headers ?? {};
99
113
  const forwardedProto = getHeaderValue(headers, "x-forwarded-proto");
100
- const protocol = forwardedProto?.split(",")[0] ?? (req.socket?.encrypted ? "https" : "http");
114
+ const protocol = forwardedProto?.split(",")[0]?.trim() ?? (req.socket?.encrypted ? "https" : "http");
101
115
  const host = getHeaderValue(headers, "x-forwarded-host") ?? getHeaderValue(headers, "host") ?? "localhost";
102
116
  const url = new URL(req.url ?? "/", `${protocol}://${host}`);
103
117
  return new Request(url, { method: req.method ?? "GET", headers: toHeaders(headers) });
@@ -119,11 +133,11 @@ var sendResponse = async (res, response) => {
119
133
  function priceosPagesHandler(options = {}) {
120
134
  return async (req, res) => {
121
135
  const getCustomerId = options.getCustomerId;
136
+ const request = buildRequest(req);
122
137
  const handler = priceosHandler({
123
138
  ...options,
124
- getCustomerId: getCustomerId ? () => getCustomerId(req) : void 0
139
+ getCustomerId: getCustomerId ? (normalizedRequest) => getCustomerId(req, normalizedRequest) : void 0
125
140
  });
126
- const request = buildRequest(req);
127
141
  const response = await handler(request);
128
142
  await sendResponse(res, response);
129
143
  };
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/index.ts"],"sourcesContent":["import { PriceOS, PriceOSError } from \"priceos\";\n\nexport type PriceOSGetCustomerId = (\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 PriceOSPagesGetCustomerId = (\n request: PriceOSPagesRequest\n) => string | null | undefined | Promise<string | null | undefined>;\n\nexport type PriceOSNextHandlerOptions = {\n apiKey?: string;\n basePath?: string;\n getCustomerId?: PriceOSGetCustomerId;\n};\n\nexport type PriceOSPagesHandlerOptions = Omit<PriceOSNextHandlerOptions, \"getCustomerId\"> & {\n getCustomerId?: PriceOSPagesGetCustomerId;\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 getCustomerId = options.getCustomerId;\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 (!getCustomerId) {\n return jsonResponse(500, { error: \"Missing getCustomerId 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);\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 getCustomerId\" });\n }\n\n let customerId: string | null | undefined;\n try {\n customerId = await getCustomerId(request);\n } catch (error) {\n return jsonResponse(500, { error: \"Failed to resolve customerId\" });\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.customers.get(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 getCustomerId = options.getCustomerId;\n const handler = priceosHandler({\n ...options,\n getCustomerId: getCustomerId ? () => getCustomerId(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;AAkCtC,IAAM,oBAAoB;AAE1B,IAAM,gBAAgB,CAAC,UAAkB,MA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AC3D,SAAO,IAAI,QAAQ,KAAK,EAAE,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,gBAAgB,QAAQ;AAC9B,UAAM,UAAU,eAAe;AAAA,MAC7B,GAAG;AAAA,MACH,eAAe,gBAAgB,MAAM,cAAc,GAAG,IAAI;AAAA,IAC5D,CAAC;AACD,UAAM,UAAU,aAAa,GAAG;AAChC,UAAM,WAAW,MAAM,QAAQ,OAAO;AACtC,UAAM,aAAa,KAAK,QAAQ;AAAA,EAClC;AACF;","names":[]}
1
+ {"version":3,"sources":["../src/index.ts"],"sourcesContent":["import { PriceOS, PriceOSError } from \"priceos\";\n\nexport type PriceOSGetCustomerId = (\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 PriceOSPagesGetCustomerId = (\n request: PriceOSPagesRequest,\n normalizedRequest: Request\n) => string | null | undefined | Promise<string | null | undefined>;\n\nexport type PriceOSNextHandlerOptions = {\n apiKey?: string;\n basePath?: string;\n getCustomerId?: PriceOSGetCustomerId;\n};\n\nexport type PriceOSPagesHandlerOptions = Omit<PriceOSNextHandlerOptions, \"getCustomerId\"> & {\n getCustomerId?: PriceOSPagesGetCustomerId;\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\nconst normalizeCustomerId = (value: string | null | undefined) => {\n if (typeof value !== \"string\") return null;\n const normalized = value.trim();\n return normalized.length ? normalized : null;\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 getCustomerId = options.getCustomerId;\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 (!getCustomerId) {\n return jsonResponse(500, { error: \"Missing getCustomerId 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);\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 getCustomerId\" });\n }\n\n let customerId: string | null | undefined;\n try {\n customerId = await getCustomerId(request);\n } catch (error) {\n return jsonResponse(500, { error: \"Failed to resolve customerId\" });\n }\n\n const normalizedCustomerId = normalizeCustomerId(customerId);\n if (!normalizedCustomerId) {\n return jsonResponse(401, { error: \"Customer not identified\" });\n }\n\n try {\n const data = await client.customers.get(normalizedCustomerId);\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 if (!headers) return undefined;\n const value = headers[key] ?? headers[key.toLowerCase()] ?? headers[key.toUpperCase()];\n if (value !== undefined) {\n if (Array.isArray(value)) return value.join(\",\");\n return value;\n }\n const entry = Object.entries(headers).find(([headerKey]) => headerKey.toLowerCase() === key.toLowerCase());\n if (!entry) return undefined;\n const [, entryValue] = entry;\n if (Array.isArray(entryValue)) return entryValue.join(\",\");\n return entryValue;\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]?.trim() ?? (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 getCustomerId = options.getCustomerId;\n const request = buildRequest(req);\n const handler = priceosHandler({\n ...options,\n getCustomerId: getCustomerId ? (normalizedRequest) => getCustomerId(req, normalizedRequest) : undefined,\n });\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,EACjC;AAAA,EACA,SAAS,EAAE,gBAAgB,mBAAmB;AAChD,CAAC;AAEH,IAAM,sBAAsB,CAAC,UAAqC;AAChE,MAAI,OAAO,UAAU,SAAU,QAAO;AACtC,QAAM,aAAa,MAAM,KAAK;AAC9B,SAAO,WAAW,SAAS,aAAa;AAC1C;AAEO,SAAS,eAAe,UAAqC,CAAC,GAAG;AACtE,QAAM,YAAa,WAChB,SAAS,KAAK;AACjB,QAAM,SAAS,QAAQ,UAAU;AACjC,QAAM,gBAAgB,QAAQ;AAC9B,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,eAAe;AAClB,aAAO,aAAa,KAAK,EAAE,OAAO,iCAAiC,CAAC;AAAA,IACtE;AAEA,QAAI,OAAO;AACX,UAAM,MAAM,IAAI,IAAI,QAAQ,GAAG;AAC/B,UAAM,WAAW,IAAI;AACrB,QAAI,SAAS,WAAW,QAAQ,GAAG;A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AAO,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,gBAAgB,QAAQ;AAC9B,UAAM,UAAU,aAAa,GAAG;AAChC,UAAM,UAAU,eAAe;AAAA,MAC7B,GAAG;AAAA,MACH,eAAe,gBAAgB,CAAC,sBAAsB,cAAc,KAAK,iBAAiB,IAAI;AAAA,IAChG,CAAC;AACD,UAAM,WAAW,MAAM,QAAQ,OAAO;AACtC,UAAM,aAAa,KAAK,QAAQ;AAAA,EAClC;AACF;","names":[]}
package/dist/index.d.cts CHANGED
@@ -13,7 +13,7 @@ type PriceOSPagesResponse = {
13
13
  end?: (body?: string) => void;
14
14
  statusCode?: number;
15
15
  };
16
- type PriceOSPagesGetCustomerId = (request: PriceOSPagesRequest) => string | null | undefined | Promise<string | null | undefined>;
16
+ type PriceOSPagesGetCustomerId = (request: PriceOSPagesRequest, normalizedRequest: Request) => string | null | undefined | Promise<string | null | undefined>;
17
17
  type PriceOSNextHandlerOptions = {
18
18
  apiKey?: string;
19
19
  basePath?: string;
package/dist/index.d.ts CHANGED
@@ -13,7 +13,7 @@ type PriceOSPagesResponse = {
13
13
  end?: (body?: string) => void;
14
14
  statusCode?: number;
15
15
  };
16
- type PriceOSPagesGetCustomerId = (request: PriceOSPagesRequest) => string | null | undefined | Promise<string | null | undefined>;
16
+ type PriceOSPagesGetCustomerId = (request: PriceOSPagesRequest, normalizedRequest: Request) => string | null | undefined | Promise<string | null | undefined>;
17
17
  type PriceOSNextHandlerOptions = {
18
18
  apiKey?: string;
19
19
  basePath?: string;
package/dist/index.js CHANGED
@@ -6,6 +6,11 @@ var jsonResponse = (status, body) => new Response(JSON.stringify(body), {
6
6
  status,
7
7
  headers: { "content-type": "application/json" }
8
8
  });
9
+ var normalizeCustomerId = (value) => {
10
+ if (typeof value !== "string") return null;
11
+ const normalized = value.trim();
12
+ return normalized.length ? normalized : null;
13
+ };
9
14
  function priceosHandler(options = {}) {
10
15
  const envApiKey = globalThis.process?.env?.PRICEOS_API_KEY;
11
16
  const apiKey = options.apiKey ?? envApiKey;
@@ -41,11 +46,12 @@ function priceosHandler(options = {}) {
41
46
  } catch (error) {
42
47
  return jsonResponse(500, { error: "Failed to resolve customerId" });
43
48
  }
44
- if (typeof customerId !== "string" || !customerId) {
49
+ const normalizedCustomerId = normalizeCustomerId(customerId);
50
+ if (!normalizedCustomerId) {
45
51
  return jsonResponse(401, { error: "Customer not identified" });
46
52
  }
47
53
  try {
48
- const data = await client.customers.get(customerId);
54
+ const data = await client.customers.get(normalizedCustomerId);
49
55
  return jsonResponse(200, data);
50
56
  } catch (error) {
51
57
  if (error instanceof PriceOSError) {
@@ -56,9 +62,17 @@ function priceosHandler(options = {}) {
56
62
  };
57
63
  }
58
64
  var getHeaderValue = (headers, key) => {
59
- const value = headers?.[key];
60
- if (Array.isArray(value)) return value.join(",");
61
- return value;
65
+ if (!headers) return void 0;
66
+ const value = headers[key] ?? headers[key.toLowerCase()] ?? headers[key.toUpperCase()];
67
+ if (value !== void 0) {
68
+ if (Array.isArray(value)) return value.join(",");
69
+ return value;
70
+ }
71
+ const entry = Object.entries(headers).find(([headerKey]) => headerKey.toLowerCase() === key.toLowerCase());
72
+ if (!entry) return void 0;
73
+ const [, entryValue] = entry;
74
+ if (Array.isArray(entryValue)) return entryValue.join(",");
75
+ return entryValue;
62
76
  };
63
77
  var toHeaders = (headers) => {
64
78
  const result = new Headers();
@@ -72,7 +86,7 @@ var toHeaders = (headers) => {
72
86
  var buildRequest = (req) => {
73
87
  const headers = req.headers ?? {};
74
88
  const forwardedProto = getHeaderValue(headers, "x-forwarded-proto");
75
- const protocol = forwardedProto?.split(",")[0] ?? (req.socket?.encrypted ? "https" : "http");
89
+ const protocol = forwardedProto?.split(",")[0]?.trim() ?? (req.socket?.encrypted ? "https" : "http");
76
90
  const host = getHeaderValue(headers, "x-forwarded-host") ?? getHeaderValue(headers, "host") ?? "localhost";
77
91
  const url = new URL(req.url ?? "/", `${protocol}://${host}`);
78
92
  return new Request(url, { method: req.method ?? "GET", headers: toHeaders(headers) });
@@ -94,11 +108,11 @@ var sendResponse = async (res, response) => {
94
108
  function priceosPagesHandler(options = {}) {
95
109
  return async (req, res) => {
96
110
  const getCustomerId = options.getCustomerId;
111
+ const request = buildRequest(req);
97
112
  const handler = priceosHandler({
98
113
  ...options,
99
- getCustomerId: getCustomerId ? () => getCustomerId(req) : void 0
114
+ getCustomerId: getCustomerId ? (normalizedRequest) => getCustomerId(req, normalizedRequest) : void 0
100
115
  });
101
- const request = buildRequest(req);
102
116
  const response = await handler(request);
103
117
  await sendResponse(res, response);
104
118
  };
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/index.ts"],"sourcesContent":["import { PriceOS, PriceOSError } from \"priceos\";\n\nexport type PriceOSGetCustomerId = (\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 PriceOSPagesGetCustomerId = (\n request: PriceOSPagesRequest\n) => string | null | undefined | Promise<string | null | undefined>;\n\nexport type PriceOSNextHandlerOptions = {\n apiKey?: string;\n basePath?: string;\n getCustomerId?: PriceOSGetCustomerId;\n};\n\nexport type PriceOSPagesHandlerOptions = Omit<PriceOSNextHandlerOptions, \"getCustomerId\"> & {\n getCustomerId?: PriceOSPagesGetCustomerId;\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 getCustomerId = options.getCustomerId;\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 (!getCustomerId) {\n return jsonResponse(500, { error: \"Missing getCustomerId 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);\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 getCustomerId\" });\n }\n\n let customerId: string | null | undefined;\n try {\n customerId = await getCustomerId(request);\n } catch (error) {\n return jsonResponse(500, { error: \"Failed to resolve customerId\" });\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.customers.get(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 getCustomerId = options.getCustomerId;\n const handler = priceosHandler({\n ...options,\n getCustomerId: getCustomerId ? () => getCustomerId(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;AAkCtC,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,UAA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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,gBAAgB,QAAQ;AAC9B,UAAM,UAAU,eAAe;AAAA,MAC7B,GAAG;AAAA,MACH,eAAe,gBAAgB,MAAM,cAAc,GAAG,IAAI;AAAA,IAC5D,CAAC;AACD,UAAM,UAAU,aAAa,GAAG;AAChC,UAAM,WAAW,MAAM,QAAQ,OAAO;AACtC,UAAM,aAAa,KAAK,QAAQ;AAAA,EAClC;AACF;","names":[]}
1
+ {"version":3,"sources":["../src/index.ts"],"sourcesContent":["import { PriceOS, PriceOSError } from \"priceos\";\n\nexport type PriceOSGetCustomerId = (\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 PriceOSPagesGetCustomerId = (\n request: PriceOSPagesRequest,\n normalizedRequest: Request\n) => string | null | undefined | Promise<string | null | undefined>;\n\nexport type PriceOSNextHandlerOptions = {\n apiKey?: string;\n basePath?: string;\n getCustomerId?: PriceOSGetCustomerId;\n};\n\nexport type PriceOSPagesHandlerOptions = Omit<PriceOSNextHandlerOptions, \"getCustomerId\"> & {\n getCustomerId?: PriceOSPagesGetCustomerId;\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\nconst normalizeCustomerId = (value: string | null | undefined) => {\n if (typeof value !== \"string\") return null;\n const normalized = value.trim();\n return normalized.length ? normalized : null;\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 getCustomerId = options.getCustomerId;\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 (!getCustomerId) {\n return jsonResponse(500, { error: \"Missing getCustomerId 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);\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 getCustomerId\" });\n }\n\n let customerId: string | null | undefined;\n try {\n customerId = await getCustomerId(request);\n } catch (error) {\n return jsonResponse(500, { error: \"Failed to resolve customerId\" });\n }\n\n const normalizedCustomerId = normalizeCustomerId(customerId);\n if (!normalizedCustomerId) {\n return jsonResponse(401, { error: \"Customer not identified\" });\n }\n\n try {\n const data = await client.customers.get(normalizedCustomerId);\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 if (!headers) return undefined;\n const value = headers[key] ?? headers[key.toLowerCase()] ?? headers[key.toUpperCase()];\n if (value !== undefined) {\n if (Array.isArray(value)) return value.join(\",\");\n return value;\n }\n const entry = Object.entries(headers).find(([headerKey]) => headerKey.toLowerCase() === key.toLowerCase());\n if (!entry) return undefined;\n const [, entryValue] = entry;\n if (Array.isArray(entryValue)) return entryValue.join(\",\");\n return entryValue;\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]?.trim() ?? (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 getCustomerId = options.getCustomerId;\n const request = buildRequest(req);\n const handler = priceosHandler({\n ...options,\n getCustomerId: getCustomerId ? (normalizedRequest) => getCustomerId(req, normalizedRequest) : undefined,\n });\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;AAEH,IAAM,sBAAsB,CAAC,UAAqC;AAChE,MAAI,OAAO,UAAU,SAAU,QAAO;AACtC,QAAM,aAAa,MAAM,KAAK;AAC9B,SAAO,WAAW,SAAS,aAAa;AAC1C;AAEO,SAAS,eAAe,UAAqC,CAAC,GAAG;AACtE,QAAM,YAAa,WAChB,SAAS,KAAK;AACjB,QAAM,SAAS,QAAQ,UAAU;AACjC,QAAM,gBAAgB,QAAQ;AAC9B,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,eAAe;AAClB,aAAO,aAAa,KAAK,EAAE,OAAO,iCAAiC,CAAC;AAAA,IACtE;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;AAAA,IACnD;AAEA,UAAM,SAAS,IAAI,QAAQ,MAAM;AACjC,UAAM,SAAS,QAAQ,OAAO,YAAY;AAC1C,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,GAAG;AAC5E,SAAO,OAAO,KAA0B,QAA8B;AACpE,UAAM,gBAAgB,QAAQ;AAC9B,UAAM,UAAU,aAAa,GAAG;AAChC,UAAM,UAAU,eAAe;AAAA,MAC7B,GAAG;AAAA,MACH,eAAe,gBAAgB,CAAC,sBAAsB,cAAc,KAAK,iBAAiB,IAAI;AAAA,IAChG,CAAC;AACD,UAAM,WAAW,MAAM,QAAQ,OAAO;AACtC,UAAM,aAAa,KAAK,QAAQ;AAAA,EAClC;AACF;","names":[]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@priceos/next",
3
- "version": "0.0.33",
3
+ "version": "0.0.34",
4
4
  "private": false,
5
5
  "type": "module",
6
6
  "main": "dist/index.cjs",