@wix/sdk-types 1.13.10 → 1.13.12

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,416 @@
1
+ import { MonitoringClient } from '@wix/monitoring-types';
2
+ import { ConditionalExcept, EmptyObject, Simplify, Paths, SetRequiredDeep } from 'type-fest';
3
+
4
+ interface BiLogger {
5
+ log(params: LogParams, context?: LogOptions): Promise<any>;
6
+ log(eventName: string, params: LogParams, context?: LogOptions): Promise<any>;
7
+ report(reportProps: ReportProps): Promise<any>;
8
+ flush(): Promise<any>;
9
+ updateDefaults(params: LogParams): this;
10
+ }
11
+ interface LogParams {
12
+ [key: string]: any;
13
+ }
14
+ interface LogOptions {
15
+ endpoint?: string;
16
+ useBatch?: number | boolean;
17
+ category?: EventCategories;
18
+ }
19
+ interface ReportProps {
20
+ evid: number;
21
+ src: number;
22
+ endpoint: string;
23
+ params: LogParams;
24
+ }
25
+ declare enum EventCategories {
26
+ Essential = "essential",
27
+ Functional = "functional",
28
+ Analytics = "analytics"
29
+ }
30
+
31
+ declare class ReadOnlyExperiments {
32
+ protected experiments: ExperimentsBag;
33
+ constructor(options: ReadOnlyExperimentsOptions);
34
+ get(key: string): any;
35
+ enabled(key: string): boolean;
36
+ pending(): boolean;
37
+ ready(): Promise<any>;
38
+ all(): ExperimentsBag;
39
+ }
40
+ declare class Experiments extends ReadOnlyExperiments {
41
+ private readonly loaders;
42
+ private readonly baseUrl;
43
+ private requestContext;
44
+ readonly useNewApi: boolean;
45
+ private onError;
46
+ constructor(obj?: ExperimentsProps);
47
+ add(obj: ExperimentsBag): void;
48
+ private _addLoader;
49
+ private _getUrlWithFallback;
50
+ load(scope: string): Promise<void>;
51
+ conduct(spec: string, fallbackValue?: string): Promise<string>;
52
+ pending(): boolean;
53
+ ready(): Promise<any>;
54
+ _addConductResult(spec: string, conductResponse: string): Promise<string>;
55
+ }
56
+ interface ExperimentsBag {
57
+ [key: string]: string | boolean;
58
+ }
59
+ interface ReadOnlyExperimentsOptions {
60
+ experiments?: ExperimentsBag;
61
+ }
62
+ interface ExperimentsProps extends ReadOnlyExperimentsOptions {
63
+ useNewApi?: boolean;
64
+ baseUrl?: string;
65
+ scope?: string;
66
+ scopes?: string[];
67
+ requestContext?: RequestContext$1;
68
+ onError?: (error: Error) => void;
69
+ }
70
+ type RequestContext$1 = RequestContextMetasite & OneOf<RequestContextOwner, RequestContextVisitors>;
71
+ interface RequestContextMetasite {
72
+ overrideCriteria?: {
73
+ entityId: string;
74
+ };
75
+ }
76
+ interface RequestContextOwner {
77
+ forSiteOwner?: {
78
+ loggedInUserId: string;
79
+ siteOwnerId: string;
80
+ };
81
+ }
82
+ interface RequestContextVisitors {
83
+ forSiteVisitors?: {
84
+ visitorId: string;
85
+ siteOwnerId: string;
86
+ };
87
+ }
88
+ type OneOf<T1, T2> = ({
89
+ [P in keyof T1]?: never;
90
+ } & T2) | ({
91
+ [P in keyof T2]?: never;
92
+ } & T1);
93
+
94
+ type Conductor = {
95
+ isFeatureOn: (featureName: string) => boolean;
96
+ getABTestVariant: (abTestName: string) => 'a' | 'b' | 'c' | 'd' | 'e';
97
+ };
98
+
99
+ type HostModule<T, H extends Host> = {
100
+ __type: 'host';
101
+ create(host: H): T;
102
+ };
103
+ type HostModuleAPI<T extends HostModule<any, any>> = T extends HostModule<infer U, any> ? U : never;
104
+ type Host<Environment = unknown> = {
105
+ channel?: {
106
+ observeState(callback: (props: unknown, environment: Environment) => unknown): {
107
+ disconnect: () => void;
108
+ } | Promise<{
109
+ disconnect: () => void;
110
+ }>;
111
+ };
112
+ environment?: Environment;
113
+ /**
114
+ * Optional name of the environment, use for logging
115
+ */
116
+ name?: string;
117
+ /**
118
+ * Optional bast url to use for API requests, for example `www.wixapis.com`
119
+ */
120
+ apiBaseUrl?: string;
121
+ /**
122
+ * Optional function to get a monitoring client
123
+ */
124
+ getMonitoringClient?: () => MonitoringClient;
125
+ /**
126
+ * Optional function to get a bi logger
127
+ * @internal
128
+ */
129
+ getBiLogger?: () => BiLogger;
130
+ /**
131
+ * Optional function to get petri experiments
132
+ * @internal
133
+ */
134
+ getPetriExperimentsClient?: () => Experiments;
135
+ /**
136
+ * Optional function to get conductor experiments
137
+ * @internal
138
+ */
139
+ getConductorClient?: () => Conductor;
140
+ /**
141
+ * Possible data to be provided by every host, for cross cutting concerns
142
+ * like internationalization, billing, etc.
143
+ */
144
+ essentials?: {
145
+ /**
146
+ * The language of the currently viewed session
147
+ */
148
+ language?: string;
149
+ /**
150
+ * The locale of the currently viewed session
151
+ */
152
+ locale?: string;
153
+ /**
154
+ * Any headers that should be passed through to the API requests
155
+ */
156
+ passThroughHeaders?: Record<string, string>;
157
+ };
158
+ };
159
+
160
+ type HTTPMethod = 'POST' | 'GET' | 'PUT' | 'DELETE' | 'PATCH' | 'HEAD' | 'OPTIONS';
161
+ type RESTFunctionDescriptor<T extends (...args: any[]) => any = (...args: any[]) => any> = (httpClient: HttpClient) => T;
162
+ interface HttpClient {
163
+ request<TResponse, TData = any>(req: RequestOptionsFactory<TResponse, TData>): Promise<HttpResponse<TResponse>>;
164
+ fetchWithAuth: typeof fetch;
165
+ wixAPIFetch: (relativeUrl: string, options: RequestInit) => Promise<Response>;
166
+ getActiveToken?: () => string | undefined;
167
+ }
168
+ type RequestOptionsFactory<TResponse = any, TData = any> = (context: any) => RequestOptions<TResponse, TData>;
169
+ type HttpResponse<T = any> = {
170
+ data: T;
171
+ status: number;
172
+ statusText: string;
173
+ headers: any;
174
+ request?: any;
175
+ };
176
+ type RequestOptions<_TResponse = any, Data = any> = {
177
+ method: HTTPMethod;
178
+ url: string;
179
+ data?: Data;
180
+ params?: URLSearchParams;
181
+ } & APIMetadata;
182
+ type APIMetadata = {
183
+ methodFqn?: string;
184
+ entityFqdn?: string;
185
+ packageName?: string;
186
+ };
187
+ type BuildRESTFunction<T extends RESTFunctionDescriptor> = T extends RESTFunctionDescriptor<infer U> ? U : never;
188
+ type RestModuleMeta<TMethod extends HTTPMethod = HTTPMethod, TPathParams = unknown, RequestType = unknown, TOriginalRequestType = unknown, ResponseType = unknown, OriginalResponseType = unknown> = {
189
+ getUrl(context: {
190
+ host: string;
191
+ }): string;
192
+ httpMethod: TMethod;
193
+ pathParams: TPathParams;
194
+ path: string;
195
+ __requestType: RequestType;
196
+ __originalRequestType: TOriginalRequestType;
197
+ __responseType: ResponseType;
198
+ __originalResponseType: OriginalResponseType;
199
+ };
200
+
201
+ type AuthenticationStrategy<Host = unknown> = {
202
+ getAuthHeaders: (host: Host) => Promise<{
203
+ headers: Record<string, string>;
204
+ }> | {
205
+ headers: Record<string, string>;
206
+ };
207
+ decodeJWT?: (token: string, verifyCallerClaims?: boolean) => Promise<{
208
+ decoded: {
209
+ data: unknown;
210
+ };
211
+ valid: boolean;
212
+ }>;
213
+ /**
214
+ * This function is used to get the token that is currently active in the context of the strategy.
215
+ * This is useful when direct access to the access token is needed
216
+ * (such as getTokenInfo that requires the token in the body of the request).
217
+ * @returns the token that is currently active in the context of the strategy
218
+ */
219
+ getActiveToken?: () => string | undefined;
220
+ };
221
+ type BoundAuthenticationStrategy = {
222
+ getAuthHeaders: () => Promise<{
223
+ headers: Record<string, string>;
224
+ }> | {
225
+ headers: Record<string, string>;
226
+ };
227
+ };
228
+
229
+ type EventIdentity = {
230
+ identityType: 'ANONYMOUS_VISITOR' | 'MEMBER' | 'WIX_USER' | 'APP';
231
+ anonymousVisitorId: string;
232
+ memberId: string;
233
+ wixUserId: string;
234
+ appId: string;
235
+ };
236
+ type BaseEventMetadata = {
237
+ instanceId: string;
238
+ identity?: EventIdentity;
239
+ };
240
+ type EventDefinition<Payload = unknown, Type extends string = string> = {
241
+ __type: 'event-definition';
242
+ type: Type;
243
+ isDomainEvent?: boolean;
244
+ transformations?: (envelope: unknown) => Payload;
245
+ __payload: Payload;
246
+ };
247
+ declare function EventDefinition<Type extends string>(type: Type, isDomainEvent?: boolean, transformations?: (envelope: any) => unknown): <Payload = unknown>() => EventDefinition<Payload, Type>;
248
+ type EventHandler<T extends EventDefinition> = (payload: T['__payload']) => void | Promise<void>;
249
+ type BuildEventDefinition<T extends EventDefinition<any, string>> = (handler: EventHandler<T>) => void;
250
+
251
+ type ServicePluginMethodInput = {
252
+ request: any;
253
+ metadata: any;
254
+ };
255
+ type ServicePluginContract = Record<string, (payload: ServicePluginMethodInput) => unknown | Promise<unknown>>;
256
+ type ServicePluginMethodMetadata = {
257
+ name: string;
258
+ primaryHttpMappingPath: string;
259
+ transformations: {
260
+ fromREST: (...args: unknown[]) => ServicePluginMethodInput;
261
+ toREST: (...args: unknown[]) => unknown;
262
+ };
263
+ };
264
+ type ServicePluginDefinition<Contract extends ServicePluginContract> = {
265
+ __type: 'service-plugin-definition';
266
+ componentType: string;
267
+ methods: ServicePluginMethodMetadata[];
268
+ __contract: Contract;
269
+ };
270
+ declare function ServicePluginDefinition<Contract extends ServicePluginContract>(componentType: string, methods: ServicePluginMethodMetadata[]): ServicePluginDefinition<Contract>;
271
+ type BuildServicePluginDefinition<T extends ServicePluginDefinition<any>> = (implementation: T['__contract']) => void;
272
+ declare const SERVICE_PLUGIN_ERROR_TYPE = "wix_spi_error";
273
+
274
+ type RequestContext = {
275
+ isSSR: boolean;
276
+ host: string;
277
+ protocol?: string;
278
+ };
279
+ type ResponseTransformer = (data: any, headers?: any) => any;
280
+ /**
281
+ * Ambassador request options types are copied mostly from AxiosRequestConfig.
282
+ * They are copied and not imported to reduce the amount of dependencies (to reduce install time).
283
+ * https://github.com/axios/axios/blob/3f53eb6960f05a1f88409c4b731a40de595cb825/index.d.ts#L307-L315
284
+ */
285
+ type Method = 'get' | 'GET' | 'delete' | 'DELETE' | 'head' | 'HEAD' | 'options' | 'OPTIONS' | 'post' | 'POST' | 'put' | 'PUT' | 'patch' | 'PATCH' | 'purge' | 'PURGE' | 'link' | 'LINK' | 'unlink' | 'UNLINK';
286
+ type AmbassadorRequestOptions<T = any> = {
287
+ _?: T;
288
+ url?: string;
289
+ method?: Method;
290
+ params?: any;
291
+ data?: any;
292
+ transformResponse?: ResponseTransformer | ResponseTransformer[];
293
+ };
294
+ type AmbassadorFactory<Request, Response> = (payload: Request) => ((context: RequestContext) => AmbassadorRequestOptions<Response>) & {
295
+ __isAmbassador: boolean;
296
+ };
297
+ type AmbassadorFunctionDescriptor<Request = any, Response = any> = AmbassadorFactory<Request, Response>;
298
+ type BuildAmbassadorFunction<T extends AmbassadorFunctionDescriptor> = T extends AmbassadorFunctionDescriptor<infer Request, infer Response> ? (req: Request) => Promise<Response> : never;
299
+
300
+ /**
301
+ * Descriptors are objects that describe the API of a module, and the module
302
+ * can either be a REST module or a host module.
303
+ * This type is recursive, so it can describe nested modules.
304
+ */
305
+ type Descriptors = RESTFunctionDescriptor | AmbassadorFunctionDescriptor | HostModule<any, any> | EventDefinition<any> | ServicePluginDefinition<any> | {
306
+ [key: string]: Descriptors | PublicMetadata | any;
307
+ };
308
+ /**
309
+ * This type takes in a descriptors object of a certain Host (including an `unknown` host)
310
+ * and returns an object with the same structure, but with all descriptors replaced with their API.
311
+ * Any non-descriptor properties are removed from the returned object, including descriptors that
312
+ * do not match the given host (as they will not work with the given host).
313
+ */
314
+ type BuildDescriptors<T extends Descriptors, H extends Host<any> | undefined, Depth extends number = 5> = {
315
+ done: T;
316
+ recurse: T extends {
317
+ __type: typeof SERVICE_PLUGIN_ERROR_TYPE;
318
+ } ? never : T extends AmbassadorFunctionDescriptor ? BuildAmbassadorFunction<T> : T extends RESTFunctionDescriptor ? BuildRESTFunction<T> : T extends EventDefinition<any> ? BuildEventDefinition<T> : T extends ServicePluginDefinition<any> ? BuildServicePluginDefinition<T> : T extends HostModule<any, any> ? HostModuleAPI<T> : ConditionalExcept<{
319
+ [Key in keyof T]: T[Key] extends Descriptors ? BuildDescriptors<T[Key], H, [
320
+ -1,
321
+ 0,
322
+ 1,
323
+ 2,
324
+ 3,
325
+ 4,
326
+ 5
327
+ ][Depth]> : never;
328
+ }, EmptyObject>;
329
+ }[Depth extends -1 ? 'done' : 'recurse'];
330
+ type PublicMetadata = {
331
+ PACKAGE_NAME?: string;
332
+ };
333
+
334
+ declare global {
335
+ interface ContextualClient {
336
+ }
337
+ }
338
+ /**
339
+ * A type used to create concerete types from SDK descriptors in
340
+ * case a contextual client is available.
341
+ */
342
+ type MaybeContext<T extends Descriptors> = globalThis.ContextualClient extends {
343
+ host: Host;
344
+ } ? BuildDescriptors<T, globalThis.ContextualClient['host']> : T;
345
+
346
+ /**
347
+ * Expose fields based on the current exposure toggle.
348
+ * @param T - The type to expose fields from.
349
+ * @param FieldsScope - A map of fields to their exposure scope, missing fields are considered public.
350
+ * @example Exposure toggle not set:
351
+ * ```ts
352
+ * type MyType = {
353
+ * publicField: string;
354
+ * alphaField: string;
355
+ * };
356
+ *
357
+ * type ExposedType = ExposeFieldsBasedOnToggle<MyType, { alphaField: 'alpha' }>;
358
+ * // ExposedType = { publicField: string; }
359
+ * ```
360
+ * @example Exposure toggle set to alpha:
361
+ * ```ts
362
+ * declare global {
363
+ * interface SDKExposureToggle {
364
+ * alpha: true;
365
+ * }
366
+ * }
367
+ *
368
+ * type MyType = {
369
+ * publicField: string;
370
+ * alphaField: string;
371
+ * };
372
+ *
373
+ * type ExposedType = ExposeFieldsBasedOnToggle<MyType, { alphaField: 'alpha' }>;
374
+ * // ExposedType = { publicField: string; alphaField: string; }
375
+ */
376
+ type ExposeFieldsBasedOnToggle<T extends Record<string, any>, FieldsScope extends Partial<Record<keyof T, Exposure>>> = Simplify<{
377
+ [K in keyof T as IsExposed<K, FieldsScope[K] extends Exposure ? FieldsScope[K] : 'public'>]: T[K];
378
+ }>;
379
+ declare global {
380
+ /**
381
+ * A global interface to set the exposure toggle for the SDK.
382
+ * @example
383
+ * ```ts
384
+ * declare global {
385
+ * interface SDKExposureToggle {
386
+ * alpha: true;
387
+ * }
388
+ * }
389
+ */
390
+ interface SDKExposureToggle {
391
+ }
392
+ }
393
+ type Exposure = 'alpha' | 'public';
394
+ type IsExposed<T, Scope extends Exposure> = Scope extends 'public' ? T : globalThis.SDKExposureToggle extends {
395
+ alpha: true;
396
+ } ? T : never;
397
+
398
+ declare global {
399
+ /**
400
+ * A global interface to set the type mode for the SDK.
401
+ * @example
402
+ * ```ts
403
+ * declare global {
404
+ * interface SDKTypeMode {
405
+ * strict: true;
406
+ * }
407
+ * }
408
+ */
409
+ interface SDKTypeMode {
410
+ }
411
+ }
412
+ type NonNullablePaths<T, K extends Paths<T>> = globalThis.SDKTypeMode extends {
413
+ strict: true;
414
+ } ? SetRequiredDeep<T, K> : T;
415
+
416
+ export { type APIMetadata, type AmbassadorFactory, type AmbassadorFunctionDescriptor, type AmbassadorRequestOptions, type AuthenticationStrategy, type BaseEventMetadata, type BoundAuthenticationStrategy, type BuildAmbassadorFunction, type BuildDescriptors, type BuildEventDefinition, type BuildRESTFunction, type BuildServicePluginDefinition, type Descriptors, EventDefinition, type EventHandler, type EventIdentity, type ExposeFieldsBasedOnToggle, type HTTPMethod, type Host, type HostModule, type HostModuleAPI, type HttpClient, type HttpResponse, type MaybeContext, type Method, type NonNullablePaths, type PublicMetadata, type RESTFunctionDescriptor, type RequestContext, type RequestOptions, type RequestOptionsFactory, type RestModuleMeta, SERVICE_PLUGIN_ERROR_TYPE, type ServicePluginContract, ServicePluginDefinition, type ServicePluginMethodInput, type ServicePluginMethodMetadata };
@@ -0,0 +1,24 @@
1
+ // src/event-handlers-modules.ts
2
+ function EventDefinition(type, isDomainEvent = false, transformations = (x) => x) {
3
+ return () => ({
4
+ __type: "event-definition",
5
+ type,
6
+ isDomainEvent,
7
+ transformations
8
+ });
9
+ }
10
+
11
+ // src/service-plugins.ts
12
+ function ServicePluginDefinition(componentType, methods) {
13
+ return {
14
+ __type: "service-plugin-definition",
15
+ componentType,
16
+ methods
17
+ };
18
+ }
19
+ var SERVICE_PLUGIN_ERROR_TYPE = "wix_spi_error";
20
+ export {
21
+ EventDefinition,
22
+ SERVICE_PLUGIN_ERROR_TYPE,
23
+ ServicePluginDefinition
24
+ };