@wix/sdk 1.7.5 → 1.7.6
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/build/auth/WixAppOAuthStrategy.d.ts +14 -2
- package/build/auth/WixAppOAuthStrategy.js +92 -26
- package/build/index.d.ts +1 -0
- package/build/index.js +1 -0
- package/build/wixClient.d.ts +3 -1
- package/build/wixClient.js +23 -0
- package/cjs/build/auth/WixAppOAuthStrategy.d.ts +14 -2
- package/cjs/build/auth/WixAppOAuthStrategy.js +92 -26
- package/cjs/build/index.d.ts +1 -0
- package/cjs/build/index.js +1 -0
- package/cjs/build/wixClient.d.ts +3 -1
- package/cjs/build/wixClient.js +23 -0
- package/package.json +6 -6
|
@@ -1,5 +1,11 @@
|
|
|
1
1
|
import { AuthenticationStrategy } from '@wix/sdk-types';
|
|
2
|
-
export type WixAppOAuthStrategy = AuthenticationStrategy
|
|
2
|
+
export type WixAppOAuthStrategy = AuthenticationStrategy<undefined, (opts: {
|
|
3
|
+
instanceId: string;
|
|
4
|
+
} | {
|
|
5
|
+
refreshToken: string;
|
|
6
|
+
} | {
|
|
7
|
+
fromRequest: Request;
|
|
8
|
+
}) => WixAppOAuthStrategy> & {
|
|
3
9
|
getInstallUrl(opts: {
|
|
4
10
|
redirectUrl: string;
|
|
5
11
|
state?: string;
|
|
@@ -20,6 +26,7 @@ export type WixAppOAuthStrategy = AuthenticationStrategy & {
|
|
|
20
26
|
* @param opts.appId The Wix App ID
|
|
21
27
|
* @param opts.appSecret The Wix App Secret
|
|
22
28
|
* @param opts.refreshToken An optional refresh token previously retrieved from Wix OAuth API
|
|
29
|
+
* @param opts.instanceId An optional instance ID of the Wix App instance the client is making requests on behalf of
|
|
23
30
|
* @param opts.publicKey An optional public key for validating webhook requests (supports both PEM and base64 encoded keys)
|
|
24
31
|
* @returns An authentication strategy that can be used with WixClient
|
|
25
32
|
* @example
|
|
@@ -54,5 +61,10 @@ export declare function WixAppOAuthStrategy(opts: {
|
|
|
54
61
|
appId: string;
|
|
55
62
|
appSecret?: string;
|
|
56
63
|
publicKey?: string;
|
|
64
|
+
} & ({
|
|
57
65
|
refreshToken?: string;
|
|
58
|
-
}
|
|
66
|
+
} | {
|
|
67
|
+
instanceId?: string;
|
|
68
|
+
} | {
|
|
69
|
+
accessToken?: string;
|
|
70
|
+
})): WixAppOAuthStrategy;
|
|
@@ -7,6 +7,7 @@ import { parsePublicKeyIfEncoded } from '../helpers.js';
|
|
|
7
7
|
* @param opts.appId The Wix App ID
|
|
8
8
|
* @param opts.appSecret The Wix App Secret
|
|
9
9
|
* @param opts.refreshToken An optional refresh token previously retrieved from Wix OAuth API
|
|
10
|
+
* @param opts.instanceId An optional instance ID of the Wix App instance the client is making requests on behalf of
|
|
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,7 +40,7 @@ import { parsePublicKeyIfEncoded } from '../helpers.js';
|
|
|
39
40
|
*/
|
|
40
41
|
// eslint-disable-next-line @typescript-eslint/no-redeclare
|
|
41
42
|
export function WixAppOAuthStrategy(opts) {
|
|
42
|
-
let refreshToken = opts.refreshToken;
|
|
43
|
+
let refreshToken = 'refreshToken' in opts ? opts.refreshToken : undefined;
|
|
43
44
|
return {
|
|
44
45
|
getInstallUrl({ redirectUrl, token, state }) {
|
|
45
46
|
const params = new URLSearchParams();
|
|
@@ -53,6 +54,39 @@ export function WixAppOAuthStrategy(opts) {
|
|
|
53
54
|
}
|
|
54
55
|
return `https://www.wix.com/installer/install?${params.toString()}`;
|
|
55
56
|
},
|
|
57
|
+
withAuth(params) {
|
|
58
|
+
if ('instanceId' in params) {
|
|
59
|
+
return WixAppOAuthStrategy({
|
|
60
|
+
appId: opts.appId,
|
|
61
|
+
appSecret: opts.appSecret,
|
|
62
|
+
publicKey: opts.publicKey,
|
|
63
|
+
instanceId: params.instanceId,
|
|
64
|
+
});
|
|
65
|
+
}
|
|
66
|
+
else if ('refreshToken' in params) {
|
|
67
|
+
return WixAppOAuthStrategy({
|
|
68
|
+
appId: opts.appId,
|
|
69
|
+
appSecret: opts.appSecret,
|
|
70
|
+
publicKey: opts.publicKey,
|
|
71
|
+
refreshToken: params.refreshToken,
|
|
72
|
+
});
|
|
73
|
+
}
|
|
74
|
+
else if ('fromRequest' in params) {
|
|
75
|
+
const authFromRequest = params.fromRequest.headers.get('Authorization');
|
|
76
|
+
if (!authFromRequest) {
|
|
77
|
+
throw new Error('Missing Authorization header in the request');
|
|
78
|
+
}
|
|
79
|
+
return WixAppOAuthStrategy({
|
|
80
|
+
appId: opts.appId,
|
|
81
|
+
appSecret: opts.appSecret,
|
|
82
|
+
publicKey: opts.publicKey,
|
|
83
|
+
accessToken: authFromRequest,
|
|
84
|
+
});
|
|
85
|
+
}
|
|
86
|
+
else {
|
|
87
|
+
throw new Error('Invalid parameters for withAuth');
|
|
88
|
+
}
|
|
89
|
+
},
|
|
56
90
|
async handleOAuthCallback(url, oauthOpts) {
|
|
57
91
|
if (!opts.appSecret) {
|
|
58
92
|
throw new Error('App secret is required for handling OAuth callback. Make sure to pass it to the WixAppOAuthStrategy');
|
|
@@ -91,34 +125,66 @@ export function WixAppOAuthStrategy(opts) {
|
|
|
91
125
|
};
|
|
92
126
|
},
|
|
93
127
|
async getAuthHeaders() {
|
|
94
|
-
if (!refreshToken) {
|
|
95
|
-
throw new Error('Missing refresh token. Either pass it to the WixAppOAuthStrategy or use the handleOAuthCallback method to retrieve it.');
|
|
96
|
-
}
|
|
97
128
|
if (!opts.appSecret) {
|
|
98
|
-
throw new Error('App secret is required for
|
|
129
|
+
throw new Error('App secret is required for retrieveing app-level access tokens. Make sure to pass it to the WixAppOAuthStrategy');
|
|
99
130
|
}
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
131
|
+
if ('refreshToken' in opts || refreshToken) {
|
|
132
|
+
const tokensRes = await fetch('https://www.wixapis.com/oauth/access', {
|
|
133
|
+
method: 'POST',
|
|
134
|
+
headers: {
|
|
135
|
+
'Content-Type': 'application/json',
|
|
136
|
+
},
|
|
137
|
+
body: JSON.stringify({
|
|
138
|
+
refresh_token: refreshToken,
|
|
139
|
+
client_id: opts.appId,
|
|
140
|
+
client_secret: opts.appSecret,
|
|
141
|
+
grant_type: 'refresh_token',
|
|
142
|
+
}),
|
|
143
|
+
});
|
|
144
|
+
if (tokensRes.status !== 200) {
|
|
145
|
+
throw new Error(`Failed to exchange refresh token for access token. Unexpected status code from Wix OAuth API: ${tokensRes.status}`);
|
|
146
|
+
}
|
|
147
|
+
const tokens = (await tokensRes.json());
|
|
148
|
+
refreshToken = tokens.refresh_token;
|
|
149
|
+
return {
|
|
150
|
+
headers: {
|
|
151
|
+
Authorization: tokens.access_token,
|
|
152
|
+
},
|
|
153
|
+
};
|
|
154
|
+
}
|
|
155
|
+
else if ('instanceId' in opts) {
|
|
156
|
+
const tokensRes = await fetch('https://www.wixapis.com/oauth2/token', {
|
|
157
|
+
method: 'POST',
|
|
158
|
+
headers: {
|
|
159
|
+
'Content-Type': 'application/json',
|
|
160
|
+
},
|
|
161
|
+
body: JSON.stringify({
|
|
162
|
+
instance_id: opts.instanceId,
|
|
163
|
+
client_id: opts.appId,
|
|
164
|
+
client_secret: opts.appSecret,
|
|
165
|
+
grant_type: 'client_credentials',
|
|
166
|
+
}),
|
|
167
|
+
});
|
|
168
|
+
if (tokensRes.status !== 200) {
|
|
169
|
+
throw new Error(`Failed to exchange instance ID for access token. Unexpected status code from Wix OAuth API: ${tokensRes.status}`);
|
|
170
|
+
}
|
|
171
|
+
const tokens = (await tokensRes.json());
|
|
172
|
+
return {
|
|
173
|
+
headers: {
|
|
174
|
+
Authorization: tokens.access_token,
|
|
175
|
+
},
|
|
176
|
+
};
|
|
177
|
+
}
|
|
178
|
+
else if ('accessToken' in opts && opts.accessToken) {
|
|
179
|
+
return {
|
|
180
|
+
headers: {
|
|
181
|
+
Authorization: opts.accessToken,
|
|
182
|
+
},
|
|
183
|
+
};
|
|
184
|
+
}
|
|
185
|
+
else {
|
|
186
|
+
throw new Error('Missing refresh token or instance ID. Either one is needed to get app level access tokens. Make sure to pass one of them to the WixAppOAuthStrategy');
|
|
114
187
|
}
|
|
115
|
-
const tokens = (await tokensRes.json());
|
|
116
|
-
refreshToken = tokens.refresh_token;
|
|
117
|
-
return {
|
|
118
|
-
headers: {
|
|
119
|
-
Authorization: tokens.access_token,
|
|
120
|
-
},
|
|
121
|
-
};
|
|
122
188
|
},
|
|
123
189
|
async decodeJWT(token, verifyCallerClaims = false) {
|
|
124
190
|
if (!opts.publicKey) {
|
package/build/index.d.ts
CHANGED
|
@@ -3,6 +3,7 @@ export * from './wixMedia.js';
|
|
|
3
3
|
export * from './auth/oauth2/OAuthStrategy.js';
|
|
4
4
|
export * from './auth/oauth2/types.js';
|
|
5
5
|
export * from './auth/ApiKeyAuthStrategy.js';
|
|
6
|
+
export * from './auth/WixAppOAuthStrategy.js';
|
|
6
7
|
export * from '@wix/sdk-types';
|
|
7
8
|
export { getDefaultDomain } from './rest-modules.js';
|
|
8
9
|
export { API_URL } from './common.js';
|
package/build/index.js
CHANGED
|
@@ -3,6 +3,7 @@ export * from './wixMedia.js';
|
|
|
3
3
|
export * from './auth/oauth2/OAuthStrategy.js';
|
|
4
4
|
export * from './auth/oauth2/types.js';
|
|
5
5
|
export * from './auth/ApiKeyAuthStrategy.js';
|
|
6
|
+
export * from './auth/WixAppOAuthStrategy.js';
|
|
6
7
|
export * from '@wix/sdk-types';
|
|
7
8
|
export { getDefaultDomain } from './rest-modules.js';
|
|
8
9
|
export { API_URL } from './common.js';
|
package/build/wixClient.d.ts
CHANGED
|
@@ -54,7 +54,9 @@ type TypedQueryInput<Result = {
|
|
|
54
54
|
export type WixClient<H extends Host<any> | undefined = undefined, Z extends AuthenticationStrategy<H> = AuthenticationStrategy<H>, T extends Descriptors = Descriptors> = {
|
|
55
55
|
setHeaders(headers: Headers): void;
|
|
56
56
|
auth: Omit<Z, 'getAuthHeaders'> & BoundAuthenticationStrategy;
|
|
57
|
+
withAuth: Z['withAuth'] extends undefined ? never : (...args: Parameters<NonNullable<Z['withAuth']>>) => WixClient<H, Z, T>;
|
|
57
58
|
fetch(relativeUrl: string, options: RequestInit): Promise<Response>;
|
|
59
|
+
fetchWithAuth: typeof fetch;
|
|
58
60
|
use<R extends Descriptors = EmptyObject>(modules: H extends Host<any> ? AssertHostMatches<R, H> : R): BuildDescriptors<R, H>;
|
|
59
61
|
graphql<Result, Variables>(query: string | ((string | String) & TypedQueryInput<Result, Variables>), variables?: Variables): Promise<{
|
|
60
62
|
data: Result;
|
|
@@ -95,7 +97,7 @@ export type ProcessedEvent<T extends EventDefinition<any>[] = []> = {
|
|
|
95
97
|
eventType: string;
|
|
96
98
|
payload: unknown;
|
|
97
99
|
} : ResolvePossibleEvents<T>);
|
|
98
|
-
export declare function createClient<H extends Host<any> | undefined = undefined, Z extends AuthenticationStrategy<H> = AuthenticationStrategy<H>, T extends Descriptors = EmptyObject>(config: {
|
|
100
|
+
export declare function createClient<H extends Host<any> | undefined = undefined, Z extends AuthenticationStrategy<H, any> = AuthenticationStrategy<H>, T extends Descriptors = EmptyObject>(config: {
|
|
99
101
|
modules?: H extends Host<any> ? AssertHostMatches<T, H> : T;
|
|
100
102
|
auth?: Z;
|
|
101
103
|
headers?: Headers;
|
package/build/wixClient.js
CHANGED
|
@@ -62,6 +62,12 @@ export function createClient(config) {
|
|
|
62
62
|
return {
|
|
63
63
|
...wrappedModules,
|
|
64
64
|
auth: authStrategy,
|
|
65
|
+
withAuth: (authStrategy.withAuth
|
|
66
|
+
? (...args) => createClient({
|
|
67
|
+
...config,
|
|
68
|
+
auth: authStrategy.withAuth(...args),
|
|
69
|
+
})
|
|
70
|
+
: undefined),
|
|
65
71
|
setHeaders,
|
|
66
72
|
use,
|
|
67
73
|
fetch: (relativeUrl, options) => {
|
|
@@ -70,6 +76,23 @@ export function createClient(config) {
|
|
|
70
76
|
finalUrl.protocol = 'https';
|
|
71
77
|
return boundFetch(finalUrl.toString(), options);
|
|
72
78
|
},
|
|
79
|
+
fetchWithAuth: async (urlOrRequest, requestInit) => {
|
|
80
|
+
if (typeof urlOrRequest === 'string' || urlOrRequest instanceof URL) {
|
|
81
|
+
return fetch(urlOrRequest, {
|
|
82
|
+
...requestInit,
|
|
83
|
+
headers: {
|
|
84
|
+
...requestInit?.headers,
|
|
85
|
+
...(await boundGetAuthHeaders()).headers,
|
|
86
|
+
},
|
|
87
|
+
});
|
|
88
|
+
}
|
|
89
|
+
else {
|
|
90
|
+
for (const [k, v] of Object.entries((await boundGetAuthHeaders()).headers)) {
|
|
91
|
+
urlOrRequest.headers.set(k, v);
|
|
92
|
+
}
|
|
93
|
+
return fetch(urlOrRequest, requestInit);
|
|
94
|
+
}
|
|
95
|
+
},
|
|
73
96
|
async graphql(query, variables, opts = {
|
|
74
97
|
apiVersion: 'alpha',
|
|
75
98
|
}) {
|
|
@@ -1,5 +1,11 @@
|
|
|
1
1
|
import { AuthenticationStrategy } from '@wix/sdk-types';
|
|
2
|
-
export type WixAppOAuthStrategy = AuthenticationStrategy
|
|
2
|
+
export type WixAppOAuthStrategy = AuthenticationStrategy<undefined, (opts: {
|
|
3
|
+
instanceId: string;
|
|
4
|
+
} | {
|
|
5
|
+
refreshToken: string;
|
|
6
|
+
} | {
|
|
7
|
+
fromRequest: Request;
|
|
8
|
+
}) => WixAppOAuthStrategy> & {
|
|
3
9
|
getInstallUrl(opts: {
|
|
4
10
|
redirectUrl: string;
|
|
5
11
|
state?: string;
|
|
@@ -20,6 +26,7 @@ export type WixAppOAuthStrategy = AuthenticationStrategy & {
|
|
|
20
26
|
* @param opts.appId The Wix App ID
|
|
21
27
|
* @param opts.appSecret The Wix App Secret
|
|
22
28
|
* @param opts.refreshToken An optional refresh token previously retrieved from Wix OAuth API
|
|
29
|
+
* @param opts.instanceId An optional instance ID of the Wix App instance the client is making requests on behalf of
|
|
23
30
|
* @param opts.publicKey An optional public key for validating webhook requests (supports both PEM and base64 encoded keys)
|
|
24
31
|
* @returns An authentication strategy that can be used with WixClient
|
|
25
32
|
* @example
|
|
@@ -54,5 +61,10 @@ export declare function WixAppOAuthStrategy(opts: {
|
|
|
54
61
|
appId: string;
|
|
55
62
|
appSecret?: string;
|
|
56
63
|
publicKey?: string;
|
|
64
|
+
} & ({
|
|
57
65
|
refreshToken?: string;
|
|
58
|
-
}
|
|
66
|
+
} | {
|
|
67
|
+
instanceId?: string;
|
|
68
|
+
} | {
|
|
69
|
+
accessToken?: string;
|
|
70
|
+
})): WixAppOAuthStrategy;
|
|
@@ -10,6 +10,7 @@ const helpers_js_1 = require("../helpers.js");
|
|
|
10
10
|
* @param opts.appId The Wix App ID
|
|
11
11
|
* @param opts.appSecret The Wix App Secret
|
|
12
12
|
* @param opts.refreshToken An optional refresh token previously retrieved from Wix OAuth API
|
|
13
|
+
* @param opts.instanceId An optional instance ID of the Wix App instance the client is making requests on behalf of
|
|
13
14
|
* @param opts.publicKey An optional public key for validating webhook requests (supports both PEM and base64 encoded keys)
|
|
14
15
|
* @returns An authentication strategy that can be used with WixClient
|
|
15
16
|
* @example
|
|
@@ -42,7 +43,7 @@ const helpers_js_1 = require("../helpers.js");
|
|
|
42
43
|
*/
|
|
43
44
|
// eslint-disable-next-line @typescript-eslint/no-redeclare
|
|
44
45
|
function WixAppOAuthStrategy(opts) {
|
|
45
|
-
let refreshToken = opts.refreshToken;
|
|
46
|
+
let refreshToken = 'refreshToken' in opts ? opts.refreshToken : undefined;
|
|
46
47
|
return {
|
|
47
48
|
getInstallUrl({ redirectUrl, token, state }) {
|
|
48
49
|
const params = new URLSearchParams();
|
|
@@ -56,6 +57,39 @@ function WixAppOAuthStrategy(opts) {
|
|
|
56
57
|
}
|
|
57
58
|
return `https://www.wix.com/installer/install?${params.toString()}`;
|
|
58
59
|
},
|
|
60
|
+
withAuth(params) {
|
|
61
|
+
if ('instanceId' in params) {
|
|
62
|
+
return WixAppOAuthStrategy({
|
|
63
|
+
appId: opts.appId,
|
|
64
|
+
appSecret: opts.appSecret,
|
|
65
|
+
publicKey: opts.publicKey,
|
|
66
|
+
instanceId: params.instanceId,
|
|
67
|
+
});
|
|
68
|
+
}
|
|
69
|
+
else if ('refreshToken' in params) {
|
|
70
|
+
return WixAppOAuthStrategy({
|
|
71
|
+
appId: opts.appId,
|
|
72
|
+
appSecret: opts.appSecret,
|
|
73
|
+
publicKey: opts.publicKey,
|
|
74
|
+
refreshToken: params.refreshToken,
|
|
75
|
+
});
|
|
76
|
+
}
|
|
77
|
+
else if ('fromRequest' in params) {
|
|
78
|
+
const authFromRequest = params.fromRequest.headers.get('Authorization');
|
|
79
|
+
if (!authFromRequest) {
|
|
80
|
+
throw new Error('Missing Authorization header in the request');
|
|
81
|
+
}
|
|
82
|
+
return WixAppOAuthStrategy({
|
|
83
|
+
appId: opts.appId,
|
|
84
|
+
appSecret: opts.appSecret,
|
|
85
|
+
publicKey: opts.publicKey,
|
|
86
|
+
accessToken: authFromRequest,
|
|
87
|
+
});
|
|
88
|
+
}
|
|
89
|
+
else {
|
|
90
|
+
throw new Error('Invalid parameters for withAuth');
|
|
91
|
+
}
|
|
92
|
+
},
|
|
59
93
|
async handleOAuthCallback(url, oauthOpts) {
|
|
60
94
|
if (!opts.appSecret) {
|
|
61
95
|
throw new Error('App secret is required for handling OAuth callback. Make sure to pass it to the WixAppOAuthStrategy');
|
|
@@ -94,34 +128,66 @@ function WixAppOAuthStrategy(opts) {
|
|
|
94
128
|
};
|
|
95
129
|
},
|
|
96
130
|
async getAuthHeaders() {
|
|
97
|
-
if (!refreshToken) {
|
|
98
|
-
throw new Error('Missing refresh token. Either pass it to the WixAppOAuthStrategy or use the handleOAuthCallback method to retrieve it.');
|
|
99
|
-
}
|
|
100
131
|
if (!opts.appSecret) {
|
|
101
|
-
throw new Error('App secret is required for
|
|
132
|
+
throw new Error('App secret is required for retrieveing app-level access tokens. Make sure to pass it to the WixAppOAuthStrategy');
|
|
102
133
|
}
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
134
|
+
if ('refreshToken' in opts || refreshToken) {
|
|
135
|
+
const tokensRes = await fetch('https://www.wixapis.com/oauth/access', {
|
|
136
|
+
method: 'POST',
|
|
137
|
+
headers: {
|
|
138
|
+
'Content-Type': 'application/json',
|
|
139
|
+
},
|
|
140
|
+
body: JSON.stringify({
|
|
141
|
+
refresh_token: refreshToken,
|
|
142
|
+
client_id: opts.appId,
|
|
143
|
+
client_secret: opts.appSecret,
|
|
144
|
+
grant_type: 'refresh_token',
|
|
145
|
+
}),
|
|
146
|
+
});
|
|
147
|
+
if (tokensRes.status !== 200) {
|
|
148
|
+
throw new Error(`Failed to exchange refresh token for access token. Unexpected status code from Wix OAuth API: ${tokensRes.status}`);
|
|
149
|
+
}
|
|
150
|
+
const tokens = (await tokensRes.json());
|
|
151
|
+
refreshToken = tokens.refresh_token;
|
|
152
|
+
return {
|
|
153
|
+
headers: {
|
|
154
|
+
Authorization: tokens.access_token,
|
|
155
|
+
},
|
|
156
|
+
};
|
|
157
|
+
}
|
|
158
|
+
else if ('instanceId' in opts) {
|
|
159
|
+
const tokensRes = await fetch('https://www.wixapis.com/oauth2/token', {
|
|
160
|
+
method: 'POST',
|
|
161
|
+
headers: {
|
|
162
|
+
'Content-Type': 'application/json',
|
|
163
|
+
},
|
|
164
|
+
body: JSON.stringify({
|
|
165
|
+
instance_id: opts.instanceId,
|
|
166
|
+
client_id: opts.appId,
|
|
167
|
+
client_secret: opts.appSecret,
|
|
168
|
+
grant_type: 'client_credentials',
|
|
169
|
+
}),
|
|
170
|
+
});
|
|
171
|
+
if (tokensRes.status !== 200) {
|
|
172
|
+
throw new Error(`Failed to exchange instance ID for access token. Unexpected status code from Wix OAuth API: ${tokensRes.status}`);
|
|
173
|
+
}
|
|
174
|
+
const tokens = (await tokensRes.json());
|
|
175
|
+
return {
|
|
176
|
+
headers: {
|
|
177
|
+
Authorization: tokens.access_token,
|
|
178
|
+
},
|
|
179
|
+
};
|
|
180
|
+
}
|
|
181
|
+
else if ('accessToken' in opts && opts.accessToken) {
|
|
182
|
+
return {
|
|
183
|
+
headers: {
|
|
184
|
+
Authorization: opts.accessToken,
|
|
185
|
+
},
|
|
186
|
+
};
|
|
187
|
+
}
|
|
188
|
+
else {
|
|
189
|
+
throw new Error('Missing refresh token or instance ID. Either one is needed to get app level access tokens. Make sure to pass one of them to the WixAppOAuthStrategy');
|
|
117
190
|
}
|
|
118
|
-
const tokens = (await tokensRes.json());
|
|
119
|
-
refreshToken = tokens.refresh_token;
|
|
120
|
-
return {
|
|
121
|
-
headers: {
|
|
122
|
-
Authorization: tokens.access_token,
|
|
123
|
-
},
|
|
124
|
-
};
|
|
125
191
|
},
|
|
126
192
|
async decodeJWT(token, verifyCallerClaims = false) {
|
|
127
193
|
if (!opts.publicKey) {
|
package/cjs/build/index.d.ts
CHANGED
|
@@ -3,6 +3,7 @@ export * from './wixMedia.js';
|
|
|
3
3
|
export * from './auth/oauth2/OAuthStrategy.js';
|
|
4
4
|
export * from './auth/oauth2/types.js';
|
|
5
5
|
export * from './auth/ApiKeyAuthStrategy.js';
|
|
6
|
+
export * from './auth/WixAppOAuthStrategy.js';
|
|
6
7
|
export * from '@wix/sdk-types';
|
|
7
8
|
export { getDefaultDomain } from './rest-modules.js';
|
|
8
9
|
export { API_URL } from './common.js';
|
package/cjs/build/index.js
CHANGED
|
@@ -20,6 +20,7 @@ __exportStar(require("./wixMedia.js"), exports);
|
|
|
20
20
|
__exportStar(require("./auth/oauth2/OAuthStrategy.js"), exports);
|
|
21
21
|
__exportStar(require("./auth/oauth2/types.js"), exports);
|
|
22
22
|
__exportStar(require("./auth/ApiKeyAuthStrategy.js"), exports);
|
|
23
|
+
__exportStar(require("./auth/WixAppOAuthStrategy.js"), exports);
|
|
23
24
|
__exportStar(require("@wix/sdk-types"), exports);
|
|
24
25
|
var rest_modules_js_1 = require("./rest-modules.js");
|
|
25
26
|
Object.defineProperty(exports, "getDefaultDomain", { enumerable: true, get: function () { return rest_modules_js_1.getDefaultDomain; } });
|
package/cjs/build/wixClient.d.ts
CHANGED
|
@@ -54,7 +54,9 @@ type TypedQueryInput<Result = {
|
|
|
54
54
|
export type WixClient<H extends Host<any> | undefined = undefined, Z extends AuthenticationStrategy<H> = AuthenticationStrategy<H>, T extends Descriptors = Descriptors> = {
|
|
55
55
|
setHeaders(headers: Headers): void;
|
|
56
56
|
auth: Omit<Z, 'getAuthHeaders'> & BoundAuthenticationStrategy;
|
|
57
|
+
withAuth: Z['withAuth'] extends undefined ? never : (...args: Parameters<NonNullable<Z['withAuth']>>) => WixClient<H, Z, T>;
|
|
57
58
|
fetch(relativeUrl: string, options: RequestInit): Promise<Response>;
|
|
59
|
+
fetchWithAuth: typeof fetch;
|
|
58
60
|
use<R extends Descriptors = EmptyObject>(modules: H extends Host<any> ? AssertHostMatches<R, H> : R): BuildDescriptors<R, H>;
|
|
59
61
|
graphql<Result, Variables>(query: string | ((string | String) & TypedQueryInput<Result, Variables>), variables?: Variables): Promise<{
|
|
60
62
|
data: Result;
|
|
@@ -95,7 +97,7 @@ export type ProcessedEvent<T extends EventDefinition<any>[] = []> = {
|
|
|
95
97
|
eventType: string;
|
|
96
98
|
payload: unknown;
|
|
97
99
|
} : ResolvePossibleEvents<T>);
|
|
98
|
-
export declare function createClient<H extends Host<any> | undefined = undefined, Z extends AuthenticationStrategy<H> = AuthenticationStrategy<H>, T extends Descriptors = EmptyObject>(config: {
|
|
100
|
+
export declare function createClient<H extends Host<any> | undefined = undefined, Z extends AuthenticationStrategy<H, any> = AuthenticationStrategy<H>, T extends Descriptors = EmptyObject>(config: {
|
|
99
101
|
modules?: H extends Host<any> ? AssertHostMatches<T, H> : T;
|
|
100
102
|
auth?: Z;
|
|
101
103
|
headers?: Headers;
|
package/cjs/build/wixClient.js
CHANGED
|
@@ -65,6 +65,12 @@ function createClient(config) {
|
|
|
65
65
|
return {
|
|
66
66
|
...wrappedModules,
|
|
67
67
|
auth: authStrategy,
|
|
68
|
+
withAuth: (authStrategy.withAuth
|
|
69
|
+
? (...args) => createClient({
|
|
70
|
+
...config,
|
|
71
|
+
auth: authStrategy.withAuth(...args),
|
|
72
|
+
})
|
|
73
|
+
: undefined),
|
|
68
74
|
setHeaders,
|
|
69
75
|
use,
|
|
70
76
|
fetch: (relativeUrl, options) => {
|
|
@@ -73,6 +79,23 @@ function createClient(config) {
|
|
|
73
79
|
finalUrl.protocol = 'https';
|
|
74
80
|
return boundFetch(finalUrl.toString(), options);
|
|
75
81
|
},
|
|
82
|
+
fetchWithAuth: async (urlOrRequest, requestInit) => {
|
|
83
|
+
if (typeof urlOrRequest === 'string' || urlOrRequest instanceof URL) {
|
|
84
|
+
return fetch(urlOrRequest, {
|
|
85
|
+
...requestInit,
|
|
86
|
+
headers: {
|
|
87
|
+
...requestInit?.headers,
|
|
88
|
+
...(await boundGetAuthHeaders()).headers,
|
|
89
|
+
},
|
|
90
|
+
});
|
|
91
|
+
}
|
|
92
|
+
else {
|
|
93
|
+
for (const [k, v] of Object.entries((await boundGetAuthHeaders()).headers)) {
|
|
94
|
+
urlOrRequest.headers.set(k, v);
|
|
95
|
+
}
|
|
96
|
+
return fetch(urlOrRequest, requestInit);
|
|
97
|
+
}
|
|
98
|
+
},
|
|
76
99
|
async graphql(query, variables, opts = {
|
|
77
100
|
apiVersion: 'alpha',
|
|
78
101
|
}) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@wix/sdk",
|
|
3
|
-
"version": "1.7.
|
|
3
|
+
"version": "1.7.6",
|
|
4
4
|
"license": "UNLICENSED",
|
|
5
5
|
"author": {
|
|
6
6
|
"name": "Ronny Ringel",
|
|
@@ -60,9 +60,9 @@
|
|
|
60
60
|
"dependencies": {
|
|
61
61
|
"@babel/runtime": "^7.23.2",
|
|
62
62
|
"@wix/identity": "^1.0.73",
|
|
63
|
-
"@wix/image-kit": "^1.
|
|
63
|
+
"@wix/image-kit": "^1.56.0",
|
|
64
64
|
"@wix/redirects": "^1.0.32",
|
|
65
|
-
"@wix/sdk-types": "^1.5.
|
|
65
|
+
"@wix/sdk-types": "^1.5.9",
|
|
66
66
|
"crypto-js": "^4.2.0",
|
|
67
67
|
"jose": "^5.2.1",
|
|
68
68
|
"pkce-challenge": "^3.1.0",
|
|
@@ -80,8 +80,8 @@
|
|
|
80
80
|
"@wix/ecom": "^1.0.477",
|
|
81
81
|
"@wix/events": "^1.0.145",
|
|
82
82
|
"@wix/metro": "^1.0.73",
|
|
83
|
-
"@wix/metro-runtime": "^1.
|
|
84
|
-
"@wix/sdk-runtime": "0.2.
|
|
83
|
+
"@wix/metro-runtime": "^1.1635.0",
|
|
84
|
+
"@wix/sdk-runtime": "0.2.8",
|
|
85
85
|
"eslint": "^8.56.0",
|
|
86
86
|
"eslint-config-sdk": "0.0.0",
|
|
87
87
|
"graphql": "^16.8.0",
|
|
@@ -115,5 +115,5 @@
|
|
|
115
115
|
"wallaby": {
|
|
116
116
|
"autoDetect": true
|
|
117
117
|
},
|
|
118
|
-
"falconPackageHash": "
|
|
118
|
+
"falconPackageHash": "676b313e3902721adc8337cd1542cd05dbe775e43ddee5030eef4706"
|
|
119
119
|
}
|