@wix/sdk 1.12.6 → 1.12.8

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.
@@ -12,7 +12,23 @@ export type AppStrategy = AuthenticationStrategy<undefined> & {
12
12
  accessToken: string;
13
13
  refreshToken: string;
14
14
  }>;
15
+ /**
16
+ * Return a new instance of the AppStrategy which uses an elevated access token
17
+ */
15
18
  elevated(): Promise<AppStrategy>;
19
+ /**
20
+ * Returns infromation about the active token
21
+ */
22
+ getTokenInfo(): Promise<{
23
+ active: boolean;
24
+ subjectType: 'APP' | 'USER' | 'MEMBER' | 'VISITOR' | 'UNKNOWN';
25
+ subjectId: string;
26
+ exp: number;
27
+ iat: number;
28
+ clientId?: string;
29
+ siteId: string;
30
+ instanceId?: string;
31
+ }>;
16
32
  };
17
33
  /**
18
34
  * Creates an authentication strategy for Wix Apps OAuth installation process.
@@ -22,6 +38,7 @@ export type AppStrategy = AuthenticationStrategy<undefined> & {
22
38
  * @param opts.appSecret The Wix App Secret
23
39
  * @param opts.refreshToken An optional refresh token previously retrieved from Wix OAuth API
24
40
  * @param opts.instanceId An optional instance ID of the Wix App instance the client is making requests on behalf of
41
+ * @param opts.accessToken An optional access token previously retrieved from a client Wix Extension
25
42
  * @param opts.publicKey An optional public key for validating webhook requests (supports both PEM and base64 encoded keys)
26
43
  * @returns An authentication strategy that can be used with WixClient
27
44
  * @example
@@ -56,6 +73,7 @@ export declare function AppStrategy(opts: {
56
73
  appId: string;
57
74
  appSecret?: string;
58
75
  publicKey?: string;
76
+ authServerBaseUrl?: string;
59
77
  } & ({
60
78
  refreshToken?: string;
61
79
  } | {
@@ -7,6 +7,7 @@ import { parsePublicKeyIfEncoded } from '../helpers.js';
7
7
  * @param opts.appSecret The Wix App Secret
8
8
  * @param opts.refreshToken An optional refresh token previously retrieved from Wix OAuth API
9
9
  * @param opts.instanceId An optional instance ID of the Wix App instance the client is making requests on behalf of
10
+ * @param opts.accessToken An optional access token previously retrieved from a client Wix Extension
10
11
  * @param opts.publicKey An optional public key for validating webhook requests (supports both PEM and base64 encoded keys)
11
12
  * @returns An authentication strategy that can be used with WixClient
12
13
  * @example
@@ -39,6 +40,7 @@ import { parsePublicKeyIfEncoded } from '../helpers.js';
39
40
  */
40
41
  // eslint-disable-next-line @typescript-eslint/no-redeclare
