@priceos/next 0.0.5 → 0.0.8

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
@@ -34,15 +34,15 @@ var jsonResponse = (status, body) => new Response(JSON.stringify(body), {
34
34
  function priceosHandler(options = {}) {
35
35
  const envApiKey = globalThis.process?.env?.PRICEOS_API_KEY;
36
36
  const apiKey = options.apiKey ?? envApiKey;
37
- const identify = options.identify;
37
+ const getCustomerId = options.getCustomerId;
38
38
  const baseUrl = (options.baseUrl ?? "https://api.priceos.com").replace(/\/$/, "");
39
39
  const basePath = (options.basePath ?? DEFAULT_BASE_PATH).replace(/\/+$/, "");
40
40
  return async (request) => {
41
41
  if (!apiKey) {
42
42
  return new Response("Missing PRICEOS_API_KEY.", { status: 500 });
43
43
  }
44
- if (!identify) {
45
- return jsonResponse(500, { error: "Missing identify function" });
44
+ if (!getCustomerId) {
45
+ return jsonResponse(500, { error: "Missing getCustomerId function" });
46
46
  }
47
47
  let path = "";
48
48
  const url = new URL(request.url);
@@ -59,13 +59,13 @@ function priceosHandler(options = {}) {
59
59
  return new Response("Method not allowed.", { status: 405 });
60
60
  }
61
61
  if (url.searchParams.has("customerId")) {
62
- return jsonResponse(400, { error: "customerId must be set via identify" });
62
+ return jsonResponse(400, { error: "customerId must be set via getCustomerId" });
63
63
  }
64
64
  let customerId;
65
65
  try {
66
- customerId = await identify(request);
66
+ customerId = await getCustomerId(request);
67
67
  } catch (error) {
68
- return jsonResponse(500, { error: "Failed to identify customer" });
68
+ return jsonResponse(500, { error: "Failed to resolve customerId" });
69
69
  }
70
70
  if (typeof customerId !== "string" || !customerId) {
71
71
  return jsonResponse(401, { error: "Customer not identified" });
@@ -119,10 +119,10 @@ var sendResponse = async (res, response) => {
119
119
  };
120
120
  function priceosPagesHandler(options = {}) {
121
121
  return async (req, res) => {
122
- const identify = options.identify;
122
+ const getCustomerId = options.getCustomerId;
123
123
  const handler = priceosHandler({
124
124
  ...options,
125
- identify: identify ? () => identify(req) : void 0
125
+ getCustomerId: getCustomerId ? () => getCustomerId(req) : void 0
126
126
  });
127
127
  const request = buildRequest(req);
128
128
  const response = await handler(request);
@@ -1 +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,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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AAQ;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":[]}
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 baseUrl?: 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 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 (!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, { 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 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.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 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;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,gBAAgB,QAAQ;AAC9B,QAAM,WAAW,QAAQ,WAAW,2BAA2B,QAAQ,OAAO,EAAE;AAChF,QAAM,YAAY,QAAQ,YAAY,mBAAmB,QAAQ,QAAQ,EAAE;AAE3E,SAAO,OAAO,YAAqB;AACjC,QAAI,CAA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B,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":[]}
package/dist/index.d.cts CHANGED
@@ -1,4 +1,4 @@
1
- type PriceOSIdentify = (request: Request) => string | null | undefined | Promise<string | null | undefined>;
1
+ type PriceOSGetCustomerId = (request: Request) => string | null | undefined | Promise<string | null | undefined>;
2
2
  type PriceOSPagesRequest = {
3
3
  method?: string;
4
4
  headers?: Record<string, string | string[] | undefined>;
@@ -13,17 +13,17 @@ type PriceOSPagesResponse = {
13
13
  end?: (body?: string) => void;
14
14
  statusCode?: number;
15
15
  };
16
- type PriceOSPagesIdentify = (request: PriceOSPagesRequest) => string | null | undefined | Promise<string | null | undefined>;
16
+ type PriceOSPagesGetCustomerId = (request: PriceOSPagesRequest) => string | null | undefined | Promise<string | null | undefined>;
17
17
  type PriceOSNextHandlerOptions = {
18
18
  apiKey?: string;
19
19
  baseUrl?: string;
20
20
  basePath?: string;
21
- identify?: PriceOSIdentify;
21
+ getCustomerId?: PriceOSGetCustomerId;
22
22
  };
23
- type PriceOSPagesHandlerOptions = Omit<PriceOSNextHandlerOptions, "identify"> & {
24
- identify?: PriceOSPagesIdentify;
23
+ type PriceOSPagesHandlerOptions = Omit<PriceOSNextHandlerOptions, "getCustomerId"> & {
24
+ getCustomerId?: PriceOSPagesGetCustomerId;
25
25
  };
26
26
  declare function priceosHandler(options?: PriceOSNextHandlerOptions): (request: Request) => Promise<Response>;
27
27
  declare function priceosPagesHandler(options?: PriceOSPagesHandlerOptions): (req: PriceOSPagesRequest, res: PriceOSPagesResponse) => Promise<void>;
28
28
 
29
- export { type PriceOSIdentify, type PriceOSNextHandlerOptions, type PriceOSPagesHandlerOptions, type PriceOSPagesIdentify, type PriceOSPagesRequest, type PriceOSPagesResponse, priceosHandler, priceosPagesHandler };
29
+ export { type PriceOSGetCustomerId, type PriceOSNextHandlerOptions, type PriceOSPagesGetCustomerId, type PriceOSPagesHandlerOptions, type PriceOSPagesRequest, type PriceOSPagesResponse, priceosHandler, priceosPagesHandler };
package/dist/index.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- type PriceOSIdentify = (request: Request) => string | null | undefined | Promise<string | null | undefined>;
1
+ type PriceOSGetCustomerId = (request: Request) => string | null | undefined | Promise<string | null | undefined>;
2
2
  type PriceOSPagesRequest = {
3
3
  method?: string;
4
4
  headers?: Record<string, string | string[] | undefined>;
@@ -13,17 +13,17 @@ type PriceOSPagesResponse = {
13
13
  end?: (body?: string) => void;
14
14
  statusCode?: number;
15
15
  };
16
- type PriceOSPagesIdentify = (request: PriceOSPagesRequest) => string | null | undefined | Promise<string | null | undefined>;
16
+ type PriceOSPagesGetCustomerId = (request: PriceOSPagesRequest) => string | null | undefined | Promise<string | null | undefined>;
17
17
  type PriceOSNextHandlerOptions = {
18
18
  apiKey?: string;
19
19
  baseUrl?: string;
20
20
  basePath?: string;
21
- identify?: PriceOSIdentify;
21
+ getCustomerId?: PriceOSGetCustomerId;
22
22
  };
23
- type PriceOSPagesHandlerOptions = Omit<PriceOSNextHandlerOptions, "identify"> & {
24
- identify?: PriceOSPagesIdentify;
23
+ type PriceOSPagesHandlerOptions = Omit<PriceOSNextHandlerOptions, "getCustomerId"> & {
24
+ getCustomerId?: PriceOSPagesGetCustomerId;
25
25
  };
26
26
  declare function priceosHandler(options?: PriceOSNextHandlerOptions): (request: Request) => Promise<Response>;
27
27
  declare function priceosPagesHandler(options?: PriceOSPagesHandlerOptions): (req: PriceOSPagesRequest, res: PriceOSPagesResponse) => Promise<void>;
28
28
 
29
- export { type PriceOSIdentify, type PriceOSNextHandlerOptions, type PriceOSPagesHandlerOptions, type PriceOSPagesIdentify, type PriceOSPagesRequest, type PriceOSPagesResponse, priceosHandler, priceosPagesHandler };
29
+ export { type PriceOSGetCustomerId, type PriceOSNextHandlerOptions, type PriceOSPagesGetCustomerId, type PriceOSPagesHandlerOptions, type PriceOSPagesRequest, type PriceOSPagesResponse, priceosHandler, priceosPagesHandler };
package/dist/index.js CHANGED
@@ -9,15 +9,15 @@ var jsonResponse = (status, body) => new Response(JSON.stringify(body), {
9
9
  function priceosHandler(options = {}) {
10
10
  const envApiKey = globalThis.process?.env?.PRICEOS_API_KEY;
11
11
  const apiKey = options.apiKey ?? envApiKey;
12
- const identify = options.identify;
12
+ const getCustomerId = options.getCustomerId;
13
13
  const baseUrl = (options.baseUrl ?? "https://api.priceos.com").replace(/\/$/, "");
14
14
  const basePath = (options.basePath ?? DEFAULT_BASE_PATH).replace(/\/+$/, "");
15
15
  return async (request) => {
16
16
  if (!apiKey) {
17
17
  return new Response("Missing PRICEOS_API_KEY.", { status: 500 });
18
18
  }
19
- if (!identify) {
20
- return jsonResponse(500, { error: "Missing identify function" });
19
+ if (!getCustomerId) {
20
+ return jsonResponse(500, { error: "Missing getCustomerId function" });
21
21
  }
22
22
  let path = "";
23
23
  const url = new URL(request.url);
@@ -34,13 +34,13 @@ function priceosHandler(options = {}) {
34
34
  return new Response("Method not allowed.", { status: 405 });
35
35
  }
36
36
  if (url.searchParams.has("customerId")) {
37
- return jsonResponse(400, { error: "customerId must be set via identify" });
37
+ return jsonResponse(400, { error: "customerId must be set via getCustomerId" });
38
38
  }
39
39
  let customerId;
40
40
  try {
41
- customerId = await identify(request);
41
+ customerId = await getCustomerId(request);
42
42
  } catch (error) {
43
- return jsonResponse(500, { error: "Failed to identify customer" });
43
+ return jsonResponse(500, { error: "Failed to resolve customerId" });
44
44
  }
45
45
  if (typeof customerId !== "string" || !customerId) {
46
46
  return jsonResponse(401, { error: "Customer not identified" });
@@ -94,10 +94,10 @@ var sendResponse = async (res, response) => {
94
94
  };
95
95
  function priceosPagesHandler(options = {}) {
96
96
  return async (req, res) => {
97
- const identify = options.identify;
97
+ const getCustomerId = options.getCustomerId;
98
98
  const handler = priceosHandler({
99
99
  ...options,
100
- identify: identify ? () => identify(req) : void 0
100
+ getCustomerId: getCustomerId ? () => getCustomerId(req) : void 0
101
101
  });
102
102
  const request = buildRequest(req);
103
103
  const response = await handler(request);
package/dist/index.js.map CHANGED
@@ -1 +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,YAAq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AK;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":[]}
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 baseUrl?: 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 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 (!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, { 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 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.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 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;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,gBAAgB,QAAQ;AAC9B,Q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AI,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":[]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@priceos/next",
3
- "version": "0.0.5",
3
+ "version": "0.0.8",
4
4
  "private": false,
5
5
  "type": "module",
6
6
  "main": "dist/index.cjs",