commerce-kit 0.6.1-experimental.8 → 0.7.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/LICENSE.md +650 -0
- package/README.md +234 -0
- package/dist/currencies.d.ts +11 -0
- package/dist/currencies.js +1 -0
- package/dist/index.d.ts +6 -26
- package/dist/index.js +1 -2
- package/dist/internal.d.ts +383 -0
- package/dist/internal.js +1 -0
- package/dist/provider-CeP9uHnB.d.ts +166 -0
- package/dist/stripe.d.ts +75 -0
- package/dist/stripe.js +1 -0
- package/dist/yns-context.d.ts +22 -0
- package/dist/yns-context.js +1 -0
- package/dist/yns.d.ts +101 -0
- package/dist/yns.js +1 -0
- package/package.json +44 -22
- package/dist/api-types.d.ts +0 -4955
- package/dist/api-types.js +0 -2
- package/dist/api-types.js.map +0 -1
- package/dist/chunk-PNKB6P4V.js +0 -1
- package/dist/chunk-PNKB6P4V.js.map +0 -1
- package/dist/index.js.map +0 -1
|
@@ -0,0 +1,22 @@
|
|
|
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 {
|
|
14
|
+
/**
|
|
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
|
|
17
|
+
*/
|
|
18
|
+
function __ynsFindStripeAccount(): YnsFindStripeAccountResult | undefined | Promise<YnsFindStripeAccountResult | undefined>;
|
|
19
|
+
}
|
|
20
|
+
declare const getYnsContext: () => Promise<YnsContextResult>;
|
|
21
|
+
|
|
22
|
+
export { getYnsContext };
|
|
@@ -0,0 +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};
|
package/dist/yns.d.ts
ADDED
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
import { q as YnsProviderConfig, k as ProductBrowseParams, l as ProductBrowseResult, m as ProductGetParams, P as Product, o as ProductSearchParams, p as ProductSearchResult, a as CartAddParams, C as Cart, f as CartUpdateParams, e as CartRemoveParams, b as CartClearParams, c as CartGetParams, h as OrderGetParams, O as Order, i as OrderListParams, j as OrderListResult } from './provider-CeP9uHnB.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 - Simplified API
|
|
59
|
+
*/
|
|
60
|
+
get cart(): {
|
|
61
|
+
/**
|
|
62
|
+
* Add items to cart (additive behavior)
|
|
63
|
+
* If cartId is provided, adds to existing quantity
|
|
64
|
+
* If no cartId, creates new cart
|
|
65
|
+
*/
|
|
66
|
+
add: (params: CartAddParams) => Promise<Cart>;
|
|
67
|
+
/**
|
|
68
|
+
* Update item in cart (absolute behavior)
|
|
69
|
+
* Sets quantity to exact number
|
|
70
|
+
* Requires cartId
|
|
71
|
+
*/
|
|
72
|
+
update: (params: CartUpdateParams) => Promise<Cart>;
|
|
73
|
+
/**
|
|
74
|
+
* Remove specific item from cart
|
|
75
|
+
*/
|
|
76
|
+
remove: (params: CartRemoveParams) => Promise<Cart>;
|
|
77
|
+
/**
|
|
78
|
+
* Clear entire cart
|
|
79
|
+
*/
|
|
80
|
+
clear: (params: CartClearParams) => Promise<Cart>;
|
|
81
|
+
/**
|
|
82
|
+
* Get cart details
|
|
83
|
+
*/
|
|
84
|
+
get: (params: CartGetParams) => Promise<Cart | null>;
|
|
85
|
+
};
|
|
86
|
+
/**
|
|
87
|
+
* Order operations - YNS specific
|
|
88
|
+
*/
|
|
89
|
+
get order(): {
|
|
90
|
+
/**
|
|
91
|
+
* Get single order
|
|
92
|
+
*/
|
|
93
|
+
get: (params: OrderGetParams) => Promise<Order | null>;
|
|
94
|
+
/**
|
|
95
|
+
* List orders with GraphQL field selection
|
|
96
|
+
*/
|
|
97
|
+
list: (params?: OrderListParams) => Promise<OrderListResult>;
|
|
98
|
+
};
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
export { Commerce };
|
package/dist/yns.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
var v=Object.defineProperty;var m=(a,r)=>()=>(a&&(r=a(a=0)),r);var l=(a,r)=>{for(var t in r)v(a,t,{get:r[t],enumerable:!0})};var u={};l(u,{YnsProvider:()=>d,createYnsProvider:()=>h});function h(a){return new d(a)}var d,p=m(()=>{"use strict";d=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/v1${r}`,{method:t,headers:{"Content-Type":"application/json",Authorization:`Bearer ${this.config.token}`},body:e?JSON.stringify(e):void 0});if(!i.ok){let n=i.headers.get("content-type"),s=`YNS REST request failed: ${i.status} ${i.statusText}`;if(n?.includes("application/json"))try{let c=await i.json();s=c.error||c.message||s}catch{}throw new Error(s)}let o=i.headers.get("content-type");if(!o?.includes("application/json"))throw new Error(`YNS API returned ${o} instead of JSON for ${r}`);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}))||[]}}mapCart(r){let t=this.mapCartItems(r.lineItems);return{id:r.id,customerId:r.customerId,storeId:r.storeId,customer:this.mapCustomer(r.customer),items:t,total:this.calculateTotal(t),currency:"USD",createdAt:r.createdAt,updatedAt:r.updatedAt}}mapCartItems(r){return r?.map(t=>this.mapCartItem(t))||[]}mapCartItem(r){let t=r.productVariant;return{id:r.id,productId:t?.product?.id||t?.id||"",variantId:t?.id,quantity:r.quantity,price:Number.parseFloat(t?.price||"0"),stock:t?.stock,product:this.mapProduct(t?.product)}}mapCustomer(r){if(r)return{id:r.id,email:r.email}}mapProduct(r){if(r)return{id:r.id,name:r.name,images:r.images||[]}}calculateTotal(r){return r.reduce((t,e)=>t+e.price*e.quantity,0)}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(o=>this.mapYnsProduct(o)),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=`/products/${r.id||r.slug}`,e=await this.restRequest(t);return e?this.mapYnsProduct(e):null}}async cartAdd(r){let t=r.quantity;if(r.cartId)try{let o=await this.cartGet({cartId:r.cartId});if(o){let n=o.items.find(s=>s.variantId===r.variantId);n&&(t=n.quantity+r.quantity)}}catch{}let e={variantId:r.variantId,cartId:r.cartId,quantity:t,subscriptionId:r.subscriptionId},i=await this.restRequest("/cart","POST",e);return this.mapCart(i)}async cartUpdate(r){let t={variantId:r.variantId,cartId:r.cartId,quantity:r.quantity},e=await this.restRequest("/cart","POST",t);return this.mapCart(e)}async cartRemove(r){let t=`/cart/${r.cartId}/items/${r.variantId}`,e=await this.restRequest(t,"DELETE");return this.mapCart(e)}async cartClear(r){let t=await this.restRequest(`/cart/${r.cartId}`,"DELETE");return this.mapCart(t)}async cartGet(r){let t=await this.restRequest(`/cart/${r.cartId}`);return t?this.mapCart(t):null}}});var P=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(()=>(p(),u));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),remove:async r=>(await this.getProvider()).cartRemove(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{P 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.
|
|
4
|
+
"version": "0.7.0",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "AGPL-3.0-only",
|
|
7
7
|
"keywords": [
|
|
@@ -22,6 +22,30 @@
|
|
|
22
22
|
".": {
|
|
23
23
|
"import": "./dist/index.js",
|
|
24
24
|
"types": "./dist/index.d.ts"
|
|
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
|
+
},
|
|
34
|
+
"./currencies": {
|
|
35
|
+
"import": "./dist/currencies.js",
|
|
36
|
+
"types": "./dist/currencies.d.ts"
|
|
37
|
+
},
|
|
38
|
+
"./internal": {
|
|
39
|
+
"import": "./dist/internal.js",
|
|
40
|
+
"types": "./dist/internal.d.ts"
|
|
41
|
+
},
|
|
42
|
+
"./yns-context": {
|
|
43
|
+
"import": "./dist/yns-context.js",
|
|
44
|
+
"types": "./dist/yns-context.d.ts"
|
|
45
|
+
},
|
|
46
|
+
"./db": {
|
|
47
|
+
"import": "./dist/db.js",
|
|
48
|
+
"types": "./dist/db.d.ts"
|
|
25
49
|
}
|
|
26
50
|
},
|
|
27
51
|
"files": [
|
|
@@ -36,45 +60,43 @@
|
|
|
36
60
|
"prepublishOnly": "bun run build",
|
|
37
61
|
"dev": "tsup --config tsup.config.ts --watch",
|
|
38
62
|
"lint": "biome check --write --unsafe",
|
|
39
|
-
"test": "bun test"
|
|
40
|
-
"prepare": "husky"
|
|
63
|
+
"test": "bun test"
|
|
41
64
|
},
|
|
42
65
|
"devDependencies": {
|
|
43
66
|
"@biomejs/biome": "2.2.3",
|
|
67
|
+
"@neondatabase/serverless": "^1.0.1",
|
|
44
68
|
"@types/node": "^24.3.1",
|
|
69
|
+
"@types/react": "^19.1.12",
|
|
70
|
+
"@types/react-dom": "^19.1.9",
|
|
45
71
|
"@typescript/native-preview": "7.0.0-dev.20251022.1",
|
|
46
|
-
"
|
|
47
|
-
"
|
|
72
|
+
"next": "15.5.2",
|
|
73
|
+
"react": "19.1.1",
|
|
74
|
+
"react-dom": "19.1.1",
|
|
48
75
|
"rimraf": "6.0.1",
|
|
76
|
+
"server-only": "0.0.1",
|
|
77
|
+
"stripe": "^18.5.0",
|
|
49
78
|
"tsup": "8.5.0",
|
|
50
79
|
"tsx": "^4.20.5",
|
|
51
|
-
"typescript": "5.9.3",
|
|
52
80
|
"zod": "^4.1.5"
|
|
53
81
|
},
|
|
54
82
|
"peerDependencies": {
|
|
83
|
+
"@neondatabase/serverless": "^0.9.5",
|
|
55
84
|
"@types/node": "^24.3.0",
|
|
56
|
-
"
|
|
57
|
-
"@
|
|
85
|
+
"@types/react": "^19.0.8",
|
|
86
|
+
"@types/react-dom": "^19.0.3",
|
|
87
|
+
"next": "15.5.2",
|
|
88
|
+
"react": "^19.1.1",
|
|
89
|
+
"react-dom": "19.1.1",
|
|
90
|
+
"server-only": "0.0.1",
|
|
91
|
+
"stripe": "^18.5.0",
|
|
92
|
+
"typescript": "^5.9.2",
|
|
58
93
|
"zod": "^4.1.5"
|
|
59
94
|
},
|
|
60
|
-
"peerDependenciesMeta": {
|
|
61
|
-
"typescript": {
|
|
62
|
-
"optional": true
|
|
63
|
-
},
|
|
64
|
-
"@typescript/native-preview": {
|
|
65
|
-
"optional": true
|
|
66
|
-
}
|
|
67
|
-
},
|
|
68
95
|
"peerDependencyRules": {
|
|
69
96
|
"allowedVersions": {
|
|
70
97
|
"react": "^19",
|
|
71
98
|
"react-dom": "^19"
|
|
72
99
|
}
|
|
73
100
|
},
|
|
74
|
-
"packageManager": "bun@1.2.21"
|
|
75
|
-
"lint-staged": {
|
|
76
|
-
"*": [
|
|
77
|
-
"bun biome check --write --unsafe --staged --no-errors-on-unmatched --files-ignore-unknown=true"
|
|
78
|
-
]
|
|
79
|
-
}
|
|
101
|
+
"packageManager": "bun@1.2.21"
|
|
80
102
|
}
|