@storecraft/sdk 0.1.0 → 1.0.2

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/src/discounts.js CHANGED
@@ -2,11 +2,11 @@ import { StorecraftSDK } from '../index.js'
2
2
  import { collection_base } from './utils.api.fetch.js';
3
3
 
4
4
  /**
5
- * Base `discounts` **CRUD**
5
+ * @description Base `discounts` **CRUD**
6
6
  *
7
7
  * @extends {collection_base<
8
- * import('@storecraft/core/v-api').DiscountTypeUpsert,
9
- * import('@storecraft/core/v-api').DiscountType>
8
+ * import('@storecraft/core/api').DiscountTypeUpsert,
9
+ * import('@storecraft/core/api').DiscountType>
10
10
  * }
11
11
  */
12
12
  export default class Discounts extends collection_base {
@@ -19,131 +19,131 @@ export default class Discounts extends collection_base {
19
19
  super(sdk, 'discounts');
20
20
  }
21
21
 
22
- /**
23
- *
24
- * @param {DiscountData} discount_data
25
- * @param {number} limit
26
- */
27
- publish = async (discount_data, limit=10000) => {
28
- const coll_handle = `discount-${discount_data.code}`
29
- const dd = {
30
- ...discount_data,
31
- _published: coll_handle
32
- }
33
- if(dd.info.details.meta.type==='order')
34
- throw 'Exporting a discount collection is only available for \
35
- Product discounts (you chose Order discount)'
36
-
37
- // save current document, allow to fail
38
- await this.set(
39
- dd.code, dd
40
- )
41
- // await this.update(dd.code, { _published: coll_handle })
22
+ // /**
23
+ // *
24
+ // * @param {DiscountData} discount_data
25
+ // * @param {number} limit
26
+ // */
27
+ // publish = async (discount_data, limit=10000) => {
28
+ // const coll_handle = `discount-${discount_data.code}`
29
+ // const dd = {
30
+ // ...discount_data,
31
+ // _published: coll_handle
32
+ // }
33
+ // if(dd.info.details.meta.type==='order')
34
+ // throw 'Exporting a discount collection is only available for \
35
+ // Product discounts (you chose Order discount)'
36
+
37
+ // // save current document, allow to fail
38
+ // await this.set(
39
+ // dd.code, dd
40
+ // )
41
+ // // await this.update(dd.code, { _published: coll_handle })
42
42
 
43
43
 
44
- // first, remove all previous collection tag from previous products
45
- try {
46
- const products_to_remove =
47
- await this.context.products.list([`col:${coll_handle}`], 10000)
48
- // console.log('products_to_remove ', products_to_remove);
49
- const batch_remove = writeBatch(this.context.firebase.db)
50
- products_to_remove.forEach(it => {
51
- const ref = doc(this.context.firebase.db, 'products', it[0])
52
- batch_remove.update(ref, {
53
- search : arrayRemove(`col:${coll_handle}`),
54
- collections : arrayRemove(coll_handle),
55
- [`discounts.${dd.code}`]: deleteField()
56
- })
57
- })
58
- await batch_remove.commit()
59
-
60
- } catch (e) {
61
- console.log('Remove old: ' + String(e))
62
- console.log(e)
63
- }
64
-
65
- try {
66
- // filter in product filters
67
- var product_filters = dd.info.filters.filter(f => f.meta.type==='product')
68
-
69
- // then, make a server search that will filter out as much as possible
70
- /**@type {Filter} */
71
- var first_guided_filter = undefined
72
- /**@type {string[]} */
73
- var first_guided_search_terms = undefined
74
-
75
- if(first_guided_filter = product_filters.find(f => f.meta.op==='p-in-handles')) {
76
- first_guided_search_terms = first_guided_filter.value
77
- }
78
- else if(first_guided_filter = product_filters.find(f => f.meta.op==='p-in-tags')) {
79
- first_guided_search_terms = first_guided_filter.value.map(t => `tag:${t}`)
80
- }
81
- else if(first_guided_filter = product_filters.find(f => f.meta.op==='p-in-collections')) {
82
- first_guided_search_terms = first_guided_filter.value.map(c => `col:${c}`)
83
- }
84
- } catch (e) {
85
- throw 'Filter preparing error: ' + String(e)
86
- }
87
-
88
- try {
89
- // then, global filtering, this helps to reduce legal products for filtering
90
- var products = await
91
- this.context.products.list(first_guided_search_terms, limit)
92
-
93
- // now local filtering (due to firebase limitations with filtering)
94
- var filtered_products =
95
- this.filterProductsWithFilters(products, product_filters)
44
+ // // first, remove all previous collection tag from previous products
45
+ // try {
46
+ // const products_to_remove =
47
+ // await this.context.products.list([`col:${coll_handle}`], 10000)
48
+ // // console.log('products_to_remove ', products_to_remove);
49
+ // const batch_remove = writeBatch(this.context.firebase.db)
50
+ // products_to_remove.forEach(it => {
51
+ // const ref = doc(this.context.firebase.db, 'products', it[0])
52
+ // batch_remove.update(ref, {
53
+ // search : arrayRemove(`col:${coll_handle}`),
54
+ // collections : arrayRemove(coll_handle),
55
+ // [`discounts.${dd.code}`]: deleteField()
56
+ // })
57
+ // })
58
+ // await batch_remove.commit()
59
+
60
+ // } catch (e) {
61
+ // console.log('Remove old: ' + String(e))
62
+ // console.log(e)
63
+ // }
64
+
65
+ // try {
66
+ // // filter in product filters
67
+ // var product_filters = dd.info.filters.filter(f => f.meta.type==='product')
68
+
69
+ // // then, make a server search that will filter out as much as possible
70
+ // /**@type {Filter} */
71
+ // var first_guided_filter = undefined
72
+ // /**@type {string[]} */
73
+ // var first_guided_search_terms = undefined
74
+
75
+ // if(first_guided_filter = product_filters.find(f => f.meta.op==='p-in-handles')) {
76
+ // first_guided_search_terms = first_guided_filter.value
77
+ // }
78
+ // else if(first_guided_filter = product_filters.find(f => f.meta.op==='p-in-tags')) {
79
+ // first_guided_search_terms = first_guided_filter.value.map(t => `tag:${t}`)
80
+ // }
81
+ // else if(first_guided_filter = product_filters.find(f => f.meta.op==='p-in-collections')) {
82
+ // first_guided_search_terms = first_guided_filter.value.map(c => `col:${c}`)
83
+ // }
84
+ // } catch (e) {
85
+ // throw 'Filter preparing error: ' + String(e)
86
+ // }
87
+
88
+ // try {
89
+ // // then, global filtering, this helps to reduce legal products for filtering
90
+ // var products = await
91
+ // this.context.products.list(first_guided_search_terms, limit)
92
+
93
+ // // now local filtering (due to firebase limitations with filtering)
94
+ // var filtered_products =
95
+ // this.filterProductsWithFilters(products, product_filters)
96
96
 
97
- // products = products.slice(0, 400)
98
- } catch (e) {
99
- throw 'Filtering error: ' + String(e)
100
- }
101
-
102
- try {
103
- // add collection tag to each product with batch write
104
- const batch = writeBatch(this.context.firebase.db)
105
- filtered_products.forEach(it => {
106
- const p = it[1]
107
- const isActive = p?.active==true || (p.active===undefined)
108
- if(!isActive)
109
- return;
110
-
111
- const ref = doc(this.context.firebase.db, 'products', it[0])
112
- const dd_mod = {...dd}
113
- delete dd_mod.search
114
- delete dd_mod.order
115
- batch.update(ref, {
116
- collections : arrayUnion(coll_handle),
117
- search : arrayUnion(`col:${coll_handle}`),
118
- [`discounts.${dd_mod.code}`]: dd_mod
119
- })
120
- })
121
- await batch.commit()
122
- } catch (e) {
123
- throw 'Products update failed: ' + String(e)
124
- }
125
-
126
- try {
127
- // now, create a new collection
128
- /**@type {import('./js-docs-types').CollectionData} */
129
- const col_discount = {
130
- desc : dd.desc,
131
- handle : coll_handle,
132
- title : dd.title,
133
- media : dd.media,
134
- tags : dd.tags,
135
- attributes: dd.attributes,
136
- createdAt: Date.now()
137
- }
138
-
139
- await this.context.collections.set(
140
- col_discount.handle, col_discount
141
- )
142
- // await this.update(discount_data.code, { _published: coll_handle })
143
- } catch (e) {
144
- throw 'Collection creation failed: ' + String(e)
145
- }
97
+ // // products = products.slice(0, 400)
98
+ // } catch (e) {
99
+ // throw 'Filtering error: ' + String(e)
100
+ // }
101
+
102
+ // try {
103
+ // // add collection tag to each product with batch write
104
+ // const batch = writeBatch(this.context.firebase.db)
105
+ // filtered_products.forEach(it => {
106
+ // const p = it[1]
107
+ // const isActive = p?.active==true || (p.active===undefined)
108
+ // if(!isActive)
109
+ // return;
110
+
111
+ // const ref = doc(this.context.firebase.db, 'products', it[0])
112
+ // const dd_mod = {...dd}
113
+ // delete dd_mod.search
114
+ // delete dd_mod.order
115
+ // batch.update(ref, {
116
+ // collections : arrayUnion(coll_handle),
117
+ // search : arrayUnion(`col:${coll_handle}`),
118
+ // [`discounts.${dd_mod.code}`]: dd_mod
119
+ // })
120
+ // })
121
+ // await batch.commit()
122
+ // } catch (e) {
123
+ // throw 'Products update failed: ' + String(e)
124
+ // }
125
+
126
+ // try {
127
+ // // now, create a new collection
128
+ // /**@type {import('./js-docs-types').CollectionData} */
129
+ // const col_discount = {
130
+ // desc : dd.desc,
131
+ // handle : coll_handle,
132
+ // title : dd.title,
133
+ // media : dd.media,
134
+ // tags : dd.tags,
135
+ // attributes: dd.attributes,
136
+ // createdAt: Date.now()
137
+ // }
138
+
139
+ // await this.context.collections.set(
140
+ // col_discount.handle, col_discount
141
+ // )
142
+ // // await this.update(discount_data.code, { _published: coll_handle })
143
+ // } catch (e) {
144
+ // throw 'Collection creation failed: ' + String(e)
145
+ // }
146
146
 
147
- }
147
+ // }
148
148
 
149
149
  }
