@wix/sdk 1.9.5 → 1.9.7
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/oauth2/OAuthStrategy.js +19 -1
- package/build/auth/oauth2/types.d.ts +1 -0
- package/build/event-handlers-modules.js +12 -10
- package/build/service-plugin-modules.d.ts +2 -9
- package/build/service-plugin-modules.js +3 -32
- package/build/wixClient.d.ts +12 -1
- package/build/wixClient.js +42 -1
- package/cjs/build/auth/oauth2/OAuthStrategy.js +19 -1
- package/cjs/build/auth/oauth2/types.d.ts +1 -0
- package/cjs/build/event-handlers-modules.js +12 -10
- package/cjs/build/service-plugin-modules.d.ts +2 -9
- package/cjs/build/service-plugin-modules.js +3 -32
- package/cjs/build/wixClient.d.ts +12 -1
- package/cjs/build/wixClient.js +42 -1
- package/package.json +4 -4
|
@@ -319,6 +319,22 @@ export function OAuthStrategy(config) {
|
|
|
319
319
|
const loggedIn = () => {
|
|
320
320
|
return _tokens.refreshToken.role === TokenRole.MEMBER;
|
|
321
321
|
};
|
|
322
|
+
const getMemberTokensForExternalLogin = async (memberId, apiKey) => {
|
|
323
|
+
const tokensResponse = await fetchTokens({
|
|
324
|
+
grant_type: 'authorized_client',
|
|
325
|
+
scope: 'offline_access',
|
|
326
|
+
member_id: memberId,
|
|
327
|
+
}, {
|
|
328
|
+
Authorization: _tokens.accessToken.value + ',' + apiKey,
|
|
329
|
+
});
|
|
330
|
+
return {
|
|
331
|
+
accessToken: createAccessToken(tokensResponse.access_token, tokensResponse.expires_in),
|
|
332
|
+
refreshToken: {
|
|
333
|
+
value: tokensResponse.refresh_token,
|
|
334
|
+
role: TokenRole.MEMBER,
|
|
335
|
+
},
|
|
336
|
+
};
|
|
337
|
+
};
|
|
322
338
|
return {
|
|
323
339
|
generateVisitorTokens,
|
|
324
340
|
renewToken,
|
|
@@ -335,12 +351,13 @@ export function OAuthStrategy(config) {
|
|
|
335
351
|
processVerification,
|
|
336
352
|
login,
|
|
337
353
|
getMemberTokensForDirectLogin,
|
|
354
|
+
getMemberTokensForExternalLogin,
|
|
338
355
|
sendPasswordResetEmail,
|
|
339
356
|
captchaInvisibleSiteKey: '6LdoPaUfAAAAAJphvHoUoOob7mx0KDlXyXlgrx5v',
|
|
340
357
|
captchaVisibleSiteKey: '6Ld0J8IcAAAAANyrnxzrRlX1xrrdXsOmsepUYosy',
|
|
341
358
|
};
|
|
342
359
|
}
|
|
343
|
-
const fetchTokens = async (payload) => {
|
|
360
|
+
const fetchTokens = async (payload, headers = {}) => {
|
|
344
361
|
const res = await fetch(`https://${API_URL}/oauth2/token`, {
|
|
345
362
|
method: 'POST',
|
|
346
363
|
body: JSON.stringify(payload),
|
|
@@ -351,6 +368,7 @@ const fetchTokens = async (payload) => {
|
|
|
351
368
|
packageName: '@wix/sdk',
|
|
352
369
|
}),
|
|
353
370
|
'Content-Type': 'application/json',
|
|
371
|
+
...headers,
|
|
354
372
|
},
|
|
355
373
|
});
|
|
356
374
|
if (res.status !== 200) {
|
|
@@ -66,6 +66,7 @@ export interface IOAuthStrategy extends AuthenticationStrategy {
|
|
|
66
66
|
captchaInvisibleSiteKey: string;
|
|
67
67
|
captchaVisibleSiteKey: string;
|
|
68
68
|
loggedIn: () => boolean;
|
|
69
|
+
getMemberTokensForExternalLogin: (memberId: string, apiKey: string) => Promise<Tokens>;
|
|
69
70
|
}
|
|
70
71
|
export declare enum LoginState {
|
|
71
72
|
SUCCESS = "SUCCESS",
|
|
@@ -6,6 +6,7 @@ export function buildEventDefinition(eventDefinition, registerHandler) {
|
|
|
6
6
|
};
|
|
7
7
|
}
|
|
8
8
|
export function runHandler(eventDefinition, handler, payload, baseEventMetadata) {
|
|
9
|
+
let envelope;
|
|
9
10
|
if (eventDefinition.isDomainEvent) {
|
|
10
11
|
const domainEventPayload = payload;
|
|
11
12
|
const { deletedEvent, actionEvent, createdEvent, updatedEvent, ...domainEventMetadata } = domainEventPayload;
|
|
@@ -14,26 +15,27 @@ export function runHandler(eventDefinition, handler, payload, baseEventMetadata)
|
|
|
14
15
|
...domainEventMetadata,
|
|
15
16
|
};
|
|
16
17
|
if (deletedEvent) {
|
|
17
|
-
|
|
18
|
+
envelope = { metadata };
|
|
18
19
|
}
|
|
19
20
|
else if (actionEvent) {
|
|
20
|
-
|
|
21
|
-
data:
|
|
21
|
+
envelope = {
|
|
22
|
+
data: actionEvent.body,
|
|
22
23
|
metadata,
|
|
23
|
-
}
|
|
24
|
+
};
|
|
24
25
|
}
|
|
25
26
|
else {
|
|
26
|
-
|
|
27
|
-
entity:
|
|
28
|
-
domainEventPayload.updatedEvent?.currentEntity,
|
|
27
|
+
envelope = {
|
|
28
|
+
entity: createdEvent?.entity ?? updatedEvent?.currentEntity,
|
|
29
29
|
metadata,
|
|
30
|
-
}
|
|
30
|
+
};
|
|
31
31
|
}
|
|
32
32
|
}
|
|
33
33
|
else {
|
|
34
|
-
|
|
34
|
+
envelope = {
|
|
35
35
|
data: payload,
|
|
36
36
|
metadata: baseEventMetadata,
|
|
37
|
-
}
|
|
37
|
+
};
|
|
38
38
|
}
|
|
39
|
+
const transformFromRESTFn = eventDefinition.transformations ?? ((x) => x);
|
|
40
|
+
return handler(transformFromRESTFn(envelope));
|
|
39
41
|
}
|
|
@@ -1,15 +1,8 @@
|
|
|
1
1
|
import { ServicePluginContract, ServicePluginDefinition } from '@wix/sdk-types';
|
|
2
2
|
export declare const isServicePluginModule: (val: any) => val is ServicePluginDefinition<ServicePluginContract>;
|
|
3
|
-
export declare function buildServicePluginDefinition<T extends ServicePluginDefinition<any>>(servicePluginDefinition: T, decodeJWT?: (token: string, verifyCallerClaims?: boolean) => Promise<{
|
|
3
|
+
export declare function buildServicePluginDefinition<T extends ServicePluginDefinition<any>>(servicePluginDefinition: T, registrServicePluginImplementation: (servicePluginDefinition: T, implementation: T['__contract']) => void, decodeJWT?: (token: string, verifyCallerClaims?: boolean) => Promise<{
|
|
4
4
|
decoded: {
|
|
5
5
|
data: string;
|
|
6
6
|
};
|
|
7
7
|
valid: boolean;
|
|
8
|
-
}>):
|
|
9
|
-
provide(implementation: T['__contract']): void;
|
|
10
|
-
processRequest(request: Request): Promise<Response>;
|
|
11
|
-
process: (request: {
|
|
12
|
-
url: string;
|
|
13
|
-
body: string;
|
|
14
|
-
}) => Promise<any>;
|
|
15
|
-
};
|
|
8
|
+
}>): (implementation: T['__contract']) => void;
|
|
@@ -1,36 +1,7 @@
|
|
|
1
1
|
import { isObject } from './helpers.js';
|
|
2
2
|
export const isServicePluginModule = (val) => isObject(val) && val.__type === 'service-plugin-definition';
|
|
3
|
-
export function buildServicePluginDefinition(servicePluginDefinition, decodeJWT) {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
if (!decodeJWT) {
|
|
7
|
-
throw new Error('decodeJWT is not supported by the authentication strategy');
|
|
8
|
-
}
|
|
9
|
-
const { decoded, valid } = await decodeJWT(request.body, true);
|
|
10
|
-
if (!valid) {
|
|
11
|
-
throw new Error('JWT is not valid');
|
|
12
|
-
}
|
|
13
|
-
const method = servicePluginDefinition.methods.find((m) => request.url.endsWith(m.primaryHttpMappingPath));
|
|
14
|
-
if (!method) {
|
|
15
|
-
throw new Error('Unexpect request: request url did not match any method: ' +
|
|
16
|
-
request.url);
|
|
17
|
-
}
|
|
18
|
-
const implMethod = impl[method.name];
|
|
19
|
-
if (!implMethod) {
|
|
20
|
-
throw new Error(`Got request for service plugin method ${method.name} but no implementation was provided. Available methods: ${Object.keys(impl).join(', ')}`);
|
|
21
|
-
}
|
|
22
|
-
return implMethod(method.transformations.fromREST(decoded.data));
|
|
23
|
-
}
|
|
24
|
-
return {
|
|
25
|
-
provide(implementation) {
|
|
26
|
-
impl = implementation;
|
|
27
|
-
},
|
|
28
|
-
async processRequest(request) {
|
|
29
|
-
const url = request.url;
|
|
30
|
-
const body = await request.text();
|
|
31
|
-
const implMethodResult = await process({ url, body });
|
|
32
|
-
return Response.json(implMethodResult);
|
|
33
|
-
},
|
|
34
|
-
process,
|
|
3
|
+
export function buildServicePluginDefinition(servicePluginDefinition, registrServicePluginImplementation, decodeJWT) {
|
|
4
|
+
return (implementation) => {
|
|
5
|
+
registrServicePluginImplementation(servicePluginDefinition, implementation);
|
|
35
6
|
};
|
|
36
7
|
}
|
package/build/wixClient.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { AuthenticationStrategy, BoundAuthenticationStrategy, BuildEventDefinition, BuildRESTFunction, EventDefinition, EventIdentity, Host, HostModule, HostModuleAPI, RESTFunctionDescriptor, ServicePluginDefinition, BuildServicePluginDefinition } from '@wix/sdk-types';
|
|
1
|
+
import { AuthenticationStrategy, BoundAuthenticationStrategy, BuildEventDefinition, BuildRESTFunction, EventDefinition, EventIdentity, Host, HostModule, HostModuleAPI, RESTFunctionDescriptor, ServicePluginDefinition, BuildServicePluginDefinition, ServicePluginContract } from '@wix/sdk-types';
|
|
2
2
|
import { ConditionalExcept } from 'type-fest/source/conditional-except.js';
|
|
3
3
|
import { EmptyObject } from 'type-fest/source/empty-object.js';
|
|
4
4
|
import { AmbassadorFunctionDescriptor, BuildAmbassadorFunction } from './ambassador-modules.js';
|
|
@@ -77,6 +77,17 @@ export type WixClient<H extends Host<any> | undefined = undefined, Z extends Aut
|
|
|
77
77
|
}, 'AppRemoved'>;
|
|
78
78
|
};
|
|
79
79
|
};
|
|
80
|
+
servicePlugins: {
|
|
81
|
+
getRegisteredServicePlugins(): Map<string, {
|
|
82
|
+
servicePluginDefinition: ServicePluginDefinition<any>;
|
|
83
|
+
implementation: ServicePluginContract;
|
|
84
|
+
}[]>;
|
|
85
|
+
process(request: {
|
|
86
|
+
url: string;
|
|
87
|
+
body: string;
|
|
88
|
+
}, componentType: string): Promise<unknown>;
|
|
89
|
+
processRequest(request: Request, componentType: string): Promise<Response>;
|
|
90
|
+
};
|
|
80
91
|
} & BuildDescriptors<T, H>;
|
|
81
92
|
type ResolvePossibleEvents<T extends EventDefinition<any>[]> = {
|
|
82
93
|
[K in keyof T]: T[K] extends EventDefinition<any> ? {
|
package/build/wixClient.js
CHANGED
|
@@ -10,6 +10,7 @@ import { buildServicePluginDefinition, isServicePluginModule, } from './service-
|
|
|
10
10
|
export function createClient(config) {
|
|
11
11
|
const _headers = config.headers || { Authorization: '' };
|
|
12
12
|
const eventHandlers = new Map();
|
|
13
|
+
const servicePluginsImplementations = new Map();
|
|
13
14
|
const authStrategy = config.auth ||
|
|
14
15
|
{
|
|
15
16
|
getAuthHeaders: (_) => Promise.resolve({ headers: {} }),
|
|
@@ -41,7 +42,11 @@ export function createClient(config) {
|
|
|
41
42
|
});
|
|
42
43
|
}
|
|
43
44
|
else if (isServicePluginModule(modules)) {
|
|
44
|
-
return buildServicePluginDefinition(modules,
|
|
45
|
+
return buildServicePluginDefinition(modules, (servicePluginDefinition, implementation) => {
|
|
46
|
+
const implementations = servicePluginsImplementations.get(servicePluginDefinition.componentType) ?? [];
|
|
47
|
+
implementations.push({ servicePluginDefinition, implementation });
|
|
48
|
+
servicePluginsImplementations.set(servicePluginDefinition.componentType, implementations);
|
|
49
|
+
});
|
|
45
50
|
}
|
|
46
51
|
else if (isHostModule(modules) && config.host) {
|
|
47
52
|
return buildHostModule(modules, config.host);
|
|
@@ -172,5 +177,41 @@ export function createClient(config) {
|
|
|
172
177
|
AppRemoved: EventDefinition('AppRemoved')(),
|
|
173
178
|
},
|
|
174
179
|
},
|
|
180
|
+
servicePlugins: {
|
|
181
|
+
getRegisteredServicePlugins: () => servicePluginsImplementations,
|
|
182
|
+
async process(request, componentType) {
|
|
183
|
+
if (!authStrategy.decodeJWT) {
|
|
184
|
+
throw new Error('decodeJWT is not supported by the authentication strategy');
|
|
185
|
+
}
|
|
186
|
+
const { decoded, valid } = await authStrategy.decodeJWT(request.body, true);
|
|
187
|
+
if (!valid) {
|
|
188
|
+
throw new Error('JWT is not valid');
|
|
189
|
+
}
|
|
190
|
+
const implementations = servicePluginsImplementations.get(componentType) ?? [];
|
|
191
|
+
if (implementations.length === 0) {
|
|
192
|
+
throw new Error(`No service plugin implementations found for component type ${componentType}`);
|
|
193
|
+
}
|
|
194
|
+
else if (implementations.length > 1) {
|
|
195
|
+
throw new Error(`Multiple service plugin implementations found for component type ${componentType}. This is currently not supported`);
|
|
196
|
+
}
|
|
197
|
+
const { implementation: impl, servicePluginDefinition } = implementations[0];
|
|
198
|
+
const method = servicePluginDefinition.methods.find((m) => request.url.endsWith(m.primaryHttpMappingPath));
|
|
199
|
+
if (!method) {
|
|
200
|
+
throw new Error('Unexpect request: request url did not match any method: ' +
|
|
201
|
+
request.url);
|
|
202
|
+
}
|
|
203
|
+
const implMethod = impl[method.name];
|
|
204
|
+
if (!implMethod) {
|
|
205
|
+
throw new Error(`Got request for service plugin method ${method.name} but no implementation was provided. Available methods: ${Object.keys(impl).join(', ')}`);
|
|
206
|
+
}
|
|
207
|
+
return implMethod(method.transformations.fromREST(decoded.data));
|
|
208
|
+
},
|
|
209
|
+
async processRequest(request, componentType) {
|
|
210
|
+
const url = request.url;
|
|
211
|
+
const body = await request.text();
|
|
212
|
+
const implMethodResult = await this.process({ url, body }, componentType);
|
|
213
|
+
return Response.json(implMethodResult);
|
|
214
|
+
},
|
|
215
|
+
},
|
|
175
216
|
};
|
|
176
217
|
}
|
|
@@ -322,6 +322,22 @@ function OAuthStrategy(config) {
|
|
|
322
322
|
const loggedIn = () => {
|
|
323
323
|
return _tokens.refreshToken.role === types_js_1.TokenRole.MEMBER;
|
|
324
324
|
};
|
|
325
|
+
const getMemberTokensForExternalLogin = async (memberId, apiKey) => {
|
|
326
|
+
const tokensResponse = await fetchTokens({
|
|
327
|
+
grant_type: 'authorized_client',
|
|
328
|
+
scope: 'offline_access',
|
|
329
|
+
member_id: memberId,
|
|
330
|
+
}, {
|
|
331
|
+
Authorization: _tokens.accessToken.value + ',' + apiKey,
|
|
332
|
+
});
|
|
333
|
+
return {
|
|
334
|
+
accessToken: (0, tokenHelpers_js_1.createAccessToken)(tokensResponse.access_token, tokensResponse.expires_in),
|
|
335
|
+
refreshToken: {
|
|
336
|
+
value: tokensResponse.refresh_token,
|
|
337
|
+
role: types_js_1.TokenRole.MEMBER,
|
|
338
|
+
},
|
|
339
|
+
};
|
|
340
|
+
};
|
|
325
341
|
return {
|
|
326
342
|
generateVisitorTokens,
|
|
327
343
|
renewToken,
|
|
@@ -338,13 +354,14 @@ function OAuthStrategy(config) {
|
|
|
338
354
|
processVerification,
|
|
339
355
|
login,
|
|
340
356
|
getMemberTokensForDirectLogin,
|
|
357
|
+
getMemberTokensForExternalLogin,
|
|
341
358
|
sendPasswordResetEmail,
|
|
342
359
|
captchaInvisibleSiteKey: '6LdoPaUfAAAAAJphvHoUoOob7mx0KDlXyXlgrx5v',
|
|
343
360
|
captchaVisibleSiteKey: '6Ld0J8IcAAAAANyrnxzrRlX1xrrdXsOmsepUYosy',
|
|
344
361
|
};
|
|
345
362
|
}
|
|
346
363
|
exports.OAuthStrategy = OAuthStrategy;
|
|
347
|
-
const fetchTokens = async (payload) => {
|
|
364
|
+
const fetchTokens = async (payload, headers = {}) => {
|
|
348
365
|
const res = await fetch(`https://${common_js_1.API_URL}/oauth2/token`, {
|
|
349
366
|
method: 'POST',
|
|
350
367
|
body: JSON.stringify(payload),
|
|
@@ -355,6 +372,7 @@ const fetchTokens = async (payload) => {
|
|
|
355
372
|
packageName: '@wix/sdk',
|
|
356
373
|
}),
|
|
357
374
|
'Content-Type': 'application/json',
|
|
375
|
+
...headers,
|
|
358
376
|
},
|
|
359
377
|
});
|
|
360
378
|
if (res.status !== 200) {
|
|
@@ -66,6 +66,7 @@ export interface IOAuthStrategy extends AuthenticationStrategy {
|
|
|
66
66
|
captchaInvisibleSiteKey: string;
|
|
67
67
|
captchaVisibleSiteKey: string;
|
|
68
68
|
loggedIn: () => boolean;
|
|
69
|
+
getMemberTokensForExternalLogin: (memberId: string, apiKey: string) => Promise<Tokens>;
|
|
69
70
|
}
|
|
70
71
|
export declare enum LoginState {
|
|
71
72
|
SUCCESS = "SUCCESS",
|
|
@@ -11,6 +11,7 @@ function buildEventDefinition(eventDefinition, registerHandler) {
|
|
|
11
11
|
}
|
|
12
12
|
exports.buildEventDefinition = buildEventDefinition;
|
|
13
13
|
function runHandler(eventDefinition, handler, payload, baseEventMetadata) {
|
|
14
|
+
let envelope;
|
|
14
15
|
if (eventDefinition.isDomainEvent) {
|
|
15
16
|
const domainEventPayload = payload;
|
|
16
17
|
const { deletedEvent, actionEvent, createdEvent, updatedEvent, ...domainEventMetadata } = domainEventPayload;
|
|
@@ -19,27 +20,28 @@ function runHandler(eventDefinition, handler, payload, baseEventMetadata) {
|
|
|
19
20
|
...domainEventMetadata,
|
|
20
21
|
};
|
|
21
22
|
if (deletedEvent) {
|
|
22
|
-
|
|
23
|
+
envelope = { metadata };
|
|
23
24
|
}
|
|
24
25
|
else if (actionEvent) {
|
|
25
|
-
|
|
26
|
-
data:
|
|
26
|
+
envelope = {
|
|
27
|
+
data: actionEvent.body,
|
|
27
28
|
metadata,
|
|
28
|
-
}
|
|
29
|
+
};
|
|
29
30
|
}
|
|
30
31
|
else {
|
|
31
|
-
|
|
32
|
-
entity:
|
|
33
|
-
domainEventPayload.updatedEvent?.currentEntity,
|
|
32
|
+
envelope = {
|
|
33
|
+
entity: createdEvent?.entity ?? updatedEvent?.currentEntity,
|
|
34
34
|
metadata,
|
|
35
|
-
}
|
|
35
|
+
};
|
|
36
36
|
}
|
|
37
37
|
}
|
|
38
38
|
else {
|
|
39
|
-
|
|
39
|
+
envelope = {
|
|
40
40
|
data: payload,
|
|
41
41
|
metadata: baseEventMetadata,
|
|
42
|
-
}
|
|
42
|
+
};
|
|
43
43
|
}
|
|
44
|
+
const transformFromRESTFn = eventDefinition.transformations ?? ((x) => x);
|
|
45
|
+
return handler(transformFromRESTFn(envelope));
|
|
44
46
|
}
|
|
45
47
|
exports.runHandler = runHandler;
|
|
@@ -1,15 +1,8 @@
|
|
|
1
1
|
import { ServicePluginContract, ServicePluginDefinition } from '@wix/sdk-types';
|
|
2
2
|
export declare const isServicePluginModule: (val: any) => val is ServicePluginDefinition<ServicePluginContract>;
|
|
3
|
-
export declare function buildServicePluginDefinition<T extends ServicePluginDefinition<any>>(servicePluginDefinition: T, decodeJWT?: (token: string, verifyCallerClaims?: boolean) => Promise<{
|
|
3
|
+
export declare function buildServicePluginDefinition<T extends ServicePluginDefinition<any>>(servicePluginDefinition: T, registrServicePluginImplementation: (servicePluginDefinition: T, implementation: T['__contract']) => void, decodeJWT?: (token: string, verifyCallerClaims?: boolean) => Promise<{
|
|
4
4
|
decoded: {
|
|
5
5
|
data: string;
|
|
6
6
|
};
|
|
7
7
|
valid: boolean;
|
|
8
|
-
}>):
|
|
9
|
-
provide(implementation: T['__contract']): void;
|
|
10
|
-
processRequest(request: Request): Promise<Response>;
|
|
11
|
-
process: (request: {
|
|
12
|
-
url: string;
|
|
13
|
-
body: string;
|
|
14
|
-
}) => Promise<any>;
|
|
15
|
-
};
|
|
8
|
+
}>): (implementation: T['__contract']) => void;
|
|
@@ -4,38 +4,9 @@ exports.buildServicePluginDefinition = exports.isServicePluginModule = void 0;
|
|
|
4
4
|
const helpers_js_1 = require("./helpers.js");
|
|
5
5
|
const isServicePluginModule = (val) => (0, helpers_js_1.isObject)(val) && val.__type === 'service-plugin-definition';
|
|
6
6
|
exports.isServicePluginModule = isServicePluginModule;
|
|
7
|
-
function buildServicePluginDefinition(servicePluginDefinition, decodeJWT) {
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
if (!decodeJWT) {
|
|
11
|
-
throw new Error('decodeJWT is not supported by the authentication strategy');
|
|
12
|
-
}
|
|
13
|
-
const { decoded, valid } = await decodeJWT(request.body, true);
|
|
14
|
-
if (!valid) {
|
|
15
|
-
throw new Error('JWT is not valid');
|
|
16
|
-
}
|
|
17
|
-
const method = servicePluginDefinition.methods.find((m) => request.url.endsWith(m.primaryHttpMappingPath));
|
|
18
|
-
if (!method) {
|
|
19
|
-
throw new Error('Unexpect request: request url did not match any method: ' +
|
|
20
|
-
request.url);
|
|
21
|
-
}
|
|
22
|
-
const implMethod = impl[method.name];
|
|
23
|
-
if (!implMethod) {
|
|
24
|
-
throw new Error(`Got request for service plugin method ${method.name} but no implementation was provided. Available methods: ${Object.keys(impl).join(', ')}`);
|
|
25
|
-
}
|
|
26
|
-
return implMethod(method.transformations.fromREST(decoded.data));
|
|
27
|
-
}
|
|
28
|
-
return {
|
|
29
|
-
provide(implementation) {
|
|
30
|
-
impl = implementation;
|
|
31
|
-
},
|
|
32
|
-
async processRequest(request) {
|
|
33
|
-
const url = request.url;
|
|
34
|
-
const body = await request.text();
|
|
35
|
-
const implMethodResult = await process({ url, body });
|
|
36
|
-
return Response.json(implMethodResult);
|
|
37
|
-
},
|
|
38
|
-
process,
|
|
7
|
+
function buildServicePluginDefinition(servicePluginDefinition, registrServicePluginImplementation, decodeJWT) {
|
|
8
|
+
return (implementation) => {
|
|
9
|
+
registrServicePluginImplementation(servicePluginDefinition, implementation);
|
|
39
10
|
};
|
|
40
11
|
}
|
|
41
12
|
exports.buildServicePluginDefinition = buildServicePluginDefinition;
|
package/cjs/build/wixClient.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { AuthenticationStrategy, BoundAuthenticationStrategy, BuildEventDefinition, BuildRESTFunction, EventDefinition, EventIdentity, Host, HostModule, HostModuleAPI, RESTFunctionDescriptor, ServicePluginDefinition, BuildServicePluginDefinition } from '@wix/sdk-types';
|
|
1
|
+
import { AuthenticationStrategy, BoundAuthenticationStrategy, BuildEventDefinition, BuildRESTFunction, EventDefinition, EventIdentity, Host, HostModule, HostModuleAPI, RESTFunctionDescriptor, ServicePluginDefinition, BuildServicePluginDefinition, ServicePluginContract } from '@wix/sdk-types';
|
|
2
2
|
import { ConditionalExcept } from 'type-fest/source/conditional-except.js';
|
|
3
3
|
import { EmptyObject } from 'type-fest/source/empty-object.js';
|
|
4
4
|
import { AmbassadorFunctionDescriptor, BuildAmbassadorFunction } from './ambassador-modules.js';
|
|
@@ -77,6 +77,17 @@ export type WixClient<H extends Host<any> | undefined = undefined, Z extends Aut
|
|
|
77
77
|
}, 'AppRemoved'>;
|
|
78
78
|
};
|
|
79
79
|
};
|
|
80
|
+
servicePlugins: {
|
|
81
|
+
getRegisteredServicePlugins(): Map<string, {
|
|
82
|
+
servicePluginDefinition: ServicePluginDefinition<any>;
|
|
83
|
+
implementation: ServicePluginContract;
|
|
84
|
+
}[]>;
|
|
85
|
+
process(request: {
|
|
86
|
+
url: string;
|
|
87
|
+
body: string;
|
|
88
|
+
}, componentType: string): Promise<unknown>;
|
|
89
|
+
processRequest(request: Request, componentType: string): Promise<Response>;
|
|
90
|
+
};
|
|
80
91
|
} & BuildDescriptors<T, H>;
|
|
81
92
|
type ResolvePossibleEvents<T extends EventDefinition<any>[]> = {
|
|
82
93
|
[K in keyof T]: T[K] extends EventDefinition<any> ? {
|
package/cjs/build/wixClient.js
CHANGED
|
@@ -13,6 +13,7 @@ const service_plugin_modules_js_1 = require("./service-plugin-modules.js");
|
|
|
13
13
|
function createClient(config) {
|
|
14
14
|
const _headers = config.headers || { Authorization: '' };
|
|
15
15
|
const eventHandlers = new Map();
|
|
16
|
+
const servicePluginsImplementations = new Map();
|
|
16
17
|
const authStrategy = config.auth ||
|
|
17
18
|
{
|
|
18
19
|
getAuthHeaders: (_) => Promise.resolve({ headers: {} }),
|
|
@@ -44,7 +45,11 @@ function createClient(config) {
|
|
|
44
45
|
});
|
|
45
46
|
}
|
|
46
47
|
else if ((0, service_plugin_modules_js_1.isServicePluginModule)(modules)) {
|
|
47
|
-
return (0, service_plugin_modules_js_1.buildServicePluginDefinition)(modules,
|
|
48
|
+
return (0, service_plugin_modules_js_1.buildServicePluginDefinition)(modules, (servicePluginDefinition, implementation) => {
|
|
49
|
+
const implementations = servicePluginsImplementations.get(servicePluginDefinition.componentType) ?? [];
|
|
50
|
+
implementations.push({ servicePluginDefinition, implementation });
|
|
51
|
+
servicePluginsImplementations.set(servicePluginDefinition.componentType, implementations);
|
|
52
|
+
});
|
|
48
53
|
}
|
|
49
54
|
else if ((0, host_modules_js_1.isHostModule)(modules) && config.host) {
|
|
50
55
|
return (0, host_modules_js_1.buildHostModule)(modules, config.host);
|
|
@@ -175,6 +180,42 @@ function createClient(config) {
|
|
|
175
180
|
AppRemoved: (0, sdk_types_1.EventDefinition)('AppRemoved')(),
|
|
176
181
|
},
|
|
177
182
|
},
|
|
183
|
+
servicePlugins: {
|
|
184
|
+
getRegisteredServicePlugins: () => servicePluginsImplementations,
|
|
185
|
+
async process(request, componentType) {
|
|
186
|
+
if (!authStrategy.decodeJWT) {
|
|
187
|
+
throw new Error('decodeJWT is not supported by the authentication strategy');
|
|
188
|
+
}
|
|
189
|
+
const { decoded, valid } = await authStrategy.decodeJWT(request.body, true);
|
|
190
|
+
if (!valid) {
|
|
191
|
+
throw new Error('JWT is not valid');
|
|
192
|
+
}
|
|
193
|
+
const implementations = servicePluginsImplementations.get(componentType) ?? [];
|
|
194
|
+
if (implementations.length === 0) {
|
|
195
|
+
throw new Error(`No service plugin implementations found for component type ${componentType}`);
|
|
196
|
+
}
|
|
197
|
+
else if (implementations.length > 1) {
|
|
198
|
+
throw new Error(`Multiple service plugin implementations found for component type ${componentType}. This is currently not supported`);
|
|
199
|
+
}
|
|
200
|
+
const { implementation: impl, servicePluginDefinition } = implementations[0];
|
|
201
|
+
const method = servicePluginDefinition.methods.find((m) => request.url.endsWith(m.primaryHttpMappingPath));
|
|
202
|
+
if (!method) {
|
|
203
|
+
throw new Error('Unexpect request: request url did not match any method: ' +
|
|
204
|
+
request.url);
|
|
205
|
+
}
|
|
206
|
+
const implMethod = impl[method.name];
|
|
207
|
+
if (!implMethod) {
|
|
208
|
+
throw new Error(`Got request for service plugin method ${method.name} but no implementation was provided. Available methods: ${Object.keys(impl).join(', ')}`);
|
|
209
|
+
}
|
|
210
|
+
return implMethod(method.transformations.fromREST(decoded.data));
|
|
211
|
+
},
|
|
212
|
+
async processRequest(request, componentType) {
|
|
213
|
+
const url = request.url;
|
|
214
|
+
const body = await request.text();
|
|
215
|
+
const implMethodResult = await this.process({ url, body }, componentType);
|
|
216
|
+
return Response.json(implMethodResult);
|
|
217
|
+
},
|
|
218
|
+
},
|
|
178
219
|
};
|
|
179
220
|
}
|
|
180
221
|
exports.createClient = createClient;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@wix/sdk",
|
|
3
|
-
"version": "1.9.
|
|
3
|
+
"version": "1.9.7",
|
|
4
4
|
"license": "UNLICENSED",
|
|
5
5
|
"author": {
|
|
6
6
|
"name": "Ronny Ringel",
|
|
@@ -67,7 +67,7 @@
|
|
|
67
67
|
"@wix/identity": "^1.0.78",
|
|
68
68
|
"@wix/image-kit": "^1.68.0",
|
|
69
69
|
"@wix/redirects": "^1.0.41",
|
|
70
|
-
"@wix/sdk-types": "^1.7.
|
|
70
|
+
"@wix/sdk-types": "^1.7.2",
|
|
71
71
|
"crypto-js": "^4.2.0",
|
|
72
72
|
"jose": "^5.2.1",
|
|
73
73
|
"pkce-challenge": "^3.1.0",
|
|
@@ -86,7 +86,7 @@
|
|
|
86
86
|
"@wix/events": "^1.0.179",
|
|
87
87
|
"@wix/metro": "^1.0.73",
|
|
88
88
|
"@wix/metro-runtime": "^1.1677.0",
|
|
89
|
-
"@wix/sdk-runtime": "0.2.
|
|
89
|
+
"@wix/sdk-runtime": "0.2.12",
|
|
90
90
|
"eslint": "^8.56.0",
|
|
91
91
|
"eslint-config-sdk": "0.0.0",
|
|
92
92
|
"graphql": "^16.8.0",
|
|
@@ -120,5 +120,5 @@
|
|
|
120
120
|
"wallaby": {
|
|
121
121
|
"autoDetect": true
|
|
122
122
|
},
|
|
123
|
-
"falconPackageHash": "
|
|
123
|
+
"falconPackageHash": "d4dbbb6460aeb154746786c06a55f71816e5fd35e26dbe365fbe69aa"
|
|
124
124
|
}
|