commerce-kit 0.9.1 → 0.11.0
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/README.md +113 -0
- package/dist/index.d.ts +11 -7
- package/dist/index.js +1 -1
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/README.md
ADDED
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
# commerce-kit
|
|
2
|
+
|
|
3
|
+
TypeScript SDK for the [Your Next Store](https://yournextstore.com) API.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
bun add commerce-kit
|
|
9
|
+
# or
|
|
10
|
+
npm install commerce-kit
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
## Quick Start
|
|
14
|
+
|
|
15
|
+
```typescript
|
|
16
|
+
import { Commerce } from 'commerce-kit';
|
|
17
|
+
|
|
18
|
+
const commerce = Commerce();
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
That's it! The SDK reads `YNS_API_TOKEN` from environment and auto-detects the correct store and API base URL: `https://yns.store`
|
|
22
|
+
|
|
23
|
+
## Products
|
|
24
|
+
|
|
25
|
+
```typescript
|
|
26
|
+
// List products
|
|
27
|
+
const { data, meta } = await commerce.productBrowse({
|
|
28
|
+
limit: 10,
|
|
29
|
+
offset: 0,
|
|
30
|
+
category: 'shoes',
|
|
31
|
+
query: 'running',
|
|
32
|
+
active: true,
|
|
33
|
+
orderBy: 'createdAt',
|
|
34
|
+
orderDirection: 'desc',
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
// Get single product by ID or slug
|
|
38
|
+
const product = await commerce.productGet({ idOrSlug: 'running-shoes' });
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
## Cart
|
|
42
|
+
|
|
43
|
+
```typescript
|
|
44
|
+
// Create or update cart
|
|
45
|
+
const cart = await commerce.cartUpsert({
|
|
46
|
+
cartId: 'cart_123', // optional, creates new if omitted
|
|
47
|
+
variantId: 'variant_456',
|
|
48
|
+
quantity: 2,
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
// Get cart
|
|
52
|
+
const cart = await commerce.cartGet({ cartId: 'cart_123' });
|
|
53
|
+
|
|
54
|
+
// Remove item from cart
|
|
55
|
+
await commerce.cartRemoveItem({
|
|
56
|
+
cartId: 'cart_123',
|
|
57
|
+
variantId: 'variant_456',
|
|
58
|
+
});
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
## Orders
|
|
62
|
+
|
|
63
|
+
```typescript
|
|
64
|
+
// List orders
|
|
65
|
+
const { data, meta } = await commerce.orderBrowse({
|
|
66
|
+
limit: 10,
|
|
67
|
+
offset: 0,
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
// Get single order
|
|
71
|
+
const order = await commerce.orderGet({ id: 'order_789' });
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
## Raw API Requests
|
|
75
|
+
|
|
76
|
+
For endpoints not yet implemented in the SDK, use the `request()` method:
|
|
77
|
+
|
|
78
|
+
```typescript
|
|
79
|
+
// GET request
|
|
80
|
+
const webhooks = await commerce.request<Webhook[]>('/webhooks');
|
|
81
|
+
|
|
82
|
+
// POST with body
|
|
83
|
+
const webhook = await commerce.request<Webhook, CreateWebhookBody>('/webhooks', {
|
|
84
|
+
method: 'POST',
|
|
85
|
+
body: { url: 'https://example.com/hook', events: ['order.created'] },
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
// GET with query parameters
|
|
89
|
+
const results = await commerce.request<SearchResult>('/search', {
|
|
90
|
+
query: { q: 'shoes', limit: 5 },
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
// Path parameters via template literals
|
|
94
|
+
const variant = await commerce.request<Variant>(`/products/${productId}/variants/${variantId}`);
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
## TypeScript
|
|
98
|
+
|
|
99
|
+
All API types are exported:
|
|
100
|
+
|
|
101
|
+
```typescript
|
|
102
|
+
import type {
|
|
103
|
+
APIProductsBrowseResult,
|
|
104
|
+
APIProductGetByIdResult,
|
|
105
|
+
APICartCreateBody,
|
|
106
|
+
APIOrderGetByIdResult,
|
|
107
|
+
CommerceConfig,
|
|
108
|
+
} from 'commerce-kit';
|
|
109
|
+
```
|
|
110
|
+
|
|
111
|
+
## License
|
|
112
|
+
|
|
113
|
+
AGPL-3.0-only
|
package/dist/index.d.ts
CHANGED
|
@@ -1,14 +1,16 @@
|
|
|
1
1
|
import { APIProductsBrowseQueryParams, APIProductsBrowseResult, APIProductGetByIdParams, APIProductGetByIdResult, APIOrdersBrowseQueryParams, APIOrdersBrowseResult, APIOrderGetByIdParams, APIOrderGetByIdResult, APICartCreateBody, APICartCreateResult, APICartGetResult } from './api-types.js';
|
|
2
2
|
export { APICartAddBody, APICartAddResult, APICartRemoveItemQueryParams, APICartRemoveItemResult, JSONContent } from './api-types.js';
|
|
3
3
|
|
|
4
|
-
interface
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
4
|
+
interface CommerceConfig {
|
|
5
|
+
/** API key. Defaults to process.env.YNS_API_TOKEN */
|
|
6
|
+
token?: string;
|
|
7
|
+
endpoint?: string;
|
|
8
|
+
version?: "v1";
|
|
8
9
|
}
|
|
9
|
-
|
|
10
|
+
|
|
11
|
+
declare class YNSProvider {
|
|
10
12
|
#private;
|
|
11
|
-
constructor(config
|
|
13
|
+
constructor(config?: CommerceConfig);
|
|
12
14
|
productBrowse(params: APIProductsBrowseQueryParams): Promise<APIProductsBrowseResult>;
|
|
13
15
|
productGet(params: APIProductGetByIdParams): Promise<APIProductGetByIdResult | null>;
|
|
14
16
|
orderBrowse(params: APIOrdersBrowseQueryParams): Promise<APIOrdersBrowseResult>;
|
|
@@ -50,4 +52,6 @@ declare class YnsProvider {
|
|
|
50
52
|
}): Promise<TResponse>;
|
|
51
53
|
}
|
|
52
54
|
|
|
53
|
-
|
|
55
|
+
declare function Commerce(config?: CommerceConfig): YNSProvider;
|
|
56
|
+
|
|
57
|
+
export { APICartCreateBody, APICartCreateResult, APICartGetResult, APIOrderGetByIdParams, APIOrderGetByIdResult, APIOrdersBrowseQueryParams, APIOrdersBrowseResult, APIProductGetByIdParams, APIProductGetByIdResult, APIProductsBrowseQueryParams, APIProductsBrowseResult, Commerce, type CommerceConfig, YNSProvider };
|
package/dist/index.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import"./chunk-PNKB6P4V.js";var n={DEBUG:0,LOG:1,WARN:2,ERROR:3},
|
|
1
|
+
import"./chunk-PNKB6P4V.js";var f=process.env.YNS_TOKEN,p={YNS_TOKEN:f,YNS_API_TOKEN:f};var n={DEBUG:0,LOG:1,WARN:2,ERROR:3},R=process.env.NEXT_PUBLIC_LOG_LEVEL||"LOG",a=n[R],P="\x1B[0m",E="\x1B[34m",w="\x1B[32m",B="\x1B[33m",h="\x1B[31m",$="\u23F1\uFE0F",A="\u{1F41B}",L="\u2714\uFE0F",O="\u26A0\uFE0F",S="\u274C",y=`${$} `,T=`${E}${A}${P} `,G=`${w}${L}${P} `,C=`${B}${O}${P} `,v=`${h}${S}${P} `,g=c=>{let e=c?`[${c}] `:"";return{getLogger(t){return g([c,t].filter(Boolean).join(" > "))},time(t){a>n.DEBUG||console.time([y,e,t].filter(Boolean).join(" "))},timeEnd(t){a>n.DEBUG||console.timeEnd([y,e,t].filter(Boolean).join(" "))},debug(...t){a>n.DEBUG||console.log(...[T,e,...t].filter(Boolean))},log(...t){a>n.LOG||console.log(...[G,e,...t].filter(Boolean))},dir(t,r){a>n.LOG||console.dir(t,r)},warn(...t){a>n.WARN||console.warn(...[C,e,...t].filter(Boolean))},error(...t){a>n.ERROR||console.error(...[v,e,...t].filter(Boolean))}}},x=g();var d=class{#t;#r=g("YNSProvider");constructor(e={}){let t=e.token??p.YNS_API_TOKEN;if(!t)throw new Error("YNS API key is required. Set YNS_API_TOKEN environment variable or pass token in config.");let r=e.endpoint??"https://yns.store";this.#t={version:"v1",...e,token:t,endpoint:r},this.#r.debug("YNSProvider initialized",{endpoint:r,token:e.token?"from config":"from env"})}async#e(e,t="GET",r){let i=this.#r.getLogger("#restRequest"),s=`${this.#t.endpoint}/api/${this.#t.version}${e}`;i.debug(`Making ${t} request to YNS API: ${s}`);let o=await fetch(s,{method:t,headers:{"Content-Type":"application/json",Authorization:`Bearer ${this.#t.token}`},body:r?JSON.stringify(r):void 0});if(!o.ok){let I=o.headers.get("content-type"),l=`YNS REST request failed: ${t} ${s} ${o.status} ${o.statusText}`;if(I?.includes("application/json"))try{let u=await o.json();l=u.error||u.message||l}catch(u){i.error("Failed to parse YNS error response as JSON",u)}else{let u=await o.text();i.error(`YNS API request failed: ${o.status} ${o.statusText}`,l,u)}throw new Error(l)}let m=o.headers.get("content-type");if(!m?.includes("application/json"))throw new Error(`YNS API returned ${m} instead of JSON for ${e}`);return o.json()}async productBrowse(e){let t=this.#r.getLogger("productBrowse");t.debug("Browsing products with params:",e);let r=new URLSearchParams;e.limit&&r.append("limit",e.limit.toString()),e.offset&&r.append("offset",e.offset.toString()),e.category&&r.append("category",e.category),e.query&&r.append("query",e.query),e.active!==void 0&&r.append("active",e.active.toString()),e.orderBy&&r.append("orderBy",e.orderBy),e.orderDirection&&r.append("orderDirection",e.orderDirection);let s=`/products${r.size?`?${r}`:""}`;t.debug("Constructed pathname:",s);let o=await this.#e(s);return t.debug("Received product browse result:",{meta:o.meta}),o}async productGet(e){let t=`/products/${e.idOrSlug}`,r=await this.#e(t);return r||null}async orderBrowse(e){let t=this.#r.getLogger("orderBrowse");t.debug("Browsing orders with params:",e);let r=new URLSearchParams;e.limit&&r.append("limit",e.limit.toString()),e.offset&&r.append("offset",e.offset.toString());let s=`/orders${r.size?`?${r}`:""}`;t.debug("Constructed pathname:",s);let o=await this.#e(s);return t.debug("Received orders browse result:",{meta:o.meta}),o}async orderGet(e){let t=`/orders/${e.id}`,r=await this.#e(t);return r||null}async cartUpsert(e){return await this.#e("/carts","POST",e)}async cartRemoveItem(e){let t=`/carts/${e.cartId}/line-items/${e.variantId}`,r=await this.#e(t,"DELETE");return null}async cartGet(e){let t=`/carts/${e.cartId}`;return await this.#e(t)}async request(e,t){let r=t?.query?`?${new URLSearchParams(Object.entries(t.query).map(([i,s])=>[i,String(s)]))}`:"";return this.#e(`${e}${r}`,t?.method??"GET",t?.body)}};function N(c={}){return new d(c)}export{N as Commerce,d as YNSProvider};
|
|
2
2
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/logger.ts","../src/providers/yns.ts"],"sourcesContent":["import type { InspectOptions } from \"node:util\";\n\ntype LogParms = [message: unknown, ...optionalParams: unknown[]];\n\n/**\n * Vercel only supports 3 levels of logging. We're adding additional DEBUG level.\n * https://vercel.com/docs/observability/runtime-logs#level\n *\n * ERROR - Fatal for a particular request. Should be fixed sooner than later.\n *\n * WARN - A note on something that should probably be looked at eventually.\n *\n * LOG - Detail on regular operation.\n *\n * DEBUG - Debug only info as well as time and timeEnd functions.\n */\nconst LogLevel = {\n\tDEBUG: 0,\n\tLOG: 1,\n\tWARN: 2,\n\tERROR: 3,\n} as const;\ntype LogLevel = keyof typeof LogLevel;\n\nconst strLogLevel = (process.env.NEXT_PUBLIC_LOG_LEVEL || \"LOG\") as LogLevel;\nconst valueLogLevel = LogLevel[strLogLevel];\n\nconst RESET = \"\\x1b[0m\";\nconst BLUE = \"\\x1b[34m\";\nconst GREEN = \"\\x1b[32m\";\nconst YELLOW = \"\\x1b[33m\";\nconst RED = \"\\x1b[31m\";\n\nconst TIME = `⏱️`;\nconst DEBUG = `🐛`;\nconst OK = `✔️`;\nconst WARN = `⚠️`;\nconst ERROR = `❌`;\n\nconst PREFIX_TIME = `${TIME} `;\nconst PREFIX_DEBUG = `${BLUE}${DEBUG}${RESET} `;\nconst PREFIX_OK = `${GREEN}${OK}${RESET} `;\nconst PREFIX_WARN = `${YELLOW}${WARN}${RESET} `;\nconst PREFIX_ERROR = `${RED}${ERROR}${RESET} `;\n\nexport const getLogger = (groupLabel?: string) => {\n\tconst PREFIX = groupLabel ? `[${groupLabel}] ` : \"\";\n\treturn {\n\t\tgetLogger(subGroupLabel: string) {\n\t\t\treturn getLogger([groupLabel, subGroupLabel].filter(Boolean).join(\" > \"));\n\t\t},\n\t\ttime(label: string) {\n\t\t\tif (valueLogLevel > LogLevel.DEBUG) return;\n\t\t\tconsole.time([PREFIX_TIME, PREFIX, label].filter(Boolean).join(\" \"));\n\t\t},\n\t\ttimeEnd(label: string) {\n\t\t\tif (valueLogLevel > LogLevel.DEBUG) return;\n\t\t\tconsole.timeEnd([PREFIX_TIME, PREFIX, label].filter(Boolean).join(\" \"));\n\t\t},\n\t\tdebug(...args: LogParms) {\n\t\t\tif (valueLogLevel > LogLevel.DEBUG) return;\n\t\t\tconsole.log(...[PREFIX_DEBUG, PREFIX, ...args].filter(Boolean));\n\t\t},\n\t\tlog(...args: LogParms) {\n\t\t\tif (valueLogLevel > LogLevel.LOG) return;\n\t\t\tconsole.log(...[PREFIX_OK, PREFIX, ...args].filter(Boolean));\n\t\t},\n\t\tdir(item?: unknown, options?: InspectOptions) {\n\t\t\tif (valueLogLevel > LogLevel.LOG) return;\n\t\t\tconsole.dir(item, options);\n\t\t},\n\t\twarn(...args: LogParms) {\n\t\t\tif (valueLogLevel > LogLevel.WARN) return;\n\t\t\tconsole.warn(...[PREFIX_WARN, PREFIX, ...args].filter(Boolean));\n\t\t},\n\t\terror(...args: LogParms) {\n\t\t\tif (valueLogLevel > LogLevel.ERROR) return;\n\t\t\tconsole.error(...[PREFIX_ERROR, PREFIX, ...args].filter(Boolean));\n\t\t},\n\t};\n};\n\nexport const logger = getLogger();\n","import type {\n\tAPICartCreateBody,\n\tAPICartCreateResult,\n\tAPICartGetResult,\n\tAPIOrderGetByIdParams,\n\tAPIOrderGetByIdResult,\n\tAPIOrdersBrowseQueryParams,\n\tAPIOrdersBrowseResult,\n\tAPIProductGetByIdParams,\n\tAPIProductGetByIdResult,\n\tAPIProductsBrowseQueryParams,\n\tAPIProductsBrowseResult,\n} from \"../api-types\";\nimport { getLogger } from \"../logger\";\n\ninterface YnsProviderConfig {\n\tversion: \"v1\";\n\tendpoint: string;\n\ttoken: string;\n}\n\nexport class YnsProvider {\n\t#config;\n\t#logger = getLogger(\"YnsProvider\");\n\tconstructor(config: YnsProviderConfig) {\n\t\tthis.#config = config;\n\t\tthis.#logger.debug(\"YnsProvider initialized\");\n\t}\n\n\tasync #restRequest<T>(\n\t\tpathname: `/${string}`,\n\t\tmethod: \"GET\" | \"POST\" | \"PUT\" | \"DELETE\" = \"GET\",\n\t\tbody?: unknown,\n\t): Promise<T> {\n\t\tconst logger = this.#logger.getLogger(\"#restRequest\");\n\n\t\tconst endpoint = `${this.#config.endpoint}/api/${this.#config.version}${pathname}`;\n\t\tlogger.debug(`Making ${method} request to YNS API: ${endpoint}`);\n\t\tconst response = await fetch(endpoint, {\n\t\t\tmethod,\n\t\t\theaders: {\n\t\t\t\t\"Content-Type\": \"application/json\",\n\t\t\t\tAuthorization: `Bearer ${this.#config.token}`,\n\t\t\t},\n\t\t\tbody: body ? JSON.stringify(body) : undefined,\n\t\t});\n\n\t\tif (!response.ok) {\n\t\t\t// Handle different error types\n\t\t\tconst contentType = response.headers.get(\"content-type\");\n\t\t\tlet errorMessage = `YNS REST request failed: ${method} ${endpoint} ${response.status} ${response.statusText}`;\n\n\t\t\tif (contentType?.includes(\"application/json\")) {\n\t\t\t\ttry {\n\t\t\t\t\tconst errorData = await response.json();\n\t\t\t\t\terrorMessage = errorData.error || errorData.message || errorMessage;\n\t\t\t\t} catch (error) {\n\t\t\t\t\tlogger.error(\"Failed to parse YNS error response as JSON\", error);\n\t\t\t\t}\n\t\t\t} else {\n\t\t\t\tconst errorText = await response.text();\n\t\t\t\tlogger.error(\n\t\t\t\t\t`YNS API request failed: ${response.status} ${response.statusText}`,\n\t\t\t\t\terrorMessage,\n\t\t\t\t\terrorText,\n\t\t\t\t);\n\t\t\t}\n\n\t\t\tthrow new Error(errorMessage);\n\t\t}\n\n\t\t// Check if response is JSON before parsing\n\t\tconst contentType = response.headers.get(\"content-type\");\n\t\tif (!contentType?.includes(\"application/json\")) {\n\t\t\tthrow new Error(`YNS API returned ${contentType} instead of JSON for ${pathname}`);\n\t\t}\n\n\t\treturn response.json();\n\t}\n\n\tasync productBrowse(params: APIProductsBrowseQueryParams): Promise<APIProductsBrowseResult> {\n\t\tconst logger = this.#logger.getLogger(\"productBrowse\");\n\t\tlogger.debug(\"Browsing products with params:\", params);\n\n\t\tconst queryParams = new URLSearchParams();\n\t\tif (params.limit) queryParams.append(\"limit\", params.limit.toString());\n\t\tif (params.offset) queryParams.append(\"offset\", params.offset.toString());\n\t\tif (params.category) queryParams.append(\"category\", params.category);\n\t\tif (params.query) queryParams.append(\"query\", params.query);\n\t\tif (params.active !== undefined) queryParams.append(\"active\", params.active.toString());\n\t\tif (params.orderBy) queryParams.append(\"orderBy\", params.orderBy);\n\t\tif (params.orderDirection) queryParams.append(\"orderDirection\", params.orderDirection);\n\n\t\tconst searchParams = queryParams.size ? `?${queryParams}` : \"\";\n\t\tconst pathname = `/products${searchParams}` as const;\n\t\tlogger.debug(\"Constructed pathname:\", pathname);\n\t\tconst result = await this.#restRequest<APIProductsBrowseResult>(pathname);\n\t\tlogger.debug(\"Received product browse result:\", { meta: result.meta });\n\t\treturn result;\n\t}\n\n\tasync productGet(params: APIProductGetByIdParams): Promise<APIProductGetByIdResult | null> {\n\t\tconst pathname = `/products/${params.idOrSlug}` as const;\n\n\t\tconst result = await this.#restRequest<APIProductGetByIdResult>(pathname);\n\n\t\tif (!result) {\n\t\t\treturn null;\n\t\t}\n\n\t\treturn result;\n\t}\n\n\tasync orderBrowse(params: APIOrdersBrowseQueryParams): Promise<APIOrdersBrowseResult> {\n\t\tconst logger = this.#logger.getLogger(\"orderBrowse\");\n\t\tlogger.debug(\"Browsing orders with params:\", params);\n\n\t\tconst queryParams = new URLSearchParams();\n\n\t\tif (params.limit) queryParams.append(\"limit\", params.limit.toString());\n\t\tif (params.offset) queryParams.append(\"offset\", params.offset.toString());\n\n\t\tconst searchParams = queryParams.size ? `?${queryParams}` : \"\";\n\t\tconst pathname = `/orders${searchParams}` as const;\n\t\tlogger.debug(\"Constructed pathname:\", pathname);\n\t\tconst result = await this.#restRequest<APIOrdersBrowseResult>(pathname);\n\t\tlogger.debug(\"Received orders browse result:\", { meta: result.meta });\n\t\treturn result;\n\t}\n\n\tasync orderGet(params: APIOrderGetByIdParams): Promise<APIOrderGetByIdResult | null> {\n\t\tconst pathname = `/orders/${params.id}` as const;\n\n\t\tconst result = await this.#restRequest<APIOrderGetByIdResult>(pathname);\n\n\t\tif (!result) {\n\t\t\treturn null;\n\t\t}\n\n\t\treturn result;\n\t}\n\n\t// @todo\n\t// async cartAdd(params: APICartAddBody): Promise<APICartAddResult> {\n\t// \tconst body = {\n\t// \t\tvariantId: params.variantId,\n\t// \t\tcartId: params.cartId,\n\t// \t\tquantity: params.quantity,\n\t// \t\tsubscriptionId: params.subscriptionId,\n\t// \t};\n\n\t// \tconst result = await this.#restRequest<APICartCreateResult>(\"/carts\", \"PUT\", body);\n\t// \treturn result;\n\t// }\n\n\tasync cartUpsert(body: APICartCreateBody): Promise<APICartCreateResult> {\n\t\tconst result = await this.#restRequest<APICartCreateResult>(\"/carts\", \"POST\", body);\n\t\treturn result;\n\t}\n\n\tasync cartRemoveItem(params: { cartId: string; variantId: string }): Promise<null> {\n\t\tconst pathname = `/carts/${params.cartId}/line-items/${params.variantId}` as const;\n\t\tconst result = await this.#restRequest<unknown>(pathname, \"DELETE\");\n\t\t// return result;\n\t\treturn null;\n\t}\n\n\tasync cartGet(params: { cartId: string }): Promise<APICartGetResult | null> {\n\t\tconst pathname = `/carts/${params.cartId}` as const;\n\t\tconst result = await this.#restRequest<APICartGetResult>(pathname);\n\t\treturn result;\n\t}\n\n\t/**\n\t * Make a raw API request to any endpoint.\n\t * Use this for new API features not yet supported by typed methods.\n\t *\n\t * @example\n\t * // GET request (default method)\n\t * const webhooks = await provider.request<Webhook[]>('/webhooks');\n\t *\n\t * // POST with typed body\n\t * const webhook = await provider.request<Webhook, CreateWebhookBody>('/webhooks', {\n\t * method: 'POST',\n\t * body: { url: 'https://...' }\n\t * });\n\t *\n\t * // GET with query parameters\n\t * const products = await provider.request<Product[]>('/products', {\n\t * query: { limit: 10, category: 'shoes' }\n\t * });\n\t *\n\t * // Path parameters via template literals\n\t * const product = await provider.request<Product>(`/products/${id}`);\n\t */\n\tasync request<TResponse = unknown, TBody = unknown>(\n\t\tpathname: `/${string}`,\n\t\toptions?: {\n\t\t\tmethod?: \"GET\" | \"POST\" | \"PUT\" | \"DELETE\";\n\t\t\tbody?: TBody;\n\t\t\tquery?: Record<string, string | number | boolean>;\n\t\t},\n\t): Promise<TResponse> {\n\t\tconst queryString = options?.query\n\t\t\t? `?${new URLSearchParams(Object.entries(options.query).map(([k, v]) => [k, String(v)]))}`\n\t\t\t: \"\";\n\n\t\treturn this.#restRequest<TResponse>(\n\t\t\t`${pathname}${queryString}` as `/${string}`,\n\t\t\toptions?.method ?? \"GET\",\n\t\t\toptions?.body,\n\t\t);\n\t}\n}\n"],"mappings":"4BAgBA,IAAMA,EAAW,CAChB,MAAO,EACP,IAAK,EACL,KAAM,EACN,MAAO,CACR,EAGMC,EAAe,QAAQ,IAAI,uBAAyB,MACpDC,EAAgBF,EAASC,CAAW,EAEpCE,EAAQ,UACRC,EAAO,WACPC,EAAQ,WACRC,EAAS,WACTC,EAAM,WAENC,EAAO,eACPC,EAAQ,YACRC,EAAK,eACLC,EAAO,eACPC,EAAQ,SAERC,EAAc,GAAGL,CAAI,IACrBM,EAAe,GAAGV,CAAI,GAAGK,CAAK,GAAGN,CAAK,IACtCY,EAAY,GAAGV,CAAK,GAAGK,CAAE,GAAGP,CAAK,IACjCa,EAAc,GAAGV,CAAM,GAAGK,CAAI,GAAGR,CAAK,IACtCc,EAAe,GAAGV,CAAG,GAAGK,CAAK,GA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MAAM,iCAAkC,CAAE,KAAMU,EAAO,IAAK,CAAC,EAC7DA,CACR,CAEA,MAAM,SAASF,EAAsE,CACpF,IAAMX,EAAW,WAAWW,EAAO,EAAE,GAE/BE,EAAS,MAAM,KAAKd,GAAoCC,CAAQ,EAEtE,OAAKa,GACG,IAIT,CAeA,MAAM,WAAWX,EAAuD,CAEvE,OADe,MAAM,KAAKH,GAAkC,SAAU,OAAQG,CAAI,CAEnF,CAEA,MAAM,eAAeS,EAA8D,CAClF,IAAMX,EAAW,UAAUW,EAAO,MAAM,eAAeA,EAAO,SAAS,GACjEE,EAAS,MAAM,KAAKd,GAAsBC,EAAU,QAAQ,EAElE,OAAO,IACR,CAEA,MAAM,QAAQW,EAA8D,CAC3E,IAAMX,EAAW,UAAUW,EAAO,MAAM,GAExC,OADe,MAAM,KAAKZ,GAA+BC,CAAQ,CAElE,CAwBA,MAAM,QACLA,EACAc,EAKqB,CACrB,IAAMC,EAAcD,GAAS,MAC1B,IAAI,IAAI,gBAAgB,OAAO,QAAQA,EAAQ,KAAK,EAAE,IAAI,CAAC,CAACE,EAAGC,CAAC,IAAM,CAACD,EAAG,OAAOC,CAAC,CAAC,CAAC,CAAC,CAAC,GACtF,GAEH,OAAO,KAAKlB,GACX,GAAGC,CAAQ,GAAGe,CAAW,GACzBD,GAAS,QAAU,MACnBA,GAAS,IACV,CACD,CACD","names":["LogLevel","strLogLevel","valueLogLevel","RESET","BLUE","GREEN","YELLOW","RED","TIME","DEBUG","OK","WARN","ERROR","PREFIX_TIME","PREFIX_DEBUG","PREFIX_OK","PREFIX_WARN","PREFIX_ERROR","getLogger","groupLabel","PREFIX","subGroupLabel","label","args","item","options","logger","YnsProvider","#config","#logger","getLogger","config","#restRequest","pathname","method","body","logger","endpoint","response","contentType","errorMessage","errorData","error","errorText","params","queryParams","result","options","queryString","k","v"]}
|
|
1
|
+
{"version":3,"sources":["../src/env.ts","../src/logger.ts","../src/providers/yns.ts","../src/commerce.ts"],"sourcesContent":["const YNS_TOKEN = process.env.YNS_TOKEN;\n\nexport const env = {\n\tYNS_TOKEN,\n\tYNS_API_TOKEN: YNS_TOKEN,\n};\n","import type { InspectOptions } from \"node:util\";\n\ntype LogParms = [message: unknown, ...optionalParams: unknown[]];\n\n/**\n * Vercel only supports 3 levels of logging. We're adding additional DEBUG level.\n * https://vercel.com/docs/observability/runtime-logs#level\n *\n * ERROR - Fatal for a particular request. Should be fixed sooner than later.\n *\n * WARN - A note on something that should probably be looked at eventually.\n *\n * LOG - Detail on regular operation.\n *\n * DEBUG - Debug only info as well as time and timeEnd functions.\n */\nconst LogLevel = {\n\tDEBUG: 0,\n\tLOG: 1,\n\tWARN: 2,\n\tERROR: 3,\n} as const;\ntype LogLevel = keyof typeof LogLevel;\n\nconst strLogLevel = (process.env.NEXT_PUBLIC_LOG_LEVEL || \"LOG\") as LogLevel;\nconst valueLogLevel = LogLevel[strLogLevel];\n\nconst RESET = \"\\x1b[0m\";\nconst BLUE = \"\\x1b[34m\";\nconst GREEN = \"\\x1b[32m\";\nconst YELLOW = \"\\x1b[33m\";\nconst RED = \"\\x1b[31m\";\n\nconst TIME = `⏱️`;\nconst DEBUG = `🐛`;\nconst OK = `✔️`;\nconst WARN = `⚠️`;\nconst ERROR = `❌`;\n\nconst PREFIX_TIME = `${TIME} `;\nconst PREFIX_DEBUG = `${BLUE}${DEBUG}${RESET} `;\nconst PREFIX_OK = `${GREEN}${OK}${RESET} `;\nconst PREFIX_WARN = `${YELLOW}${WARN}${RESET} `;\nconst PREFIX_ERROR = `${RED}${ERROR}${RESET} `;\n\nexport const getLogger = (groupLabel?: string) => {\n\tconst PREFIX = groupLabel ? `[${groupLabel}] ` : \"\";\n\treturn {\n\t\tgetLogger(subGroupLabel: string) {\n\t\t\treturn getLogger([groupLabel, subGroupLabel].filter(Boolean).join(\" > \"));\n\t\t},\n\t\ttime(label: string) {\n\t\t\tif (valueLogLevel > LogLevel.DEBUG) return;\n\t\t\tconsole.time([PREFIX_TIME, PREFIX, label].filter(Boolean).join(\" \"));\n\t\t},\n\t\ttimeEnd(label: string) {\n\t\t\tif (valueLogLevel > LogLevel.DEBUG) return;\n\t\t\tconsole.timeEnd([PREFIX_TIME, PREFIX, label].filter(Boolean).join(\" \"));\n\t\t},\n\t\tdebug(...args: LogParms) {\n\t\t\tif (valueLogLevel > LogLevel.DEBUG) return;\n\t\t\tconsole.log(...[PREFIX_DEBUG, PREFIX, ...args].filter(Boolean));\n\t\t},\n\t\tlog(...args: LogParms) {\n\t\t\tif (valueLogLevel > LogLevel.LOG) return;\n\t\t\tconsole.log(...[PREFIX_OK, PREFIX, ...args].filter(Boolean));\n\t\t},\n\t\tdir(item?: unknown, options?: InspectOptions) {\n\t\t\tif (valueLogLevel > LogLevel.LOG) return;\n\t\t\tconsole.dir(item, options);\n\t\t},\n\t\twarn(...args: LogParms) {\n\t\t\tif (valueLogLevel > LogLevel.WARN) return;\n\t\t\tconsole.warn(...[PREFIX_WARN, PREFIX, ...args].filter(Boolean));\n\t\t},\n\t\terror(...args: LogParms) {\n\t\t\tif (valueLogLevel > LogLevel.ERROR) return;\n\t\t\tconsole.error(...[PREFIX_ERROR, PREFIX, ...args].filter(Boolean));\n\t\t},\n\t};\n};\n\nexport const logger = getLogger();\n","import type {\n\tAPICartCreateBody,\n\tAPICartCreateResult,\n\tAPICartGetResult,\n\tAPIOrderGetByIdParams,\n\tAPIOrderGetByIdResult,\n\tAPIOrdersBrowseQueryParams,\n\tAPIOrdersBrowseResult,\n\tAPIProductGetByIdParams,\n\tAPIProductGetByIdResult,\n\tAPIProductsBrowseQueryParams,\n\tAPIProductsBrowseResult,\n} from \"../api-types\";\nimport { env } from \"../env\";\nimport { getLogger } from \"../logger\";\nimport type { CommerceConfig } from \"../types\";\n\nexport class YNSProvider {\n\t#config: CommerceConfig;\n\t#logger = getLogger(\"YNSProvider\");\n\n\tconstructor(config: CommerceConfig = {}) {\n\t\tconst token = config.token ?? env.YNS_API_TOKEN;\n\t\tif (!token) {\n\t\t\tthrow new Error(\n\t\t\t\t\"YNS API key is required. Set YNS_API_TOKEN environment variable or pass token in config.\",\n\t\t\t);\n\t\t}\n\t\tconst endpoint = config.endpoint ?? \"https://yns.store\";\n\t\tthis.#config = { version: \"v1\", ...config, token, endpoint };\n\t\tthis.#logger.debug(\"YNSProvider initialized\", {\n\t\t\tendpoint,\n\t\t\ttoken: config.token ? `from config` : \"from env\",\n\t\t});\n\t}\n\n\tasync #restRequest<T>(\n\t\tpathname: `/${string}`,\n\t\tmethod: \"GET\" | \"POST\" | \"PUT\" | \"DELETE\" = \"GET\",\n\t\tbody?: unknown,\n\t): Promise<T> {\n\t\tconst logger = this.#logger.getLogger(\"#restRequest\");\n\n\t\tconst endpoint = `${this.#config.endpoint}/api/${this.#config.version}${pathname}`;\n\t\tlogger.debug(`Making ${method} request to YNS API: ${endpoint}`);\n\t\tconst response = await fetch(endpoint, {\n\t\t\tmethod,\n\t\t\theaders: {\n\t\t\t\t\"Content-Type\": \"application/json\",\n\t\t\t\tAuthorization: `Bearer ${this.#config.token}`,\n\t\t\t},\n\t\t\tbody: body ? JSON.stringify(body) : undefined,\n\t\t});\n\n\t\tif (!response.ok) {\n\t\t\t// Handle different error types\n\t\t\tconst contentType = response.headers.get(\"content-type\");\n\t\t\tlet errorMessage = `YNS REST request failed: ${method} ${endpoint} ${response.status} ${response.statusText}`;\n\n\t\t\tif (contentType?.includes(\"application/json\")) {\n\t\t\t\ttry {\n\t\t\t\t\tconst errorData = await response.json();\n\t\t\t\t\terrorMessage = errorData.error || errorData.message || errorMessage;\n\t\t\t\t} catch (error) {\n\t\t\t\t\tlogger.error(\"Failed to parse YNS error response as JSON\", error);\n\t\t\t\t}\n\t\t\t} else {\n\t\t\t\tconst errorText = await response.text();\n\t\t\t\tlogger.error(\n\t\t\t\t\t`YNS API request failed: ${response.status} ${response.statusText}`,\n\t\t\t\t\terrorMessage,\n\t\t\t\t\terrorText,\n\t\t\t\t);\n\t\t\t}\n\n\t\t\tthrow new Error(errorMessage);\n\t\t}\n\n\t\t// Check if response is JSON before parsing\n\t\tconst contentType = response.headers.get(\"content-type\");\n\t\tif (!contentType?.includes(\"application/json\")) {\n\t\t\tthrow new Error(`YNS API returned ${contentType} instead of JSON for ${pathname}`);\n\t\t}\n\n\t\treturn response.json();\n\t}\n\n\tasync productBrowse(params: APIProductsBrowseQueryParams): Promise<APIProductsBrowseResult> {\n\t\tconst logger = this.#logger.getLogger(\"productBrowse\");\n\t\tlogger.debug(\"Browsing products with params:\", params);\n\n\t\tconst queryParams = new URLSearchParams();\n\t\tif (params.limit) queryParams.append(\"limit\", params.limit.toString());\n\t\tif (params.offset) queryParams.append(\"offset\", params.offset.toString());\n\t\tif (params.category) queryParams.append(\"category\", params.category);\n\t\tif (params.query) queryParams.append(\"query\", params.query);\n\t\tif (params.active !== undefined) queryParams.append(\"active\", params.active.toString());\n\t\tif (params.orderBy) queryParams.append(\"orderBy\", params.orderBy);\n\t\tif (params.orderDirection) queryParams.append(\"orderDirection\", params.orderDirection);\n\n\t\tconst searchParams = queryParams.size ? `?${queryParams}` : \"\";\n\t\tconst pathname = `/products${searchParams}` as const;\n\t\tlogger.debug(\"Constructed pathname:\", pathname);\n\t\tconst result = await this.#restRequest<APIProductsBrowseResult>(pathname);\n\t\tlogger.debug(\"Received product browse result:\", { meta: result.meta });\n\t\treturn result;\n\t}\n\n\tasync productGet(params: APIProductGetByIdParams): Promise<APIProductGetByIdResult | null> {\n\t\tconst pathname = `/products/${params.idOrSlug}` as const;\n\n\t\tconst result = await this.#restRequest<APIProductGetByIdResult>(pathname);\n\n\t\tif (!result) {\n\t\t\treturn null;\n\t\t}\n\n\t\treturn result;\n\t}\n\n\tasync orderBrowse(params: APIOrdersBrowseQueryParams): Promise<APIOrdersBrowseResult> {\n\t\tconst logger = this.#logger.getLogger(\"orderBrowse\");\n\t\tlogger.debug(\"Browsing orders with params:\", params);\n\n\t\tconst queryParams = new URLSearchParams();\n\n\t\tif (params.limit) queryParams.append(\"limit\", params.limit.toString());\n\t\tif (params.offset) queryParams.append(\"offset\", params.offset.toString());\n\n\t\tconst searchParams = queryParams.size ? `?${queryParams}` : \"\";\n\t\tconst pathname = `/orders${searchParams}` as const;\n\t\tlogger.debug(\"Constructed pathname:\", pathname);\n\t\tconst result = await this.#restRequest<APIOrdersBrowseResult>(pathname);\n\t\tlogger.debug(\"Received orders browse result:\", { meta: result.meta });\n\t\treturn result;\n\t}\n\n\tasync orderGet(params: APIOrderGetByIdParams): Promise<APIOrderGetByIdResult | null> {\n\t\tconst pathname = `/orders/${params.id}` as const;\n\n\t\tconst result = await this.#restRequest<APIOrderGetByIdResult>(pathname);\n\n\t\tif (!result) {\n\t\t\treturn null;\n\t\t}\n\n\t\treturn result;\n\t}\n\n\t// @todo\n\t// async cartAdd(params: APICartAddBody): Promise<APICartAddResult> {\n\t// \tconst body = {\n\t// \t\tvariantId: params.variantId,\n\t// \t\tcartId: params.cartId,\n\t// \t\tquantity: params.quantity,\n\t// \t\tsubscriptionId: params.subscriptionId,\n\t// \t};\n\n\t// \tconst result = await this.#restRequest<APICartCreateResult>(\"/carts\", \"PUT\", body);\n\t// \treturn result;\n\t// }\n\n\tasync cartUpsert(body: APICartCreateBody): Promise<APICartCreateResult> {\n\t\tconst result = await this.#restRequest<APICartCreateResult>(\"/carts\", \"POST\", body);\n\t\treturn result;\n\t}\n\n\tasync cartRemoveItem(params: { cartId: string; variantId: string }): Promise<null> {\n\t\tconst pathname = `/carts/${params.cartId}/line-items/${params.variantId}` as const;\n\t\tconst result = await this.#restRequest<unknown>(pathname, \"DELETE\");\n\t\t// return result;\n\t\treturn null;\n\t}\n\n\tasync cartGet(params: { cartId: string }): Promise<APICartGetResult | null> {\n\t\tconst pathname = `/carts/${params.cartId}` as const;\n\t\tconst result = await this.#restRequest<APICartGetResult>(pathname);\n\t\treturn result;\n\t}\n\n\t/**\n\t * Make a raw API request to any endpoint.\n\t * Use this for new API features not yet supported by typed methods.\n\t *\n\t * @example\n\t * // GET request (default method)\n\t * const webhooks = await provider.request<Webhook[]>('/webhooks');\n\t *\n\t * // POST with typed body\n\t * const webhook = await provider.request<Webhook, CreateWebhookBody>('/webhooks', {\n\t * method: 'POST',\n\t * body: { url: 'https://...' }\n\t * });\n\t *\n\t * // GET with query parameters\n\t * const products = await provider.request<Product[]>('/products', {\n\t * query: { limit: 10, category: 'shoes' }\n\t * });\n\t *\n\t * // Path parameters via template literals\n\t * const product = await provider.request<Product>(`/products/${id}`);\n\t */\n\tasync request<TResponse = unknown, TBody = unknown>(\n\t\tpathname: `/${string}`,\n\t\toptions?: {\n\t\t\tmethod?: \"GET\" | \"POST\" | \"PUT\" | \"DELETE\";\n\t\t\tbody?: TBody;\n\t\t\tquery?: Record<string, string | number | boolean>;\n\t\t},\n\t) {\n\t\tconst queryString = options?.query\n\t\t\t? `?${new URLSearchParams(Object.entries(options.query).map(([k, v]) => [k, String(v)]))}`\n\t\t\t: \"\";\n\n\t\treturn this.#restRequest<TResponse>(`${pathname}${queryString}`, options?.method ?? \"GET\", options?.body);\n\t}\n}\n","import { YNSProvider } from \"./providers/yns\";\nimport type { CommerceConfig } from \"./types\";\n\nexport function Commerce(config: CommerceConfig = {}): YNSProvider {\n\treturn new YNSProvider(config);\n}\n"],"mappings":"4BAAA,IAAMA,EAAY,QAAQ,IAAI,UAEjBC,EAAM,CAClB,UAAAD,EACA,cAAeA,CAChB,ECWA,IAAME,EAAW,CAChB,MAAO,EACP,IAAK,EACL,KAAM,EACN,MAAO,CACR,EAGMC,EAAe,QAAQ,IAAI,uBAAyB,MACpDC,EAAgBF,EAASC,CAAW,EAEpCE,EAAQ,UACRC,EAAO,WACPC,EAAQ,WACRC,EAAS,WACTC,EAAM,WAENC,EAAO,eACPC,EAAQ,YACRC,EAAK,eACLC,EAAO,eACPC,EAAQ,SAERC,EAAc,GAAGL,CAAI,IACrBM,EAAe,GAAGV,CAAI,GAAGK,CAAK,GAAGN,CAAK,IACtCY,EAAY,GAAGV,CAAK,GAAGK,CAAE,GAAGP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AAgB,CAClE,OAAO,IAAIC,EAAYD,CAAM,CAC9B","names":["YNS_TOKEN","env","LogLevel","strLogLevel","valueLogLevel","RESET","BLUE","GREEN","YELLOW","RED","TIME","DEBUG","OK","WARN","ERROR","PREFIX_TIME","PREFIX_DEBUG","PREFIX_OK","PREFIX_WARN","PREFIX_ERROR","getLogger","groupLabel","PREFIX","subGroupLabel","label","args","item","options","logger","YNSProvider","#config","#logger","getLogger","config","token","env","endpoint","#restRequest","pathname","method","body","logger","response","contentType","errorMessage","errorData","error","errorText","params","queryParams","result","options","queryString","k","v","Commerce","config","YNSProvider"]}
|