@storecraft/sdk 1.0.17 → 1.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/README.md +66 -36
- package/index.js +20 -2
- package/package.json +2 -3
- package/src/ai.js +8 -1
- package/src/chats.js +50 -0
- package/src/checkout.js +1 -5
- package/src/collections.js +0 -1
- package/src/customers.js +0 -1
- package/src/discounts.js +0 -1
- package/src/notifications.js +0 -1
- package/src/orders.js +0 -1
- package/src/payments.js +14 -1
- package/src/posts.js +0 -1
- package/src/products.js +0 -1
- package/src/reference.js +3 -3
- package/src/shipping.js +0 -1
- package/src/statistics.js +1 -1
- package/src/storefronts.js +0 -1
- package/src/tags.js +0 -1
- package/src/templates.js +0 -1
package/README.md
CHANGED
@@ -5,52 +5,78 @@
|
|
5
5
|
width='90%' />
|
6
6
|
</div><hr/><br/>
|
7
7
|
|
8
|
+
# **Storecraft** Official Universal `Javascript` **SDK**
|
9
|
+
|
8
10
|
This is the official `storecraft` universal javascript `SDK` which is `fetch` based,
|
9
|
-
which means you can you it both at browser and at backend runtimes such
|
11
|
+
which means you can you it both at browser and at backend runtimes such as,
|
12
|
+
- `node`
|
13
|
+
- `bun`
|
14
|
+
- `deno`
|
15
|
+
- `cloudflare workers`
|
16
|
+
|
17
|
+
Start by installing the package:
|
10
18
|
|
11
|
-
|
12
|
-
|
19
|
+
```bash
|
20
|
+
npm i @storecraft/sdk
|
21
|
+
```
|
13
22
|
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
23
|
+
## Resources
|
24
|
+
|
25
|
+
The SDK is a wrapper around the `storecraft` REST API, and it allows you to
|
26
|
+
perform operations on the following resources:
|
27
|
+
|
28
|
+
### collections
|
29
|
+
- **products** - manage products
|
30
|
+
- **collections** - manage collections
|
31
|
+
- **auth_users** - manage auth users
|
32
|
+
- **customers** - manage customers
|
33
|
+
- **orders** - manage orders
|
34
|
+
- **discounts** - manage discounts
|
35
|
+
- **storefronts** - manage storefronts
|
36
|
+
- **shipping** - manage shipping
|
37
|
+
- **tags** - manage tags
|
38
|
+
- **posts** - manage posts
|
39
|
+
- **notifications** - manage notifications
|
40
|
+
- **templates** - manage templates
|
41
|
+
- **extensions** - manage extensions
|
42
|
+
- **payments** - manage payment gateways
|
43
|
+
- **images** - manage images
|
44
|
+
|
45
|
+
### Auth
|
18
46
|
|
19
|
-
#### Auth
|
20
47
|
Perform authentication such as `signin` / `signup` / `api-key`
|
21
48
|
|
22
|
-
|
23
|
-
|
49
|
+
### Checkout
|
50
|
+
|
51
|
+
Perform checkout `create` / `complete` with payment gateways such as `stripe` / `paypal`.
|
52
|
+
|
53
|
+
### Storage
|
24
54
|
|
25
|
-
#### Storage
|
26
55
|
Perform storage operations such as `put` / `get` / `delete`
|
27
56
|
|
28
|
-
|
57
|
+
### Payments
|
58
|
+
|
29
59
|
Perform payments `status-check` / `invoke-action`
|
30
60
|
|
31
|
-
|
61
|
+
### Statistics
|
62
|
+
|
32
63
|
- Query some basic statistics about `orders` in a time span
|
33
64
|
- Query count of items in collections
|
34
65
|
|
35
|
-
|
66
|
+
### AI
|
67
|
+
|
36
68
|
Speak with a `storecraft` agent (Supports streaming :))
|
37
69
|
|
38
|
-
|
70
|
+
### Semantic / Similarity Search
|
71
|
+
|
39
72
|
Search Storecraft with AI for similar `products`, `discounts`, `collections`, `shipping` based on a prompt.
|
40
73
|
|
41
|
-
|
74
|
+
### Quick Search
|
75
|
+
|
42
76
|
List super lite search results with `id`, `handle`, `title` over most resources
|
43
77
|
|
44
78
|
<hr/>
|
45
79
|
|
46
|
-
Start by installing,
|
47
|
-
|
48
|
-
Plus, everything is typed so you dont have to guess any parameter or queryable key
|
49
|
-
|
50
|
-
```bash
|
51
|
-
npm i @storecraft/sdk
|
52
|
-
```
|
53
|
-
|
54
80
|
## Authentication
|
55
81
|
|
56
82
|
Authentication and authorization is required for some resources, such as:
|
@@ -70,7 +96,7 @@ import { StorecraftSDK } from '@storecraft/sdk'
|
|
70
96
|
|
71
97
|
const sdk = new StorecraftSDK(
|
72
98
|
{
|
73
|
-
|
99
|
+
endpoint: 'http://localhost:8000',
|
74
100
|
auth: {
|
75
101
|
apikey: <YOUR-API-KEY>
|
76
102
|
}
|
@@ -90,7 +116,7 @@ import { StorecraftSDK } from '@storecraft/sdk'
|
|
90
116
|
|
91
117
|
const sdk = new StorecraftSDK(
|
92
118
|
{
|
93
|
-
|
119
|
+
endpoint: 'http://localhost:8000',
|
94
120
|
}
|
95
121
|
);
|
96
122
|
|
@@ -106,7 +132,7 @@ import { StorecraftSDK } from '@storecraft/sdk'
|
|
106
132
|
|
107
133
|
const sdk = new StorecraftSDK(
|
108
134
|
{
|
109
|
-
|
135
|
+
endpoint: 'http://localhost:8000',
|
110
136
|
auth: {
|
111
137
|
access_token: <OPTIONAL-ACCESS-TOKEN>,
|
112
138
|
refresh_token: <OPTIONAL-REFRESH-TOKEN>,
|
@@ -125,7 +151,7 @@ import { StorecraftSDK } from '@storecraft/sdk'
|
|
125
151
|
|
126
152
|
const sdk = new StorecraftSDK(
|
127
153
|
{
|
128
|
-
|
154
|
+
endpoint: 'http://localhost:8000',
|
129
155
|
}
|
130
156
|
);
|
131
157
|
|
@@ -143,7 +169,6 @@ const auth_result = await sdk.auth.signout();
|
|
143
169
|
|
144
170
|
## Querying
|
145
171
|
|
146
|
-
|
147
172
|
Here are some examples for querying.
|
148
173
|
|
149
174
|
- Every key and string in the example below is fully typed with `typescript`,
|
@@ -159,13 +184,19 @@ const sdk = new StorecraftSDK();
|
|
159
184
|
const products: ProductType[] = await sdk.products.list(
|
160
185
|
{
|
161
186
|
expand: ['collections', 'variants'],
|
162
|
-
sortBy: ['updated_at', 'id'],
|
187
|
+
sortBy: ['updated_at', 'id'],
|
163
188
|
order: 'desc',
|
164
|
-
|
165
|
-
|
166
|
-
|
189
|
+
vql: {
|
190
|
+
updated_at: {
|
191
|
+
$gte: '2024-03-24',
|
192
|
+
},
|
193
|
+
active: true,
|
194
|
+
$or: [
|
195
|
+
{ $search: 'tag:genre_action' },
|
196
|
+
{ $search: 'tag:genre_sports' },
|
197
|
+
],
|
198
|
+
}
|
167
199
|
limit: 5,
|
168
|
-
vql: '(keyword1 | keyword2) -(keyword3)'
|
169
200
|
}
|
170
201
|
)
|
171
202
|
|
@@ -180,7 +211,7 @@ const sdk = new StorecraftSDK();
|
|
180
211
|
|
181
212
|
const collections: CollectionType[] = await sdk.collections.list(
|
182
213
|
{
|
183
|
-
|
214
|
+
vql: 'active=true & (nintendo | playstation)'
|
184
215
|
limit: 5,
|
185
216
|
}
|
186
217
|
);
|
@@ -195,7 +226,6 @@ as part of the **REST API** tests.
|
|
195
226
|
This package will hold more `unit` tests for the `sdk` itself, which include
|
196
227
|
side effects and particular behaviours.
|
197
228
|
|
198
|
-
|
199
229
|
```text
|
200
230
|
Author: Tomer Shalev (tomer.shalev@gmail.com)
|
201
231
|
```
|
package/index.js
CHANGED
@@ -21,6 +21,7 @@ import Notifications from './src/notifications.js'
|
|
21
21
|
import Storage from './src/storage.js'
|
22
22
|
import AI from './src/ai.js'
|
23
23
|
import Search from './src/search.js'
|
24
|
+
import Chats from './src/chats.js'
|
24
25
|
import {
|
25
26
|
fetchApiWithAuth, fetchOnlyApiResponseWithAuth
|
26
27
|
} from './src/utils.api.fetch.js'
|
@@ -45,7 +46,8 @@ export class StorecraftSDK {
|
|
45
46
|
*/
|
46
47
|
constructor(config, fetcher) {
|
47
48
|
this.#config = config;
|
48
|
-
this.#fetcher = fetcher ??
|
49
|
+
this.#fetcher = fetcher ??
|
50
|
+
((input, init) => fetch(input, init));
|
49
51
|
|
50
52
|
this.ai = new AI(this);
|
51
53
|
this.search = new Search(this);
|
@@ -68,6 +70,7 @@ export class StorecraftSDK {
|
|
68
70
|
this.reference = new Reference(this);
|
69
71
|
this.notifications = new Notifications(this);
|
70
72
|
this.emails = new Email(this);
|
73
|
+
this.chats = new Chats(this);
|
71
74
|
}
|
72
75
|
|
73
76
|
get fetcher() {
|
@@ -111,17 +114,31 @@ export class StorecraftSDK {
|
|
111
114
|
}
|
112
115
|
|
113
116
|
/**
|
117
|
+
* @description Update / override config with
|
118
|
+
* new properties.
|
114
119
|
* @param {StorecraftSDKConfig} [config]
|
115
120
|
*/
|
116
121
|
updateConfig(config) {
|
117
122
|
this.#config = {
|
118
|
-
...config
|
123
|
+
...(this.#config ?? {}),
|
124
|
+
...(config ?? {})
|
119
125
|
};
|
120
126
|
}
|
121
127
|
|
128
|
+
/**
|
129
|
+
* @description get the current config
|
130
|
+
*/
|
122
131
|
get config() {
|
123
132
|
return this.#config
|
124
133
|
}
|
134
|
+
|
135
|
+
/**
|
136
|
+
* @description set a config
|
137
|
+
* @param {StorecraftSDKConfig} config
|
138
|
+
*/
|
139
|
+
set config(config) {
|
140
|
+
this.#config = config;
|
141
|
+
}
|
125
142
|
}
|
126
143
|
|
127
144
|
/**
|
@@ -136,6 +153,7 @@ export const validateConfig = (config) => {
|
|
136
153
|
*/
|
137
154
|
export const create = (config) => {
|
138
155
|
const sdk = new StorecraftSDK(config);
|
156
|
+
|
139
157
|
return sdk;
|
140
158
|
}
|
141
159
|
|
package/package.json
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
{
|
2
2
|
"name": "@storecraft/sdk",
|
3
|
-
"version": "1.0
|
3
|
+
"version": "1.3.0",
|
4
4
|
"description": "Official storecraft Universal Javascript SDK",
|
5
5
|
"license": "MIT",
|
6
6
|
"author": "Tomer Shalev (https://github.com/store-craft)",
|
@@ -19,8 +19,7 @@
|
|
19
19
|
"scripts": {
|
20
20
|
"sdk:test": "echo \"Error: no test specified\" && exit 1",
|
21
21
|
"test": "npm run sdk:test",
|
22
|
-
"sc-publish": "npm publish"
|
23
|
-
"prepublishOnly": "npm version patch --force"
|
22
|
+
"sc-publish": "npm publish"
|
24
23
|
},
|
25
24
|
"type": "module",
|
26
25
|
"types": "./types.d.ts",
|
package/src/ai.js
CHANGED
@@ -56,7 +56,7 @@ export default class AI {
|
|
56
56
|
* @param {AgentRunParameters} params
|
57
57
|
*/
|
58
58
|
streamSpeak = async (agent_handle, params) => {
|
59
|
-
|
59
|
+
// console.log({config: this.sdk.config})
|
60
60
|
const response = await this.sdk.fetcher(
|
61
61
|
url(this.sdk.config, `ai/agents/${agent_handle}/stream`),
|
62
62
|
{
|
@@ -68,6 +68,13 @@ export default class AI {
|
|
68
68
|
}
|
69
69
|
);
|
70
70
|
|
71
|
+
if(!response.ok) {
|
72
|
+
const error = await response.json();
|
73
|
+
throw new Error(
|
74
|
+
`Error ${response.status}: ${error.message}`
|
75
|
+
);
|
76
|
+
}
|
77
|
+
|
71
78
|
const threadId = response.headers.get(
|
72
79
|
HEADER_STORECRAFT_THREAD_ID ?? 'X-Storecraft-Thread-Id'
|
73
80
|
);
|
package/src/chats.js
ADDED
@@ -0,0 +1,50 @@
|
|
1
|
+
/**
|
2
|
+
* @import { ChatTypeUpsert, ChatType, ChatHistoryType } from '@storecraft/core/api'
|
3
|
+
* @import { StorageSignedOperation } from '@storecraft/core/storage'
|
4
|
+
*/
|
5
|
+
import { StorecraftSDK } from '../index.js'
|
6
|
+
import { collection_base, fetchApiWithAuth } from './utils.api.fetch.js';
|
7
|
+
|
8
|
+
/**
|
9
|
+
* @description Base `chats` metadata **CRUD**
|
10
|
+
* @extends {collection_base<ChatTypeUpsert, ChatType>}
|
11
|
+
*/
|
12
|
+
export default class Chats extends collection_base {
|
13
|
+
|
14
|
+
/**
|
15
|
+
* @param {StorecraftSDK} sdk
|
16
|
+
*/
|
17
|
+
constructor(sdk) {
|
18
|
+
super(sdk, 'chats');
|
19
|
+
}
|
20
|
+
|
21
|
+
/**
|
22
|
+
* @description Download chat contents and metadata.
|
23
|
+
* @template {true | false} [PRESIGNED=false]
|
24
|
+
* @param {string} thread_id the chat thread id
|
25
|
+
* @param {PRESIGNED} [prefers_presigned_urls=false] Do you
|
26
|
+
* prefer presigned urls?
|
27
|
+
* @returns {Promise<
|
28
|
+
* PRESIGNED extends false ?
|
29
|
+
* ChatHistoryType :
|
30
|
+
* StorageSignedOperation
|
31
|
+
* >}
|
32
|
+
*/
|
33
|
+
download = async (
|
34
|
+
thread_id,
|
35
|
+
prefers_presigned_urls=/** @type {PRESIGNED} */(false)
|
36
|
+
) => {
|
37
|
+
|
38
|
+
const result = await fetchApiWithAuth(
|
39
|
+
this.sdk,
|
40
|
+
`chats/download/${thread_id}?signed=${prefers_presigned_urls}`,
|
41
|
+
{
|
42
|
+
method: 'get',
|
43
|
+
}
|
44
|
+
);
|
45
|
+
|
46
|
+
return result
|
47
|
+
}
|
48
|
+
|
49
|
+
|
50
|
+
}
|
package/src/checkout.js
CHANGED
@@ -23,10 +23,8 @@ export default class Checkout {
|
|
23
23
|
|
24
24
|
/**
|
25
25
|
* @description Create a `checkout`
|
26
|
-
*
|
27
26
|
* @param {CheckoutCreateType} input
|
28
27
|
* @param {string} gateway_handle
|
29
|
-
*
|
30
28
|
* @returns {Promise<Partial<OrderData>>}
|
31
29
|
*/
|
32
30
|
create = async (input, gateway_handle) => {
|
@@ -48,9 +46,7 @@ export default class Checkout {
|
|
48
46
|
|
49
47
|
/**
|
50
48
|
* @description Complete a `checkout`
|
51
|
-
*
|
52
49
|
* @param {string} order_id
|
53
|
-
*
|
54
50
|
* @returns {Promise<Partial<OrderData>>}
|
55
51
|
*/
|
56
52
|
complete = async (order_id) => {
|
@@ -70,7 +66,7 @@ export default class Checkout {
|
|
70
66
|
* @description calculate the pricing of an `order`.
|
71
67
|
* Using auto-discounts, coupons, shipping and line-items.
|
72
68
|
* @param {CheckoutCreateTypeAfterValidation} order
|
73
|
-
* @returns {Promise<Partial<
|
69
|
+
* @returns {Promise<Partial<Pick<OrderData, "pricing" | "validation">>>}
|
74
70
|
*/
|
75
71
|
pricing = async (order) => {
|
76
72
|
|
package/src/collections.js
CHANGED
package/src/customers.js
CHANGED
package/src/discounts.js
CHANGED
package/src/notifications.js
CHANGED
package/src/orders.js
CHANGED
package/src/payments.js
CHANGED
@@ -6,7 +6,8 @@
|
|
6
6
|
import { StorecraftSDK } from '../index.js'
|
7
7
|
import {
|
8
8
|
fetchApiWithAuth, get_from_collection_resource,
|
9
|
-
list_from_collection_resource
|
9
|
+
list_from_collection_resource,
|
10
|
+
url
|
10
11
|
} from './utils.api.fetch.js';
|
11
12
|
|
12
13
|
/**
|
@@ -94,6 +95,18 @@ export default class Payments {
|
|
94
95
|
)
|
95
96
|
}
|
96
97
|
|
98
|
+
/**
|
99
|
+
* Get an optional HTML Pay UI of the `payment gateway`
|
100
|
+
* @param {string} order_id the `id` of the `order`
|
101
|
+
* @returns {string} buy UI url
|
102
|
+
*/
|
103
|
+
getBuyUiUrl(order_id) {
|
104
|
+
return url(
|
105
|
+
this.sdk.config,
|
106
|
+
`/payments/buy_ui/${order_id}`
|
107
|
+
);
|
108
|
+
}
|
109
|
+
|
97
110
|
/**
|
98
111
|
* invoke the webhook endpoint for async payment
|
99
112
|
* @param {string} gateway_handle The handle of the `payment gateway`
|
package/src/posts.js
CHANGED
package/src/products.js
CHANGED
package/src/reference.js
CHANGED
@@ -1,5 +1,5 @@
|
|
1
1
|
/**
|
2
|
-
* @import { StorecraftConfig } from '@storecraft/core'
|
2
|
+
* @import { App, StorecraftConfig } from '@storecraft/core'
|
3
3
|
* @import { StorecraftAppPublicInfo } from '@storecraft/core/api'
|
4
4
|
*/
|
5
5
|
import { StorecraftSDK } from '../index.js'
|
@@ -24,7 +24,7 @@ export default class Reference {
|
|
24
24
|
/** @type {StorecraftConfig} */
|
25
25
|
const json = await fetchApiWithAuth(
|
26
26
|
this.sdk,
|
27
|
-
'
|
27
|
+
'settings',
|
28
28
|
{ method: 'get' },
|
29
29
|
);
|
30
30
|
return json;
|
@@ -37,7 +37,7 @@ export default class Reference {
|
|
37
37
|
/** @type {StorecraftAppPublicInfo} */
|
38
38
|
const json = await fetchApiWithAuth(
|
39
39
|
this.sdk,
|
40
|
-
'
|
40
|
+
'info',
|
41
41
|
{ method: 'get' },
|
42
42
|
);
|
43
43
|
return json;
|
package/src/shipping.js
CHANGED
package/src/statistics.js
CHANGED
@@ -58,7 +58,7 @@ export default class Statistics {
|
|
58
58
|
|
59
59
|
/**
|
60
60
|
* @description Load **count** `statistics`
|
61
|
-
* @param {keyof App["db"]["resources"]} table
|
61
|
+
* @param {keyof App["__show_me_everything"]["db"]["resources"]} table
|
62
62
|
* @param {ApiQuery} [query]
|
63
63
|
* @returns {Promise<number>}
|
64
64
|
* @throws
|
package/src/storefronts.js
CHANGED
@@ -7,7 +7,6 @@ import { collection_base, fetchApiWithAuth } from './utils.api.fetch.js';
|
|
7
7
|
|
8
8
|
/**
|
9
9
|
* @description Base `storefronts` **CRUD**
|
10
|
-
*
|
11
10
|
* @extends {collection_base<StorefrontTypeUpsert, StorefrontType>}
|
12
11
|
*/
|
13
12
|
export default class Storefronts extends collection_base {
|
package/src/tags.js
CHANGED
package/src/templates.js
CHANGED