commerce-kit 0.2.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/index.d.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  export { formatMoney } from './currencies.js';
2
2
  export { MappedCart, MappedProduct, MappedShippingRate } from './internal.js';
3
- export { B as BaseProvider, C as Cart, a as CartAddParams, b as CartClearParams, c as CartGetParams, d as CartItem, e as CartUpdateParams, O as Order, f as OrderGetParams, g as OrderListParams, h as OrderListResult, P as Product, i as ProductBrowseParams, j as ProductBrowseResult, k as ProductGetParams, l as ProductSearchParams, m as ProductSearchResult, S as StripeProviderConfig, Y as YnsProviderConfig } from './provider-CvZe8Dqo.js';
3
+ export { B as BaseProvider, C as Cart, a as CartAddParams, b as CartClearParams, c as CartGetParams, d as CartItem, e as CartUpdateParams, O as Order, f as OrderGetParams, g as OrderListParams, h as OrderListResult, P as Product, i as ProductBrowseParams, j as ProductBrowseResult, k as ProductGetParams, l as ProductSearchParams, m as ProductSearchResult, S as StripeProviderConfig, Y as YnsProviderConfig } from './provider-Co3VegT-.js';
4
4
  export { Commerce } from './yns.js';
5
5
  import 'stripe';
6
6
  import 'zod';
