@wix/sdk 1.13.0 → 1.14.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.
@@ -42,6 +42,7 @@ import { parsePublicKeyIfEncoded } from '../helpers.js';
42
42
  export function AppStrategy(opts) {
43
43
  const authServerBaseUrl = opts.authServerBaseUrl ?? 'https://www.wixapis.com';
44
44
  let refreshToken = 'refreshToken' in opts ? opts.refreshToken : undefined;
45
+ let cachedToken;
45
46
  return {
46
47
  getInstallUrl({ redirectUrl, token, state }) {
47
48
  const params = new URLSearchParams();
@@ -94,6 +95,13 @@ export function AppStrategy(opts) {
94
95
  };
95
96
  },
96
97
  async getAuthHeaders() {
98
+ if (cachedToken && cachedToken.expiresAt > Date.now()) {
99
+ return {
100
+ headers: {
101
+ Authorization: cachedToken.token,
102
+ },
103
+ };
104
+ }
97
105
  if ('refreshToken' in opts || refreshToken) {
98
106
  if (!opts.appSecret) {
99
107
  throw new Error('App secret is required for retrieveing app-level access tokens. Make sure to pass it to the AppStrategy');
@@ -143,6 +151,10 @@ export function AppStrategy(opts) {
143
151
  throw new Error(`Failed to exchange instance ID for access token. Unexpected status code from Wix OAuth API: ${tokensRes.status}`);
144
152
  }
145
153
  const tokens = (await tokensRes.json());
154
+ cachedToken = {
155
+ token: tokens.access_token,
156
+ expiresAt: Date.now() + tokens.expires_in * 1000,
157
+ };
146
158
  return {
147
159
  headers: {
148
160
  Authorization: tokens.access_token,
@@ -150,9 +162,28 @@ export function AppStrategy(opts) {
150
162
  };
151
163
  }
152
164
  else if ('accessToken' in opts && opts.accessToken) {
165
+ const tokenRes = await fetch(new URL('/oauth2/token', authServerBaseUrl), {
166
+ method: 'POST',
167
+ headers: {
168
+ 'Content-Type': 'application/json',
169
+ },
170
+ body: JSON.stringify({
171
+ grant_type: 'strip_bound_session',
172
+ client_secret: opts.appSecret,
173
+ access_token: opts.accessToken,
174
+ }),
175
+ });
176
+ if (tokenRes.status !== 200) {
177
+ throw new Error(`Failed to get unbound token. Unexpected status code from Wix OAuth API: ${tokenRes.status}`);
178
+ }
179
+ const { access_token, expires_in } = (await tokenRes.json());
180
+ cachedToken = {
181
+ token: access_token,
182
+ expiresAt: Date.now() + expires_in * 1000,
183
+ };
153
184
  return {
154
185
  headers: {
155
- Authorization: opts.accessToken,
186
+ Authorization: access_token,
156
187
  },
157
188
  };
158
189
  }
@@ -68,6 +68,7 @@ const helpers_js_1 = require("../helpers.js");
68
68
  function AppStrategy(opts) {
69
69
  const authServerBaseUrl = opts.authServerBaseUrl ?? 'https://www.wixapis.com';
70
70
  let refreshToken = 'refreshToken' in opts ? opts.refreshToken : undefined;
71
+ let cachedToken;
71
72
  return {
72
73
  getInstallUrl({ redirectUrl, token, state }) {
73
74
  const params = new URLSearchParams();
@@ -120,6 +121,13 @@ function AppStrategy(opts) {
120
121
  };
121
122
  },
122
123
  async getAuthHeaders() {
124
+ if (cachedToken && cachedToken.expiresAt > Date.now()) {
125
+ return {
126
+ headers: {
127
+ Authorization: cachedToken.token,
128
+ },
129
+ };
130
+ }
123
131
  if ('refreshToken' in opts || refreshToken) {
124
132
  if (!opts.appSecret) {
125
133
  throw new Error('App secret is required for retrieveing app-level access tokens. Make sure to pass it to the AppStrategy');
@@ -169,6 +177,10 @@ function AppStrategy(opts) {
169
177
  throw new Error(`Failed to exchange instance ID for access token. Unexpected status code from Wix OAuth API: ${tokensRes.status}`);
170
178
  }
171
179
  const tokens = (await tokensRes.json());
180
+ cachedToken = {
181
+ token: tokens.access_token,
182
+ expiresAt: Date.now() + tokens.expires_in * 1000,
183
+ };
172
184
  return {
173
185
  headers: {
174
186
  Authorization: tokens.access_token,
@@ -176,9 +188,28 @@ function AppStrategy(opts) {
176
188
  };
177
189
  }
178
190
  else if ('accessToken' in opts && opts.accessToken) {
191
+ const tokenRes = await fetch(new URL('/oauth2/token', authServerBaseUrl), {
192
+ method: 'POST',
193
+ headers: {
194
+ 'Content-Type': 'application/json',
195
+ },
196
+ body: JSON.stringify({
197
+ grant_type: 'strip_bound_session',
198
+ client_secret: opts.appSecret,
199
+ access_token: opts.accessToken,
200
+ }),
201
+ });
202
+ if (tokenRes.status !== 200) {
203
+ throw new Error(`Failed to get unbound token. Unexpected status code from Wix OAuth API: ${tokenRes.status}`);
204
+ }
205
+ const { access_token, expires_in } = (await tokenRes.json());
206
+ cachedToken = {
207
+ token: access_token,
208
+ expiresAt: Date.now() + expires_in * 1000,
209
+ };
179
210
  return {
180
211
  headers: {
181
- Authorization: opts.accessToken,
212
+ Authorization: access_token,
182
213
  },
183
214
  };
184
215
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@wix/sdk",
3
- "version": "1.13.0",
3
+ "version": "1.14.0",
4
4
  "license": "UNLICENSED",
5
5
  "author": {
6
6
  "name": "Ronny Ringel",
@@ -65,11 +65,11 @@
65
65
  "dependencies": {
66
66
  "@babel/runtime": "^7.23.2",
67
67
  "@wix/identity": "^1.0.78",
68
- "@wix/image-kit": "^1.87.0",
68
+ "@wix/image-kit": "^1.88.0",
69
69
  "@wix/redirects": "^1.0.41",
70
70
  "@wix/sdk-context": "^0.0.1",
71
- "@wix/sdk-runtime": "0.3.20",
72
- "@wix/sdk-types": "^1.12.1",
71
+ "@wix/sdk-runtime": "0.3.21",
72
+ "@wix/sdk-types": "^1.12.3",
73
73
  "jose": "^5.2.1",
74
74
  "querystring": "^0.2.1",
75
75
  "type-fest": "^4.9.0"
@@ -85,7 +85,7 @@
85
85
  "@wix/events": "^1.0.179",
86
86
  "@wix/metro": "^1.0.73",
87
87
  "@wix/metro-runtime": "^1.1677.0",
88
- "@wix/sdk-runtime": "0.3.20",
88
+ "@wix/sdk-runtime": "0.3.21",
89
89
  "eslint": "^8.56.0",
90
90
  "eslint-config-sdk": "0.0.0",
91
91
  "graphql": "^16.8.0",
@@ -119,5 +119,5 @@
119
119
  "wallaby": {
120
120
  "autoDetect": true
121
121
  },
122
- "falconPackageHash": "2a7a057c5af253e6e991b4f7f14cd28485c55e8631c6c057f3373333"
122
+ "falconPackageHash": "9237cbbe00c3cd810629e85264f0387d8320bc0e96e18d082de1c0eb"
123
123
  }