@wix/sdk 1.1.21 → 1.2.5
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/browser/index.mjs +783 -0
- package/build/index.d.mts +244 -0
- package/build/index.d.ts +244 -0
- package/build/index.js +818 -0
- package/build/index.mjs +774 -0
- package/package.json +38 -24
- package/dist/cjs/__tests__/fixtures/constants.js +0 -9
- package/dist/cjs/__tests__/fixtures/constants.js.map +0 -1
- package/dist/cjs/auth/OAuthStrategy.js +0 -90
- package/dist/cjs/auth/OAuthStrategy.js.map +0 -1
- package/dist/cjs/auth/strategy.js +0 -2
- package/dist/cjs/auth/strategy.js.map +0 -1
- package/dist/cjs/external-types.d.js +0 -2
- package/dist/cjs/external-types.d.js.map +0 -1
- package/dist/cjs/index.js +0 -22
- package/dist/cjs/index.js.map +0 -1
- package/dist/cjs/test-types.d.js +0 -2
- package/dist/cjs/test-types.d.js.map +0 -1
- package/dist/cjs/wixClient.js +0 -111
- package/dist/cjs/wixClient.js.map +0 -1
- package/dist/cjs/wixMedia.js +0 -69
- package/dist/cjs/wixMedia.js.map +0 -1
- package/dist/esm/__tests__/fixtures/constants.js +0 -3
- package/dist/esm/__tests__/fixtures/constants.js.map +0 -1
- package/dist/esm/auth/OAuthStrategy.js +0 -86
- package/dist/esm/auth/OAuthStrategy.js.map +0 -1
- package/dist/esm/auth/strategy.js +0 -2
- package/dist/esm/auth/strategy.js.map +0 -1
- package/dist/esm/external-types.d.js +0 -2
- package/dist/esm/external-types.d.js.map +0 -1
- package/dist/esm/index.js +0 -4
- package/dist/esm/index.js.map +0 -1
- package/dist/esm/test-types.d.js +0 -2
- package/dist/esm/test-types.d.js.map +0 -1
- package/dist/esm/wixClient.js +0 -108
- package/dist/esm/wixClient.js.map +0 -1
- package/dist/esm/wixMedia.js +0 -63
- package/dist/esm/wixMedia.js.map +0 -1
- package/dist/tsconfig.tsbuildinfo +0 -1
- package/dist/types/__tests__/fixtures/constants.d.ts +0 -3
- package/dist/types/__tests__/fixtures/constants.d.ts.map +0 -1
- package/dist/types/auth/OAuthStrategy.d.ts +0 -18
- package/dist/types/auth/OAuthStrategy.d.ts.map +0 -1
- package/dist/types/auth/strategy.d.ts +0 -6
- package/dist/types/auth/strategy.d.ts.map +0 -1
- package/dist/types/index.d.ts +0 -4
- package/dist/types/index.d.ts.map +0 -1
- package/dist/types/wixClient.d.ts +0 -18
- package/dist/types/wixClient.d.ts.map +0 -1
- package/dist/types/wixMedia.d.ts +0 -26
- package/dist/types/wixMedia.d.ts.map +0 -1
package/build/index.js
ADDED
|
@@ -0,0 +1,818 @@
|
|
|
1
|
+
var __create = Object.create;
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
6
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
7
|
+
var __export = (target, all) => {
|
|
8
|
+
for (var name in all)
|
|
9
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
10
|
+
};
|
|
11
|
+
var __copyProps = (to, from, except, desc) => {
|
|
12
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
13
|
+
for (let key of __getOwnPropNames(from))
|
|
14
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
15
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
16
|
+
}
|
|
17
|
+
return to;
|
|
18
|
+
};
|
|
19
|
+
var __reExport = (target, mod, secondTarget) => (__copyProps(target, mod, "default"), secondTarget && __copyProps(secondTarget, mod, "default"));
|
|
20
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
+
mod
|
|
27
|
+
));
|
|
28
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
|
+
|
|
30
|
+
// src/index.ts
|
|
31
|
+
var src_exports = {};
|
|
32
|
+
__export(src_exports, {
|
|
33
|
+
ApiKeyStrategy: () => ApiKeyStrategy,
|
|
34
|
+
LoginState: () => LoginState,
|
|
35
|
+
OAuthStrategy: () => OAuthStrategy,
|
|
36
|
+
TokenRole: () => TokenRole,
|
|
37
|
+
createClient: () => createClient,
|
|
38
|
+
decodeText: () => decodeText,
|
|
39
|
+
media: () => media
|
|
40
|
+
});
|
|
41
|
+
module.exports = __toCommonJS(src_exports);
|
|
42
|
+
|
|
43
|
+
// src/common.ts
|
|
44
|
+
var PUBLIC_METADATA_KEY = "__metadata";
|
|
45
|
+
var API_URL = "www.wixapis.com";
|
|
46
|
+
|
|
47
|
+
// src/helpers.ts
|
|
48
|
+
var getDefaultContentHeader = (options) => {
|
|
49
|
+
if (options?.method && ["post", "put", "patch"].includes(options.method.toLocaleLowerCase()) && options.body) {
|
|
50
|
+
return { "Content-Type": "application/json" };
|
|
51
|
+
}
|
|
52
|
+
return {};
|
|
53
|
+
};
|
|
54
|
+
var isObject = (val) => val && typeof val === "object" && !Array.isArray(val);
|
|
55
|
+
|
|
56
|
+
// src/bi/biHeaderGenerator.ts
|
|
57
|
+
var WixBIHeaderName = "x-wix-bi-gateway";
|
|
58
|
+
function biHeaderGenerator(apiMetadata, publicMetadata) {
|
|
59
|
+
return {
|
|
60
|
+
[WixBIHeaderName]: objectToKeyValue({
|
|
61
|
+
environment: "js-sdk",
|
|
62
|
+
"package-name": apiMetadata.packageName ?? publicMetadata?.PACKAGE_NAME,
|
|
63
|
+
"method-fqn": apiMetadata.methodFqn,
|
|
64
|
+
entity: apiMetadata.entityFqdn
|
|
65
|
+
})
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
function objectToKeyValue(input) {
|
|
69
|
+
return Object.entries(input).filter(([_, value]) => Boolean(value)).map(([key, value]) => `${key}=${value}`).join(",");
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
// src/rest-modules.ts
|
|
73
|
+
function buildRESTDescriptor(origFunc, publicMetadata, boundFetch) {
|
|
74
|
+
return origFunc({
|
|
75
|
+
request: async (factory) => {
|
|
76
|
+
const requestOptions = factory({ host: API_URL });
|
|
77
|
+
let url = `https://${API_URL}${requestOptions.url}`;
|
|
78
|
+
if (requestOptions.params && requestOptions.params.toString()) {
|
|
79
|
+
url += `?${requestOptions.params.toString()}`;
|
|
80
|
+
}
|
|
81
|
+
try {
|
|
82
|
+
const biHeader = biHeaderGenerator(requestOptions, publicMetadata);
|
|
83
|
+
const res = await boundFetch(url, {
|
|
84
|
+
method: requestOptions.method,
|
|
85
|
+
...requestOptions.data && {
|
|
86
|
+
body: JSON.stringify(requestOptions.data)
|
|
87
|
+
},
|
|
88
|
+
headers: {
|
|
89
|
+
...biHeader
|
|
90
|
+
}
|
|
91
|
+
});
|
|
92
|
+
if (res.status !== 200) {
|
|
93
|
+
let dataError = null;
|
|
94
|
+
try {
|
|
95
|
+
dataError = await res.json();
|
|
96
|
+
} catch (e) {
|
|
97
|
+
}
|
|
98
|
+
throw errorBuilder(
|
|
99
|
+
res.status,
|
|
100
|
+
dataError?.message,
|
|
101
|
+
dataError?.details,
|
|
102
|
+
{
|
|
103
|
+
requestId: res.headers.get("X-Wix-Request-Id"),
|
|
104
|
+
details: dataError
|
|
105
|
+
}
|
|
106
|
+
);
|
|
107
|
+
}
|
|
108
|
+
const data = await res.json();
|
|
109
|
+
return {
|
|
110
|
+
data,
|
|
111
|
+
headers: res.headers,
|
|
112
|
+
status: res.status,
|
|
113
|
+
statusText: res.statusText
|
|
114
|
+
};
|
|
115
|
+
} catch (e) {
|
|
116
|
+
if (e.message?.includes("fetch is not defined")) {
|
|
117
|
+
console.error("Node.js v18+ is required");
|
|
118
|
+
}
|
|
119
|
+
throw e;
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
});
|
|
123
|
+
}
|
|
124
|
+
var errorBuilder = (code, description, details, data) => {
|
|
125
|
+
return {
|
|
126
|
+
response: {
|
|
127
|
+
data: {
|
|
128
|
+
details: {
|
|
129
|
+
...!details?.validationError && {
|
|
130
|
+
applicationError: {
|
|
131
|
+
description,
|
|
132
|
+
code,
|
|
133
|
+
data
|
|
134
|
+
}
|
|
135
|
+
},
|
|
136
|
+
...details
|
|
137
|
+
},
|
|
138
|
+
message: description
|
|
139
|
+
},
|
|
140
|
+
status: code
|
|
141
|
+
}
|
|
142
|
+
};
|
|
143
|
+
};
|
|
144
|
+
|
|
145
|
+
// src/host-modules.ts
|
|
146
|
+
var isHostModule = (val) => isObject(val) && val.__type === "host";
|
|
147
|
+
function buildHostModule(val, host) {
|
|
148
|
+
return val.create(host);
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
// src/wixClient.ts
|
|
152
|
+
function createClient(config) {
|
|
153
|
+
const _headers = config.headers || { Authorization: "" };
|
|
154
|
+
const authStrategy = config.auth || {
|
|
155
|
+
getAuthHeaders: () => Promise.resolve({ headers: {} })
|
|
156
|
+
};
|
|
157
|
+
const boundFetch = async (url, options) => {
|
|
158
|
+
const authHeaders = await authStrategy.getAuthHeaders(config.host);
|
|
159
|
+
const defaultContentTypeHeader = getDefaultContentHeader(options);
|
|
160
|
+
return fetch(url, {
|
|
161
|
+
...options,
|
|
162
|
+
headers: {
|
|
163
|
+
...defaultContentTypeHeader,
|
|
164
|
+
..._headers,
|
|
165
|
+
...authHeaders?.headers,
|
|
166
|
+
...options?.headers
|
|
167
|
+
}
|
|
168
|
+
});
|
|
169
|
+
};
|
|
170
|
+
const use = (modules, metadata) => {
|
|
171
|
+
if (isHostModule(modules)) {
|
|
172
|
+
return buildHostModule(modules, config.host);
|
|
173
|
+
} else if (typeof modules === "function") {
|
|
174
|
+
return buildRESTDescriptor(
|
|
175
|
+
modules,
|
|
176
|
+
metadata ?? {},
|
|
177
|
+
boundFetch
|
|
178
|
+
);
|
|
179
|
+
} else if (isObject(modules)) {
|
|
180
|
+
return Object.fromEntries(
|
|
181
|
+
Object.entries(
|
|
182
|
+
modules
|
|
183
|
+
).map(([key, value]) => {
|
|
184
|
+
return [key, use(value, modules[PUBLIC_METADATA_KEY])];
|
|
185
|
+
})
|
|
186
|
+
);
|
|
187
|
+
} else {
|
|
188
|
+
return modules;
|
|
189
|
+
}
|
|
190
|
+
};
|
|
191
|
+
const setHeaders = (headers) => {
|
|
192
|
+
for (const k in headers) {
|
|
193
|
+
_headers[k] = headers[k];
|
|
194
|
+
}
|
|
195
|
+
};
|
|
196
|
+
const wrappedModules = config.modules ? use(config.modules) : {};
|
|
197
|
+
return {
|
|
198
|
+
...wrappedModules,
|
|
199
|
+
auth: authStrategy,
|
|
200
|
+
setHeaders,
|
|
201
|
+
use,
|
|
202
|
+
fetch: (relativeUrl, options) => {
|
|
203
|
+
const finalUrl = new URL(relativeUrl, `https://${API_URL}`);
|
|
204
|
+
finalUrl.host = API_URL;
|
|
205
|
+
finalUrl.protocol = "https";
|
|
206
|
+
return boundFetch(finalUrl, options);
|
|
207
|
+
}
|
|
208
|
+
};
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
// src/wixMedia.ts
|
|
212
|
+
var import_image_kit = require("@wix/image-kit");
|
|
213
|
+
var import_querystring = require("querystring");
|
|
214
|
+
var URL_HASH_PREFIX = "#";
|
|
215
|
+
var WIX_PROTOCOL = "wix:";
|
|
216
|
+
var WIX_IMAGE = "image";
|
|
217
|
+
var WIX_IMAGE_URL = "https://static.wixstatic.com/media/";
|
|
218
|
+
function getScaledToFillImageUrl(wixMediaIdentifier, targetWidth, targetHeight, options) {
|
|
219
|
+
const img = getImageUrl(wixMediaIdentifier);
|
|
220
|
+
return import_image_kit.sdk.getScaleToFillImageURL(
|
|
221
|
+
img.id,
|
|
222
|
+
img.height,
|
|
223
|
+
img.width,
|
|
224
|
+
targetWidth,
|
|
225
|
+
targetHeight,
|
|
226
|
+
options
|
|
227
|
+
);
|
|
228
|
+
}
|
|
229
|
+
function getScaledToFitImageUrl(wixMediaIdentifier, targetWidth, targetHeight, options) {
|
|
230
|
+
const img = getImageUrl(wixMediaIdentifier);
|
|
231
|
+
return import_image_kit.sdk.getScaleToFitImageURL(
|
|
232
|
+
img.id,
|
|
233
|
+
img.height,
|
|
234
|
+
img.width,
|
|
235
|
+
targetWidth,
|
|
236
|
+
targetHeight,
|
|
237
|
+
options
|
|
238
|
+
);
|
|
239
|
+
}
|
|
240
|
+
function getCroppedImageUrl(wixMediaIdentifier, cropX, cropY, cropWidth, cropHeight, targetWidth, targetHeight, options) {
|
|
241
|
+
const img = getImageUrl(wixMediaIdentifier);
|
|
242
|
+
return import_image_kit.sdk.getCropImageURL(
|
|
243
|
+
img.id,
|
|
244
|
+
img.height,
|
|
245
|
+
img.width,
|
|
246
|
+
cropX,
|
|
247
|
+
cropY,
|
|
248
|
+
cropWidth,
|
|
249
|
+
cropHeight,
|
|
250
|
+
targetWidth,
|
|
251
|
+
targetHeight,
|
|
252
|
+
options
|
|
253
|
+
);
|
|
254
|
+
}
|
|
255
|
+
function getImageUrl(val) {
|
|
256
|
+
let id, filenameOrAltText;
|
|
257
|
+
let height, width;
|
|
258
|
+
if (val.startsWith(WIX_IMAGE_URL)) {
|
|
259
|
+
id = val.split(WIX_IMAGE_URL).pop().split("/")[0];
|
|
260
|
+
width = val.split("/w_").pop().split(",")[0];
|
|
261
|
+
height = val.split(",h_").pop().split(",")[0];
|
|
262
|
+
} else {
|
|
263
|
+
const alignedImage = alignIfLegacy(val, WIX_IMAGE);
|
|
264
|
+
const { hash, pathname } = new URL(alignedImage);
|
|
265
|
+
({ originHeight: height, originWidth: width } = (0, import_querystring.parse)(
|
|
266
|
+
hash.replace(URL_HASH_PREFIX, "")
|
|
267
|
+
));
|
|
268
|
+
[id, filenameOrAltText] = pathname.replace(`${WIX_IMAGE}://v1/`, "").split("/");
|
|
269
|
+
}
|
|
270
|
+
const decodedFilenameOrAltText = decodeText(filenameOrAltText);
|
|
271
|
+
const res = {
|
|
272
|
+
id,
|
|
273
|
+
url: `${WIX_IMAGE_URL}${id}`,
|
|
274
|
+
height: Number(height),
|
|
275
|
+
width: Number(width)
|
|
276
|
+
};
|
|
277
|
+
if (!decodedFilenameOrAltText) {
|
|
278
|
+
return res;
|
|
279
|
+
}
|
|
280
|
+
return {
|
|
281
|
+
...res,
|
|
282
|
+
altText: decodedFilenameOrAltText,
|
|
283
|
+
filename: decodedFilenameOrAltText
|
|
284
|
+
};
|
|
285
|
+
}
|
|
286
|
+
function decodeText(s) {
|
|
287
|
+
if (!s) {
|
|
288
|
+
return s;
|
|
289
|
+
}
|
|
290
|
+
return decodeURIComponent(s);
|
|
291
|
+
}
|
|
292
|
+
function alignIfLegacy(url, type) {
|
|
293
|
+
const { protocol } = new URL(url);
|
|
294
|
+
return protocol === `${type}:` ? `${WIX_PROTOCOL}${url}` : url;
|
|
295
|
+
}
|
|
296
|
+
var media = {
|
|
297
|
+
getCroppedImageUrl,
|
|
298
|
+
getScaledToFillImageUrl,
|
|
299
|
+
getScaledToFitImageUrl,
|
|
300
|
+
getImageUrl
|
|
301
|
+
};
|
|
302
|
+
|
|
303
|
+
// src/auth/oauth2/OAuthStrategy.ts
|
|
304
|
+
var import_redirects = require("@wix/redirects");
|
|
305
|
+
|
|
306
|
+
// src/tokenHelpers.ts
|
|
307
|
+
function getCurrentDate() {
|
|
308
|
+
return Math.floor(Date.now() / 1e3);
|
|
309
|
+
}
|
|
310
|
+
function isTokenExpired(token) {
|
|
311
|
+
const currentDate = getCurrentDate();
|
|
312
|
+
return token.expiresAt < currentDate;
|
|
313
|
+
}
|
|
314
|
+
function createAccessToken(accessToken, expiresIn) {
|
|
315
|
+
const now = getCurrentDate();
|
|
316
|
+
return { value: accessToken, expiresAt: Number(expiresIn) + now };
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
// src/auth/oauth2/OAuthStrategy.ts
|
|
320
|
+
var import_pkce_challenge = __toESM(require("pkce-challenge"));
|
|
321
|
+
var import_identity = require("@wix/identity");
|
|
322
|
+
|
|
323
|
+
// src/auth/oauth2/types.ts
|
|
324
|
+
var LoginState = /* @__PURE__ */ ((LoginState2) => {
|
|
325
|
+
LoginState2["SUCCESS"] = "SUCCESS";
|
|
326
|
+
LoginState2["INITIAL"] = "INITIAL";
|
|
327
|
+
LoginState2["FAILURE"] = "FAILURE";
|
|
328
|
+
LoginState2["EMAIL_VERIFICATION_REQUIRED"] = "EMAIL_VERIFICATION_REQUIRED";
|
|
329
|
+
LoginState2["OWNER_APPROVAL_REQUIRED"] = "OWNER_APPROVAL_REQUIRED";
|
|
330
|
+
LoginState2["USER_CAPTCHA_REQUIRED"] = "USER_CAPTCHA_REQUIRED";
|
|
331
|
+
LoginState2["SILENT_CAPTCHA_REQUIRED"] = "SILENT_CAPTCHA_REQUIRED";
|
|
332
|
+
return LoginState2;
|
|
333
|
+
})(LoginState || {});
|
|
334
|
+
var TokenRole = /* @__PURE__ */ ((TokenRole2) => {
|
|
335
|
+
TokenRole2["NONE"] = "none";
|
|
336
|
+
TokenRole2["VISITOR"] = "visitor";
|
|
337
|
+
TokenRole2["MEMBER"] = "member";
|
|
338
|
+
return TokenRole2;
|
|
339
|
+
})(TokenRole || {});
|
|
340
|
+
|
|
341
|
+
// src/iframeUtils.ts
|
|
342
|
+
function addListener(eventTarget, name, fn) {
|
|
343
|
+
if (eventTarget.addEventListener) {
|
|
344
|
+
eventTarget.addEventListener(name, fn);
|
|
345
|
+
} else {
|
|
346
|
+
eventTarget.attachEvent("on" + name, fn);
|
|
347
|
+
}
|
|
348
|
+
}
|
|
349
|
+
function removeListener(eventTarget, name, fn) {
|
|
350
|
+
if (eventTarget.removeEventListener) {
|
|
351
|
+
eventTarget.removeEventListener(name, fn);
|
|
352
|
+
} else {
|
|
353
|
+
eventTarget.detachEvent("on" + name, fn);
|
|
354
|
+
}
|
|
355
|
+
}
|
|
356
|
+
function loadFrame(src) {
|
|
357
|
+
const iframe = document.createElement("iframe");
|
|
358
|
+
iframe.style.display = "none";
|
|
359
|
+
iframe.src = src;
|
|
360
|
+
return document.body.appendChild(iframe);
|
|
361
|
+
}
|
|
362
|
+
function addPostMessageListener(state) {
|
|
363
|
+
let responseHandler;
|
|
364
|
+
let timeoutId;
|
|
365
|
+
const msgReceivedOrTimeout = new Promise((resolve, reject) => {
|
|
366
|
+
responseHandler = (e) => {
|
|
367
|
+
if (!e.data || e.data.state !== state) {
|
|
368
|
+
return;
|
|
369
|
+
}
|
|
370
|
+
resolve(e.data);
|
|
371
|
+
};
|
|
372
|
+
addListener(window, "message", responseHandler);
|
|
373
|
+
timeoutId = setTimeout(() => {
|
|
374
|
+
reject(new Error("OAuth flow timed out"));
|
|
375
|
+
}, 12e4);
|
|
376
|
+
});
|
|
377
|
+
return msgReceivedOrTimeout.finally(() => {
|
|
378
|
+
clearTimeout(timeoutId);
|
|
379
|
+
removeListener(window, "message", responseHandler);
|
|
380
|
+
});
|
|
381
|
+
}
|
|
382
|
+
|
|
383
|
+
// src/auth/oauth2/constants.ts
|
|
384
|
+
var MISSING_CAPTCHA = "-19971";
|
|
385
|
+
var INVALID_CAPTCHA = "-19970";
|
|
386
|
+
var EMAIL_EXISTS = "-19995";
|
|
387
|
+
var INVALID_PASSWORD = "-19976";
|
|
388
|
+
var RESET_PASSWORD = "-19973";
|
|
389
|
+
|
|
390
|
+
// src/auth/oauth2/OAuthStrategy.ts
|
|
391
|
+
var moduleWithTokens = { redirects: import_redirects.redirects, authentication: import_identity.authentication, recovery: import_identity.recovery, verification: import_identity.verification };
|
|
392
|
+
var WIX_RECAPTCHA_ID = "6LdoPaUfAAAAAJphvHoUoOob7mx0KDlXyXlgrx5v";
|
|
393
|
+
function OAuthStrategy(config) {
|
|
394
|
+
const _tokens = config.tokens || {
|
|
395
|
+
accessToken: { value: "", expiresAt: 0 },
|
|
396
|
+
refreshToken: { value: "", role: "none" /* NONE */ }
|
|
397
|
+
};
|
|
398
|
+
const setTokens = (tokens) => {
|
|
399
|
+
_tokens.accessToken = tokens.accessToken;
|
|
400
|
+
_tokens.refreshToken = tokens.refreshToken;
|
|
401
|
+
};
|
|
402
|
+
let _state = {
|
|
403
|
+
stateKind: "initial",
|
|
404
|
+
loginState: "INITIAL" /* INITIAL */
|
|
405
|
+
};
|
|
406
|
+
const getAuthHeaders = async () => {
|
|
407
|
+
if (!_tokens.accessToken?.value || isTokenExpired(_tokens.accessToken)) {
|
|
408
|
+
const tokens = await generateVisitorTokens({
|
|
409
|
+
refreshToken: _tokens.refreshToken
|
|
410
|
+
});
|
|
411
|
+
setTokens(tokens);
|
|
412
|
+
}
|
|
413
|
+
return Promise.resolve({
|
|
414
|
+
headers: { Authorization: _tokens.accessToken.value }
|
|
415
|
+
});
|
|
416
|
+
};
|
|
417
|
+
const wixClientWithTokens = createClient({
|
|
418
|
+
modules: moduleWithTokens,
|
|
419
|
+
auth: { getAuthHeaders }
|
|
420
|
+
});
|
|
421
|
+
const generateVisitorTokens = async (tokens) => {
|
|
422
|
+
if (tokens?.accessToken?.value && tokens?.refreshToken?.value && !isTokenExpired(tokens.accessToken)) {
|
|
423
|
+
return tokens;
|
|
424
|
+
}
|
|
425
|
+
if (tokens?.refreshToken?.value) {
|
|
426
|
+
try {
|
|
427
|
+
const newTokens = await renewToken(tokens.refreshToken);
|
|
428
|
+
return newTokens;
|
|
429
|
+
} catch (e) {
|
|
430
|
+
}
|
|
431
|
+
}
|
|
432
|
+
const tokensResponse = await fetchTokens({
|
|
433
|
+
clientId: config.clientId,
|
|
434
|
+
grantType: "anonymous"
|
|
435
|
+
});
|
|
436
|
+
return {
|
|
437
|
+
accessToken: createAccessToken(
|
|
438
|
+
tokensResponse.access_token,
|
|
439
|
+
tokensResponse.expires_in
|
|
440
|
+
),
|
|
441
|
+
refreshToken: {
|
|
442
|
+
value: tokensResponse.refresh_token,
|
|
443
|
+
role: "visitor" /* VISITOR */
|
|
444
|
+
}
|
|
445
|
+
};
|
|
446
|
+
};
|
|
447
|
+
const renewToken = async (refreshToken) => {
|
|
448
|
+
const tokensResponse = await fetchTokens({
|
|
449
|
+
refreshToken: refreshToken.value,
|
|
450
|
+
grantType: "refresh_token"
|
|
451
|
+
});
|
|
452
|
+
const accessToken = createAccessToken(
|
|
453
|
+
tokensResponse.access_token,
|
|
454
|
+
tokensResponse.expires_in
|
|
455
|
+
);
|
|
456
|
+
return {
|
|
457
|
+
accessToken,
|
|
458
|
+
refreshToken
|
|
459
|
+
};
|
|
460
|
+
};
|
|
461
|
+
const generatePKCE = () => {
|
|
462
|
+
const pkceState = (0, import_pkce_challenge.default)();
|
|
463
|
+
return {
|
|
464
|
+
codeChallenge: pkceState.code_challenge,
|
|
465
|
+
codeVerifier: pkceState.code_verifier,
|
|
466
|
+
state: (0, import_pkce_challenge.default)().code_challenge
|
|
467
|
+
};
|
|
468
|
+
};
|
|
469
|
+
const generateOAuthData = (redirectUri, originalUri) => {
|
|
470
|
+
const state = { redirectUri };
|
|
471
|
+
const pkceState = generatePKCE();
|
|
472
|
+
return {
|
|
473
|
+
...state,
|
|
474
|
+
originalUri: originalUri ?? "",
|
|
475
|
+
codeChallenge: pkceState.codeChallenge,
|
|
476
|
+
codeVerifier: pkceState.codeVerifier,
|
|
477
|
+
state: (0, import_pkce_challenge.default)().code_challenge
|
|
478
|
+
};
|
|
479
|
+
};
|
|
480
|
+
const getAuthorizationUrlWithOptions = async (oauthData, responseMode, prompt, sessionToken) => {
|
|
481
|
+
const { redirectSession } = await wixClientWithTokens.redirects.createRedirectSession({
|
|
482
|
+
auth: {
|
|
483
|
+
authRequest: {
|
|
484
|
+
redirectUri: oauthData.redirectUri,
|
|
485
|
+
...oauthData.redirectUri && {
|
|
486
|
+
redirectUri: oauthData.redirectUri
|
|
487
|
+
},
|
|
488
|
+
clientId: config.clientId,
|
|
489
|
+
codeChallenge: oauthData.codeChallenge,
|
|
490
|
+
codeChallengeMethod: "S256",
|
|
491
|
+
responseMode,
|
|
492
|
+
responseType: "code",
|
|
493
|
+
scope: "offline_access",
|
|
494
|
+
state: oauthData.state,
|
|
495
|
+
...sessionToken && { sessionToken }
|
|
496
|
+
},
|
|
497
|
+
prompt: import_redirects.redirects.Prompt[prompt]
|
|
498
|
+
}
|
|
499
|
+
});
|
|
500
|
+
return { authUrl: redirectSession.fullUrl };
|
|
501
|
+
};
|
|
502
|
+
const getAuthUrl = async (oauthData, opts = {
|
|
503
|
+
prompt: "login"
|
|
504
|
+
}) => {
|
|
505
|
+
return getAuthorizationUrlWithOptions(
|
|
506
|
+
oauthData,
|
|
507
|
+
"fragment",
|
|
508
|
+
opts.prompt ?? "login"
|
|
509
|
+
);
|
|
510
|
+
};
|
|
511
|
+
const parseFromUrl = () => {
|
|
512
|
+
const params = new URLSearchParams(window.location.hash.substring(1));
|
|
513
|
+
const code = params.get("code");
|
|
514
|
+
const state = params.get("state");
|
|
515
|
+
const error = params.get("error");
|
|
516
|
+
const errorDescription = params.get("error_description");
|
|
517
|
+
return { code, state, ...error && { error, errorDescription } };
|
|
518
|
+
};
|
|
519
|
+
const getMemberTokens = async (code, state, oauthData) => {
|
|
520
|
+
if (!code || !state) {
|
|
521
|
+
throw new Error("Missing code or _state");
|
|
522
|
+
} else if (state !== oauthData.state) {
|
|
523
|
+
throw new Error("Invalid _state");
|
|
524
|
+
}
|
|
525
|
+
try {
|
|
526
|
+
const tokensResponse = await fetchTokens({
|
|
527
|
+
clientId: config.clientId,
|
|
528
|
+
grantType: "authorization_code",
|
|
529
|
+
...oauthData.redirectUri && { redirectUri: oauthData.redirectUri },
|
|
530
|
+
code,
|
|
531
|
+
codeVerifier: oauthData.codeVerifier
|
|
532
|
+
});
|
|
533
|
+
return {
|
|
534
|
+
accessToken: createAccessToken(
|
|
535
|
+
tokensResponse.access_token,
|
|
536
|
+
tokensResponse.expires_in
|
|
537
|
+
),
|
|
538
|
+
refreshToken: {
|
|
539
|
+
value: tokensResponse.refresh_token,
|
|
540
|
+
role: "member" /* MEMBER */
|
|
541
|
+
}
|
|
542
|
+
};
|
|
543
|
+
} catch (e) {
|
|
544
|
+
throw new Error("Failed to get member tokens");
|
|
545
|
+
}
|
|
546
|
+
};
|
|
547
|
+
const logout = async (originalUrl) => {
|
|
548
|
+
const { redirectSession } = await wixClientWithTokens.redirects.createRedirectSession({
|
|
549
|
+
logout: { clientId: config.clientId },
|
|
550
|
+
callbacks: {
|
|
551
|
+
postFlowUrl: originalUrl
|
|
552
|
+
}
|
|
553
|
+
});
|
|
554
|
+
_tokens.accessToken = { value: "", expiresAt: 0 };
|
|
555
|
+
_tokens.refreshToken = { value: "", role: "none" /* NONE */ };
|
|
556
|
+
return { logoutUrl: redirectSession.fullUrl };
|
|
557
|
+
};
|
|
558
|
+
const handleState = (response) => {
|
|
559
|
+
if (response.state === import_identity.authentication.StateType.SUCCESS) {
|
|
560
|
+
return {
|
|
561
|
+
loginState: "SUCCESS" /* SUCCESS */,
|
|
562
|
+
stateKind: "success",
|
|
563
|
+
data: { sessionToken: response.sessionToken }
|
|
564
|
+
};
|
|
565
|
+
} else if (response.state === import_identity.authentication.StateType.REQUIRE_OWNER_APPROVAL) {
|
|
566
|
+
return {
|
|
567
|
+
loginState: "OWNER_APPROVAL_REQUIRED" /* OWNER_APPROVAL_REQUIRED */,
|
|
568
|
+
stateKind: "ownerApprovalRequired"
|
|
569
|
+
};
|
|
570
|
+
} else if (response.state === import_identity.authentication.StateType.REQUIRE_EMAIL_VERIFICATION) {
|
|
571
|
+
_state = {
|
|
572
|
+
loginState: "EMAIL_VERIFICATION_REQUIRED" /* EMAIL_VERIFICATION_REQUIRED */,
|
|
573
|
+
stateKind: "emailVerificationRequired",
|
|
574
|
+
data: { stateToken: response.stateToken }
|
|
575
|
+
};
|
|
576
|
+
return _state;
|
|
577
|
+
}
|
|
578
|
+
return {
|
|
579
|
+
stateKind: "failure",
|
|
580
|
+
loginState: "FAILURE" /* FAILURE */,
|
|
581
|
+
error: "Unknown _state"
|
|
582
|
+
};
|
|
583
|
+
};
|
|
584
|
+
const register = async (params) => {
|
|
585
|
+
try {
|
|
586
|
+
const res = await wixClientWithTokens.authentication.registerV2(
|
|
587
|
+
{
|
|
588
|
+
email: params.email
|
|
589
|
+
},
|
|
590
|
+
{
|
|
591
|
+
password: params.password,
|
|
592
|
+
profile: params.profile,
|
|
593
|
+
...params.captchaTokens && {
|
|
594
|
+
captchaTokens: [
|
|
595
|
+
{
|
|
596
|
+
Recaptcha: params.captchaTokens?.recaptchaToken,
|
|
597
|
+
InvisibleRecaptcha: params.captchaTokens?.invisibleRecaptchaToken
|
|
598
|
+
}
|
|
599
|
+
]
|
|
600
|
+
}
|
|
601
|
+
}
|
|
602
|
+
);
|
|
603
|
+
return handleState(res);
|
|
604
|
+
} catch (e) {
|
|
605
|
+
const emailValidation = e.details.validationError?.fieldViolations?.find(
|
|
606
|
+
(v) => v.data.type === "EMAIL"
|
|
607
|
+
);
|
|
608
|
+
if (emailValidation) {
|
|
609
|
+
return {
|
|
610
|
+
stateKind: "failure",
|
|
611
|
+
loginState: "FAILURE" /* FAILURE */,
|
|
612
|
+
error: emailValidation.description,
|
|
613
|
+
errorCode: "invalidEmail"
|
|
614
|
+
};
|
|
615
|
+
}
|
|
616
|
+
if (e.details.applicationError?.code === MISSING_CAPTCHA) {
|
|
617
|
+
return {
|
|
618
|
+
stateKind: "failure",
|
|
619
|
+
loginState: "FAILURE" /* FAILURE */,
|
|
620
|
+
error: e.message,
|
|
621
|
+
errorCode: "missingCaptchaToken"
|
|
622
|
+
};
|
|
623
|
+
}
|
|
624
|
+
if (e.details.applicationError?.code === EMAIL_EXISTS) {
|
|
625
|
+
return {
|
|
626
|
+
stateKind: "failure",
|
|
627
|
+
loginState: "FAILURE" /* FAILURE */,
|
|
628
|
+
error: e.message,
|
|
629
|
+
errorCode: "emailAlreadyExists"
|
|
630
|
+
};
|
|
631
|
+
}
|
|
632
|
+
if (e.details.applicationError?.code === INVALID_CAPTCHA) {
|
|
633
|
+
return {
|
|
634
|
+
stateKind: "failure",
|
|
635
|
+
loginState: "FAILURE" /* FAILURE */,
|
|
636
|
+
error: e.message,
|
|
637
|
+
errorCode: "invalidCaptchaToken"
|
|
638
|
+
};
|
|
639
|
+
}
|
|
640
|
+
return {
|
|
641
|
+
stateKind: "failure",
|
|
642
|
+
loginState: "FAILURE" /* FAILURE */,
|
|
643
|
+
error: e.message
|
|
644
|
+
};
|
|
645
|
+
}
|
|
646
|
+
};
|
|
647
|
+
const login = async (params) => {
|
|
648
|
+
try {
|
|
649
|
+
const res = await wixClientWithTokens.authentication.loginV2(
|
|
650
|
+
{
|
|
651
|
+
email: params.email
|
|
652
|
+
},
|
|
653
|
+
{
|
|
654
|
+
password: params.password,
|
|
655
|
+
...params.captchaTokens && {
|
|
656
|
+
captchaTokens: [
|
|
657
|
+
{
|
|
658
|
+
Recaptcha: params.captchaTokens?.recaptchaToken,
|
|
659
|
+
InvisibleRecaptcha: params.captchaTokens?.invisibleRecaptchaToken
|
|
660
|
+
}
|
|
661
|
+
]
|
|
662
|
+
}
|
|
663
|
+
}
|
|
664
|
+
);
|
|
665
|
+
return handleState(res);
|
|
666
|
+
} catch (e) {
|
|
667
|
+
return {
|
|
668
|
+
stateKind: "failure",
|
|
669
|
+
loginState: "FAILURE" /* FAILURE */,
|
|
670
|
+
error: e.message,
|
|
671
|
+
errorCode: e.details.applicationError?.code === MISSING_CAPTCHA ? "missingCaptchaToken" : e.details.applicationError?.code === INVALID_CAPTCHA ? "invalidCaptchaToken" : e.details.applicationError.code === INVALID_PASSWORD ? "invalidPassword" : e.details.applicationError.code === RESET_PASSWORD ? "resetPassword" : "invalidEmail"
|
|
672
|
+
};
|
|
673
|
+
}
|
|
674
|
+
};
|
|
675
|
+
const processVerification = async (nextInputs) => {
|
|
676
|
+
if (_state.stateKind === "emailVerificationRequired") {
|
|
677
|
+
const code = nextInputs.verificationCode ?? nextInputs.code;
|
|
678
|
+
const res = await wixClientWithTokens.verification.verifyDuringAuthentication(
|
|
679
|
+
code,
|
|
680
|
+
{ stateToken: _state.data.stateToken }
|
|
681
|
+
);
|
|
682
|
+
return handleState(res);
|
|
683
|
+
}
|
|
684
|
+
return {
|
|
685
|
+
stateKind: "failure",
|
|
686
|
+
loginState: "FAILURE" /* FAILURE */,
|
|
687
|
+
error: "Unknown _state"
|
|
688
|
+
};
|
|
689
|
+
};
|
|
690
|
+
const getMemberTokensForDirectLogin = async (sessionToken) => {
|
|
691
|
+
const oauthPKCE = generatePKCE();
|
|
692
|
+
const { authUrl } = await getAuthorizationUrlWithOptions(
|
|
693
|
+
oauthPKCE,
|
|
694
|
+
"web_message",
|
|
695
|
+
"none",
|
|
696
|
+
sessionToken
|
|
697
|
+
);
|
|
698
|
+
const iframePromise = addPostMessageListener(oauthPKCE.state);
|
|
699
|
+
const iframeEl = loadFrame(authUrl);
|
|
700
|
+
return iframePromise.then((res) => {
|
|
701
|
+
return getMemberTokens(res.code, res.state, oauthPKCE);
|
|
702
|
+
}).finally(() => {
|
|
703
|
+
if (document.body.contains(iframeEl)) {
|
|
704
|
+
iframeEl.parentElement?.removeChild(iframeEl);
|
|
705
|
+
}
|
|
706
|
+
});
|
|
707
|
+
};
|
|
708
|
+
const sendPasswordResetEmail = async (email, redirectUri) => {
|
|
709
|
+
await wixClientWithTokens.recovery.sendRecoveryEmail(email, {
|
|
710
|
+
redirect: { url: redirectUri, clientId: config.clientId }
|
|
711
|
+
});
|
|
712
|
+
};
|
|
713
|
+
const getRecaptchaScriptUrl = () => {
|
|
714
|
+
return `https://www.google.com/recaptcha/enterprise.js?render=${WIX_RECAPTCHA_ID}`;
|
|
715
|
+
};
|
|
716
|
+
const getRecaptchaToken = async () => {
|
|
717
|
+
return new Promise((resolve) => {
|
|
718
|
+
grecaptcha.enterprise.ready(() => {
|
|
719
|
+
grecaptcha.enterprise.execute(WIX_RECAPTCHA_ID, { action: "submit" }).then((token) => {
|
|
720
|
+
resolve(token);
|
|
721
|
+
});
|
|
722
|
+
});
|
|
723
|
+
});
|
|
724
|
+
};
|
|
725
|
+
const loggedIn = () => {
|
|
726
|
+
return _tokens.refreshToken.role === "member" /* MEMBER */;
|
|
727
|
+
};
|
|
728
|
+
return {
|
|
729
|
+
generateVisitorTokens,
|
|
730
|
+
renewToken,
|
|
731
|
+
parseFromUrl,
|
|
732
|
+
getAuthUrl,
|
|
733
|
+
getMemberTokens,
|
|
734
|
+
generateOAuthData,
|
|
735
|
+
getAuthHeaders,
|
|
736
|
+
setTokens,
|
|
737
|
+
getTokens: () => _tokens,
|
|
738
|
+
loggedIn,
|
|
739
|
+
logout,
|
|
740
|
+
register,
|
|
741
|
+
proceed: (nextInputs) => {
|
|
742
|
+
const { code, ...restProps } = nextInputs;
|
|
743
|
+
return processVerification({
|
|
744
|
+
verificationCode: code,
|
|
745
|
+
...restProps
|
|
746
|
+
});
|
|
747
|
+
},
|
|
748
|
+
processVerification,
|
|
749
|
+
login,
|
|
750
|
+
complete: getMemberTokensForDirectLogin,
|
|
751
|
+
getMemberTokensForDirectLogin,
|
|
752
|
+
sendResetPasswordMail: sendPasswordResetEmail,
|
|
753
|
+
sendPasswordResetEmail,
|
|
754
|
+
getRecaptchaScriptUrl,
|
|
755
|
+
getRecaptchaToken
|
|
756
|
+
};
|
|
757
|
+
}
|
|
758
|
+
var fetchTokens = async (payload) => {
|
|
759
|
+
const res = await fetch(`https://${API_URL}/oauth2/token`, {
|
|
760
|
+
method: "POST",
|
|
761
|
+
body: JSON.stringify(payload),
|
|
762
|
+
headers: {
|
|
763
|
+
...biHeaderGenerator({
|
|
764
|
+
entityFqdn: "wix.identity.oauth.v1.refresh_token",
|
|
765
|
+
methodFqn: "wix.identity.oauth2.v1.Oauth2Ng.Token",
|
|
766
|
+
packageName: "@wix/sdk"
|
|
767
|
+
}),
|
|
768
|
+
"Content-Type": "application/json"
|
|
769
|
+
}
|
|
770
|
+
});
|
|
771
|
+
if (res.status !== 200) {
|
|
772
|
+
throw new Error("something went wrong");
|
|
773
|
+
}
|
|
774
|
+
const json = await res.json();
|
|
775
|
+
return json;
|
|
776
|
+
};
|
|
777
|
+
|
|
778
|
+
// src/auth/ApiKeyAuthStrategy.ts
|
|
779
|
+
function ApiKeyStrategy({
|
|
780
|
+
siteId,
|
|
781
|
+
accountId,
|
|
782
|
+
apiKey
|
|
783
|
+
}) {
|
|
784
|
+
const headers = { Authorization: apiKey };
|
|
785
|
+
if (siteId) {
|
|
786
|
+
headers["wix-site-id"] = siteId;
|
|
787
|
+
}
|
|
788
|
+
if (accountId) {
|
|
789
|
+
headers["wix-account-id"] = accountId;
|
|
790
|
+
}
|
|
791
|
+
return {
|
|
792
|
+
setSiteId(_siteId) {
|
|
793
|
+
headers["wix-site-id"] = _siteId;
|
|
794
|
+
},
|
|
795
|
+
setAccountId(_accountId) {
|
|
796
|
+
headers["wix-account-id"] = _accountId;
|
|
797
|
+
},
|
|
798
|
+
async getAuthHeaders() {
|
|
799
|
+
return {
|
|
800
|
+
headers
|
|
801
|
+
};
|
|
802
|
+
}
|
|
803
|
+
};
|
|
804
|
+
}
|
|
805
|
+
|
|
806
|
+
// src/index.ts
|
|
807
|
+
__reExport(src_exports, require("@wix/sdk-types"), module.exports);
|
|
808
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
809
|
+
0 && (module.exports = {
|
|
810
|
+
ApiKeyStrategy,
|
|
811
|
+
LoginState,
|
|
812
|
+
OAuthStrategy,
|
|
813
|
+
TokenRole,
|
|
814
|
+
createClient,
|
|
815
|
+
decodeText,
|
|
816
|
+
media,
|
|
817
|
+
...require("@wix/sdk-types")
|
|
818
|
+
});
|