@wix/sdk 1.15.23 → 1.15.25

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.
@@ -1,5 +1,5 @@
1
- import { BuildRESTFunction, PublicMetadata, RESTFunctionDescriptor } from '@wix/sdk-types';
1
+ import { BuildRESTFunction, PublicMetadata, RESTFunctionDescriptor, WixClientErrorHandler } from '@wix/sdk-types';
2
2
  export type RESTModuleOptions = {
3
3
  HTTPHost?: string;
4
4
  };
5
- export declare function buildRESTDescriptor<T extends RESTFunctionDescriptor>(origFunc: T, publicMetadata: PublicMetadata, boundFetch: typeof fetch, wixAPIFetch: (relativeUrl: string, options: RequestInit) => Promise<Response>, getActiveToken?: () => string | undefined, options?: RESTModuleOptions, hostName?: string | undefined, useCDN?: boolean): BuildRESTFunction<T>;
5
+ export declare function buildRESTDescriptor<T extends RESTFunctionDescriptor>(origFunc: T, publicMetadata: PublicMetadata, boundFetch: typeof fetch, errorHandler: WixClientErrorHandler | undefined, wixAPIFetch: (relativeUrl: string, options: RequestInit) => Promise<Response>, getActiveToken?: () => string | undefined, options?: RESTModuleOptions, hostName?: string | undefined, useCDN?: boolean): BuildRESTFunction<T>;
@@ -1,7 +1,8 @@
1
1
  import { biHeaderGenerator } from './bi/biHeaderGenerator.js';
2
2
  import { DEFAULT_API_URL, DEFAULT_EDGE_API_URL } from './common.js';
3
3
  import { runWithoutContext } from '@wix/sdk-runtime/context';
