@wix/sdk 1.5.9 → 1.6.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/auth/api-key/package.json +3 -0
- package/auth/oauth2/package.json +3 -0
- package/auth/wix-app-oauth/package.json +3 -0
- package/build/ambassador-modules.d.ts +31 -0
- package/build/ambassador-modules.js +89 -0
- package/build/auth/ApiKeyAuthStrategy.d.ts +16 -0
- package/build/auth/ApiKeyAuthStrategy.js +22 -0
- package/build/auth/WixAppOAuthStrategy.d.ts +54 -0
- package/build/auth/WixAppOAuthStrategy.js +106 -0
- package/build/auth/oauth2/OAuthStrategy.d.ts +12 -0
- package/build/auth/oauth2/OAuthStrategy.js +357 -0
- package/build/auth/oauth2/constants.d.ts +5 -0
- package/build/auth/oauth2/constants.js +5 -0
- package/build/auth/oauth2/pkce-challenge.d.ts +5 -0
- package/build/auth/oauth2/pkce-challenge.js +33 -0
- package/build/auth/oauth2/types.d.ts +121 -0
- package/build/auth/oauth2/types.js +16 -0
- package/build/bi/biHeaderGenerator.d.ts +12 -0
- package/build/bi/biHeaderGenerator.js +17 -0
- package/build/common.d.ts +7 -0
- package/build/common.js +4 -0
- package/build/fetch-error.d.ts +9 -0
- package/build/fetch-error.js +31 -0
- package/build/helpers.d.ts +4 -0
- package/build/helpers.js +11 -0
- package/build/host-modules.d.ts +3 -0
- package/build/host-modules.js +5 -0
- package/build/iframeUtils.d.ts +4 -0
- package/build/iframeUtils.js +43 -0
- package/build/index.d.ts +8 -388
- package/build/index.js +9 -1115
- package/build/rest-modules.d.ts +7 -0
- package/build/rest-modules.js +82 -0
- package/build/tokenHelpers.d.ts +4 -0
- package/build/tokenHelpers.js +11 -0
- package/build/wixClient.d.ts +70 -0
- package/build/wixClient.js +86 -0
- package/build/wixMedia.d.ts +46 -0
- package/build/wixMedia.js +156 -0
- package/cjs/build/ambassador-modules.d.ts +31 -0
- package/cjs/build/ambassador-modules.js +95 -0
- package/cjs/build/auth/ApiKeyAuthStrategy.d.ts +16 -0
- package/cjs/build/auth/ApiKeyAuthStrategy.js +26 -0
- package/cjs/build/auth/WixAppOAuthStrategy.d.ts +54 -0
- package/cjs/build/auth/WixAppOAuthStrategy.js +110 -0
- package/cjs/build/auth/oauth2/OAuthStrategy.d.ts +12 -0
- package/cjs/build/auth/oauth2/OAuthStrategy.js +361 -0
- package/cjs/build/auth/oauth2/constants.d.ts +5 -0
- package/cjs/build/auth/oauth2/constants.js +8 -0
- package/cjs/build/auth/oauth2/pkce-challenge.d.ts +5 -0
- package/cjs/build/auth/oauth2/pkce-challenge.js +41 -0
- package/cjs/build/auth/oauth2/types.d.ts +121 -0
- package/cjs/build/auth/oauth2/types.js +19 -0
- package/cjs/build/bi/biHeaderGenerator.d.ts +12 -0
- package/cjs/build/bi/biHeaderGenerator.js +21 -0
- package/cjs/build/common.d.ts +7 -0
- package/cjs/build/common.js +7 -0
- package/cjs/build/fetch-error.d.ts +9 -0
- package/cjs/build/fetch-error.js +35 -0
- package/cjs/build/helpers.d.ts +4 -0
- package/cjs/build/helpers.js +16 -0
- package/cjs/build/host-modules.d.ts +3 -0
- package/cjs/build/host-modules.js +10 -0
- package/cjs/build/iframeUtils.d.ts +4 -0
- package/cjs/build/iframeUtils.js +50 -0
- package/cjs/build/index.d.ts +9 -0
- package/cjs/build/index.js +28 -0
- package/cjs/build/rest-modules.d.ts +7 -0
- package/cjs/build/rest-modules.js +87 -0
- package/cjs/build/tokenHelpers.d.ts +4 -0
- package/cjs/build/tokenHelpers.js +17 -0
- package/cjs/build/wixClient.d.ts +70 -0
- package/cjs/build/wixClient.js +90 -0
- package/cjs/build/wixMedia.d.ts +46 -0
- package/cjs/build/wixMedia.js +160 -0
- package/cjs/package.json +3 -0
- package/client/package.json +3 -0
- package/package.json +45 -22
- package/build/browser/index.mjs +0 -1075
- package/build/index.d.mts +0 -389
- package/build/index.mjs +0 -1066
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
import { authentication } from '@wix/identity';
|
|
2
|
+
import { AuthenticationStrategy } from '@wix/sdk-types';
|
|
3
|
+
export interface Tokens {
|
|
4
|
+
accessToken: AccessToken;
|
|
5
|
+
refreshToken: RefreshToken;
|
|
6
|
+
}
|
|
7
|
+
export interface Token {
|
|
8
|
+
value: string;
|
|
9
|
+
}
|
|
10
|
+
export interface AccessToken extends Token {
|
|
11
|
+
expiresAt: number;
|
|
12
|
+
}
|
|
13
|
+
export interface RefreshToken extends Token {
|
|
14
|
+
role: TokenRole;
|
|
15
|
+
}
|
|
16
|
+
export interface OauthData extends OauthPKCE {
|
|
17
|
+
originalUri: string;
|
|
18
|
+
redirectUri: string;
|
|
19
|
+
}
|
|
20
|
+
export interface OauthPKCE {
|
|
21
|
+
codeVerifier: string;
|
|
22
|
+
codeChallenge: string;
|
|
23
|
+
state: string;
|
|
24
|
+
}
|
|
25
|
+
export interface RegisterParams extends LoginParams {
|
|
26
|
+
profile?: authentication.IdentityProfile;
|
|
27
|
+
}
|
|
28
|
+
export interface LoginParams {
|
|
29
|
+
email: string;
|
|
30
|
+
password: string;
|
|
31
|
+
captchaTokens?: {
|
|
32
|
+
invisibleRecaptchaToken?: string;
|
|
33
|
+
recaptchaToken?: string;
|
|
34
|
+
};
|
|
35
|
+
}
|
|
36
|
+
export interface IOAuthStrategy extends AuthenticationStrategy {
|
|
37
|
+
generateVisitorTokens(tokens?: {
|
|
38
|
+
refreshToken?: RefreshToken;
|
|
39
|
+
accessToken?: AccessToken;
|
|
40
|
+
}): Promise<Tokens>;
|
|
41
|
+
renewToken: (refreshToken: RefreshToken) => Promise<Tokens>;
|
|
42
|
+
setTokens: (tokens: Tokens) => void;
|
|
43
|
+
getTokens: () => Tokens;
|
|
44
|
+
generateOAuthData: (redirectUri: string, originalUri?: string) => OauthData;
|
|
45
|
+
getAuthUrl: (oauthData: OauthData, opts?: {
|
|
46
|
+
prompt?: 'login' | 'none';
|
|
47
|
+
responseMode?: 'fragment' | 'web_message' | 'query';
|
|
48
|
+
}) => Promise<{
|
|
49
|
+
authUrl: string;
|
|
50
|
+
}>;
|
|
51
|
+
getMemberTokens: (code: string, state: string, oauthData: OauthData) => Promise<Tokens>;
|
|
52
|
+
logout: (originalUrl: string) => Promise<{
|
|
53
|
+
logoutUrl: string;
|
|
54
|
+
}>;
|
|
55
|
+
parseFromUrl: (url?: string, responseMode?: 'query' | 'fragment') => {
|
|
56
|
+
code: string;
|
|
57
|
+
state: string;
|
|
58
|
+
error?: string;
|
|
59
|
+
errorDescription?: string;
|
|
60
|
+
};
|
|
61
|
+
register: (params: RegisterParams) => Promise<StateMachine>;
|
|
62
|
+
login: (params: LoginParams) => Promise<StateMachine>;
|
|
63
|
+
processVerification<T extends ProcessableState>(nextInputs: CalculateNextState<T>, state?: StateMachine): Promise<StateMachine>;
|
|
64
|
+
getMemberTokensForDirectLogin: (sessionToken: string) => Promise<Tokens>;
|
|
65
|
+
sendPasswordResetEmail: (email: string, redirectUri: string) => Promise<void>;
|
|
66
|
+
captchaInvisibleSiteKey: string;
|
|
67
|
+
captchaVisibleSiteKey: string;
|
|
68
|
+
loggedIn: () => boolean;
|
|
69
|
+
}
|
|
70
|
+
export declare enum LoginState {
|
|
71
|
+
SUCCESS = "SUCCESS",
|
|
72
|
+
INITIAL = "INITIAL",
|
|
73
|
+
FAILURE = "FAILURE",
|
|
74
|
+
EMAIL_VERIFICATION_REQUIRED = "EMAIL_VERIFICATION_REQUIRED",
|
|
75
|
+
OWNER_APPROVAL_REQUIRED = "OWNER_APPROVAL_REQUIRED",
|
|
76
|
+
USER_CAPTCHA_REQUIRED = "USER_CAPTCHA_REQUIRED",
|
|
77
|
+
SILENT_CAPTCHA_REQUIRED = "SILENT_CAPTCHA_REQUIRED"
|
|
78
|
+
}
|
|
79
|
+
interface LoginResults<LK extends LoginState> {
|
|
80
|
+
loginState: LK;
|
|
81
|
+
}
|
|
82
|
+
interface SuccessState extends LoginResults<LoginState.SUCCESS> {
|
|
83
|
+
data: {
|
|
84
|
+
sessionToken: string;
|
|
85
|
+
};
|
|
86
|
+
}
|
|
87
|
+
interface InitialState extends LoginResults<LoginState.INITIAL> {
|
|
88
|
+
}
|
|
89
|
+
interface ErrorState extends LoginResults<LoginState.FAILURE> {
|
|
90
|
+
errorCode?: 'invalidEmail' | 'invalidPassword' | 'resetPassword' | 'missingCaptchaToken' | 'emailAlreadyExists' | 'invalidCaptchaToken';
|
|
91
|
+
error: string;
|
|
92
|
+
}
|
|
93
|
+
interface EmailVerificationRequiredState extends LoginResults<LoginState.EMAIL_VERIFICATION_REQUIRED> {
|
|
94
|
+
data: {
|
|
95
|
+
stateToken: string;
|
|
96
|
+
};
|
|
97
|
+
}
|
|
98
|
+
interface OwnerApprovalRequiredState extends LoginResults<LoginState.OWNER_APPROVAL_REQUIRED> {
|
|
99
|
+
}
|
|
100
|
+
interface SilentCaptchaRequiredState extends LoginResults<LoginState.SILENT_CAPTCHA_REQUIRED> {
|
|
101
|
+
data: {
|
|
102
|
+
stateToken: string;
|
|
103
|
+
};
|
|
104
|
+
}
|
|
105
|
+
interface UserCaptchaRequiredState extends LoginResults<LoginState.USER_CAPTCHA_REQUIRED> {
|
|
106
|
+
data: {
|
|
107
|
+
stateToken: string;
|
|
108
|
+
};
|
|
109
|
+
}
|
|
110
|
+
export declare enum TokenRole {
|
|
111
|
+
NONE = "none",
|
|
112
|
+
VISITOR = "visitor",
|
|
113
|
+
MEMBER = "member"
|
|
114
|
+
}
|
|
115
|
+
export type StateMachine = InitialState | SuccessState | ErrorState | EmailVerificationRequiredState | OwnerApprovalRequiredState | SilentCaptchaRequiredState | UserCaptchaRequiredState;
|
|
116
|
+
type VerificationCode = {
|
|
117
|
+
verificationCode: string;
|
|
118
|
+
};
|
|
119
|
+
export type CalculateNextState<T> = T extends EmailVerificationRequiredState ? VerificationCode : never;
|
|
120
|
+
export type ProcessableState = EmailVerificationRequiredState;
|
|
121
|
+
export {};
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.TokenRole = exports.LoginState = void 0;
|
|
4
|
+
var LoginState;
|
|
5
|
+
(function (LoginState) {
|
|
6
|
+
LoginState["SUCCESS"] = "SUCCESS";
|
|
7
|
+
LoginState["INITIAL"] = "INITIAL";
|
|
8
|
+
LoginState["FAILURE"] = "FAILURE";
|
|
9
|
+
LoginState["EMAIL_VERIFICATION_REQUIRED"] = "EMAIL_VERIFICATION_REQUIRED";
|
|
10
|
+
LoginState["OWNER_APPROVAL_REQUIRED"] = "OWNER_APPROVAL_REQUIRED";
|
|
11
|
+
LoginState["USER_CAPTCHA_REQUIRED"] = "USER_CAPTCHA_REQUIRED";
|
|
12
|
+
LoginState["SILENT_CAPTCHA_REQUIRED"] = "SILENT_CAPTCHA_REQUIRED";
|
|
13
|
+
})(LoginState || (exports.LoginState = LoginState = {}));
|
|
14
|
+
var TokenRole;
|
|
15
|
+
(function (TokenRole) {
|
|
16
|
+
TokenRole["NONE"] = "none";
|
|
17
|
+
TokenRole["VISITOR"] = "visitor";
|
|
18
|
+
TokenRole["MEMBER"] = "member";
|
|
19
|
+
})(TokenRole || (exports.TokenRole = TokenRole = {}));
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { APIMetadata } from '@wix/sdk-types';
|
|
2
|
+
import { PublicMetadata } from '../common.js';
|
|
3
|
+
export declare const WixBIHeaderName = "x-wix-bi-gateway";
|
|
4
|
+
export type WixBIHeaderValues = {
|
|
5
|
+
['environment']: 'js-sdk';
|
|
6
|
+
['package-name']?: string;
|
|
7
|
+
['method-fqn']?: string;
|
|
8
|
+
['entity']?: string;
|
|
9
|
+
};
|
|
10
|
+
export declare function biHeaderGenerator(apiMetadata: APIMetadata, publicMetadata?: PublicMetadata): {
|
|
11
|
+
[WixBIHeaderName]: string;
|
|
12
|
+
};
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.biHeaderGenerator = exports.WixBIHeaderName = void 0;
|
|
4
|
+
exports.WixBIHeaderName = 'x-wix-bi-gateway';
|
|
5
|
+
function biHeaderGenerator(apiMetadata, publicMetadata) {
|
|
6
|
+
return {
|
|
7
|
+
[exports.WixBIHeaderName]: objectToKeyValue({
|
|
8
|
+
environment: 'js-sdk',
|
|
9
|
+
'package-name': apiMetadata.packageName ?? publicMetadata?.PACKAGE_NAME,
|
|
10
|
+
'method-fqn': apiMetadata.methodFqn,
|
|
11
|
+
entity: apiMetadata.entityFqdn,
|
|
12
|
+
}),
|
|
13
|
+
};
|
|
14
|
+
}
|
|
15
|
+
exports.biHeaderGenerator = biHeaderGenerator;
|
|
16
|
+
function objectToKeyValue(input) {
|
|
17
|
+
return Object.entries(input)
|
|
18
|
+
.filter(([_, value]) => Boolean(value))
|
|
19
|
+
.map(([key, value]) => `${key}=${value}`)
|
|
20
|
+
.join(',');
|
|
21
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
export declare const PUBLIC_METADATA_KEY = "__metadata";
|
|
2
|
+
export type PublicMetadata = {
|
|
3
|
+
PACKAGE_NAME?: string;
|
|
4
|
+
};
|
|
5
|
+
export declare const API_URL = "www.wixapis.com";
|
|
6
|
+
export declare const READ_ONLY_API_URL = "readonly.wixapis.com";
|
|
7
|
+
export declare const FORCE_WRITE_API_URLS: string[];
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.FORCE_WRITE_API_URLS = exports.READ_ONLY_API_URL = exports.API_URL = exports.PUBLIC_METADATA_KEY = void 0;
|
|
4
|
+
exports.PUBLIC_METADATA_KEY = '__metadata';
|
|
5
|
+
exports.API_URL = 'www.wixapis.com';
|
|
6
|
+
exports.READ_ONLY_API_URL = 'readonly.wixapis.com';
|
|
7
|
+
exports.FORCE_WRITE_API_URLS = ['/ecom/v1/carts/current'];
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.FetchErrorResponse = void 0;
|
|
4
|
+
class FetchErrorResponse extends Error {
|
|
5
|
+
message;
|
|
6
|
+
response;
|
|
7
|
+
constructor(message, response) {
|
|
8
|
+
super(message);
|
|
9
|
+
this.message = message;
|
|
10
|
+
this.response = response;
|
|
11
|
+
}
|
|
12
|
+
async details() {
|
|
13
|
+
const dataError = await this.response.json();
|
|
14
|
+
return errorBuilder(this.response.status, dataError?.message, dataError?.details, {
|
|
15
|
+
requestId: this.response.headers.get('X-Wix-Request-Id'),
|
|
16
|
+
details: dataError,
|
|
17
|
+
});
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
exports.FetchErrorResponse = FetchErrorResponse;
|
|
21
|
+
const errorBuilder = (code, description, details, data) => {
|
|
22
|
+
return {
|
|
23
|
+
details: {
|
|
24
|
+
...(!details?.validationError && {
|
|
25
|
+
applicationError: {
|
|
26
|
+
description,
|
|
27
|
+
code,
|
|
28
|
+
data,
|
|
29
|
+
},
|
|
30
|
+
}),
|
|
31
|
+
...details,
|
|
32
|
+
},
|
|
33
|
+
message: description,
|
|
34
|
+
};
|
|
35
|
+
};
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.isObject = exports.getDefaultContentHeader = void 0;
|
|
4
|
+
// we follow a simplified version of the axios convention
|
|
5
|
+
// https://github.com/axios/axios/blob/649d739288c8e2c55829ac60e2345a0f3439c730/lib/defaults/index.js#L65
|
|
6
|
+
const getDefaultContentHeader = (options) => {
|
|
7
|
+
if (options?.method &&
|
|
8
|
+
['post', 'put', 'patch'].includes(options.method.toLocaleLowerCase()) &&
|
|
9
|
+
options.body) {
|
|
10
|
+
return { 'Content-Type': 'application/json' };
|
|
11
|
+
}
|
|
12
|
+
return {};
|
|
13
|
+
};
|
|
14
|
+
exports.getDefaultContentHeader = getDefaultContentHeader;
|
|
15
|
+
const isObject = (val) => val && typeof val === 'object' && !Array.isArray(val);
|
|
16
|
+
exports.isObject = isObject;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.buildHostModule = exports.isHostModule = void 0;
|
|
4
|
+
const helpers_js_1 = require("./helpers.js");
|
|
5
|
+
const isHostModule = (val) => (0, helpers_js_1.isObject)(val) && val.__type === 'host';
|
|
6
|
+
exports.isHostModule = isHostModule;
|
|
7
|
+
function buildHostModule(val, host) {
|
|
8
|
+
return val.create(host);
|
|
9
|
+
}
|
|
10
|
+
exports.buildHostModule = buildHostModule;
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
export declare function addListener(eventTarget: any, name: string, fn: Function): void;
|
|
2
|
+
export declare function removeListener(eventTarget: any, name: string, fn: Function): void;
|
|
3
|
+
export declare function loadFrame(src: string): HTMLIFrameElement;
|
|
4
|
+
export declare function addPostMessageListener(state: string): Promise<unknown>;
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.addPostMessageListener = exports.loadFrame = exports.removeListener = exports.addListener = void 0;
|
|
4
|
+
function addListener(eventTarget, name, fn) {
|
|
5
|
+
if (eventTarget.addEventListener) {
|
|
6
|
+
eventTarget.addEventListener(name, fn);
|
|
7
|
+
}
|
|
8
|
+
else {
|
|
9
|
+
eventTarget.attachEvent('on' + name, fn);
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
exports.addListener = addListener;
|
|
13
|
+
function removeListener(eventTarget, name, fn) {
|
|
14
|
+
if (eventTarget.removeEventListener) {
|
|
15
|
+
eventTarget.removeEventListener(name, fn);
|
|
16
|
+
}
|
|
17
|
+
else {
|
|
18
|
+
eventTarget.detachEvent('on' + name, fn);
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
exports.removeListener = removeListener;
|
|
22
|
+
function loadFrame(src) {
|
|
23
|
+
const iframe = document.createElement('iframe');
|
|
24
|
+
iframe.style.display = 'none';
|
|
25
|
+
iframe.src = src;
|
|
26
|
+
return document.body.appendChild(iframe);
|
|
27
|
+
}
|
|
28
|
+
exports.loadFrame = loadFrame;
|
|
29
|
+
function addPostMessageListener(state) {
|
|
30
|
+
let responseHandler;
|
|
31
|
+
let timeoutId;
|
|
32
|
+
const msgReceivedOrTimeout = new Promise((resolve, reject) => {
|
|
33
|
+
responseHandler = (e) => {
|
|
34
|
+
if (!e.data || e.data.state !== state) {
|
|
35
|
+
// A message not meant for us
|
|
36
|
+
return;
|
|
37
|
+
}
|
|
38
|
+
resolve(e.data);
|
|
39
|
+
};
|
|
40
|
+
addListener(window, 'message', responseHandler);
|
|
41
|
+
timeoutId = setTimeout(() => {
|
|
42
|
+
reject(new Error('OAuth flow timed out'));
|
|
43
|
+
}, 120000);
|
|
44
|
+
});
|
|
45
|
+
return msgReceivedOrTimeout.finally(() => {
|
|
46
|
+
clearTimeout(timeoutId);
|
|
47
|
+
removeListener(window, 'message', responseHandler);
|
|
48
|
+
});
|
|
49
|
+
}
|
|
50
|
+
exports.addPostMessageListener = addPostMessageListener;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export * from './wixClient.js';
|
|
2
|
+
export * from './wixMedia.js';
|
|
3
|
+
export * from './auth/oauth2/OAuthStrategy.js';
|
|
4
|
+
export * from './auth/oauth2/types.js';
|
|
5
|
+
export * from './auth/ApiKeyAuthStrategy.js';
|
|
6
|
+
export * from './auth/WixAppOAuthStrategy.js';
|
|
7
|
+
export * from '@wix/sdk-types';
|
|
8
|
+
export { getDefaultDomain } from './rest-modules.js';
|
|
9
|
+
export { API_URL } from './common.js';
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
exports.API_URL = exports.getDefaultDomain = void 0;
|
|
18
|
+
__exportStar(require("./wixClient.js"), exports);
|
|
19
|
+
__exportStar(require("./wixMedia.js"), exports);
|
|
20
|
+
__exportStar(require("./auth/oauth2/OAuthStrategy.js"), exports);
|
|
21
|
+
__exportStar(require("./auth/oauth2/types.js"), exports);
|
|
22
|
+
__exportStar(require("./auth/ApiKeyAuthStrategy.js"), exports);
|
|
23
|
+
__exportStar(require("./auth/WixAppOAuthStrategy.js"), exports);
|
|
24
|
+
__exportStar(require("@wix/sdk-types"), exports);
|
|
25
|
+
var rest_modules_js_1 = require("./rest-modules.js");
|
|
26
|
+
Object.defineProperty(exports, "getDefaultDomain", { enumerable: true, get: function () { return rest_modules_js_1.getDefaultDomain; } });
|
|
27
|
+
var common_js_1 = require("./common.js");
|
|
28
|
+
Object.defineProperty(exports, "API_URL", { enumerable: true, get: function () { return common_js_1.API_URL; } });
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { PublicMetadata } from './common.js';
|
|
2
|
+
import { BuildRESTFunction, RESTFunctionDescriptor } from '@wix/sdk-types';
|
|
3
|
+
export type RESTModuleOptions = {
|
|
4
|
+
HTTPHost?: string;
|
|
5
|
+
};
|
|
6
|
+
export declare const getDefaultDomain: (method: string, url: string) => "www.wixapis.com" | "readonly.wixapis.com";
|
|
7
|
+
export declare function buildRESTDescriptor<T extends RESTFunctionDescriptor>(origFunc: T, publicMetadata: PublicMetadata, boundFetch: (url: string | URL, init?: RequestInit) => Promise<Response>, options?: RESTModuleOptions): BuildRESTFunction<T>;
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.buildRESTDescriptor = exports.getDefaultDomain = void 0;
|
|
4
|
+
const biHeaderGenerator_js_1 = require("./bi/biHeaderGenerator.js");
|
|
5
|
+
const common_js_1 = require("./common.js");
|
|
6
|
+
const getDefaultDomain = (method, url) => method === 'GET' &&
|
|
7
|
+
!common_js_1.FORCE_WRITE_API_URLS.some((write_url) => url === write_url)
|
|
8
|
+
? common_js_1.READ_ONLY_API_URL
|
|
9
|
+
: common_js_1.API_URL;
|
|
10
|
+
exports.getDefaultDomain = getDefaultDomain;
|
|
11
|
+
function buildRESTDescriptor(origFunc, publicMetadata, boundFetch, options) {
|
|
12
|
+
return origFunc({
|
|
13
|
+
request: async (factory) => {
|
|
14
|
+
const requestOptions = factory({ host: options?.HTTPHost || common_js_1.API_URL });
|
|
15
|
+
let request = requestOptions;
|
|
16
|
+
if (request.method === 'GET' &&
|
|
17
|
+
request.fallback?.length &&
|
|
18
|
+
request.params.toString().length > 4000) {
|
|
19
|
+
request = requestOptions.fallback[0];
|
|
20
|
+
}
|
|
21
|
+
const domain = options?.HTTPHost ?? (0, exports.getDefaultDomain)(request.method, request.url);
|
|
22
|
+
let url = `https://${domain}${request.url}`;
|
|
23
|
+
if (request.params && request.params.toString()) {
|
|
24
|
+
url += `?${request.params.toString()}`;
|
|
25
|
+
}
|
|
26
|
+
try {
|
|
27
|
+
const biHeader = (0, biHeaderGenerator_js_1.biHeaderGenerator)(requestOptions, publicMetadata);
|
|
28
|
+
const res = await boundFetch(url, {
|
|
29
|
+
method: request.method,
|
|
30
|
+
...(request.data && {
|
|
31
|
+
body: JSON.stringify(request.data),
|
|
32
|
+
}),
|
|
33
|
+
headers: {
|
|
34
|
+
...biHeader,
|
|
35
|
+
},
|
|
36
|
+
});
|
|
37
|
+
if (res.status !== 200) {
|
|
38
|
+
let dataError = null;
|
|
39
|
+
try {
|
|
40
|
+
dataError = await res.json();
|
|
41
|
+
}
|
|
42
|
+
catch (e) {
|
|
43
|
+
//
|
|
44
|
+
}
|
|
45
|
+
throw errorBuilder(res.status, dataError?.message, dataError?.details, {
|
|
46
|
+
requestId: res.headers.get('X-Wix-Request-Id'),
|
|
47
|
+
details: dataError,
|
|
48
|
+
});
|
|
49
|
+
}
|
|
50
|
+
const data = await res.json();
|
|
51
|
+
return {
|
|
52
|
+
data,
|
|
53
|
+
headers: res.headers,
|
|
54
|
+
status: res.status,
|
|
55
|
+
statusText: res.statusText,
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
catch (e) {
|
|
59
|
+
if (e.message?.includes('fetch is not defined')) {
|
|
60
|
+
console.error('Node.js v18+ is required');
|
|
61
|
+
}
|
|
62
|
+
throw e;
|
|
63
|
+
}
|
|
64
|
+
},
|
|
65
|
+
});
|
|
66
|
+
}
|
|
67
|
+
exports.buildRESTDescriptor = buildRESTDescriptor;
|
|
68
|
+
const errorBuilder = (code, description, details, data) => {
|
|
69
|
+
return {
|
|
70
|
+
response: {
|
|
71
|
+
data: {
|
|
72
|
+
details: {
|
|
73
|
+
...(!details?.validationError && {
|
|
74
|
+
applicationError: {
|
|
75
|
+
description,
|
|
76
|
+
code,
|
|
77
|
+
data,
|
|
78
|
+
},
|
|
79
|
+
}),
|
|
80
|
+
...details,
|
|
81
|
+
},
|
|
82
|
+
message: description,
|
|
83
|
+
},
|
|
84
|
+
status: code,
|
|
85
|
+
},
|
|
86
|
+
};
|
|
87
|
+
};
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import { AccessToken } from './auth/oauth2/types.js';
|
|
2
|
+
export declare function getCurrentDate(): number;
|
|
3
|
+
export declare function isTokenExpired(token: AccessToken): boolean;
|
|
4
|
+
export declare function createAccessToken(accessToken: string, expiresIn: number): AccessToken;
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.createAccessToken = exports.isTokenExpired = exports.getCurrentDate = void 0;
|
|
4
|
+
function getCurrentDate() {
|
|
5
|
+
return Math.floor(Date.now() / 1000);
|
|
6
|
+
}
|
|
7
|
+
exports.getCurrentDate = getCurrentDate;
|
|
8
|
+
function isTokenExpired(token) {
|
|
9
|
+
const currentDate = getCurrentDate();
|
|
10
|
+
return token.expiresAt < currentDate;
|
|
11
|
+
}
|
|
12
|
+
exports.isTokenExpired = isTokenExpired;
|
|
13
|
+
function createAccessToken(accessToken, expiresIn) {
|
|
14
|
+
const now = getCurrentDate();
|
|
15
|
+
return { value: accessToken, expiresAt: Number(expiresIn) + now };
|
|
16
|
+
}
|
|
17
|
+
exports.createAccessToken = createAccessToken;
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import { AuthenticationStrategy, BoundAuthenticationStrategy, BuildRESTFunction, Host, HostModule, HostModuleAPI, RESTFunctionDescriptor } from '@wix/sdk-types';
|
|
2
|
+
import { ConditionalExcept, EmptyObject } from 'type-fest';
|
|
3
|
+
import { AmbassadorFunctionDescriptor, BuildAmbassadorFunction } from './ambassador-modules.js';
|
|
4
|
+
import { PublicMetadata } from './common.js';
|
|
5
|
+
import type { GraphQLFormattedError } from 'graphql';
|
|
6
|
+
type Headers = Record<string, string>;
|
|
7
|
+
/**
|
|
8
|
+
* This type takes in a descriptors object of a certain Host (including an `unknown` host)
|
|
9
|
+
* and returns an object with the same structure, but with all descriptors replaced with their API.
|
|
10
|
+
* Any non-descriptor properties are removed from the returned object, including descriptors that
|
|
11
|
+
* do not match the given host (as they will not work with the given host).
|
|
12
|
+
*/
|
|
13
|
+
export type BuildDescriptors<T extends Descriptors, H extends Host<any> | undefined> = BuildRESTDescriptors<T> & BuildAmbassadorDescriptors<T> & (H extends Host<any> ? BuildHostDescriptors<T> : {});
|
|
14
|
+
type BuildRESTDescriptors<T extends Descriptors> = T extends RESTFunctionDescriptor ? BuildRESTFunction<T> : ConditionalExcept<{
|
|
15
|
+
[Key in keyof T]: T[Key] extends Descriptors ? BuildRESTDescriptors<T[Key]> : never;
|
|
16
|
+
}, EmptyObject>;
|
|
17
|
+
type BuildAmbassadorDescriptors<T extends Descriptors> = T extends AmbassadorFunctionDescriptor ? BuildAmbassadorFunction<T> : ConditionalExcept<{
|
|
18
|
+
[Key in keyof T]: T[Key] extends Descriptors ? BuildAmbassadorDescriptors<T[Key]> : never;
|
|
19
|
+
}, EmptyObject>;
|
|
20
|
+
type BuildHostDescriptors<T extends Descriptors> = T extends HostModule<any, any> ? HostModuleAPI<T> : ConditionalExcept<{
|
|
21
|
+
[Key in keyof T]: T[Key] extends Descriptors ? BuildHostDescriptors<T[Key]> : never;
|
|
22
|
+
}, EmptyObject>;
|
|
23
|
+
/**
|
|
24
|
+
* Descriptors are objects that describe the API of a module, and the module
|
|
25
|
+
* can either be a REST module or a host module.
|
|
26
|
+
* This type is recursive, so it can describe nested modules.
|
|
27
|
+
*/
|
|
28
|
+
export type Descriptors = RESTFunctionDescriptor | AmbassadorFunctionDescriptor | HostModule<any, any> | {
|
|
29
|
+
[key: string]: Descriptors | PublicMetadata | any;
|
|
30
|
+
};
|
|
31
|
+
/**
|
|
32
|
+
* This type is used in `createClient` to ensure that the given host matches the host of the given descriptors.
|
|
33
|
+
* If the host does not match, the descriptor is replaced with a host module that will throw an error when used.
|
|
34
|
+
*/
|
|
35
|
+
export type AssertHostMatches<T extends Descriptors, H extends Host<any>> = T extends HostModule<any, infer U> ? H extends undefined ? never : H extends U ? T : HostModule<any, H> : T extends RESTFunctionDescriptor<any> ? T : {
|
|
36
|
+
[Key in keyof T]: T[Key] extends Descriptors ? AssertHostMatches<T[Key], H> : T[Key];
|
|
37
|
+
};
|
|
38
|
+
type TypedQueryInput<Result = {
|
|
39
|
+
[key: string]: any;
|
|
40
|
+
}, Variables = {
|
|
41
|
+
[key: string]: any;
|
|
42
|
+
}> = {
|
|
43
|
+
/**
|
|
44
|
+
* Type to support `@graphql-typed-document-node/core`
|
|
45
|
+
* @internal
|
|
46
|
+
*/
|
|
47
|
+
__apiType?: (variables: Variables) => Result;
|
|
48
|
+
/**
|
|
49
|
+
* Type to support `TypedQueryDocumentNode` from `graphql`
|
|
50
|
+
* @internal
|
|
51
|
+
*/
|
|
52
|
+
__ensureTypesOfVariablesAndResultMatching?: (variables: Variables) => Result;
|
|
53
|
+
};
|
|
54
|
+
export type WixClient<H extends Host<any> | undefined = undefined, Z extends AuthenticationStrategy<H> = AuthenticationStrategy<H>, T extends Descriptors = Descriptors> = {
|
|
55
|
+
setHeaders(headers: Headers): void;
|
|
56
|
+
auth: Omit<Z, 'getAuthHeaders'> & BoundAuthenticationStrategy;
|
|
57
|
+
fetch(relativeUrl: string, options: RequestInit): Promise<Response>;
|
|
58
|
+
use<R extends Descriptors = EmptyObject>(modules: H extends Host<any> ? AssertHostMatches<R, H> : R): BuildDescriptors<R, H>;
|
|
59
|
+
graphql<Result, Variables>(query: string | ((string | String) & TypedQueryInput<Result, Variables>), variables?: Variables): Promise<{
|
|
60
|
+
data: Result;
|
|
61
|
+
errors?: GraphQLFormattedError[];
|
|
62
|
+
}>;
|
|
63
|
+
} & BuildDescriptors<T, H>;
|
|
64
|
+
export declare function createClient<H extends Host<any> | undefined = undefined, Z extends AuthenticationStrategy<H> = AuthenticationStrategy<H>, T extends Descriptors = EmptyObject>(config: {
|
|
65
|
+
modules?: H extends Host<any> ? AssertHostMatches<T, H> : T;
|
|
66
|
+
auth?: Z;
|
|
67
|
+
headers?: Headers;
|
|
68
|
+
host?: H;
|
|
69
|
+
}): WixClient<H, Z, T>;
|
|
70
|
+
export {};
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.createClient = void 0;
|
|
4
|
+
const ambassador_modules_js_1 = require("./ambassador-modules.js");
|
|
5
|
+
const common_js_1 = require("./common.js");
|
|
6
|
+
const helpers_js_1 = require("./helpers.js");
|
|
7
|
+
const host_modules_js_1 = require("./host-modules.js");
|
|
8
|
+
const rest_modules_js_1 = require("./rest-modules.js");
|
|
9
|
+
const fetch_error_js_1 = require("./fetch-error.js");
|
|
10
|
+
function createClient(config) {
|
|
11
|
+
const _headers = config.headers || { Authorization: '' };
|
|
12
|
+
const authStrategy = config.auth || {
|
|
13
|
+
getAuthHeaders: () => Promise.resolve({ headers: {} }),
|
|
14
|
+
};
|
|
15
|
+
const boundGetAuthHeaders = authStrategy.getAuthHeaders.bind(undefined, config.host);
|
|
16
|
+
authStrategy.getAuthHeaders = boundGetAuthHeaders;
|
|
17
|
+
const boundFetch = async (url, options) => {
|
|
18
|
+
const authHeaders = await boundGetAuthHeaders();
|
|
19
|
+
const defaultContentTypeHeader = (0, helpers_js_1.getDefaultContentHeader)(options);
|
|
20
|
+
return fetch(url, {
|
|
21
|
+
...options,
|
|
22
|
+
headers: {
|
|
23
|
+
...defaultContentTypeHeader,
|
|
24
|
+
..._headers,
|
|
25
|
+
...authHeaders?.headers,
|
|
26
|
+
...options?.headers,
|
|
27
|
+
},
|
|
28
|
+
});
|
|
29
|
+
};
|
|
30
|
+
// This is typed as `any` because when trying to properly type it as defined
|
|
31
|
+
// on the WixClient, typescript starts failing with `Type instantiation is
|
|
32
|
+
// excessively deep and possibly infinite.`
|
|
33
|
+
const use = (modules, metadata) => {
|
|
34
|
+
if ((0, host_modules_js_1.isHostModule)(modules) && config.host) {
|
|
35
|
+
return (0, host_modules_js_1.buildHostModule)(modules, config.host);
|
|
36
|
+
}
|
|
37
|
+
else if (typeof modules === 'function') {
|
|
38
|
+
const { module, options } = (0, ambassador_modules_js_1.isAmbassadorModule)(modules)
|
|
39
|
+
? {
|
|
40
|
+
module: (0, ambassador_modules_js_1.toHTTPModule)(modules),
|
|
41
|
+
options: (0, ambassador_modules_js_1.ambassadorModuleOptions)(),
|
|
42
|
+
}
|
|
43
|
+
: { module: modules, options: undefined };
|
|
44
|
+
return (0, rest_modules_js_1.buildRESTDescriptor)(module, metadata ?? {}, boundFetch, options);
|
|
45
|
+
}
|
|
46
|
+
else if ((0, helpers_js_1.isObject)(modules)) {
|
|
47
|
+
return Object.fromEntries(Object.entries(modules).map(([key, value]) => {
|
|
48
|
+
return [key, use(value, modules[common_js_1.PUBLIC_METADATA_KEY])];
|
|
49
|
+
}));
|
|
50
|
+
}
|
|
51
|
+
else {
|
|
52
|
+
return modules;
|
|
53
|
+
}
|
|
54
|
+
};
|
|
55
|
+
const setHeaders = (headers) => {
|
|
56
|
+
for (const k in headers) {
|
|
57
|
+
_headers[k] = headers[k];
|
|
58
|
+
}
|
|
59
|
+
};
|
|
60
|
+
const wrappedModules = config.modules ? use(config.modules) : {};
|
|
61
|
+
return {
|
|
62
|
+
...wrappedModules,
|
|
63
|
+
auth: authStrategy,
|
|
64
|
+
setHeaders,
|
|
65
|
+
use,
|
|
66
|
+
fetch: (relativeUrl, options) => {
|
|
67
|
+
const finalUrl = new URL(relativeUrl, `https://${common_js_1.API_URL}`);
|
|
68
|
+
finalUrl.host = common_js_1.API_URL;
|
|
69
|
+
finalUrl.protocol = 'https';
|
|
70
|
+
return boundFetch(finalUrl.toString(), options);
|
|
71
|
+
},
|
|
72
|
+
async graphql(query, variables, opts = {
|
|
73
|
+
apiVersion: 'alpha',
|
|
74
|
+
}) {
|
|
75
|
+
const res = await boundFetch(`https://${common_js_1.API_URL}/graphql/${opts.apiVersion}`, {
|
|
76
|
+
method: 'POST',
|
|
77
|
+
headers: {
|
|
78
|
+
'Content-Type': 'application/json',
|
|
79
|
+
},
|
|
80
|
+
body: JSON.stringify({ query, variables }),
|
|
81
|
+
});
|
|
82
|
+
if (res.status !== 200) {
|
|
83
|
+
throw new fetch_error_js_1.FetchErrorResponse(`GraphQL request failed with status ${res.status}`, res);
|
|
84
|
+
}
|
|
85
|
+
const { data, errors } = await res.json();
|
|
86
|
+
return { data: data ?? {}, errors };
|
|
87
|
+
},
|
|
88
|
+
};
|
|
89
|
+
}
|
|
90
|
+
exports.createClient = createClient;
|