@wix/sdk 1.9.2 → 1.9.4

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.
@@ -0,0 +1,15 @@
1
+ import { ServicePluginContract, ServicePluginDefinition } from '@wix/sdk-types';
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<{
4
+ decoded: {
5
+ data: string;
6
+ };
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
+ };
@@ -0,0 +1,36 @@
1
+ import { isObject } from './helpers.js';
2
+ export const isServicePluginModule = (val) => isObject(val) && val.__type === 'service-plugin-definition';
3
+ export function buildServicePluginDefinition(servicePluginDefinition, decodeJWT) {
4
+ let impl;
5
+ async function process(request) {
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,
35
+ };
36
+ }
@@ -1,4 +1,4 @@
1
- import { AuthenticationStrategy, BoundAuthenticationStrategy, BuildEventDefinition, BuildRESTFunction, EventDefinition, EventIdentity, Host, HostModule, HostModuleAPI, RESTFunctionDescriptor, SPIDefinition } from '@wix/sdk-types';
1
+ import { AuthenticationStrategy, BoundAuthenticationStrategy, BuildEventDefinition, BuildRESTFunction, EventDefinition, EventIdentity, Host, HostModule, HostModuleAPI, RESTFunctionDescriptor, ServicePluginDefinition, BuildServicePluginDefinition } 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';
@@ -11,19 +11,12 @@ type Headers = Record<string, string>;
11
11
  * Any non-descriptor properties are removed from the returned object, including descriptors that
12
12
  * do not match the given host (as they will not work with the given host).
13
13
  */
14
- export type BuildDescriptors<T extends Descriptors, H extends Host<any> | undefined> = BuildRESTDescriptors<T> & BuildAmbassadorDescriptors<T> & BuildEventDefinitions<T> & (H extends Host<any> ? BuildHostDescriptors<T> : {});
15
- type BuildRESTDescriptors<T extends Descriptors> = T extends RESTFunctionDescriptor ? BuildRESTFunction<T> : ConditionalExcept<{
16
- [Key in keyof T]: T[Key] extends Descriptors ? BuildRESTDescriptors<T[Key]> : never;
17
- }, EmptyObject>;
18
- type BuildAmbassadorDescriptors<T extends Descriptors> = T extends AmbassadorFunctionDescriptor ? BuildAmbassadorFunction<T> : ConditionalExcept<{
19
- [Key in keyof T]: T[Key] extends Descriptors ? BuildAmbassadorDescriptors<T[Key]> : never;
20
- }, EmptyObject>;
21
- type BuildHostDescriptors<T extends Descriptors> = T extends HostModule<any, any> ? HostModuleAPI<T> : ConditionalExcept<{
22
- [Key in keyof T]: T[Key] extends Descriptors ? BuildHostDescriptors<T[Key]> : never;
23
- }, EmptyObject>;
24
- type BuildEventDefinitions<T extends Descriptors> = T extends EventDefinition ? BuildEventDefinition<T> : ConditionalExcept<{
25
- [Key in keyof T]: T[Key] extends Descriptors ? BuildEventDefinitions<T[Key]> : never;
26
- }, EmptyObject>;
14
+ export type BuildDescriptors<T extends Descriptors, H extends Host<any> | undefined, Depth extends number = 5> = {
15
+ done: T;
16
+ recurse: T extends AmbassadorFunctionDescriptor ? BuildAmbassadorFunction<T> : T extends RESTFunctionDescriptor ? BuildRESTFunction<T> : T extends EventDefinition ? BuildEventDefinition<T> : T extends ServicePluginDefinition<any> ? BuildServicePluginDefinition<T> : T extends HostModule<any, any> ? HostModuleAPI<T> : ConditionalExcept<{
17
+ [Key in keyof T]: T[Key] extends Descriptors ? BuildDescriptors<T[Key], H, [-1, 0, 1, 2, 3, 4, 5][Depth]> : never;
18
+ }, EmptyObject>;
19
+ }[Depth extends -1 ? 'done' : 'recurse'];
27
20
  /**
28
21
  * Descriptors are objects that describe the API of a module, and the module
29
22
  * can either be a REST module or a host module.
@@ -55,7 +48,7 @@ type TypedQueryInput<Result = {
55
48
  */
56
49
  __ensureTypesOfVariablesAndResultMatching?: (variables: Variables) => Result;
57
50
  };
