swell-js 3.22.3 → 3.22.4

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.
@@ -0,0 +1,207 @@
1
+ import { c as cardApi } from './card-4257bc68.js';
2
+ import { g as getCookie, s as setCookie } from './cookie-4cde18fb.js';
3
+ import { c as cacheApi } from './cache-b92f4460.js';
4
+ import { m as methods$1 } from './cart-cec81203.js';
5
+ import { m as methods$2 } from './account-328cc590.js';
6
+ import { m as methods$3 } from './products-d194c3c6.js';
7
+ import { m as methods$4 } from './categories-b1d04223.js';
8
+ import { m as methods$5 } from './attributes-bfef7db7.js';
9
+ import { m as methods$6 } from './subscriptions-f077cac6.js';
10
+ import { d as defaultMethods, n as trimEnd, K as utils, l as trimStart, k as trimBoth, j as toSnake, o as stringifyQuery, E as base64Encode, t as toCamel, e as setOptions } from './index-ca9cb73c.js';
11
+ import { m as methods$7 } from './content-c366a9f8.js';
12
+ import { m as methods$8 } from './settings-34395c72.js';
13
+ import { P as PaymentController } from './index-ffb531dc.js';
14
+ import { m as methods$9 } from './locale-62622809.js';
15
+ import { m as methods$a } from './currency-004374be.js';
16
+
17
+ function methods(request) {
18
+ const { get, list } = defaultMethods(request, '/invoices', ['list', 'get']);
19
+ return {
20
+ get: (id, ...args) => {
21
+ return cacheApi.getFetch('invoices', id, () => get(id, ...args));
22
+ },
23
+
24
+ list,
25
+ };
26
+ }
27
+
28
+ const options = {
29
+ store: null,
30
+ key: null,
31
+ url: null,
32
+ useCamelCase: null,
33
+ previewContent: null,
34
+ };
35
+
36
+ const api = {
37
+ version: '3.22.4',
38
+ options,
39
+ request,
40
+
41
+ init(store, key, opt = {}) {
42
+ options.key = key;
43
+ options.store = store;
44
+ options.url = opt.url
45
+ ? trimEnd(opt.url)
46
+ : `https://${store}.swell.store`;
47
+ options.vaultUrl = opt.vaultUrl
48
+ ? trimEnd(opt.vaultUrl)
49
+ : `https://vault.schema.io`;
50
+ options.timeout = (opt.timeout && parseInt(opt.timeout, 10)) || 20000;
51
+ options.useCamelCase = opt.useCamelCase || false;
52
+ options.previewContent = opt.previewContent || false;
53
+ options.session = opt.session;
54
+ options.locale = opt.locale;
55
+ options.currency = opt.currency;
56
+ options.api = api;
57
+ options.getCart = opt.getCart;
58
+ options.updateCart = opt.updateCart;
59
+ setOptions(options);
60
+ },
61
+
62
+ // Backward compatibility
63
+ auth(...args) {
64
+ return this.init(...args);
65
+ },
66
+
67
+ get(url, query) {
68
+ return request('get', url, query);
69
+ },
70
+
71
+ put(url, data) {
72
+ return request('put', url, data);
73
+ },
74
+
75
+ post(url, data) {
76
+ return request('post', url, data);
77
+ },
78
+
79
+ delete(url, data) {
80
+ return request('delete', url, data);
81
+ },
82
+
83
+ cache: cacheApi,
84
+
85
+ card: cardApi,
86
+
87
+ cart: methods$1(request, options),
88
+
89
+ account: methods$2(request),
90
+
91
+ products: methods$3(request, options),
92
+
93
+ categories: methods$4(request),
94
+
95
+ attributes: methods$5(request),
96
+
97
+ subscriptions: methods$6(request),
98
+
99
+ invoices: methods(request),
100
+
101
+ content: methods$7(request, options),
102
+
103
+ settings: methods$8(request, options),
104
+
105
+ payment: new PaymentController(request, options),
106
+
107
+ locale: methods$9(request, options),
108
+
109
+ currency: methods$a(request, options),
110
+
111
+ utils,
112
+ };
113
+
114
+ async function request(
115
+ method,
116
+ url,
117
+ id = undefined,
118
+ data = undefined,
119
+ opt = undefined,
120
+ ) {
121
+ const allOptions = {
122
+ ...options,
123
+ ...opt,
124
+ };
125
+
126
+ const session = allOptions.session || getCookie('swell-session');
127
+ const locale = allOptions.locale || getCookie('swell-locale');
128
+ const currency = allOptions.currency || getCookie('swell-currency');
129
+
130
+ const baseUrl = `${allOptions.url}${allOptions.base || ''}/api`;
131
+ const reqMethod = String(method).toLowerCase();
132
+
133
+ let reqUrl = url;
134
+ let reqData = id;
135
+
136
+ if (data !== undefined || typeof id === 'string') {
137
+ reqUrl = [trimEnd(url), trimStart(id)].join('/');
138
+ reqData = data;
139
+ }
140
+
141
+ reqUrl = allOptions.fullUrl || `${baseUrl}/${trimBoth(reqUrl)}`;
142
+ reqData = allOptions.useCamelCase ? toSnake(reqData) : reqData;
143
+
144
+ let reqBody;
145
+ if (reqMethod === 'get') {
146
+ let exQuery;
147
+ [reqUrl, exQuery] = reqUrl.split('?');
148
+ const fullQuery = [exQuery, stringifyQuery(reqData)]
149
+ .join('&')
150
+ .replace(/^&/, '');
151
+ reqUrl = `${reqUrl}${fullQuery ? `?${fullQuery}` : ''}`;
152
+ } else {
153
+ reqBody = JSON.stringify(reqData);
154
+ }
155
+
156
+ const reqHeaders = {
157
+ Accept: 'application/json',
158
+ 'Content-Type': 'application/json',
159
+ Authorization: `Basic ${base64Encode(String(allOptions.key))}`,
160
+ };
161
+
162
+ if (session) {
163
+ reqHeaders['X-Session'] = session;
164
+ }
165
+
166
+ if (locale) {
167
+ reqHeaders['X-Locale'] = locale;
168
+ }
169
+
170
+ if (currency) {
171
+ reqHeaders['X-Currency'] = currency;
172
+ }
173
+
174
+ const response = await fetch(reqUrl, {
175
+ method: reqMethod,
176
+ headers: reqHeaders,
177
+ body: reqBody,
178
+ credentials: 'include',
179
+ mode: 'cors',
180
+ });
181
+
182
+ const responseSession = response.headers.get('X-Session');
183
+
184
+ if (typeof responseSession === 'string' && session !== responseSession) {
185
+ setCookie('swell-session', responseSession);
186
+ }
187
+
188
+ const result = await response.json();
189
+
190
+ if (result && result.error) {
191
+ const err = new Error(result.error.message);
192
+ err.status = response.status;
193
+ err.code = result.error.code;
194
+ err.param = result.error.param;
195
+ throw err;
196
+ } else if (!response.ok) {
197
+ const err = new Error(
198
+ 'A connection error occurred while making the request',
199
+ );
200
+ err.code = 'connection_error';
201
+ throw err;
202
+ }
203
+
204
+ return options.useCamelCase ? toCamel(result) : result;
205
+ }
206
+
207
+ export { api as a };
package/dist/api.js CHANGED
@@ -1,4 +1,4 @@
1
- export { a as default } from './api-73c0aac4.js';
1
+ export { a as default } from './api-6f09d9cb.js';
2
2
  import './card-4257bc68.js';
