@storecraft/sdk 1.0.16 → 1.2.5
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 +74 -35
- package/index.js +25 -6
- package/package.json +2 -3
- package/src/ai.js +8 -1
- package/src/auth.js +1 -1
- package/src/chats.js +50 -0
- package/src/checkout.js +0 -4
- 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 +1 -2
- package/src/posts.js +0 -1
- package/src/products.js +0 -1
- package/src/reference.js +46 -0
- package/src/search.js +2 -2
- package/src/shipping.js +0 -1
- package/src/statistics.js +2 -2
- package/src/storefronts.js +0 -1
- package/src/tags.js +0 -1
- package/src/templates.js +0 -1
- package/src/utils.api.fetch.js +1 -1
- package/src/settings.js +0 -18
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,13 +211,21 @@ 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
|
);
|
187
218
|
|
188
219
|
```
|
189
220
|
|
221
|
+
## Testing
|
222
|
+
|
223
|
+
most of the tests for this package are done in the `storecraft` `core` package,
|
224
|
+
as part of the **REST API** tests.
|
225
|
+
|
226
|
+
This package will hold more `unit` tests for the `sdk` itself, which include
|
227
|
+
side effects and particular behaviours.
|
228
|
+
|
190
229
|
```text
|
191
230
|
Author: Tomer Shalev (tomer.shalev@gmail.com)
|
192
231
|
```
|
package/index.js
CHANGED
@@ -16,18 +16,20 @@ import Images from './src/images.js'
|
|
16
16
|
import Posts from './src/posts.js'
|
17
17
|
import Checkout from './src/checkout.js'
|
18
18
|
import Payments from './src/payments.js'
|
19
|
-
import
|
19
|
+
import Reference from './src/reference.js'
|
20
20
|
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'
|
27
28
|
import Email from './src/email.js'
|
28
29
|
|
29
30
|
/**
|
30
|
-
* @description The official `storecraft` universal
|
31
|
+
* @description The official `storecraft` universal
|
32
|
+
* **SDK** for `javascript`
|
31
33
|
*/
|
32
34
|
export class StorecraftSDK {
|
33
35
|
|
@@ -44,7 +46,8 @@ export class StorecraftSDK {
|
|
44
46
|
*/
|
45
47
|
constructor(config, fetcher) {
|
46
48
|
this.#config = config;
|
47
|
-
this.#fetcher = fetcher ??
|
49
|
+
this.#fetcher = fetcher ??
|
50
|
+
((input, init) => fetch(input, init));
|
48
51
|
|
49
52
|
this.ai = new AI(this);
|
50
53
|
this.search = new Search(this);
|
@@ -64,13 +67,14 @@ export class StorecraftSDK {
|
|
64
67
|
this.posts = new Posts(this);
|
65
68
|
this.payments = new Payments(this);
|
66
69
|
this.checkout = new Checkout(this);
|
67
|
-
this.
|
70
|
+
this.reference = new Reference(this);
|
68
71
|
this.notifications = new Notifications(this);
|
69
72
|
this.emails = new Email(this);
|
73
|
+
this.chats = new Chats(this);
|
70
74
|
}
|
71
75
|
|
72
76
|
get fetcher() {
|
73
|
-
return this.#fetcher
|
77
|
+
return this.#fetcher;
|
74
78
|
}
|
75
79
|
|
76
80
|
/**
|
@@ -110,17 +114,31 @@ export class StorecraftSDK {
|
|
110
114
|
}
|
111
115
|
|
112
116
|
/**
|
117
|
+
* @description Update / override config with
|
118
|
+
* new properties.
|
113
119
|
* @param {StorecraftSDKConfig} [config]
|
114
120
|
*/
|
115
121
|
updateConfig(config) {
|
116
122
|
this.#config = {
|
117
|
-
...config
|
123
|
+
...(this.#config ?? {}),
|
124
|
+
...(config ?? {})
|
118
125
|
};
|
119
126
|
}
|
120
127
|
|
128
|
+
/**
|
129
|
+
* @description get the current config
|
130
|
+
*/
|
121
131
|
get config() {
|
122
132
|
return this.#config
|
123
133
|
}
|
134
|
+
|
135
|
+
/**
|
136
|
+
* @description set a config
|
137
|
+
* @param {StorecraftSDKConfig} config
|
138
|
+
*/
|
139
|
+
set config(config) {
|
140
|
+
this.#config = config;
|
141
|
+
}
|
124
142
|
}
|
125
143
|
|
126
144
|
/**
|
@@ -135,6 +153,7 @@ export const validateConfig = (config) => {
|
|
135
153
|
*/
|
136
154
|
export const create = (config) => {
|
137
155
|
const sdk = new StorecraftSDK(config);
|
156
|
+
|
138
157
|
return sdk;
|
139
158
|
}
|
140
159
|
|
package/package.json
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
{
|
2
2
|
"name": "@storecraft/sdk",
|
3
|
-
"version": "1.
|
3
|
+
"version": "1.2.5",
|
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/auth.js
CHANGED
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) => {
|
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
@@ -1,13 +1,12 @@
|
|
1
1
|
/**
|
2
2
|
* @import { OrderDataUpsert, OrderData, ApiQuery } from '@storecraft/core/api'
|
3
3
|
*/
|
4
|
-
import { api_query_to_searchparams } from '@storecraft/core/api/
|
4
|
+
import { api_query_to_searchparams } from '@storecraft/core/api/query.js';
|
5
5
|
import { StorecraftSDK } from '../index.js'
|
6
6
|
import { collection_base, fetchApiWithAuth } from './utils.api.fetch.js';
|
7
7
|
|
8
8
|
/**
|
9
9
|
* @description Base `orders` **CRUD**
|
10
|
-
*
|
11
10
|
* @extends {collection_base<OrderDataUpsert, OrderData>}
|
12
11
|
*/
|
13
12
|
export default class Orders extends collection_base {
|
package/src/posts.js
CHANGED
package/src/products.js
CHANGED
package/src/reference.js
ADDED
@@ -0,0 +1,46 @@
|
|
1
|
+
/**
|
2
|
+
* @import { App, StorecraftConfig } from '@storecraft/core'
|
3
|
+
* @import { StorecraftAppPublicInfo } from '@storecraft/core/api'
|
4
|
+
*/
|
5
|
+
import { StorecraftSDK } from '../index.js'
|
6
|
+
import { fetchApiWithAuth } from './utils.api.fetch.js';
|
7
|
+
|
8
|
+
/**
|
9
|
+
* @description Reference
|
10
|
+
*/
|
11
|
+
export default class Reference {
|
12
|
+
|
13
|
+
/**
|
14
|
+
* @param {StorecraftSDK} sdk
|
15
|
+
*/
|
16
|
+
constructor(sdk) {
|
17
|
+
this.sdk = sdk;
|
18
|
+
}
|
19
|
+
|
20
|
+
/**
|
21
|
+
* @returns {Promise<StorecraftConfig>}
|
22
|
+
*/
|
23
|
+
settings = async () => {
|
24
|
+
/** @type {StorecraftConfig} */
|
25
|
+
const json = await fetchApiWithAuth(
|
26
|
+
this.sdk,
|
27
|
+
'settings',
|
28
|
+
{ method: 'get' },
|
29
|
+
);
|
30
|
+
return json;
|
31
|
+
}
|
32
|
+
|
33
|
+
/**
|
34
|
+
* @returns {Promise<StorecraftAppPublicInfo>}
|
35
|
+
*/
|
36
|
+
info = async () => {
|
37
|
+
/** @type {StorecraftAppPublicInfo} */
|
38
|
+
const json = await fetchApiWithAuth(
|
39
|
+
this.sdk,
|
40
|
+
'info',
|
41
|
+
{ method: 'get' },
|
42
|
+
);
|
43
|
+
return json;
|
44
|
+
}
|
45
|
+
|
46
|
+
}
|
package/src/search.js
CHANGED
@@ -6,10 +6,10 @@
|
|
6
6
|
|
7
7
|
import {
|
8
8
|
api_query_to_searchparams, object_to_search_params,
|
9
|
-
|
10
|
-
} from '@storecraft/core/api/utils.query.js';
|
9
|
+
} from '@storecraft/core/api/query.js';
|
11
10
|
import { StorecraftSDK } from '../index.js'
|
12
11
|
import { fetchApiWithAuth, url } from './utils.api.fetch.js';
|
12
|
+
import { string_array_to_string } from '@storecraft/core/api/query.utils.js';
|
13
13
|
|
14
14
|
/**
|
15
15
|
* @description **Search** API (two options):
|
package/src/shipping.js
CHANGED
package/src/statistics.js
CHANGED
@@ -6,7 +6,7 @@ import { StorecraftSDK } from '../index.js'
|
|
6
6
|
import { fetchApiWithAuth } from './utils.api.fetch.js';
|
7
7
|
import {
|
8
8
|
api_query_to_searchparams
|
9
|
-
} from '@storecraft/core/api/
|
9
|
+
} from '@storecraft/core/api/query.js';
|
10
10
|
|
11
11
|
/**
|
12
12
|
* @description statistics endpoint
|
@@ -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
package/src/utils.api.fetch.js
CHANGED
package/src/settings.js
DELETED
@@ -1,18 +0,0 @@
|
|
1
|
-
import { StorecraftSDK } from '../index.js'
|
2
|
-
import { collection_base } from './utils.api.fetch.js';
|
3
|
-
|
4
|
-
/**
|
5
|
-
* @description Base `settings` **CRUD**
|
6
|
-
*
|
7
|
-
* @extends {collection_base<any, any>}
|
8
|
-
*/
|
9
|
-
export default class Settings extends collection_base {
|
10
|
-
|
11
|
-
/**
|
12
|
-
* @param {StorecraftSDK} sdk
|
13
|
-
*/
|
14
|
-
constructor(sdk) {
|
15
|
-
super(sdk, 'reference/settings');
|
16
|
-
}
|
17
|
-
|
18
|
-
}
|