58
- export type WixClient<H extends Host<any> | undefined = undefined, Z extends AuthenticationStrategy<H> = AuthenticationStrategy<H>, T extends Descriptors = Descriptors> = {
51
+ export type WixClient<H extends Host<any> | undefined = undefined, Z extends AuthenticationStrategy<H> = AuthenticationStrategy<H>, T extends Descriptors = {}> = {
59
52
  setHeaders(headers: Headers): void;
60
53
  auth: Omit<Z, 'getAuthHeaders'> & BoundAuthenticationStrategy;
61
54
  withAuth: Z['withAuth'] extends undefined ? never : (...args: Parameters<NonNullable<Z['withAuth']>>) => WixClient<H, Z, T>;
@@ -67,6 +60,7 @@ export type WixClient<H extends Host<any> | undefined = undefined, Z extends Aut
67
60
  errors?: GraphQLFormattedError[];
68
61
  }>;
69
62
  webhooks: {
63
+ getRegisteredEvents(): string[];
70
64
  process<ExpectedEvents extends EventDefinition<any>[] = []>(jwt: string, opts?: {
71
65
  expectedEvents: ExpectedEvents;
72
66
  }): Promise<ProcessedEvent<ExpectedEvents>>;
@@ -83,11 +77,6 @@ export type WixClient<H extends Host<any> | undefined = undefined, Z extends Aut
83
77
  }, 'AppRemoved'>;
84
78
  };
85
79
  };
86
- spi: <S extends SPIDefinition<any, any>>() => {
87
- process(jwt: string): Promise<S['__input']>;
88
- processRequest(request: Request): Promise<S['__input']>;
89
- result(result: S['__result']): S['__result'];
90
- };
91
80
  } & BuildDescriptors<T, H>;
92
81
  type ResolvePossibleEvents<T extends EventDefinition<any>[]> = {
93
82
  [K in keyof T]: T[K] extends EventDefinition<any> ? {
@@ -6,6 +6,7 @@ import { getDefaultContentHeader, isObject } from './helpers.js';
6
6
  import { buildHostModule, isHostModule } from './host-modules.js';
7
7
  import { buildRESTDescriptor } from './rest-modules.js';
8
8
  import { buildEventDefinition, isEventHandlerModule, runHandler, } from './event-handlers-modules.js';
9
+ import { buildServicePluginDefinition, isServicePluginModule, } from './service-plugin-modules.js';
9
10
  export function createClient(config) {
10
11
  const _headers = config.headers || { Authorization: '' };
11
12
  const eventHandlers = new Map();
@@ -39,6 +40,9 @@ export function createClient(config) {
39
40
  eventHandlers.set(eventDefinition.type, handlers);
40
41
  });
41
42
  }
43
+ else if (isServicePluginModule(modules)) {
44
+ return buildServicePluginDefinition(modules, authStrategy.decodeJWT);
45
+ }
42
46
  else if (isHostModule(modules) && config.host) {
43
47
  return buildHostModule(modules, config.host);
44
48
  }
@@ -119,6 +123,7 @@ export function createClient(config) {
119
123
  return { data: data ?? {}, errors };
120
124
  },
121
125
  webhooks: {
126
+ getRegisteredEvents: () => Array.from(eventHandlers.keys()),
122
127
  process: async (jwt, opts = {
123
128
  expectedEvents: [],
124
129
  }) => {
@@ -167,26 +172,5 @@ export function createClient(config) {
167
172
  AppRemoved: EventDefinition('AppRemoved')(),
168
173
  },
169
174
  },
170
- spi() {
171
- return {
172
- async process(jwt) {
173
- if (!authStrategy.decodeJWT) {
174
- throw new Error('decodeJWT is not supported by the authentication strategy');
175
- }
176
- const { decoded, valid } = await authStrategy.decodeJWT(jwt, true);
177
- if (!valid) {
178
- throw new Error('JWT is not valid');
179
- }
180
- return decoded.data;
181
- },
182
- async processRequest(request) {
183
- const body = await request.text();
184
- return this.process(body);
185
- },
186
- result(result) {
187
- return result;
188
- },
189
- };
190
- },
191
175
  };
192
176
  }