3
3
  import './index-ca9cb73c.js';
4
4
  import 'qs';
package/dist/index.js CHANGED
@@ -1,4 +1,4 @@
1
- export { a as default } from './api-73c0aac4.js';
1
+ export { a as default } from './api-6f09d9cb.js';
2
2
  import './card-4257bc68.js';
3
3
  import './index-ca9cb73c.js';
4
4
  import 'qs';
package/dist/swell.cjs.js CHANGED
@@ -9869,7 +9869,7 @@ const options = {
9869
9869
  };
9870
9870
 
9871
9871
  const api = {
9872
- version: '3.22.3',
9872
+ version: '3.22.4',
9873
9873
  options,
9874
9874
  request,
9875
9875
 
@@ -14068,7 +14068,7 @@
14068
14068
  previewContent: null
14069
14069
  };
14070
14070
  const api = {
14071
- version: "3.22.3",
14071
+ version: "3.22.4",
14072
14072
  options,
14073
14073
  request,
14074
14074
  init(store, key, opt = {}) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "swell-js",
3
- "version": "3.22.3",
3
+ "version": "3.22.4",
4
4
  "description": "Swell JS library for client-side stores",
5
5
  "engines": {
6
6
  "node": ">=14.19.1",
package/types/index.d.ts CHANGED
@@ -93,6 +93,7 @@ export interface ResultsResponse<T> {
93
93
  end: number;
94
94
  };
95
95
  };
96
+ page_count?: number;
96
97
  results: Array<T>;
97
98
  }
98
99
 
@@ -166,6 +167,7 @@ export namespace cart {
166
167
  function submitOrder(): Promise<Order>;
167
168
  function updateItem(id: string, input: CartItem): Promise<Cart>;
168
169
  function update(input: object): Promise<Cart>;
170
+ function getOrder(checkoutId?: string): Promise<Order>
169
171
  }
170
172
 
171
173
  export namespace categories {
@@ -60,7 +60,7 @@ interface ProductOptionSnake {
60
60
  | 'select'
61
61
  | 'multi_select'
62
62
  | 'file'
63
- | 'muti_file';
63
+ | 'multi_file';
64
64
  name?: string;
65
65
  parent_id?: string;
66
66
  parent_value_ids?: string[];
@@ -124,7 +124,7 @@ interface VariantSnake extends BaseModel {
124
124
 
125
125
  export interface ProductSnake extends BaseModel {
126
126
  active?: boolean;
127
- attributes?: Attribute[];
127
+ attributes?: Record<string, AttributeSnake>;
128
128
  bundle?: boolean;
129
129
  bundle_items?: Bundle[];
130
130
  category?: unknown;