commerce-kit 0.1.0 → 0.3.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/dist/yns.d.ts CHANGED
@@ -1,22 +1,99 @@
1
- type YnsFindStripeAccountResult = {
2
- stripeAccount: string | undefined;
3
- storeId: string | undefined;
4
- secretKey: string | undefined;
5
- publishableKey: string | undefined;
6
- };
7
- type YnsContextResult = {
8
- stripeAccount: string | undefined;
9
- storeId: string | undefined;
10
- secretKey: string | undefined;
11
- publishableKey: string | undefined;
12
- };
13
- declare global {
1
+ import { Y as YnsProviderConfig, i as ProductBrowseParams, j as ProductBrowseResult, k as ProductGetParams, P as Product, l as ProductSearchParams, m as ProductSearchResult, a as CartAddParams, e as CartUpdateParams, b as CartClearParams, c as CartGetParams, C as Cart, f as OrderGetParams, O as Order, g as OrderListParams, h as OrderListResult } from './provider-Co3VegT-.js';
2
+
3
+ /**
4
+ * YNS Commerce client - zero-config constructor with GraphQL support
5
+ * Reads configuration from environment variables by default
6
+ *
7
+ * @example
8
+ * ```typescript
9
+ * import { Commerce } from "commerce-kit/yns";
10
+ *
11
+ * // Zero config - uses YNS_ENDPOINT and YNS_TOKEN from environment
12
+ * const commerce = new Commerce();
13
+ *
14
+ * // GraphQL field selection supported
15
+ * const products = await commerce.product.browse({
16
+ * first: 10,
17
+ * fields: ["id", "name", "price", "category.name"]
18
+ * });
19
+ *
20
+ * const orders = await commerce.order.list({ first: 5 });
21
+ * ```
22
+ */
23
+ declare class Commerce {
24
+ private config;
25
+ private provider?;
26
+ private providerPromise?;
27
+ constructor(config?: YnsProviderConfig);
28
+ /**
29
+ * Detect YNS configuration from environment variables
30
+ */
31
+ private detectFromEnv;
32
+ /**
33
+ * Lazy-load and cache provider instance
34
+ */
35
+ private getProvider;
36
+ /**
37
+ * Load YNS provider
38
+ */
39
+ private loadProvider;
40
+ /**
41
+ * Product operations - YNS specific with GraphQL support
42
+ */
43
+ get product(): {
44
+ /**
45
+ * Browse/list products with GraphQL field selection
46
+ */
47
+ browse: (params?: ProductBrowseParams) => Promise<ProductBrowseResult>;
48
+ /**
49
+ * Get single product by ID or slug with field selection
50
+ */
51
+ get: (params: ProductGetParams) => Promise<Product | null>;
52
+ /**
53
+ * Search products with GraphQL support
54
+ */
55
+ search: (params: ProductSearchParams) => Promise<ProductSearchResult>;
56
+ };
57
+ /**
58
+ * Cart operations - YNS specific
59
+ */
60
+ get cart(): {
61
+ /**
62
+ * Add item to cart
63
+ */
64
+ add: (params: CartAddParams) => Promise<{
65
+ cartId: string;
66
+ }>;
67
+ /**
68
+ * Update item in cart
69
+ */
70
+ update: (params: CartUpdateParams) => Promise<{
71
+ cartId: string;
72
+ }>;
73
+ /**
74
+ * Clear cart
75
+ */
76
+ clear: (params: CartClearParams) => Promise<{
77
+ cartId: string;
78
+ }>;
79
+ /**
80
+ * Get cart details
81
+ */
82
+ get: (params: CartGetParams) => Promise<Cart | null>;
83
+ };
14
84
  /**
15
- * ⚠️ Warning: This might be `undefined` but TypeScript doesn't have a syntax to express that.
16
- * @see https://github.com/microsoft/TypeScript/issues/36057
85
+ * Order operations - YNS specific
17
86
  */
18
- function __ynsFindStripeAccount(): YnsFindStripeAccountResult | undefined | Promise<YnsFindStripeAccountResult | undefined>;
87
+ get order(): {
88
+ /**
89
+ * Get single order
90
+ */
91
+ get: (params: OrderGetParams) => Promise<Order | null>;
92
+ /**
93
+ * List orders with GraphQL field selection
94
+ */
95
+ list: (params?: OrderListParams) => Promise<OrderListResult>;
96
+ };
19
97
  }
20
- declare const getYnsContext: () => Promise<YnsContextResult>;
21
98
 
22
- export { getYnsContext };
99
+ export { Commerce };
package/dist/yns.js CHANGED
@@ -1 +1 @@
1
- var t=async()=>{let e={stripeAccount:void 0,storeId:void 0,secretKey:void 0,publishableKey:void 0};return await global?.__ynsFindStripeAccount?.()??e};export{t as getYnsContext};
1
+ var u=Object.defineProperty;var p=(o,r)=>()=>(o&&(r=o(o=0)),r);var P=(o,r)=>{for(var t in r)u(o,t,{get:r[t],enumerable:!0})};var n={};P(n,{YnsProvider:()=>a,createYnsProvider:()=>v});function v(o){return new a(o)}var a,d=p(()=>{"use strict";a=class{config;constructor(r){this.config=r}async graphqlRequest(r,t){let e=await fetch(`${this.config.endpoint}/api/graphql`,{method:"POST",headers:{"Content-Type":"application/json",Authorization:`Bearer ${this.config.token}`},body:JSON.stringify({query:r,variables:t})});if(!e.ok)throw new Error(`YNS GraphQL request failed: ${e.status} ${e.statusText}`);let i=await e.json();if(i.errors)throw new Error(`YNS GraphQL errors: ${JSON.stringify(i.errors)}`);return i.data}async restRequest(r,t="GET",e){let i=await fetch(`${this.config.endpoint}/api${r}`,{method:t,headers:{"Content-Type":"application/json",Authorization:`Bearer ${this.config.token}`},body:e?JSON.stringify(e):void 0});if(!i.ok)throw new Error(`YNS REST request failed: ${i.status} ${i.statusText}`);return i.json()}mapYnsProduct(r){return{id:r.id,name:r.name,slug:r.slug,summary:r.summary,images:r.images||[],active:r.active,price:r.variants?.[0]?.price?Number.parseFloat(r.variants[0].price):0,currency:"USD",stock:r.variants?.[0]?.stock,category:r.category,variants:r.variants?.map(t=>({id:t.id,price:Number.parseFloat(t.price),stock:t.stock,attributes:t.attributes}))||[]}}mapYnsCart(r){let t=r.lineItems?.map(i=>({id:i.id,productId:i.productVariant?.id||"",variantId:i.productVariantId,quantity:i.quantity,price:Number.parseFloat(i.productVariant?.price||"0")}))||[],e=t.reduce((i,s)=>i+s.price*s.quantity,0);return{id:r.id,customerId:r.customerId,items:t,total:e,currency:"USD",createdAt:r.createdAt,updatedAt:r.updatedAt}}async productBrowse(r){if(r.graphql){let t={offset:r.offset||0,limit:r.first||10,category:r.category,query:r.query,active:r.active,orderBy:r.orderBy,orderDirection:r.orderDirection},e=await this.graphqlRequest(r.graphql,t);return{data:e.products.data.map(i=>this.mapYnsProduct(i)),meta:e.products.meta}}else{let t=new URLSearchParams;r.first&&t.append("limit",r.first.toString()),r.offset&&t.append("offset",r.offset.toString()),r.category&&t.append("category",r.category),r.query&&t.append("q",r.query),r.active!==void 0&&t.append("active",r.active.toString()),r.orderBy&&t.append("orderBy",r.orderBy),r.orderDirection&&t.append("orderDirection",r.orderDirection);let e=`/products${t.toString()?`?${t.toString()}`:""}`,i=await this.restRequest(e);return{data:i.data.map(s=>this.mapYnsProduct(s)),meta:i.meta}}}async productGet(r){if(!r.slug&&!r.id)throw new Error("Either slug or id is required for productGet");if(r.graphql){let t={slug:r.slug,id:r.id},e=await this.graphqlRequest(r.graphql,t);return e.product?this.mapYnsProduct(e.product):null}else{let t=r.id?`/products/${r.id}`:`/products/slug/${r.slug}`,e=await this.restRequest(t);return e?this.mapYnsProduct(e):null}}async cartAdd(r){let t={variantId:r.variantId,cartId:r.cartId,subscriptionId:r.subscriptionId};return await this.restRequest("/cart","POST",t)}async cartUpdate(r){let t={variantId:r.variantId,quantity:r.quantity};return await this.restRequest(`/cart/${r.cartId}`,"PUT",t)}async cartClear(r){return await this.restRequest(`/cart/${r.cartId}`,"DELETE")}async cartGet(r){let t=await this.restRequest(`/cart/${r.cartId}`);return t?this.mapYnsCart(t):null}}});var c=class{config;provider;providerPromise;constructor(r){if(this.config=r||this.detectFromEnv(),!this.config.endpoint||!this.config.token)throw new Error("YNS configuration required. Provide endpoint and token in constructor or set YNS_ENDPOINT and YNS_TOKEN environment variables.")}detectFromEnv(){return{endpoint:process.env.YNS_ENDPOINT||"",token:process.env.YNS_TOKEN||""}}async getProvider(){return this.provider?this.provider:(this.providerPromise||(this.providerPromise=this.loadProvider()),this.provider=await this.providerPromise,this.provider)}async loadProvider(){try{let{createYnsProvider:r}=await Promise.resolve().then(()=>(d(),n));return r(this.config)}catch(r){throw new Error(`Failed to initialize YNS provider: ${r instanceof Error?r.message:"Unknown error"}`)}}get product(){return{browse:async(r={})=>(await this.getProvider()).productBrowse(r),get:async r=>(await this.getProvider()).productGet(r),search:async r=>{let t=await this.getProvider();if(!t.productSearch)throw new Error("Product search is not supported by YNS provider");return t.productSearch(r)}}}get cart(){return{add:async r=>(await this.getProvider()).cartAdd(r),update:async r=>(await this.getProvider()).cartUpdate(r),clear:async r=>(await this.getProvider()).cartClear(r),get:async r=>(await this.getProvider()).cartGet(r)}}get order(){return{get:async r=>{let t=await this.getProvider();if(!t.orderGet)throw new Error("Order retrieval is not supported by YNS provider");return t.orderGet(r)},list:async(r={})=>{let t=await this.getProvider();if(!t.orderList)throw new Error("Order listing is not supported by YNS provider");return t.orderList(r)}}}};export{c as Commerce};
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "$schema": "https://json.schemastore.org/package",
3
3
  "name": "commerce-kit",
4
- "version": "0.1.0",
4
+ "version": "0.3.0",
5
5
  "type": "module",
6
6
  "license": "AGPL-3.0-only",
7
7
  "keywords": [
@@ -23,6 +23,14 @@
23
23
  "import": "./dist/index.js",
24
24
  "types": "./dist/index.d.ts"
25
25
  },
26
+ "./yns": {
27
+ "import": "./dist/yns.js",
28
+ "types": "./dist/yns.d.ts"
29
+ },
30
+ "./stripe": {
31
+ "import": "./dist/stripe.js",
32
+ "types": "./dist/stripe.d.ts"
33
+ },
26
34
  "./currencies": {
27
35
  "import": "./dist/currencies.js",
28
36
  "types": "./dist/currencies.d.ts"
@@ -31,9 +39,9 @@
31
39
  "import": "./dist/internal.js",
32
40
  "types": "./dist/internal.d.ts"
33
41
  },
34
- "./yns": {
35
- "import": "./dist/yns.js",
36
- "types": "./dist/yns.d.ts"
42
+ "./yns-context": {
43
+ "import": "./dist/yns-context.js",
44
+ "types": "./dist/yns-context.d.ts"
37
45
  },
38
46
  "./db": {
39
47
  "import": "./dist/db.js",
@@ -51,12 +59,13 @@
51
59
  "build": "biome check && rimraf dist/* && tsup --config tsup.config.ts",
52
60
  "prepublishOnly": "bun run build",
53
61
  "dev": "tsup --config tsup.config.ts --watch",
54
- "lint": "biome check --write --unsafe"
62
+ "lint": "biome check --write --unsafe",
63
+ "test": "bun test"
55
64
  },
56
65
  "devDependencies": {
57
- "@biomejs/biome": "2.2.2",
66
+ "@biomejs/biome": "2.2.3",
58
67
  "@neondatabase/serverless": "^1.0.1",
59
- "@types/node": "^24.3.0",
68
+ "@types/node": "^24.3.1",
60
69
  "@types/react": "^19.1.12",
61
70
  "@types/react-dom": "^19.1.9",
62
71
  "next": "15.5.2",
@@ -67,7 +76,6 @@
67
76
  "stripe": "^18.5.0",
68
77
  "tsup": "8.5.0",
69
78
  "tsx": "^4.20.5",
70
- "vitest": "^3.2.4",
71
79
  "zod": "^4.1.5"
72
80
  },
73
81
  "peerDependencies": {