@@ -0,0 +1,15 @@
1
+ import { ServicePluginContract, ServicePluginDefinition } from '@wix/sdk-types';
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<{
4
+ decoded: {
5
+ data: string;
6
+ };
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
+ };
@@ -0,0 +1,41 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.buildServicePluginDefinition = exports.isServicePluginModule = void 0;
4
+ const helpers_js_1 = require("./helpers.js");
5
+ const isServicePluginModule = (val) => (0, helpers_js_1.isObject)(val) && val.__type === 'service-plugin-definition';
6
+ exports.isServicePluginModule = isServicePluginModule;
7
+ function buildServicePluginDefinition(servicePluginDefinition, decodeJWT) {
8
+ let impl;
9
+ async function process(request) {
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,
39
+ };
40
+ }
41
+ exports.buildServicePluginDefinition = buildServicePluginDefinition;
@@ -1,4 +1,4 @@
1
- import { AuthenticationStrategy, BoundAuthenticationStrategy, BuildEventDefinition, BuildRESTFunction, EventDefinition, EventIdentity, Host, HostModule, HostModuleAPI, RESTFunctionDescriptor, SPIDefinition } from '@wix/sdk-types';
1
+ import { AuthenticationStrategy, BoundAuthenticationStrategy, BuildEventDefinition, BuildRESTFunction, EventDefinition, EventIdentity, Host, HostModule, HostModuleAPI, RESTFunctionDescriptor, ServicePluginDefinition, BuildServicePluginDefinition } 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';
@@ -11,19 +11,12 @@ type Headers = Record<string, string>;
11
11
  * Any non-descriptor properties are removed from the returned object, including descriptors that
12
12
  * do not match the given host (as they will not work with the given host).
13
13
  */
14
- export type BuildDescriptors<T extends Descriptors, H extends Host<any> | undefined> = BuildRESTDescriptors<T> & BuildAmbassadorDescriptors<T> & BuildEventDefinitions<T> & (H extends Host<any> ? BuildHostDescriptors<T> : {});
15
- type BuildRESTDescriptors<T extends Descriptors> = T extends RESTFunctionDescriptor ? BuildRESTFunction<T> : ConditionalExcept<{
16
- [Key in keyof T]: T[Key] extends Descriptors ? BuildRESTDescriptors<T[Key]> : never;
17
- }, EmptyObject>;
18
- type BuildAmbassadorDescriptors<T extends Descriptors> = T extends AmbassadorFunctionDescriptor ? BuildAmbassadorFunction<T> : ConditionalExcept<{
19
- [Key in keyof T]: T[Key] extends Descriptors ? BuildAmbassadorDescriptors<T[Key]> : never;
20
- }, EmptyObject>;
21
- type BuildHostDescriptors<T extends Descriptors> = T extends HostModule<any, any> ? HostModuleAPI<T> : ConditionalExcept<{
22
- [Key in keyof T]: T[Key] extends Descriptors ? BuildHostDescriptors<T[Key]> : never;
23
- }, EmptyObject>;
24
- type BuildEventDefinitions<T extends Descriptors> = T extends EventDefinition ? BuildEventDefinition<T> : ConditionalExcept<{
25
- [Key in keyof T]: T[Key] extends Descriptors ? BuildEventDefinitions<T[Key]> : never;
26
- }, EmptyObject>;
14
+ export type BuildDescriptors<T extends Descriptors, H extends Host<any> | undefined, Depth extends number = 5> = {
15
+ done: T;
16
+ recurse: T extends AmbassadorFunctionDescriptor ? BuildAmbassadorFunction<T> : T extends RESTFunctionDescriptor ? BuildRESTFunction<T> : T extends EventDefinition ? BuildEventDefinition<T> : T extends ServicePluginDefinition<any> ? BuildServicePluginDefinition<T> : T extends HostModule<any, any> ? HostModuleAPI<T> : ConditionalExcept<{
17
+ [Key in keyof T]: T[Key] extends Descriptors ? BuildDescriptors<T[Key], H, [-1, 0, 1, 2, 3, 4, 5][Depth]> : never;
18
+ }, EmptyObject>;
19
+ }[Depth extends -1 ? 'done' : 'recurse'];
27
20
  /**
28
21
  * Descriptors are objects that describe the API of a module, and the module
29
22
  * can either be a REST module or a host module.
@@ -55,7 +48,7 @@ type TypedQueryInput<Result = {
55
48
  */
56
49
  __ensureTypesOfVariablesAndResultMatching?: (variables: Variables) => Result;
57
50
  };