41
42
  export function AppStrategy(opts) {
43
+ const authServerBaseUrl = opts.authServerBaseUrl ?? 'https://www.wixapis.com';
42
44
  let refreshToken = 'refreshToken' in opts ? opts.refreshToken : undefined;
43
45
  return {
44
46
  getInstallUrl({ redirectUrl, token, state }) {
@@ -67,7 +69,8 @@ export function AppStrategy(opts) {
67
69
  if (!code || !instanceId) {
68
70
  throw new Error('Invalid OAuth callback URL. Make sure you pass the url including the code and instanceId query params.');
69
71
  }
70
- const tokensRes = await fetch('https://www.wixapis.com/oauth/access', {
72
+ const tokenUrl = new URL('/oauth/access', authServerBaseUrl);
73
+ const tokensRes = await fetch(tokenUrl.href, {
71
74
  method: 'POST',
72
75
  headers: {
73
76
  'Content-Type': 'application/json',
@@ -91,11 +94,12 @@ export function AppStrategy(opts) {
91
94
  };
92
95
  },
93
96
  async getAuthHeaders() {
94
- if (!opts.appSecret) {
95
- throw new Error('App secret is required for retrieveing app-level access tokens. Make sure to pass it to the AppStrategy');
96
- }
97
97
  if ('refreshToken' in opts || refreshToken) {
98
- const tokensRes = await fetch('https://www.wixapis.com/oauth/access', {
98
+ if (!opts.appSecret) {
99
+ throw new Error('App secret is required for retrieveing app-level access tokens. Make sure to pass it to the AppStrategy');
100
+ }
101
+ const tokenUrl = new URL('/oauth/access', authServerBaseUrl);
102
+ const tokensRes = await fetch(tokenUrl.href, {
99
103
  method: 'POST',
100
104
  headers: {
101
105
  'Content-Type': 'application/json',
@@ -119,7 +123,11 @@ export function AppStrategy(opts) {
119
123
  };
120
124
  }
121
125
  else if ('instanceId' in opts) {
122
- const tokensRes = await fetch('https://www.wixapis.com/oauth2/token', {
126
+ if (!opts.appSecret) {
127
+ throw new Error('App secret is required for retrieveing app-level access tokens. Make sure to pass it to the AppStrategy');
128
+ }
129
+ const tokenUrl = new URL('/oauth2/token', authServerBaseUrl);
130
+ const tokensRes = await fetch(tokenUrl.href, {
123
131
  method: 'POST',
124
132
  headers: {
125
133
  'Content-Type': 'application/json',
@@ -154,20 +162,8 @@ export function AppStrategy(opts) {
154
162
  },
155
163
  async elevated() {
156
164
  if ('accessToken' in opts && opts.accessToken) {
157
- const tokenInfoRes = await fetch('https://www.wixapis.com/oauth2/token-info', {
158
- method: 'POST',
159
- headers: {
160
- 'Content-Type': 'application/json',
161
- },
162
- body: JSON.stringify({
163
- token: opts.accessToken,
164
- }),
165
- });
166
- if (tokenInfoRes.status !== 200) {
167
- throw new Error(`Failed to get token info. Unexpected status code from Wix OAuth API: ${tokenInfoRes.status}`);
168
- }
169
- const tokenInfo = await tokenInfoRes.json();
170
- if (tokenInfo.app_id !== opts.appId) {
165
+ const tokenInfo = await getTokenInfo(opts.accessToken, authServerBaseUrl);
166
+ if (tokenInfo.clientId !== opts.appId) {
171
167
  throw new Error(`Invalid access token. The token is not issued for the app with ID "${opts.appId}"`);
172
168
  }
173
169
  if (!tokenInfo.instanceId) {
@@ -178,6 +174,7 @@ export function AppStrategy(opts) {
178
174
  appSecret: opts.appSecret,
179
175
  publicKey: opts.publicKey,
180
176
  instanceId: tokenInfo.instanceId,
177
+ authServerBaseUrl: opts.authServerBaseUrl,
181
178
  });
182
179
  }
183
180
  else {
@@ -203,5 +200,28 @@ export function AppStrategy(opts) {
203
200
  valid: true,
204
201
  };
205
202
  },
203
+ async getTokenInfo() {
204
+ const tokenToCheck = refreshToken ?? ('accessToken' in opts ? opts.accessToken : undefined);
205
+ if (!tokenToCheck) {
206
+ throw new Error('Missing token to get info for. Either pass the token as an argument or provide it when initializing the AppStrategy');
207
+ }
208
+ return getTokenInfo(tokenToCheck, authServerBaseUrl);
209
+ },
206
210
  };
207
211
  }
212
+ async function getTokenInfo(token, authServerBaseUrl) {
213
+ const tokenInfoUrl = new URL('/oauth2/token-info', authServerBaseUrl);
214
+ const tokenInfoRes = await fetch(tokenInfoUrl.href, {
215
+ method: 'POST',
216
+ headers: {
217
+ 'Content-Type': 'application/json',
218
+ },
219
+ body: JSON.stringify({
220
+ token,
221
+ }),
222
+ });
223
+ if (tokenInfoRes.status !== 200) {
224
+ throw new Error(`Failed to get token info. Unexpected status code from Wix OAuth API: ${tokenInfoRes.status}`);
225
+ }
226
+ return (await tokenInfoRes.json());
227
+ }
@@ -1,4 +1,5 @@
1
1
  import { DocumentNode, GraphQLFormattedError } from 'graphql';
2
+ import { RESTFunctionDescriptor } from '@wix/sdk-types';
2
3
  export type TypedQueryInput<Result = {
3
4
  [key: string]: any;
4
5
  }, Variables = {
@@ -15,14 +16,14 @@ export type TypedQueryInput<Result = {
15
16
  */
16
17
  __ensureTypesOfVariablesAndResultMatching?: (variables: Variables) => Result;
17
18
  };
18
- export declare const graphql: ((query: string | String | DocumentNode | TypedQueryInput<unknown, any>, variables?: any, opts?: {
19
+ export declare const graphql: (<Result, Variables>(query: string | String | DocumentNode | TypedQueryInput<Result, Variables>, variables?: Variables | undefined, opts?: {
19
20
  apiVersion: string;
20
21
  }) => Promise<{
21
- data: unknown;
22
+ data: Result;
22
23
  errors?: GraphQLFormattedError[] | undefined;
23
- }>) & ((restModuleOpts: import("@wix/sdk-types").HttpClient) => (query: string | String | DocumentNode | TypedQueryInput<unknown, any>, variables?: any, opts?: {
24
+ }>) & RESTFunctionDescriptor<(<Result, Variables>(query: string | String | DocumentNode | TypedQueryInput<Result, Variables>, variables?: Variables | undefined, opts?: {
24
25
  apiVersion: string;
25
26
  }) => Promise<{
26
- data: unknown;
27
+ data: Result;
27
28
  errors?: GraphQLFormattedError[] | undefined;
28
- }>);
29
+ }>)>;
package/build/graphql.js CHANGED
@@ -1,6 +1,6 @@
1
1
  import { createRESTModule } from '@wix/sdk-runtime/rest-modules';
2
2
  import { FetchErrorResponse } from './fetch-error.js';
3
- export const graphql = createRESTModule((restModuleOpts) => {
3
+ export const graphql = createRESTModule(((restModuleOpts) => {
4
4
  // eslint-disable-next-line @typescript-eslint/no-shadow
5
5
  return async function graphql(query, variables, opts = {
6
6
  apiVersion: 'alpha',
@@ -18,4 +18,4 @@ export const graphql = createRESTModule((restModuleOpts) => {
18
18
  const { data, errors } = await res.json();
19
19
  return { data: data ?? {}, errors };
20
20
  };
21
- });
21
+ }));
@@ -12,7 +12,23 @@ export type AppStrategy = AuthenticationStrategy<undefined> & {
12
12
  accessToken: string;
13
13
  refreshToken: string;
14
14
  }>;
15
+ /**
16
+ * Return a new instance of the AppStrategy which uses an elevated access token
17
+ */
15
18
  elevated(): Promise<AppStrategy>;
19
+ /**
20
+ * Returns infromation about the active token
21
+ */
22
+ getTokenInfo(): Promise<{
23
+ active: boolean;
24
+ subjectType: 'APP' | 'USER' | 'MEMBER' | 'VISITOR' | 'UNKNOWN';
25
+ subjectId: string;
26
+ exp: number;
27
+ iat: number;
28
+ clientId?: string;
29
+ siteId: string;
30
+ instanceId?: string;
31
+ }>;
16
32
  };
17
33
  /**
18
34
  * Creates an authentication strategy for Wix Apps OAuth installation process.
@@ -22,6 +38,7 @@ export type AppStrategy = AuthenticationStrategy<undefined> & {
22
38
  * @param opts.appSecret The Wix App Secret
23
39
  * @param opts.refreshToken An optional refresh token previously retrieved from Wix OAuth API
24
40
  * @param opts.instanceId An optional instance ID of the Wix App instance the client is making requests on behalf of
41
+ * @param opts.accessToken An optional access token previously retrieved from a client Wix Extension
25
42
  * @param opts.publicKey An optional public key for validating webhook requests (supports both PEM and base64 encoded keys)
26
43
  * @returns An authentication strategy that can be used with WixClient
27
44
  * @example
@@ -56,6 +73,7 @@ export declare function AppStrategy(opts: {
56
73
  appId: string;
57
74
  appSecret?: string;
58
75
  publicKey?: string;
76
+ authServerBaseUrl?: string;
59
77
  } & ({
60
78
  refreshToken?: string;
61
79
  } | {
@@ -33,6 +33,7 @@ const helpers_js_1 = require("../helpers.js");
33
33
  * @param opts.appSecret The Wix App Secret
34
34
  * @param opts.refreshToken An optional refresh token previously retrieved from Wix OAuth API
35
35
  * @param opts.instanceId An optional instance ID of the Wix App instance the client is making requests on behalf of
36
+ * @param opts.accessToken An optional access token previously retrieved from a client Wix Extension
36
37
  * @param opts.publicKey An optional public key for validating webhook requests (supports both PEM and base64 encoded keys)
37
38
  * @returns An authentication strategy that can be used with WixClient
38
39
  * @example
@@ -65,6 +66,7 @@ const helpers_js_1 = require("../helpers.js");
65
66
  */
66
67
  // eslint-disable-next-line @typescript-eslint/no-redeclare
67
68
  function AppStrategy(opts) {
69
+ const authServerBaseUrl = opts.authServerBaseUrl ?? 'https://www.wixapis.com';
68
70
  let refreshToken = 'refreshToken' in opts ? opts.refreshToken : undefined;
69
71
  return {
70
72
  getInstallUrl({ redirectUrl, token, state }) {
@@ -93,7 +95,8 @@ function AppStrategy(opts) {
93
95
  if (!code || !instanceId) {
94
96
  throw new Error('Invalid OAuth callback URL. Make sure you pass the url including the code and instanceId query params.');
95
97
  }
96
- const tokensRes = await fetch('https://www.wixapis.com/oauth/access', {
98
+ const tokenUrl = new URL('/oauth/access', authServerBaseUrl);
99
+ const tokensRes = await fetch(tokenUrl.href, {
97
100
  method: 'POST',
98
101
  headers: {
99
102
  'Content-Type': 'application/json',
@@ -117,11 +120,12 @@ function AppStrategy(opts) {
117
120
  };
118
121
  },
119
122
  async getAuthHeaders() {
120
- if (!opts.appSecret) {
121
- throw new Error('App secret is required for retrieveing app-level access tokens. Make sure to pass it to the AppStrategy');
122
- }
123
123
  if ('refreshToken' in opts || refreshToken) {
124
- const tokensRes = await fetch('https://www.wixapis.com/oauth/access', {
124
+ if (!opts.appSecret) {
125
+ throw new Error('App secret is required for retrieveing app-level access tokens. Make sure to pass it to the AppStrategy');
126
+ }
127
+ const tokenUrl = new URL('/oauth/access', authServerBaseUrl);
128
+ const tokensRes = await fetch(tokenUrl.href, {
125
129
  method: 'POST',
126
130
  headers: {
127
131
  'Content-Type': 'application/json',
@@ -145,7 +149,11 @@ function AppStrategy(opts) {
145
149
  };
146
150
  }
147
151
  else if ('instanceId' in opts) {
148
- const tokensRes = await fetch('https://www.wixapis.com/oauth2/token', {
152
+ if (!opts.appSecret) {
153
+ throw new Error('App secret is required for retrieveing app-level access tokens. Make sure to pass it to the AppStrategy');
154
+ }
155
+ const tokenUrl = new URL('/oauth2/token', authServerBaseUrl);
156
+ const tokensRes = await fetch(tokenUrl.href, {
149
157
  method: 'POST',
150
158
  headers: {
151
159
  'Content-Type': 'application/json',
@@ -180,20 +188,8 @@ function AppStrategy(opts) {
180
188
  },
181
189
  async elevated() {
182
190
  if ('accessToken' in opts && opts.accessToken) {
183
- const tokenInfoRes = await fetch('https://www.wixapis.com/oauth2/token-info', {
184
- method: 'POST',
185
- headers: {
186
- 'Content-Type': 'application/json',
187
- },
188
- body: JSON.stringify({
189
- token: opts.accessToken,
190
- }),
191
- });
192
- if (tokenInfoRes.status !== 200) {
193
- throw new Error(`Failed to get token info. Unexpected status code from Wix OAuth API: ${tokenInfoRes.status}`);
194
- }
195
- const tokenInfo = await tokenInfoRes.json();
196
- if (tokenInfo.app_id !== opts.appId) {
191
+ const tokenInfo = await getTokenInfo(opts.accessToken, authServerBaseUrl);
192
+ if (tokenInfo.clientId !== opts.appId) {
197
193
  throw new Error(`Invalid access token. The token is not issued for the app with ID "${opts.appId}"`);
198
194
  }
199
195
  if (!tokenInfo.instanceId) {
@@ -204,6 +200,7 @@ function AppStrategy(opts) {
204
200
  appSecret: opts.appSecret,
205
201
  publicKey: opts.publicKey,
206
202
  instanceId: tokenInfo.instanceId,
203
+ authServerBaseUrl: opts.authServerBaseUrl,
207
204
  });
208
205
  }
209
206
  else {
@@ -229,6 +226,29 @@ function AppStrategy(opts) {
229
226
  valid: true,
230
227
  };
231
228
  },
229
+ async getTokenInfo() {
230
+ const tokenToCheck = refreshToken ?? ('accessToken' in opts ? opts.accessToken : undefined);
231
+ if (!tokenToCheck) {
232
+ throw new Error('Missing token to get info for. Either pass the token as an argument or provide it when initializing the AppStrategy');
233
+ }
234
+ return getTokenInfo(tokenToCheck, authServerBaseUrl);
235
+ },
232
236
  };
233
237
  }
234
238
  exports.AppStrategy = AppStrategy;
239
+ async function getTokenInfo(token, authServerBaseUrl) {
240
+ const tokenInfoUrl = new URL('/oauth2/token-info', authServerBaseUrl);
241
+ const tokenInfoRes = await fetch(tokenInfoUrl.href, {
242
+ method: 'POST',
243
+ headers: {
244
+ 'Content-Type': 'application/json',
245
+ },
246
+ body: JSON.stringify({
247
+ token,
248
+ }),
249
+ });
250
+ if (tokenInfoRes.status !== 200) {
251
+ throw new Error(`Failed to get token info. Unexpected status code from Wix OAuth API: ${tokenInfoRes.status}`);
252
+ }
253
+ return (await tokenInfoRes.json());
254
+ }
@@ -1,4 +1,5 @@
1
1
  import { DocumentNode, GraphQLFormattedError } from 'graphql';
2
+ import { RESTFunctionDescriptor } from '@wix/sdk-types';
2
3
  export type TypedQueryInput<Result = {
3
4
  [key: string]: any;
4
5
  }, Variables = {
@@ -15,14 +16,14 @@ export type TypedQueryInput<Result = {
15
16
  */
16
17
  __ensureTypesOfVariablesAndResultMatching?: (variables: Variables) => Result;
17
18
  };
18
- export declare const graphql: ((query: string | String | DocumentNode | TypedQueryInput<unknown, any>, variables?: any, opts?: {
19
+ export declare const graphql: (<Result, Variables>(query: string | String | DocumentNode | TypedQueryInput<Result, Variables>, variables?: Variables | undefined, opts?: {
19
20
  apiVersion: string;
20
21
  }) => Promise<{
21
- data: unknown;
22
+ data: Result;
22
23
  errors?: GraphQLFormattedError[] | undefined;
23
- }>) & ((restModuleOpts: import("@wix/sdk-types").HttpClient) => (query: string | String | DocumentNode | TypedQueryInput<unknown, any>, variables?: any, opts?: {
24
+ }>) & RESTFunctionDescriptor<(<Result, Variables>(query: string | String | DocumentNode | TypedQueryInput<Result, Variables>, variables?: Variables | undefined, opts?: {
24
25
  apiVersion: string;
25
26
  }) => Promise<{
26
- data: unknown;
27
+ data: Result;
27
28
  errors?: GraphQLFormattedError[] | undefined;
28
- }>);
29
+ }>)>;
@@ -3,7 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.graphql = void 0;
4
4
  const rest_modules_1 = require("@wix/sdk-runtime/rest-modules");
5
5
  const fetch_error_js_1 = require("./fetch-error.js");
6
- exports.graphql = (0, rest_modules_1.createRESTModule)((restModuleOpts) => {
6
+ exports.graphql = (0, rest_modules_1.createRESTModule)(((restModuleOpts) => {
7
7
  // eslint-disable-next-line @typescript-eslint/no-shadow
8
8
  return async function graphql(query, variables, opts = {
9
9
  apiVersion: 'alpha',
@@ -21,4 +21,4 @@ exports.graphql = (0, rest_modules_1.createRESTModule)((restModuleOpts) => {
21
21
  const { data, errors } = await res.json();
22
22
  return { data: data ?? {}, errors };
23
23
  };
24
- });
24
+ }));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@wix/sdk",
3
- "version": "1.12.6",
3
+ "version": "1.12.8",
4
4
  "license": "UNLICENSED",
5
5
  "author": {
6
6
  "name": "Ronny Ringel",
@@ -65,10 +65,10 @@
65
65
  "dependencies": {
66
66
  "@babel/runtime": "^7.23.2",
67
67
  "@wix/identity": "^1.0.78",
68
- "@wix/image-kit": "^1.73.0",
68
+ "@wix/image-kit": "^1.74.0",
69
69
  "@wix/redirects": "^1.0.41",
70
70
  "@wix/sdk-context": "^0.0.1",
71
- "@wix/sdk-runtime": "0.3.11",
71
+ "@wix/sdk-runtime": "0.3.12",
72
72
  "@wix/sdk-types": "^1.9.2",
73
73
  "crypto-js": "^4.2.0",
74
74
  "jose": "^5.2.1",
@@ -88,7 +88,7 @@
88
88
  "@wix/events": "^1.0.179",
89
89
  "@wix/metro": "^1.0.73",
90
90
  "@wix/metro-runtime": "^1.1677.0",
91
- "@wix/sdk-runtime": "0.3.11",
91
+ "@wix/sdk-runtime": "0.3.12",
92
92
  "eslint": "^8.56.0",
93
93
  "eslint-config-sdk": "0.0.0",
94
94
  "graphql": "^16.8.0",
@@ -122,5 +122,5 @@
122
122
  "wallaby": {
123
123
  "autoDetect": true
124
124
  },
125
- "falconPackageHash": "434acd803ec3fa432e257e25fb88fe03020ccc7a9f729faeebd656b4"
125
+ "falconPackageHash": "4a3ca02dc12093515b122a61d87d631718a0d7de19fe8ad5a6ef4ab4"
126
126
  }