package/dist/index.js CHANGED
@@ -1,238 +1 @@
1
- var C=Object.defineProperty;var p=(e,r)=>()=>(e&&(r=e(e=0)),r);var w=(e,r)=>{for(var t in r)C(e,t,{get:r[t],enumerable:!0})};function g(e){return!e||e.length===0?c:`
2
- query ProductQuery($slug: String!) {
3
- product(slug: $slug) {
4
- ${e.join(`
5
- `)}
6
- }
7
- }
8
- `}function y(e){return!e||e.length===0?d:`
9
- query ProductsQuery(
10
- $offset: Int
11
- $limit: Int
12
- $category: String
13
- $query: String
14
- $active: Boolean
15
- $excludeBundles: Boolean
16
- $orderBy: String
17
- $orderDirection: String
18
- ) {
19
- products(
20
- offset: $offset
21
- limit: $limit
22
- category: $category
23
- query: $query
24
- active: $active
25
- excludeBundles: $excludeBundles
26
- orderBy: $orderBy
27
- orderDirection: $orderDirection
28
- ) {
29
- data {
30
- ${e.join(`
31
- `)}
32
- }
33
- meta {
34
- count
35
- offset
36
- limit
37
- hasMore
38
- }
39
- }
40
- }
41
- `}var m,d,c,R,P,v=p(()=>{"use strict";m=`
42
- fragment ProductFragment on Product {
43
- id
44
- name
45
- slug
46
- summary
47
- images
48
- active
49
- type
50
- createdAt
51
- updatedAt
52
- category {
53
- id
54
- name
55
- slug
56
- description
57
- image
58
- }
59
- variants {
60
- id
61
- price
62
- originalPrice
63
- prePromotionPrice
64
- stock
65
- shippable
66
- attributes
67
- combinations {
68
- id
69
- variantValue {
70
- id
71
- value
72
- colorValue
73
- variantType {
74
- id
75
- label
76
- type
77
- }
78
- }
79
- }
80
- }
81
- collections {
82
- id
83
- name
84
- slug
85
- description
86
- image
87
- active
88
- }
89
- bundleProducts {
90
- id
91
- quantity
92
- position
93
- variant {
94
- id
95
- price
96
- originalPrice
97
- stock
98
- }
99
- }
100
- bundleDiscountPercentage
101
- }
102
- `,d=`
103
- query ProductsQuery(
104
- $offset: Int
105
- $limit: Int
106
- $category: String
107
- $query: String
108
- $active: Boolean
109
- $excludeBundles: Boolean
110
- $orderBy: String
111
- $orderDirection: String
112
- ) {
113
- products(
114
- offset: $offset
115
- limit: $limit
116
- category: $category
117
- query: $query
118
- active: $active
119
- excludeBundles: $excludeBundles
120
- orderBy: $orderBy
121
- orderDirection: $orderDirection
122
- ) {
123
- data {
124
- ...ProductFragment
125
- }
126
- meta {
127
- count
128
- offset
129
- limit
130
- hasMore
131
- }
132
- }
133
- }
134
- ${m}
135
- `,c=`
136
- query ProductQuery($slug: String!) {
137
- product(slug: $slug) {
138
- ...ProductFragment
139
- }
140
- }
141
- ${m}
142
- `,R=`
143
- fragment CartFragment on Cart {
144
- id
145
- customerId
146
- storeId
147
- shippingId
148
- couponId
149
- billingAddressId
150
- shippingAddressId
151
- stripePaymentIntentId
152
- checkoutSessionId
153
- addonData
154
- createdAt
155
- updatedAt
156
- customer {
157
- id
158
- email
159
- userId
160
- user {
161
- id
162
- email
163
- name
164
- }
165
- }
166
- coupon {
167
- code
168
- type
169
- value
170
- startDate
171
- endDate
172
- }
173
- lineItems {
174
- id
175
- cartId
176
- productVariantId
177
- subscriptionId
178
- quantity
179
- createdAt
180
- updatedAt
181
- productVariant {
182
- id
183
- price
184
- originalPrice
185
- stock
186
- shippable
187
- attributes
188
- }
189
- subscription {
190
- id
191
- name
192
- price
193
- interval
194
- intervalCount
195
- }
196
- }
197
- billingAddress {
198
- id
199
- name
200
- company
201
- line1
202
- line2
203
- city
204
- state
205
- postalCode
206
- country
207
- phone
208
- type
209
- }
210
- shippingAddress {
211
- id
212
- name
213
- company
214
- line1
215
- line2
216
- city
217
- state
218
- postalCode
219
- country
220
- phone
221
- type
222
- }
223
- shipping {
224
- id
225
- name
226
- price
227
- minValue
228
- maxValue
229
- }
230
- }
231
- `,P=`
232
- query CartQuery($id: String!) {
233
- cart(id: $id) {
234
- ...CartFragment
235
- }
236
- }
237
- ${R}
238
- `});var f={};w(f,{YnsProvider:()=>n,createYnsProvider:()=>T});function T(e){return new n(e)}var n,h=p(()=>{"use strict";v();n=class{config;constructor(r){this.config=r}async graphqlRequest(r,t){let o=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(!o.ok)throw new Error(`YNS GraphQL request failed: ${o.status} ${o.statusText}`);let i=await o.json();if(i.errors)throw new Error(`YNS GraphQL errors: ${JSON.stringify(i.errors)}`);return i.data}async restRequest(r,t="GET",o){let i=await fetch(`${this.config.endpoint}/api${r}`,{method:t,headers:{"Content-Type":"application/json",Authorization:`Bearer ${this.config.token}`},body:o?JSON.stringify(o):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")}))||[],o=t.reduce((i,a)=>i+a.price*a.quantity,0);return{id:r.id,customerId:r.customerId,items:t,total:o,currency:"USD",createdAt:r.createdAt,updatedAt:r.updatedAt}}async productBrowse(r){let t=r.fields?y(r.fields):d,o={offset:r.offset||0,limit:r.first||r.offset||10,category:r.category,query:r.query,active:r.active,excludeBundles:!1,orderBy:r.orderBy,orderDirection:r.orderDirection},i=await this.graphqlRequest(t,o);return{data:i.products.data.map(a=>this.mapYnsProduct(a)),meta:i.products.meta}}async productGet(r){if(!r.slug&&!r.id)throw new Error("Either slug or id is required for productGet");let t=r.fields?g(r.fields):c,o={slug:r.slug},i=await this.graphqlRequest(t,o);return i.product?this.mapYnsProduct(i.product):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.graphqlRequest(P,{id:r.cartId});return t.cart?this.mapYnsCart(t.cart):null}}});function s(e,r){if(!e)throw new Error(r)}var l=e=>{s(Number.isInteger(e),"Value must be an integer")};var I=e=>(s(e.length===3,"currency needs to be a 3-letter code"),b[e.toUpperCase()]??2);var $=({amount:e,currency:r})=>{l(e);let t=I(r),o=10**t;return Number.parseFloat((e/o).toFixed(t))},S=({amount:e,currency:r,locale:t="en-US"})=>{let o=$({amount:e,currency:r});return new Intl.NumberFormat(t,{style:"currency",currency:r}).format(o)},b={BIF:0,CLP:0,DJF:0,GNF:0,JPY:0,KMF:0,KRW:0,MGA:0,PYG:0,RWF:0,UGX:0,VND:0,VUV:0,XAF:0,XOF:0,XPF:0,BHD:3,JOD:3,KWD:3,OMR:3,TND:3};var u=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(()=>(h(),f));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{u as Commerce,S as formatMoney};
1
+ var l=Object.defineProperty;var P=(o,r)=>()=>(o&&(r=o(o=0)),r);var m=(o,r)=>{for(var t in r)l(o,t,{get:r[t],enumerable:!0})};var u={};m(u,{YnsProvider:()=>a,createYnsProvider:()=>y});function y(o){return new a(o)}var a,p=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}}});function n(o,r){if(!o)throw new Error(r)}var c=o=>{n(Number.isInteger(o),"Value must be an integer")};var g=o=>(n(o.length===3,"currency needs to be a 3-letter code"),h[o.toUpperCase()]??2);var v=({amount:o,currency:r})=>{c(o);let t=g(r),e=10**t;return Number.parseFloat((o/e).toFixed(t))},f=({amount:o,currency:r,locale:t="en-US"})=>{let e=v({amount:o,currency:r});return new Intl.NumberFormat(t,{style:"currency",currency:r}).format(e)},h={BIF:0,CLP:0,DJF:0,GNF:0,JPY:0,KMF:0,KRW:0,MGA:0,PYG:0,RWF:0,UGX:0,VND:0,VUV:0,XAF:0,XOF:0,XPF:0,BHD:3,JOD:3,KWD:3,OMR:3,TND:3};var d=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),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{d as Commerce,f as formatMoney};
@@ -68,13 +68,13 @@ interface ProductBrowseParams {
68
68
  active?: boolean;
69
69
  orderBy?: string;
70
70
  orderDirection?: "asc" | "desc";
71
- fields?: string[];
71
+ graphql?: string;
72
72
  _provider?: "stripe" | "yns";
73
73
  }
74
74
  interface ProductGetParams {
75
75
  slug?: string;
76
76
  id?: string;
77
- fields?: string[];
77
+ graphql?: string;
78
78
  _provider?: "stripe" | "yns";
79
79
  }
80
80
  interface ProductSearchParams {
package/dist/stripe.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { S as StripeProviderConfig, i as ProductBrowseParams, j as ProductBrowseResult, k as ProductGetParams, P as Product, a as CartAddParams, e as CartUpdateParams, b as CartClearParams, c as CartGetParams, C as Cart } from './provider-CvZe8Dqo.js';
1
+ import { S as StripeProviderConfig, i as ProductBrowseParams, j as ProductBrowseResult, k as ProductGetParams, P as Product, a as CartAddParams, e as CartUpdateParams, b as CartClearParams, c as CartGetParams, C as Cart } from './provider-Co3VegT-.js';
2
2
 
3
3
  /**
4
4
  * Stripe Commerce client - zero-config constructor
package/dist/yns.d.ts CHANGED
@@ -1,4 +1,4 @@
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-CvZe8Dqo.js';
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
2
 
3
3
  /**
4
4
  * YNS Commerce client - zero-config constructor with GraphQL support
package/dist/yns.js CHANGED
@@ -1,238 +1 @@
1
- var h=Object.defineProperty;var c=(i,r)=>()=>(i&&(r=i(i=0)),r);var f=(i,r)=>{for(var t in r)h(i,t,{get:r[t],enumerable:!0})};function l(i){return!i||i.length===0?d:`
2
- query ProductQuery($slug: String!) {
3
- product(slug: $slug) {
4
- ${i.join(`
5
- `)}
6
- }
7
- }
8
- `}function P(i){return!i||i.length===0?n:`
9
- query ProductsQuery(
10
- $offset: Int
11
- $limit: Int
12
- $category: String
13
- $query: String
14
- $active: Boolean
15
- $excludeBundles: Boolean
16
- $orderBy: String
17
- $orderDirection: String
18
- ) {
19
- products(
20
- offset: $offset
21
- limit: $limit
22
- category: $category
23
- query: $query
24
- active: $active
25
- excludeBundles: $excludeBundles
26
- orderBy: $orderBy
27
- orderDirection: $orderDirection
28
- ) {
29
- data {
30
- ${i.join(`
31
- `)}
32
- }
33
- meta {
34
- count
35
- offset
36
- limit
37
- hasMore
38
- }
39
- }
40
- }
41
- `}var u,n,d,$,p,g=c(()=>{"use strict";u=`
42
- fragment ProductFragment on Product {
43
- id
44
- name
45
- slug
46
- summary
47
- images
48
- active
49
- type
50
- createdAt
51
- updatedAt
52
- category {
53
- id
54
- name
55
- slug
56
- description
57
- image
58
- }
59
- variants {
60
- id
61
- price
62
- originalPrice
63
- prePromotionPrice
64
- stock
65
- shippable
66
- attributes
67
- combinations {
68
- id
69
- variantValue {
70
- id
71
- value
72
- colorValue
73
- variantType {
74
- id
75
- label
76
- type
77
- }
78
- }
79
- }
80
- }
81
- collections {
82
- id
83
- name
84
- slug
85
- description
86
- image
87
- active
88
- }
89
- bundleProducts {
90
- id
91
- quantity
92
- position
93
- variant {
94
- id
95
- price
96
- originalPrice
97
- stock
98
- }
99
- }
100
- bundleDiscountPercentage
101
- }
102
- `,n=`
103
- query ProductsQuery(
104
- $offset: Int
105
- $limit: Int
106
- $category: String
107
- $query: String
108
- $active: Boolean
109
- $excludeBundles: Boolean
110
- $orderBy: String
111
- $orderDirection: String
112
- ) {
113
- products(
114
- offset: $offset
115
- limit: $limit
116
- category: $category
117
- query: $query
118
- active: $active
119
- excludeBundles: $excludeBundles
120
- orderBy: $orderBy
121
- orderDirection: $orderDirection
122
- ) {
123
- data {
124
- ...ProductFragment
125
- }
126
- meta {
127
- count
128
- offset
129
- limit
130
- hasMore
131
- }
132
- }
133
- }
134
- ${u}
135
- `,d=`
136
- query ProductQuery($slug: String!) {
137
- product(slug: $slug) {
138
- ...ProductFragment
139
- }
140
- }
141
- ${u}
142
- `,$=`
143
- fragment CartFragment on Cart {
144
- id
145
- customerId
146
- storeId
147
- shippingId
148
- couponId
149
- billingAddressId
150
- shippingAddressId
151
- stripePaymentIntentId
152
- checkoutSessionId
153
- addonData
154
- createdAt
155
- updatedAt
156
- customer {
157
- id
158
- email
159
- userId
160
- user {
161
- id
162
- email
163
- name
164
- }
165
- }
166
- coupon {
167
- code
168
- type
169
- value
170
- startDate
171
- endDate
172
- }
173
- lineItems {
174
- id
175
- cartId
176
- productVariantId
177
- subscriptionId
178
- quantity
179
- createdAt
180
- updatedAt
181
- productVariant {
182
- id
183
- price
184
- originalPrice
185
- stock
186
- shippable
187
- attributes
188
- }
189
- subscription {
190
- id
191
- name
192
- price
193
- interval
194
- intervalCount
195
- }
196
- }
197
- billingAddress {
198
- id
199
- name
200
- company
201
- line1
202
- line2
203
- city
204
- state
205
- postalCode
206
- country
207
- phone
208
- type
209
- }
210
- shippingAddress {
211
- id
212
- name
213
- company
214
- line1
215
- line2
216
- city
217
- state
218
- postalCode
219
- country
220
- phone
221
- type
222
- }
223
- shipping {
224
- id
225
- name
226
- price
227
- minValue
228
- maxValue
229
- }
230
- }
231
- `,p=`
232
- query CartQuery($id: String!) {
233
- cart(id: $id) {
234
- ...CartFragment
235
- }
236
- }
237
- ${$}
238
- `});var m={};f(m,{YnsProvider:()=>s,createYnsProvider:()=>w});function w(i){return new s(i)}var s,v=c(()=>{"use strict";g();s=class{config;constructor(r){this.config=r}async graphqlRequest(r,t){let o=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(!o.ok)throw new Error(`YNS GraphQL request failed: ${o.status} ${o.statusText}`);let e=await o.json();if(e.errors)throw new Error(`YNS GraphQL errors: ${JSON.stringify(e.errors)}`);return e.data}async restRequest(r,t="GET",o){let e=await fetch(`${this.config.endpoint}/api${r}`,{method:t,headers:{"Content-Type":"application/json",Authorization:`Bearer ${this.config.token}`},body:o?JSON.stringify(o):void 0});if(!e.ok)throw new Error(`YNS REST request failed: ${e.status} ${e.statusText}`);return e.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(e=>({id:e.id,productId:e.productVariant?.id||"",variantId:e.productVariantId,quantity:e.quantity,price:Number.parseFloat(e.productVariant?.price||"0")}))||[],o=t.reduce((e,a)=>e+a.price*a.quantity,0);return{id:r.id,customerId:r.customerId,items:t,total:o,currency:"USD",createdAt:r.createdAt,updatedAt:r.updatedAt}}async productBrowse(r){let t=r.fields?P(r.fields):n,o={offset:r.offset||0,limit:r.first||r.offset||10,category:r.category,query:r.query,active:r.active,excludeBundles:!1,orderBy:r.orderBy,orderDirection:r.orderDirection},e=await this.graphqlRequest(t,o);return{data:e.products.data.map(a=>this.mapYnsProduct(a)),meta:e.products.meta}}async productGet(r){if(!r.slug&&!r.id)throw new Error("Either slug or id is required for productGet");let t=r.fields?l(r.fields):d,o={slug:r.slug},e=await this.graphqlRequest(t,o);return e.product?this.mapYnsProduct(e.product):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.graphqlRequest(p,{id:r.cartId});return t.cart?this.mapYnsCart(t.cart):null}}});var y=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(()=>(v(),m));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{y as Commerce};
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.2.0",
4
+ "version": "0.3.0",
5
5
  "type": "module",
6
6
  "license": "AGPL-3.0-only",
7
7
  "keywords": [