package/src/images.js CHANGED
@@ -2,11 +2,11 @@ import { StorecraftSDK } from '../index.js'
2
2
  import { collection_base } from './utils.api.fetch.js';
3
3
 
4
4
  /**
5
- * Base `images` **CRUD**
5
+ * @description Base `images` **CRUD**
6
6
  *
7
7
  * @extends {collection_base<
8
- * import('@storecraft/core/v-api').ImageTypeUpsert,
9
- * import('@storecraft/core/v-api').ImageType>
8
+ * import('@storecraft/core/api').ImageTypeUpsert,
9
+ * import('@storecraft/core/api').ImageType>
10
10
  * }
11
11
  */
12
12
  export default class Images extends collection_base {
@@ -4,11 +4,11 @@ import {
4
4
  } from './utils.api.fetch.js';
5
5
 
6
6
  /**
7
- * Base `notifications` **CRUD**
7
+ * @description Base `notifications` **CRUD**
8
8
  *
9
9
  * @extends {collection_base<
10
- * import('@storecraft/core/v-api').NotificationTypeUpsert,
11
- * import('@storecraft/core/v-api').NotificationType>
10
+ * import('@storecraft/core/api').NotificationTypeUpsert,
11
+ * import('@storecraft/core/api').NotificationType>
12
12
  * }
13
13
  */
14
14
  export default class Notifications extends collection_base {
@@ -23,7 +23,7 @@ export default class Notifications extends collection_base {
23
23
 
24
24
  /**
25
25
  *
26
- * @param {import('@storecraft/core/v-api').NotificationTypeUpsert[]} items
26
+ * @param {import('@storecraft/core/api').NotificationTypeUpsert[]} items
27
27
  */
28
28
  upsertBulk = items => {
29
29
  return fetchApiWithAuth(
@@ -43,60 +43,4 @@ export default class Notifications extends collection_base {
43
43
  return this.get('_meta')
44
44
  }
45
45
 
46
- /**
47
- * Test if backend moght have new data economically
48
- * @returns {Promise<boolean>}
49
- */
50
- hasChanged = async () => {
51
- try {
52
- // try cache
53
- const cached = await this.list([], 50, true, false)
54
- if (cached.length==0)
55
- return true
56
-
57
- // compute how many latest updates with max timestamp in cache
58
- const max_updated = cached.reduce(
59
- (p, c) => {
60
- const updatedAt = c[1]?.updatedAt
61
-
62
- if(updatedAt==p.timestamp)
63
- p.count+=1
64
- else if(updatedAt>p.timestamp)
65
- p.count=1
66
-
67
- p.timestamp = Math.max(updatedAt ?? -1, p.timestamp)
68
- return p
69
- },
70
- {
71
- timestamp: 0,
72
- count: 0
73
- }
74
- )
75
-
76
- // now, use a light count query to the database
77
- const count = await this.context.db.col(NAME).count(
78
- {
79
- where: [
80
- ['updatedAt', '>=', max_updated.timestamp]
81
- ]
82
- }
83
- )
84
-
85
- // console.log(cached)
86
- // console.log('count ', count)
87
- // console.log('max_updated ', max_updated)
88
-
89
- if(count > max_updated.count)
90
- return true
91
-
92
- return false
93
-
94
- } catch (e) {
95
- // error
96
- console.error(e)
97
- return true
98
- }
99
-
100
- }
101
-
102
46
  }
package/src/orders.js CHANGED
@@ -1,13 +1,12 @@
1
- import { calculate_pricing } from '@storecraft/core/v-api/con.pricing.logic.js';
2
1
  import { StorecraftSDK } from '../index.js'
3
2
  import { collection_base } from './utils.api.fetch.js';
4
3
 
5
4
  /**
6
- * Base `orders` **CRUD**
5
+ * @description Base `orders` **CRUD**
7
6
  *
8
7
  * @extends {collection_base<
9
- * import('@storecraft/core/v-api').OrderDataUpsert,
10
- * import('@storecraft/core/v-api').OrderData>
8
+ * import('@storecraft/core/api').OrderDataUpsert,
9
+ * import('@storecraft/core/api').OrderData>
11
10
  * }
12
11
  */
13
12
  export default class Orders extends collection_base {
@@ -20,28 +19,4 @@ export default class Orders extends collection_base {
20
19
  super(sdk, 'orders');
21
20
  }
22
21
 
23
- /**
24
- * calculate pricing of line items
25
- *
26
- * @param {import('@storecraft/core/v-api').LineItem[]} line_items
27
- * @param {import('@storecraft/core/v-api').DiscountType[]} coupons
28
- * @param {import('@storecraft/core/v-api').ShippingMethodType} shipping_method
29
- * @param {string} [uid]
30
- */
31
- calculatePricing = async (
32
- line_items, coupons=[], shipping_method, uid
33
- ) => {
34
- // fetch auto discounts
35
- const auto_discounts = await this.sdk.discounts.list(
36
- {
37
- limit: 100,
38
- vql: 'app:automatic'
39
- }
40
- );
41
-
42
- return calculate_pricing(
43
- line_items, auto_discounts, coupons, shipping_method, uid
44
- );
45
- }
46
-
47
22
  }
package/src/payments.js CHANGED
@@ -4,7 +4,7 @@ import {
4
4
  } from './utils.api.fetch.js';
5
5
 
6
6
  /**
7
- *
7
+ * @description payment gateways
8
8
  */
9
9
  export default class Payments {
10
10
 
@@ -24,7 +24,7 @@ export default class Payments {
24
24
  * @param {string} handle payment gateway `handle`
25
25
  *
26
26
  *
27
- * @returns {Promise<import('@storecraft/core/v-api').PaymentGatewayItemGet>}
27
+ * @returns {Promise<import('@storecraft/core/api').PaymentGatewayItemGet>}
28
28
  */
29
29
  get(handle) {
30
30
  return get(this.sdk, 'payments/gateways', handle);
@@ -33,7 +33,7 @@ export default class Payments {
33
33
  /**
34
34
  *
35
35
  *
36
- * @returns {Promise<import('@storecraft/core/v-api').PaymentGatewayItemGet[]>}
36
+ * @returns {Promise<import('@storecraft/core/api').PaymentGatewayItemGet[]>}
37
37
  */
38
38
  list() {
39
39
  return list(this.sdk, 'payments/gateways');
@@ -48,7 +48,7 @@ export default class Payments {
48
48
  *
49
49
  * @param {string} order_id
50
50
  *
51
- * @returns {Promise<import('@storecraft/core/v-api').PaymentGatewayStatus>}
51
+ * @returns {Promise<import('@storecraft/core/api').PaymentGatewayStatus>}
52
52
  */
53
53
  paymentStatusOfOrder(order_id) {
54
54
  return fetchApiWithAuth(
@@ -69,7 +69,7 @@ export default class Payments {
69
69
  * @param {string} order_id the `id` of the `order`
70
70
  *
71
71
  *
72
- * @returns {Promise<import('@storecraft/core/v-api').PaymentGatewayStatus>}
72
+ * @returns {Promise<import('@storecraft/core/api').PaymentGatewayStatus>}
73
73
  */
74
74
  invokeAction(action_handle, order_id) {
75
75
  return fetchApiWithAuth(
package/src/posts.js CHANGED
@@ -2,11 +2,11 @@ import { StorecraftSDK } from '../index.js'
2
2
  import { collection_base } from './utils.api.fetch.js';
3
3
 
4
4
  /**
5
- * Base `posts` **CRUD**
5
+ * @description Base `posts` **CRUD**
6
6
  *
7
7
  * @extends {collection_base<
8
- * import('@storecraft/core/v-api').PostTypeUpsert,
9
- * import('@storecraft/core/v-api').PostType>
8
+ * import('@storecraft/core/api').PostTypeUpsert,
9
+ * import('@storecraft/core/api').PostType>
10
10
  * }
11
11
  */
12
12
  export default class Posts extends collection_base {
package/src/products.js CHANGED
@@ -4,11 +4,11 @@ import {
4
4
  } from './utils.api.fetch.js';
5
5
 
6
6
  /**
7
- * Base `products` **CRUD**
7
+ * @description Base `products` **CRUD**
8
8
  *
9
9
  * @extends {collection_base<
10
- * import('@storecraft/core/v-api').ProductTypeUpsert,
11
- * import('@storecraft/core/v-api').ProductType>
10
+ * import('@storecraft/core/api').ProductTypeUpsert,
11
+ * import('@storecraft/core/api').ProductType>
12
12
  * }
13
13
  */
14
14
  export default class Products extends collection_base {
@@ -44,8 +44,8 @@ export default class Products extends collection_base {
44
44
  /**
45
45
  * Add `products` to `collection`
46
46
  *
47
- * @param {import('@storecraft/core/v-api').ProductType[]} products
48
- * @param {import('@storecraft/core/v-api').CollectionType} collection
47
+ * @param {import('@storecraft/core/api').ProductType[]} products
48
+ * @param {import('@storecraft/core/api').CollectionType} collection
49
49
  */
50
50
  batchAddProductsToCollection = async (products, collection) => {
51
51
  for (const pr of products) {
@@ -59,8 +59,8 @@ export default class Products extends collection_base {
59
59
  /**
60
60
  * Remove `products` from `collection`
61
61
  *
62
- * @param {import('@storecraft/core/v-api').ProductType[]} products
63
- * @param {import('@storecraft/core/v-api').CollectionType} collection
62
+ * @param {import('@storecraft/core/api').ProductType[]} products
63
+ * @param {import('@storecraft/core/api').CollectionType} collection
64
64
  */
65
65
  batchRemoveProductsFromCollection = async (products, collection) => {
66
66
  for (const pr of products) {
package/src/settings.js CHANGED
@@ -2,7 +2,7 @@ import { StorecraftSDK } from '../index.js'
2
2
  import { collection_base } from './utils.api.fetch.js';
3
3
 
4
4
  /**
5
- * Base `settings` **CRUD**
5
+ * @description Base `settings` **CRUD**
6
6
  *
7
7
  * @extends {collection_base<any, any>}
8
8
  */
package/src/shipping.js CHANGED
@@ -2,11 +2,11 @@ import { StorecraftSDK } from '../index.js'
2
2
  import { collection_base } from './utils.api.fetch.js';
3
3
 
4
4
  /**
5
- * Base `shipping` **CRUD**
5
+ * @description Base `shipping` **CRUD**
6
6
  *
7
7
  * @extends {collection_base<
8
- * import('@storecraft/core/v-api').ShippingMethodTypeUpsert,
9
- * import('@storecraft/core/v-api').ShippingMethodType>
8
+ * import('@storecraft/core/api').ShippingMethodTypeUpsert,
9
+ * import('@storecraft/core/api').ShippingMethodType>
10
10
  * }
11
11
  */
12
12
  export default class Shipping extends collection_base {
package/src/statistics.js CHANGED
@@ -1,8 +1,11 @@
1
1
  import { App } from '@storecraft/core';
2
2
  import { StorecraftSDK } from '../index.js'
3
3
  import { fetchApiWithAuth } from './utils.api.fetch.js';
4
- import { api_query_to_searchparams } from '@storecraft/core/v-api/utils.query.js';
4
+ import { api_query_to_searchparams } from '@storecraft/core/api/utils.query.js';
5
5
 
6
+ /**
7
+ * @description statistics endpoint
8
+ */
6
9
  export default class Statistics {
7
10
  /** @type {StorecraftSDK} */
8
11
  #sdk;
@@ -35,7 +38,7 @@ export default class Statistics {
35
38
  /**
36
39
  *
37
40
  * @param {string} key
38
- * @returns {import('@storecraft/core/v-api').OrdersStatisticsType}
41
+ * @returns {import('@storecraft/core/api').OrdersStatisticsType}
39
42
  */
40
43
  fromCache = (key) => {
41
44
  if(this.isCacheValid(key))
@@ -46,7 +49,7 @@ export default class Statistics {
46
49
  /**
47
50
  *
48
51
  * @param {string} key
49
- * @param {import('@storecraft/core/v-api').OrdersStatisticsType} value
52
+ * @param {import('@storecraft/core/api').OrdersStatisticsType} value
50
53
  */
51
54
  putCache = (key, value) => {
52
55
  this.#cache[key] = value
@@ -54,12 +57,12 @@ export default class Statistics {
54
57
 
55
58
 
56
59
  /**
57
- * Load **Orders** `statistics`
60
+ * @description Load **Orders** `statistics`
58
61
  *
59
62
  * @param {string | number | Date} [from_day] `ISO` string | `UTC` | `timestamp` | `Date`
60
63
  * @param {string | number | Date} [to_day] `ISO` string | `UTC` | `timestamp` | `Date`
61
64
  *
62
- * @returns {Promise<import('@storecraft/core/v-api').OrdersStatisticsType>}
65
+ * @returns {Promise<import('@storecraft/core/api').OrdersStatisticsType>}
63
66
  */
64
67
  orders = async (from_day, to_day) => {
65
68
  const search = new URLSearchParams();
@@ -76,10 +79,10 @@ export default class Statistics {
76
79
  }
77
80
 
78
81
  /**
79
- * Load **count** `statistics`
82
+ * @description Load **count** `statistics`
80
83
  *
81
84
  * @param {keyof App["db"]["resources"]} table
82
- * @param {import('@storecraft/core/v-api').ApiQuery} [query]
85
+ * @param {import('@storecraft/core/api').ApiQuery} [query]
83
86
  *
84
87
  *
85
88
  * @returns {Promise<number>}