4
- export function buildRESTDescriptor(origFunc, publicMetadata, boundFetch, wixAPIFetch, getActiveToken, options, hostName, useCDN) {
4
+ import { transformError } from '@wix/sdk-runtime/transform-error';
5
+ export function buildRESTDescriptor(origFunc, publicMetadata, boundFetch, errorHandler, wixAPIFetch, getActiveToken, options, hostName, useCDN) {
5
6
  return runWithoutContext(() => origFunc({
6
7
  request: async (factory) => {
7
8
  const requestOptions = factory({
@@ -20,7 +21,7 @@ export function buildRESTDescriptor(origFunc, publicMetadata, boundFetch, wixAPI
20
21
  }
21
22
  try {
22
23
  const biHeader = biHeaderGenerator(requestOptions, publicMetadata, hostName);
23
- const res = await boundFetch(url, {
24
+ const requestOptionsInit = {
24
25
  method: request.method,
25
26
  ...(request.data && {
26
27
  body: JSON.stringify(request.data),
@@ -28,7 +29,8 @@ export function buildRESTDescriptor(origFunc, publicMetadata, boundFetch, wixAPI
28
29
  headers: {
29
30
  ...biHeader,
30
31
  },
31
- });
32
+ };
33
+ const res = await boundFetch(url, requestOptionsInit);
32
34
  if (res.status !== 200) {
33
35
  let dataError = null;
34
36
  try {
@@ -37,10 +39,18 @@ export function buildRESTDescriptor(origFunc, publicMetadata, boundFetch, wixAPI
37
39
  catch (e) {
38
40
  //
39
41
  }
40
- throw errorBuilder(res.status, dataError?.message, dataError?.details, {
42
+ const error = errorBuilder(res.status, dataError?.message, dataError?.details, {
41
43
  requestId: res.headers.get('X-Wix-Request-Id'),
42
44
  details: dataError,
43
45
  });
46
+ const transformedError = transformError(error);
47
+ errorHandler?.handleError(transformedError, {
48
+ requestOptions: {
49
+ url: request.url,
50
+ ...requestOptionsInit,
51
+ },
52
+ });
53
+ throw error;
44
54
  }
45
55
  const data = await res.json();
46
56
  return {
@@ -62,8 +72,17 @@ export function buildRESTDescriptor(origFunc, publicMetadata, boundFetch, wixAPI
62
72
  getActiveToken,
63
73
  }));
64
74
  }
75
+ class SDKError extends Error {
76
+ response;
77
+ requestId;
78
+ constructor(params) {
79
+ super();
80
+ this.response = params.response;
81
+ this.requestId = params.requestId;
82
+ }
83
+ }
65
84
  const errorBuilder = (code, description, details, data) => {
66
- return {
85
+ return new SDKError({
67
86
  response: {
68
87
  data: {
69
88
  details: {
@@ -81,5 +100,5 @@ const errorBuilder = (code, description, details, data) => {
81
100
  status: code,
82
101
  },
83
102
  requestId: data?.requestId,
84
- };
103
+ });
85
104
  };
@@ -23,33 +23,49 @@ export function createClient(config) {
23
23
  const headers = {
24
24
  ...(requestInit?.headers ?? {}),
25
25
  ...authHeaders.headers,
26
+ ...config.host?.essentials?.passThroughHeaders,
26
27
  ...(_headers[X_WIX_CONSISTENT_HEADER]
27
28
  ? { [X_WIX_CONSISTENT_HEADER]: _headers[X_WIX_CONSISTENT_HEADER] }
28
29
  : {}),
29
30
  };
30
- if (typeof urlOrRequest === 'string' || urlOrRequest instanceof URL) {
31
- const response = await fetch(urlOrRequest, {
32
- ...requestInit,
33
- headers,
34
- });
35
- const consistentHeader = findConsistentHeader(response);
36
- if (consistentHeader) {
37
- _headers[X_WIX_CONSISTENT_HEADER] = consistentHeader;
38
- }
39
- return response;
40
- }
41
- else {
42
- for (const [k, v] of Object.entries(headers)) {
43
- if (typeof v === 'string') {
44
- urlOrRequest.headers.set(k, v);
31
+ const errorHandler = config.host?.getErrorHandler?.();
32
+ try {
33
+ if (typeof urlOrRequest === 'string' || urlOrRequest instanceof URL) {
34
+ const response = await fetch(urlOrRequest, {
35
+ ...requestInit,
36
+ headers,
37
+ });
38
+ errorHandler?.handleError(response, {
39
+ requestOptions: requestInit,
40
+ });
41
+ const consistentHeader = findConsistentHeader(response);
42
+ if (consistentHeader) {
43
+ _headers[X_WIX_CONSISTENT_HEADER] = consistentHeader;
45
44
  }
45
+ return response;
46
46
  }
47
- const response = await fetch(urlOrRequest, requestInit);
48
- const consistentHeader = findConsistentHeader(response);
49
- if (consistentHeader) {
50
- _headers[X_WIX_CONSISTENT_HEADER] = consistentHeader;
47
+ else {
48
+ for (const [k, v] of Object.entries(headers)) {
49
+ if (typeof v === 'string') {
50
+ urlOrRequest.headers.set(k, v);
51
+ }
52
+ }
53
+ const response = await fetch(urlOrRequest, requestInit);
54
+ errorHandler?.handleError(response, {
55
+ requestOptions: requestInit,
56
+ });
57
+ const consistentHeader = findConsistentHeader(response);
58
+ if (consistentHeader) {
59
+ _headers[X_WIX_CONSISTENT_HEADER] = consistentHeader;
60
+ }
61
+ return response;
51
62
  }
52
- return response;
63
+ }
64
+ catch (e) {
65
+ errorHandler?.handleError(e, {
66
+ requestOptions: requestInit,
67
+ });
68
+ throw e;
53
69
  }
54
70
  };
55
71
  const { client: servicePluginsClient, initModule: initServicePluginModule } = servicePluginsModules(authStrategy);
@@ -65,6 +81,10 @@ export function createClient(config) {
65
81
  ...authHeaders?.headers,
66
82
  ...options?.headers,
67
83
  ...config.host?.essentials?.passThroughHeaders,
84
+ // Ensure consistent header always takes precedence
85
+ ...(_headers[X_WIX_CONSISTENT_HEADER]
86
+ ? { [X_WIX_CONSISTENT_HEADER]: _headers[X_WIX_CONSISTENT_HEADER] }
87
+ : {}),
68
88
  },
69
89
  });
70
90
  const consistentHeader = findConsistentHeader(response);
@@ -97,7 +117,7 @@ export function createClient(config) {
97
117
  const shouldUseCDN = config.useCDN === undefined ? config.auth?.shouldUseCDN : config.useCDN;
98
118
  return buildRESTDescriptor(runWithoutContext(() => isAmbassadorModule(modules))
99
119
  ? toHTTPModule(modules)
100
- : modules, metadata ?? {}, boundFetch, (relativeUrl, fetchOptions) => {
120
+ : modules, metadata ?? {}, boundFetch, config.host?.getErrorHandler?.(), (relativeUrl, fetchOptions) => {
101
121
  const finalUrl = new URL(relativeUrl, `https://${apiBaseUrl}`);
102
122
  finalUrl.host = apiBaseUrl;
103
123
  finalUrl.protocol = 'https';
@@ -1,5 +1,5 @@
1
- import { BuildRESTFunction, PublicMetadata, RESTFunctionDescriptor } from '@wix/sdk-types';
1
+ import { BuildRESTFunction, PublicMetadata, RESTFunctionDescriptor, WixClientErrorHandler } from '@wix/sdk-types';
2
2
  export type RESTModuleOptions = {
3
3
  HTTPHost?: string;
4
4
  };
5
- export declare function buildRESTDescriptor<T extends RESTFunctionDescriptor>(origFunc: T, publicMetadata: PublicMetadata, boundFetch: typeof fetch, wixAPIFetch: (relativeUrl: string, options: RequestInit) => Promise<Response>, getActiveToken?: () => string | undefined, options?: RESTModuleOptions, hostName?: string | undefined, useCDN?: boolean): BuildRESTFunction<T>;
5
+ export declare function buildRESTDescriptor<T extends RESTFunctionDescriptor>(origFunc: T, publicMetadata: PublicMetadata, boundFetch: typeof fetch, errorHandler: WixClientErrorHandler | undefined, wixAPIFetch: (relativeUrl: string, options: RequestInit) => Promise<Response>, getActiveToken?: () => string | undefined, options?: RESTModuleOptions, hostName?: string | undefined, useCDN?: boolean): BuildRESTFunction<T>;
@@ -4,7 +4,8 @@ exports.buildRESTDescriptor = buildRESTDescriptor;
4
4
  const biHeaderGenerator_js_1 = require("./bi/biHeaderGenerator.js");
5
5
  const common_js_1 = require("./common.js");
6
6
  const context_1 = require("@wix/sdk-runtime/context");
7
- function buildRESTDescriptor(origFunc, publicMetadata, boundFetch, wixAPIFetch, getActiveToken, options, hostName, useCDN) {
7
+ const transform_error_1 = require("@wix/sdk-runtime/transform-error");
8
+ function buildRESTDescriptor(origFunc, publicMetadata, boundFetch, errorHandler, wixAPIFetch, getActiveToken, options, hostName, useCDN) {
8
9
  return (0, context_1.runWithoutContext)(() => origFunc({
9
10
  request: async (factory) => {
10
11
  const requestOptions = factory({
@@ -23,7 +24,7 @@ function buildRESTDescriptor(origFunc, publicMetadata, boundFetch, wixAPIFetch,
23
24
  }
24
25
  try {
25
26
  const biHeader = (0, biHeaderGenerator_js_1.biHeaderGenerator)(requestOptions, publicMetadata, hostName);
26
- const res = await boundFetch(url, {
27
+ const requestOptionsInit = {
27
28
  method: request.method,
28
29
  ...(request.data && {
29
30
  body: JSON.stringify(request.data),
@@ -31,7 +32,8 @@ function buildRESTDescriptor(origFunc, publicMetadata, boundFetch, wixAPIFetch,
31
32
  headers: {
32
33
  ...biHeader,
33
34
  },
34
- });
35
+ };
36
+ const res = await boundFetch(url, requestOptionsInit);
35
37
  if (res.status !== 200) {
36
38
  let dataError = null;
37
39
  try {
@@ -40,10 +42,18 @@ function buildRESTDescriptor(origFunc, publicMetadata, boundFetch, wixAPIFetch,
40
42
  catch (e) {
41
43
  //
42
44
  }
43
- throw errorBuilder(res.status, dataError?.message, dataError?.details, {
45
+ const error = errorBuilder(res.status, dataError?.message, dataError?.details, {
44
46
  requestId: res.headers.get('X-Wix-Request-Id'),
45
47
  details: dataError,
46
48
  });
49
+ const transformedError = (0, transform_error_1.transformError)(error);
50
+ errorHandler?.handleError(transformedError, {
51
+ requestOptions: {
52
+ url: request.url,
53
+ ...requestOptionsInit,
54
+ },
55
+ });
56
+ throw error;
47
57
  }
48
58
  const data = await res.json();
49
59
  return {
@@ -65,8 +75,17 @@ function buildRESTDescriptor(origFunc, publicMetadata, boundFetch, wixAPIFetch,
65
75
  getActiveToken,
66
76
  }));
67
77
  }
78
+ class SDKError extends Error {
79
+ response;
80
+ requestId;
81
+ constructor(params) {
82
+ super();
83
+ this.response = params.response;
84
+ this.requestId = params.requestId;
85
+ }
86
+ }
68
87
  const errorBuilder = (code, description, details, data) => {
69
- return {
88
+ return new SDKError({
70
89
  response: {
71
90
  data: {
72
91
  details: {
@@ -84,5 +103,5 @@ const errorBuilder = (code, description, details, data) => {
84
103
  status: code,
85
104
  },
86
105
  requestId: data?.requestId,
87
- };
106
+ });
88
107
  };
@@ -27,33 +27,49 @@ function createClient(config) {
27
27
  const headers = {
28
28
  ...(requestInit?.headers ?? {}),
29
29
  ...authHeaders.headers,
30
+ ...config.host?.essentials?.passThroughHeaders,
30
31
  ...(_headers[exports.X_WIX_CONSISTENT_HEADER]
31
32
  ? { [exports.X_WIX_CONSISTENT_HEADER]: _headers[exports.X_WIX_CONSISTENT_HEADER] }
32
33
  : {}),
33
34
  };
34
- if (typeof urlOrRequest === 'string' || urlOrRequest instanceof URL) {
35
- const response = await fetch(urlOrRequest, {
36
- ...requestInit,
37
- headers,
38
- });
39
- const consistentHeader = findConsistentHeader(response);
40
- if (consistentHeader) {
41
- _headers[exports.X_WIX_CONSISTENT_HEADER] = consistentHeader;
42
- }
43
- return response;
44
- }
45
- else {
46
- for (const [k, v] of Object.entries(headers)) {
47
- if (typeof v === 'string') {
48
- urlOrRequest.headers.set(k, v);
35
+ const errorHandler = config.host?.getErrorHandler?.();
36
+ try {
37
+ if (typeof urlOrRequest === 'string' || urlOrRequest instanceof URL) {
38
+ const response = await fetch(urlOrRequest, {
39
+ ...requestInit,
40
+ headers,
41
+ });
42
+ errorHandler?.handleError(response, {
43
+ requestOptions: requestInit,
44
+ });
45
+ const consistentHeader = findConsistentHeader(response);
46
+ if (consistentHeader) {
47
+ _headers[exports.X_WIX_CONSISTENT_HEADER] = consistentHeader;
49
48
  }
49
+ return response;
50
50
  }
51
- const response = await fetch(urlOrRequest, requestInit);
52
- const consistentHeader = findConsistentHeader(response);
53
- if (consistentHeader) {
54
- _headers[exports.X_WIX_CONSISTENT_HEADER] = consistentHeader;
51
+ else {
52
+ for (const [k, v] of Object.entries(headers)) {
53
+ if (typeof v === 'string') {
54
+ urlOrRequest.headers.set(k, v);
55
+ }
56
+ }
57
+ const response = await fetch(urlOrRequest, requestInit);
58
+ errorHandler?.handleError(response, {
59
+ requestOptions: requestInit,
60
+ });
61
+ const consistentHeader = findConsistentHeader(response);
62
+ if (consistentHeader) {
63
+ _headers[exports.X_WIX_CONSISTENT_HEADER] = consistentHeader;
64
+ }
65
+ return response;
55
66
  }
56
- return response;
67
+ }
68
+ catch (e) {
69
+ errorHandler?.handleError(e, {
70
+ requestOptions: requestInit,
71
+ });
72
+ throw e;
57
73
  }
58
74
  };
59
75
  const { client: servicePluginsClient, initModule: initServicePluginModule } = (0, service_plugin_modules_js_1.servicePluginsModules)(authStrategy);
@@ -69,6 +85,10 @@ function createClient(config) {
69
85
  ...authHeaders?.headers,
70
86
  ...options?.headers,
71
87
  ...config.host?.essentials?.passThroughHeaders,
88
+ // Ensure consistent header always takes precedence
89
+ ...(_headers[exports.X_WIX_CONSISTENT_HEADER]
90
+ ? { [exports.X_WIX_CONSISTENT_HEADER]: _headers[exports.X_WIX_CONSISTENT_HEADER] }
91
+ : {}),
72
92
  },
73
93
  });
74
94
  const consistentHeader = findConsistentHeader(response);
@@ -101,7 +121,7 @@ function createClient(config) {
101
121
  const shouldUseCDN = config.useCDN === undefined ? config.auth?.shouldUseCDN : config.useCDN;
102
122
  return (0, rest_modules_js_1.buildRESTDescriptor)((0, context_1.runWithoutContext)(() => (0, ambassador_modules_js_1.isAmbassadorModule)(modules))
103
123
  ? (0, ambassador_modules_js_1.toHTTPModule)(modules)
104
- : modules, metadata ?? {}, boundFetch, (relativeUrl, fetchOptions) => {
124
+ : modules, metadata ?? {}, boundFetch, config.host?.getErrorHandler?.(), (relativeUrl, fetchOptions) => {
105
125
  const finalUrl = new URL(relativeUrl, `https://${apiBaseUrl}`);
106
126
  finalUrl.host = apiBaseUrl;
107
127
  finalUrl.protocol = 'https';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@wix/sdk",
3
- "version": "1.15.23",
3
+ "version": "1.15.25",
4
4
  "license": "MIT",
5
5
  "author": {
6
6
  "name": "Ronny Ringel",
@@ -73,11 +73,11 @@
73
73
  },
74
74
  "dependencies": {
75
75
  "@wix/identity": "^1.0.104",
76
- "@wix/image-kit": "^1.109.0",
76
+ "@wix/image-kit": "^1.111.0",
77
77
  "@wix/redirects": "^1.0.70",
78
78
  "@wix/sdk-context": "0.0.1",
79
- "@wix/sdk-runtime": "0.3.54",
80
- "@wix/sdk-types": "^1.13.30",
79
+ "@wix/sdk-runtime": "0.3.58",
80
+ "@wix/sdk-types": "^1.13.37",
81
81
  "jose": "^5.10.0",
82
82
  "type-fest": "^4.41.0"
83
83
  },
@@ -86,19 +86,19 @@
86
86
  },
87
87
  "devDependencies": {
88
88
  "@types/is-ci": "^3.0.4",
89
- "@types/node": "^20.19.0",
89
+ "@types/node": "^20.19.9",
90
90
  "@vitest/ui": "^1.6.1",
91
91
  "@wix/ecom": "^1.0.886",
92
92
  "@wix/events": "^1.0.382",
93
93
  "@wix/metro": "^1.0.93",
94
94
  "@wix/metro-runtime": "^1.1891.0",
95
- "@wix/sdk-runtime": "0.3.54",
95
+ "@wix/sdk-runtime": "0.3.58",
96
96
  "eslint": "^8.57.1",
97
97
  "eslint-config-sdk": "0.0.0",
98
98
  "graphql": "^16.8.0",
99
99
  "is-ci": "^3.0.1",
100
100
  "jsdom": "^22.1.0",
101
- "msw": "^2.10.0",
101
+ "msw": "^2.10.4",
102
102
  "typescript": "^5.8.3",
103
103
  "vitest": "^1.6.1",
104
104
  "vitest-teamcity-reporter": "^0.3.1"
@@ -126,5 +126,5 @@
126
126
  "wallaby": {
127
127
  "autoDetect": true
128
128
  },
129
- "falconPackageHash": "22fd3f820965fd15526102c903e23d29c268e852ea5a4616a785e03c"
129
+ "falconPackageHash": "5fe116d36737d407ace325165141af2b145afe6613771f7f278ce04a"
130
130
  }