@foxy.io/sdk 1.15.0 → 1.16.0-beta.1
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/cjs/backend/API.js +47 -21
- package/dist/esm/backend/API.js +47 -21
- package/dist/types/backend/Graph/store.d.ts +8 -4
- package/dist/types/backend/Graph/subscription.d.ts +2 -0
- package/dist/types/backend/Graph/webhook.d.ts +1 -1
- package/dist/types/customer/Graph/item.d.ts +5 -0
- package/dist/types/customer/Graph/item_option.d.ts +31 -0
- package/dist/types/customer/Graph/item_options.d.ts +10 -0
- package/dist/types/customer/Graph/subscription.d.ts +2 -0
- package/dist/types/customer/Graph/transaction_template.d.ts +1 -1
- package/dist/types/customer/Rels.d.ts +4 -0
- package/package.json +1 -1
package/dist/cjs/backend/API.js
CHANGED
|
@@ -84,41 +84,67 @@ class API extends Core.API {
|
|
|
84
84
|
var _a, _b, _c;
|
|
85
85
|
return __awaiter(this, void 0, void 0, function* () {
|
|
86
86
|
let token = JSON.parse((_a = this.storage.getItem(API.ACCESS_TOKEN)) !== null && _a !== void 0 ? _a : 'null');
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
if (expiresAt < refreshAt) {
|
|
92
|
-
this.storage.removeItem(API.ACCESS_TOKEN);
|
|
93
|
-
this.console.info('Removed old access token from the storage.');
|
|
94
|
-
token = null;
|
|
95
|
-
}
|
|
96
|
-
}
|
|
97
|
-
if (token === null) {
|
|
98
|
-
this.console.trace("Access token isn't present in the storage. Fetching a new one...");
|
|
87
|
+
let request = new cross_fetch_1.Request(input, init);
|
|
88
|
+
let headers = request.headers;
|
|
89
|
+
const fetchNewAccessToken = () => __awaiter(this, void 0, void 0, function* () {
|
|
90
|
+
this.console.trace('Fetching a new access token...');
|
|
99
91
|
const rawToken = yield API.getToken(this, true).catch(err => {
|
|
100
92
|
this.console.error(err.message);
|
|
101
93
|
return null;
|
|
102
94
|
});
|
|
103
95
|
if (rawToken) {
|
|
104
|
-
token = Object.assign(Object.assign({}, rawToken), { date_created: new Date().toISOString() });
|
|
96
|
+
const token = Object.assign(Object.assign({}, rawToken), { date_created: new Date().toISOString() });
|
|
105
97
|
this.storage.setItem(API.ACCESS_TOKEN, JSON.stringify(token));
|
|
106
98
|
this.console.info('Access token updated.');
|
|
99
|
+
return token;
|
|
107
100
|
}
|
|
108
101
|
else {
|
|
109
102
|
this.console.warn('Failed to fetch access token. Proceeding without authentication.');
|
|
103
|
+
return null;
|
|
104
|
+
}
|
|
105
|
+
});
|
|
106
|
+
const setHeaders = (accessToken) => {
|
|
107
|
+
if (!headers.get('Authorization') && accessToken)
|
|
108
|
+
headers.set('Authorization', `Bearer ${accessToken}`);
|
|
109
|
+
if (!headers.get('Content-Type'))
|
|
110
|
+
headers.set('Content-Type', 'application/json');
|
|
111
|
+
if (!headers.get('FOXY-API-VERSION'))
|
|
112
|
+
headers.set('FOXY-API-VERSION', this.version);
|
|
113
|
+
};
|
|
114
|
+
if (token) {
|
|
115
|
+
const expiresAt = new Date(token.date_created).getTime() + token.expires_in * 1000;
|
|
116
|
+
const refreshAt = Date.now() + API.REFRESH_THRESHOLD;
|
|
117
|
+
if (expiresAt < refreshAt) {
|
|
118
|
+
this.storage.removeItem(API.ACCESS_TOKEN);
|
|
119
|
+
this.console.info('Removed old access token from the storage.');
|
|
120
|
+
token = yield fetchNewAccessToken();
|
|
110
121
|
}
|
|
111
122
|
}
|
|
112
|
-
|
|
123
|
+
else {
|
|
124
|
+
this.console.trace("Access token isn't present in the storage.");
|
|
125
|
+
token = yield fetchNewAccessToken();
|
|
126
|
+
}
|
|
127
|
+
setHeaders(token === null || token === void 0 ? void 0 : token.access_token);
|
|
113
128
|
const method = (_c = (_b = init === null || init === void 0 ? void 0 : init.method) === null || _b === void 0 ? void 0 : _b.toUpperCase()) !== null && _c !== void 0 ? _c : 'GET';
|
|
114
|
-
if (!headers.get('Authorization') && token)
|
|
115
|
-
headers.set('Authorization', `Bearer ${token.access_token}`);
|
|
116
|
-
if (!headers.get('Content-Type'))
|
|
117
|
-
headers.set('Content-Type', 'application/json');
|
|
118
|
-
if (!headers.get('FOXY-API-VERSION'))
|
|
119
|
-
headers.set('FOXY-API-VERSION', this.version);
|
|
120
129
|
this.console.trace(`${method} ${request.url}`);
|
|
121
|
-
|
|
130
|
+
let response = yield cross_fetch_1.fetch(request);
|
|
131
|
+
if (response.status === 401) {
|
|
132
|
+
const { error } = (yield response.clone().json());
|
|
133
|
+
if (error === 'invalid_token') {
|
|
134
|
+
this.console.info('Access token is invalid or expired.');
|
|
135
|
+
this.storage.removeItem(API.ACCESS_TOKEN);
|
|
136
|
+
this.console.info('Removed old access token from the storage.');
|
|
137
|
+
token = yield fetchNewAccessToken();
|
|
138
|
+
if (token) {
|
|
139
|
+
request = new cross_fetch_1.Request(input, init);
|
|
140
|
+
headers = request.headers;
|
|
141
|
+
setHeaders(token.access_token);
|
|
142
|
+
this.console.trace(`Retrying ${method} ${request.url}`);
|
|
143
|
+
response = yield cross_fetch_1.fetch(request);
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
return response;
|
|
122
148
|
});
|
|
123
149
|
}
|
|
124
150
|
}
|
package/dist/esm/backend/API.js
CHANGED
|
@@ -59,41 +59,67 @@ export class API extends Core.API {
|
|
|
59
59
|
var _a, _b, _c;
|
|
60
60
|
return __awaiter(this, void 0, void 0, function* () {
|
|
61
61
|
let token = JSON.parse((_a = this.storage.getItem(API.ACCESS_TOKEN)) !== null && _a !== void 0 ? _a : 'null');
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
if (expiresAt < refreshAt) {
|
|
67
|
-
this.storage.removeItem(API.ACCESS_TOKEN);
|
|
68
|
-
this.console.info('Removed old access token from the storage.');
|
|
69
|
-
token = null;
|
|
70
|
-
}
|
|
71
|
-
}
|
|
72
|
-
if (token === null) {
|
|
73
|
-
this.console.trace("Access token isn't present in the storage. Fetching a new one...");
|
|
62
|
+
let request = new Request(input, init);
|
|
63
|
+
let headers = request.headers;
|
|
64
|
+
const fetchNewAccessToken = () => __awaiter(this, void 0, void 0, function* () {
|
|
65
|
+
this.console.trace('Fetching a new access token...');
|
|
74
66
|
const rawToken = yield API.getToken(this, true).catch(err => {
|
|
75
67
|
this.console.error(err.message);
|
|
76
68
|
return null;
|
|
77
69
|
});
|
|
78
70
|
if (rawToken) {
|
|
79
|
-
token = Object.assign(Object.assign({}, rawToken), { date_created: new Date().toISOString() });
|
|
71
|
+
const token = Object.assign(Object.assign({}, rawToken), { date_created: new Date().toISOString() });
|
|
80
72
|
this.storage.setItem(API.ACCESS_TOKEN, JSON.stringify(token));
|
|
81
73
|
this.console.info('Access token updated.');
|
|
74
|
+
return token;
|
|
82
75
|
}
|
|
83
76
|
else {
|
|
84
77
|
this.console.warn('Failed to fetch access token. Proceeding without authentication.');
|
|
78
|
+
return null;
|
|
79
|
+
}
|
|
80
|
+
});
|
|
81
|
+
const setHeaders = (accessToken) => {
|
|
82
|
+
if (!headers.get('Authorization') && accessToken)
|
|
83
|
+
headers.set('Authorization', `Bearer ${accessToken}`);
|
|
84
|
+
if (!headers.get('Content-Type'))
|
|
85
|
+
headers.set('Content-Type', 'application/json');
|
|
86
|
+
if (!headers.get('FOXY-API-VERSION'))
|
|
87
|
+
headers.set('FOXY-API-VERSION', this.version);
|
|
88
|
+
};
|
|
89
|
+
if (token) {
|
|
90
|
+
const expiresAt = new Date(token.date_created).getTime() + token.expires_in * 1000;
|
|
91
|
+
const refreshAt = Date.now() + API.REFRESH_THRESHOLD;
|
|
92
|
+
if (expiresAt < refreshAt) {
|
|
93
|
+
this.storage.removeItem(API.ACCESS_TOKEN);
|
|
94
|
+
this.console.info('Removed old access token from the storage.');
|
|
95
|
+
token = yield fetchNewAccessToken();
|
|
85
96
|
}
|
|
86
97
|
}
|
|
87
|
-
|
|
98
|
+
else {
|
|
99
|
+
this.console.trace("Access token isn't present in the storage.");
|
|
100
|
+
token = yield fetchNewAccessToken();
|
|
101
|
+
}
|
|
102
|
+
setHeaders(token === null || token === void 0 ? void 0 : token.access_token);
|
|
88
103
|
const method = (_c = (_b = init === null || init === void 0 ? void 0 : init.method) === null || _b === void 0 ? void 0 : _b.toUpperCase()) !== null && _c !== void 0 ? _c : 'GET';
|
|
89
|
-
if (!headers.get('Authorization') && token)
|
|
90
|
-
headers.set('Authorization', `Bearer ${token.access_token}`);
|
|
91
|
-
if (!headers.get('Content-Type'))
|
|
92
|
-
headers.set('Content-Type', 'application/json');
|
|
93
|
-
if (!headers.get('FOXY-API-VERSION'))
|
|
94
|
-
headers.set('FOXY-API-VERSION', this.version);
|
|
95
104
|
this.console.trace(`${method} ${request.url}`);
|
|
96
|
-
|
|
105
|
+
let response = yield fetch(request);
|
|
106
|
+
if (response.status === 401) {
|
|
107
|
+
const { error } = (yield response.clone().json());
|
|
108
|
+
if (error === 'invalid_token') {
|
|
109
|
+
this.console.info('Access token is invalid or expired.');
|
|
110
|
+
this.storage.removeItem(API.ACCESS_TOKEN);
|
|
111
|
+
this.console.info('Removed old access token from the storage.');
|
|
112
|
+
token = yield fetchNewAccessToken();
|
|
113
|
+
if (token) {
|
|
114
|
+
request = new Request(input, init);
|
|
115
|
+
headers = request.headers;
|
|
116
|
+
setHeaders(token.access_token);
|
|
117
|
+
this.console.trace(`Retrying ${method} ${request.url}`);
|
|
118
|
+
response = yield fetch(request);
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
return response;
|
|
97
123
|
});
|
|
98
124
|
}
|
|
99
125
|
}
|
|
@@ -99,10 +99,14 @@ export interface Store extends Graph {
|
|
|
99
99
|
'fx:customer_portal_settings': CustomerPortalSettings;
|
|
100
100
|
/** POST here to resend the daily subscription webhook notification for this store. */
|
|
101
101
|
'fx:process_subscription_webhook': ProcessSubscriptionWebhook;
|
|
102
|
-
/** Add-to-cart URL for the
|
|
103
|
-
'fx:
|
|
104
|
-
/** Add-to-cart URL for the
|
|
105
|
-
'fx:
|
|
102
|
+
/** Add-to-cart URL for the Starter plan with yearly billing. */
|
|
103
|
+
'fx:activate_store_starter_yearly_url': { curie: 'fx:activate_store_starter_yearly_url' };
|
|
104
|
+
/** Add-to-cart URL for the Starter plan with monthly billing. */
|
|
105
|
+
'fx:activate_store_starter_monthly_url': { curie: 'fx:activate_store_starter_monthly_url' };
|
|
106
|
+
/** Add-to-cart URL for the Growth plan with yearly billing. */
|
|
107
|
+
'fx:activate_store_growth_yearly_url': { curie: 'fx:activate_store_growth_yearly_url' };
|
|
108
|
+
/** Add-to-cart URL for the Growth plan with monthly billing. */
|
|
109
|
+
'fx:activate_store_growth_monthly_url': { curie: 'fx:activate_store_growth_monthly_url' };
|
|
106
110
|
/** Add-to-cart URL for the Advanced plan with yearly billing. */
|
|
107
111
|
'fx:activate_store_advanced_yearly_url': { curie: 'fx:activate_store_advanced_yearly_url' };
|
|
108
112
|
/** Add-to-cart URL for the Advanced plan with monthly billing. */
|
|
@@ -55,6 +55,8 @@ export interface Subscription extends Graph {
|
|
|
55
55
|
is_active: boolean;
|
|
56
56
|
/** If this subscription is using a third party subscription system such as PayPal Express, their identifier will be set here. */
|
|
57
57
|
third_party_id: string;
|
|
58
|
+
/** The type of payment method used for this subscription. Can be `null` if payment method info is unavailable via this API. */
|
|
59
|
+
payment_type: 'plastic' | 'paypal' | 'ach' | 'amazon' | null;
|
|
58
60
|
/** The date this resource was created. */
|
|
59
61
|
date_created: string | null;
|
|
60
62
|
/** The date this resource was last modified. */
|
|
@@ -34,7 +34,7 @@ export interface Webhook extends Graph {
|
|
|
34
34
|
/** The JSON webhooks are encrypted in certain situations. This key is also used to generate a signature to verify the integrity of the payload. 1000 characters or less. */
|
|
35
35
|
encryption_key: string | null;
|
|
36
36
|
/** The type of resource to observe changes on. */
|
|
37
|
-
event_resource:
|
|
37
|
+
event_resource: 'subscription' | 'transaction' | 'transaction_log' | 'customer';
|
|
38
38
|
/** If set to `0`, events will not be triggered for this webhook. This can also be set to `0` automatically if there are too many consecutive failed attempts to send a payload to this endpoint. */
|
|
39
39
|
is_active: 0 | 1;
|
|
40
40
|
/** The date this resource was created. */
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import type { Graph } from '../../core';
|
|
2
|
+
import type { ItemOptions } from './item_options';
|
|
2
3
|
|
|
3
4
|
export interface Item extends Graph {
|
|
4
5
|
curie: 'fx:item';
|
|
@@ -62,4 +63,8 @@ export interface Item extends Graph {
|
|
|
62
63
|
/** The date this resource was last modified. */
|
|
63
64
|
date_modified: string | null;
|
|
64
65
|
};
|
|
66
|
+
|
|
67
|
+
zooms: {
|
|
68
|
+
item_options?: ItemOptions;
|
|
69
|
+
};
|
|
65
70
|
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import type { Graph } from '../../core';
|
|
2
|
+
import type { Item } from './item';
|
|
3
|
+
import type { Transaction } from './transaction';
|
|
4
|
+
|
|
5
|
+
export interface ItemOption extends Graph {
|
|
6
|
+
curie: 'fx:item_option';
|
|
7
|
+
|
|
8
|
+
links: {
|
|
9
|
+
/** This resource. */
|
|
10
|
+
'self': ItemOption;
|
|
11
|
+
/** Item this option is attached to. */
|
|
12
|
+
'fx:item': Item;
|
|
13
|
+
/** Related transaction resource. */
|
|
14
|
+
'fx:transaction': Transaction;
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
props: {
|
|
18
|
+
/** The name of this item option. */
|
|
19
|
+
name: string;
|
|
20
|
+
/** The value of this item option. */
|
|
21
|
+
value: string;
|
|
22
|
+
/** The price modifier for this item option. The price of the item in the cart will be adjusted by this amount because of this item option. */
|
|
23
|
+
price_mod: number;
|
|
24
|
+
/** The weight modifier for this item option. The weight of the item in the cart will be adjusted by this amount because of this item option. */
|
|
25
|
+
weight_mod: number;
|
|
26
|
+
/** The date this resource was created. */
|
|
27
|
+
date_created: string | null;
|
|
28
|
+
/** The date this resource was last modified. */
|
|
29
|
+
date_modified: string | null;
|
|
30
|
+
};
|
|
31
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { CollectionGraphLinks, CollectionGraphProps } from '../../core/defaults';
|
|
2
|
+
import type { Graph } from '../../core';
|
|
3
|
+
import type { ItemOption } from './item_option';
|
|
4
|
+
|
|
5
|
+
export interface ItemOptions extends Graph {
|
|
6
|
+
curie: 'fx:item_options';
|
|
7
|
+
links: CollectionGraphLinks<ItemOptions>;
|
|
8
|
+
props: CollectionGraphProps;
|
|
9
|
+
child: ItemOption;
|
|
10
|
+
}
|
|
@@ -46,6 +46,8 @@ export interface Subscription extends Graph {
|
|
|
46
46
|
is_active: boolean;
|
|
47
47
|
/** If this subscription is using a third party subscription system such as PayPal Express, their identifier will be set here. */
|
|
48
48
|
third_party_id: string;
|
|
49
|
+
/** The type of payment method used for this subscription. Can be `null` if payment method info is unavailable via this API. */
|
|
50
|
+
payment_type: 'plastic' | 'paypal' | 'ach' | 'amazon' | null;
|
|
49
51
|
/** The date this resource was created. */
|
|
50
52
|
date_created: string | null;
|
|
51
53
|
/** The date this resource was last modified. */
|
|
@@ -74,7 +74,7 @@ export interface TransactionTemplate extends Graph {
|
|
|
74
74
|
/** If this cart has any shippable subscription items which will process in the future, this will be the total amount of shipping costs for those items. */
|
|
75
75
|
total_future_shipping: string;
|
|
76
76
|
/** Total order amount of this cart including all items, taxes, shipping costs and discounts. */
|
|
77
|
-
total_order:
|
|
77
|
+
total_order: number;
|
|
78
78
|
/** The 3 character ISO code for the currency. */
|
|
79
79
|
currency_code: string;
|
|
80
80
|
/** The currency symbol, such as $, £, €, etc. */
|
|
@@ -1,11 +1,15 @@
|
|
|
1
1
|
export * from './Graph/attribute';
|
|
2
2
|
export * from './Graph/attributes';
|
|
3
|
+
export * from './Graph/custom_field';
|
|
4
|
+
export * from './Graph/custom_fields';
|
|
3
5
|
export * from './Graph/customer_address';
|
|
4
6
|
export * from './Graph/customer_addresses';
|
|
5
7
|
export * from './Graph/customer_portal_settings';
|
|
6
8
|
export * from './Graph/default_billing_address';
|
|
7
9
|
export * from './Graph/default_payment_method';
|
|
8
10
|
export * from './Graph/default_shipping_address';
|
|
11
|
+
export * from './Graph/item_option';
|
|
12
|
+
export * from './Graph/item_options';
|
|
9
13
|
export * from './Graph/item';
|
|
10
14
|
export * from './Graph/items';
|
|
11
15
|
export * from './Graph/last_transaction';
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@foxy.io/sdk",
|
|
3
3
|
"type": "commonjs",
|
|
4
|
-
"version": "1.
|
|
4
|
+
"version": "1.16.0-beta.1",
|
|
5
5
|
"description": "Universal SDK for a full server-side and a limited in-browser access to Foxy hAPI.",
|
|
6
6
|
"main": "dist/cjs/index.js",
|
|
7
7
|
"module": "dist/esm/index.js",
|