58
- export type WixClient<H extends Host<any> | undefined = undefined, Z extends AuthenticationStrategy<H> = AuthenticationStrategy<H>, T extends Descriptors = Descriptors> = {
51
+ export type WixClient<H extends Host<any> | undefined = undefined, Z extends AuthenticationStrategy<H> = AuthenticationStrategy<H>, T extends Descriptors = {}> = {
59
52
  setHeaders(headers: Headers): void;
60
53
  auth: Omit<Z, 'getAuthHeaders'> & BoundAuthenticationStrategy;
61
54
  withAuth: Z['withAuth'] extends undefined ? never : (...args: Parameters<NonNullable<Z['withAuth']>>) => WixClient<H, Z, T>;
@@ -67,6 +60,7 @@ export type WixClient<H extends Host<any> | undefined = undefined, Z extends Aut
67
60
  errors?: GraphQLFormattedError[];
68
61
  }>;
69
62
  webhooks: {
63
+ getRegisteredEvents(): string[];
70
64
  process<ExpectedEvents extends EventDefinition<any>[] = []>(jwt: string, opts?: {
71
65
  expectedEvents: ExpectedEvents;
72
66
  }): Promise<ProcessedEvent<ExpectedEvents>>;
@@ -83,11 +77,6 @@ export type WixClient<H extends Host<any> | undefined = undefined, Z extends Aut
83
77
  }, 'AppRemoved'>;
84
78
  };
85
79
  };
86
- spi: <S extends SPIDefinition<any, any>>() => {
87
- process(jwt: string): Promise<S['__input']>;
88
- processRequest(request: Request): Promise<S['__input']>;
89
- result(result: S['__result']): S['__result'];
90
- };
91
80
  } & BuildDescriptors<T, H>;
92
81
  type ResolvePossibleEvents<T extends EventDefinition<any>[]> = {
93
82
  [K in keyof T]: T[K] extends EventDefinition<any> ? {
@@ -9,6 +9,7 @@ const helpers_js_1 = require("./helpers.js");
9
9
  const host_modules_js_1 = require("./host-modules.js");
10
10
  const rest_modules_js_1 = require("./rest-modules.js");
11
11
  const event_handlers_modules_js_1 = require("./event-handlers-modules.js");
12
+ const service_plugin_modules_js_1 = require("./service-plugin-modules.js");
12
13
  function createClient(config) {
13
14
  const _headers = config.headers || { Authorization: '' };
14
15
  const eventHandlers = new Map();
@@ -42,6 +43,9 @@ function createClient(config) {
42
43
  eventHandlers.set(eventDefinition.type, handlers);
43
44
  });
44
45
  }
46
+ else if ((0, service_plugin_modules_js_1.isServicePluginModule)(modules)) {
47
+ return (0, service_plugin_modules_js_1.buildServicePluginDefinition)(modules, authStrategy.decodeJWT);
48
+ }
45
49
  else if ((0, host_modules_js_1.isHostModule)(modules) && config.host) {
46
50
  return (0, host_modules_js_1.buildHostModule)(modules, config.host);
47
51
  }
@@ -122,6 +126,7 @@ function createClient(config) {
122
126
  return { data: data ?? {}, errors };
123
127
  },
124
128
  webhooks: {
129
+ getRegisteredEvents: () => Array.from(eventHandlers.keys()),
125
130
  process: async (jwt, opts = {
126
131
  expectedEvents: [],
127
132
  }) => {
@@ -170,27 +175,6 @@ function createClient(config) {
170
175
  AppRemoved: (0, sdk_types_1.EventDefinition)('AppRemoved')(),
171
176
  },
172
177
  },
173
- spi() {
174
- return {
175
- async process(jwt) {
176
- if (!authStrategy.decodeJWT) {
177
- throw new Error('decodeJWT is not supported by the authentication strategy');
178
- }
179
- const { decoded, valid } = await authStrategy.decodeJWT(jwt, true);
180
- if (!valid) {
181
- throw new Error('JWT is not valid');
182
- }
183
- return decoded.data;
184
- },
185
- async processRequest(request) {
186
- const body = await request.text();
187
- return this.process(body);
188
- },
189
- result(result) {
190
- return result;
191
- },
192
- };
193
- },
194
178
  };
195
179
  }
196
180
  exports.createClient = createClient;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@wix/sdk",
3
- "version": "1.9.2",
3
+ "version": "1.9.4",
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.67.0",
69
69
  "@wix/redirects": "^1.0.41",
70
- "@wix/sdk-types": "^1.6.3",
70
+ "@wix/sdk-types": "^1.7.0",
71
71
  "crypto-js": "^4.2.0",
72
72
  "jose": "^5.2.1",
73
73
  "pkce-challenge": "^3.1.0",
@@ -120,5 +120,5 @@
120
120
  "wallaby": {
121
121
  "autoDetect": true
122
122
  },
123
- "falconPackageHash": "71774a6a839caae0eb644c899a798b3b14296d9c6321d8fae8dfeca5"
123
+ "falconPackageHash": "4b5ab480b70debacfd197e4f868613a615cd3854cb5b8b0aff383862"
124
124
  }