@wix/sdk 1.4.1 → 1.5.0

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.
@@ -186,6 +186,42 @@ var errorBuilder = (code, description, details, data) => {
186
186
  };
187
187
  };
188
188
 
189
+ // src/fetch-error.ts
190
+ var FetchErrorResponse = class extends Error {
191
+ constructor(message, response) {
192
+ super(message);
193
+ this.message = message;
194
+ this.response = response;
195
+ }
196
+ async details() {
197
+ const dataError = await this.response.json();
198
+ return errorBuilder2(
199
+ this.response.status,
200
+ dataError == null ? void 0 : dataError.message,
201
+ dataError == null ? void 0 : dataError.details,
202
+ {
203
+ requestId: this.response.headers.get("X-Wix-Request-Id"),
204
+ details: dataError
205
+ }
206
+ );
207
+ }
208
+ };
209
+ var errorBuilder2 = (code, description, details, data) => {
210
+ return {
211
+ details: {
212
+ ...!(details == null ? void 0 : details.validationError) && {
213
+ applicationError: {
214
+ description,
215
+ code,
216
+ data
217
+ }
218
+ },
219
+ ...details
220
+ },
221
+ message: description
222
+ };
223
+ };
224
+
189
225
  // src/wixClient.ts
190
226
  function createClient(config) {
191
227
  const _headers = config.headers || { Authorization: "" };
@@ -247,6 +283,23 @@ function createClient(config) {
247
283
  finalUrl.host = API_URL;
248
284
  finalUrl.protocol = "https";
249
285
  return boundFetch(finalUrl, options);
286
+ },
287
+ async graphql(query, variables) {
288
+ const res = await boundFetch(`https://${API_URL}/graphql`, {
289
+ method: "POST",
290
+ headers: {
291
+ "Content-Type": "application/json"
292
+ },
293
+ body: JSON.stringify({ query, variables })
294
+ });
295
+ if (res.status !== 200) {
296
+ throw new FetchErrorResponse(
297
+ `GraphQL request failed with status ${res.status}`,
298
+ res
299
+ );
300
+ }
301
+ const { data, errors } = await res.json();
302
+ return { data: data != null ? data : {}, errors };
250
303
  }
251
304
  };
252
305
  }
package/build/index.d.mts CHANGED
@@ -38,6 +38,45 @@ type AmbassadorFactory<Request, Response> = {
38
38
  type AmbassadorFunctionDescriptor<Request = any, Response = any> = AmbassadorFactory<Request, Response>;
39
39
  type BuildAmbassadorFunction<T extends AmbassadorFunctionDescriptor> = T extends AmbassadorFunctionDescriptor<infer Request, infer Response> ? (req: Request) => Promise<Response> : never;
40
40
 
41
+ /**
42
+ * Represents a location in a Source.
43
+ */
44
+ interface SourceLocation {
45
+ readonly line: number;
46
+ readonly column: number;
47
+ }
48
+
49
+ /**
50
+ * See: https://spec.graphql.org/draft/#sec-Errors
51
+ */
52
+ interface GraphQLFormattedError {
53
+ /**
54
+ * A short, human-readable summary of the problem that **SHOULD NOT** change
55
+ * from occurrence to occurrence of the problem, except for purposes of
56
+ * localization.
57
+ */
58
+ readonly message: string;
59
+ /**
60
+ * If an error can be associated to a particular point in the requested
61
+ * GraphQL document, it should contain a list of locations.
62
+ */
63
+ readonly locations?: ReadonlyArray<SourceLocation>;
64
+ /**
65
+ * If an error can be associated to a particular field in the GraphQL result,
66
+ * it _must_ contain an entry with the key `path` that details the path of
67
+ * the response field which experienced the error. This allows clients to
68
+ * identify whether a null result is intentional or caused by a runtime error.
69
+ */
70
+ readonly path?: ReadonlyArray<string | number>;
71
+ /**
72
+ * Reserved for implementors to extend the protocol however they see fit,
73
+ * and hence there are no additional restrictions on its contents.
74
+ */
75
+ readonly extensions?: {
76
+ [key: string]: unknown;
77
+ };
78
+ }
79
+
41
80
  type Headers = {
42
81
  Authorization: string;
43
82
  } & Record<string, string>;
@@ -72,11 +111,31 @@ type Descriptors = RESTFunctionDescriptor | AmbassadorFunctionDescriptor | HostM
72
111
  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 : {
73
112
  [Key in keyof T]: T[Key] extends Descriptors ? AssertHostMatches<T[Key], H> : T[Key];
74
113
  };
114
+ type TypedQueryInput<Result = {
115
+ [key: string]: any;
116
+ }, Variables = {
117
+ [key: string]: any;
118
+ }> = {
119
+ /**
120
+ * Type to support `@graphql-typed-document-node/core`
121
+ * @internal
122
+ */
123
+ __apiType?: (variables: Variables) => Result;
124
+ /**
125
+ * Type to support `TypedQueryDocumentNode` from `graphql`
126
+ * @internal
127
+ */
128
+ __ensureTypesOfVariablesAndResultMatching?: (variables: Variables) => Result;
129
+ };
75
130
  type WixClient<H extends Host<any> | undefined = undefined, Z extends AuthenticationStrategy<H> = AuthenticationStrategy<H>, T extends Descriptors = Descriptors> = {
76
131
  setHeaders(headers: Headers): void;
77
132
  auth: Z;
78
133
  fetch(relativeUrl: string, options: RequestInit): Promise<Response>;
79
134
  use<R extends Descriptors = EmptyObject>(modules: H extends Host<any> ? AssertHostMatches<R, H> : R): BuildDescriptors<R, H>;
135
+ graphql<Result, Variables>(query: string | (string & TypedQueryInput<Result, Variables>), variables?: Variables): Promise<{
136
+ data: Result;
137
+ errors?: GraphQLFormattedError[];
138
+ }>;
80
139
  } & BuildDescriptors<T, H>;
81
140
  declare function createClient<H extends Host<any> | undefined = undefined, Z extends AuthenticationStrategy<H> = AuthenticationStrategy<H>, T extends Descriptors = EmptyObject>(config: {
82
141
  modules?: H extends Host<any> ? AssertHostMatches<T, H> : T;
package/build/index.d.ts CHANGED
@@ -38,6 +38,45 @@ type AmbassadorFactory<Request, Response> = {
38
38
  type AmbassadorFunctionDescriptor<Request = any, Response = any> = AmbassadorFactory<Request, Response>;
39
39
  type BuildAmbassadorFunction<T extends AmbassadorFunctionDescriptor> = T extends AmbassadorFunctionDescriptor<infer Request, infer Response> ? (req: Request) => Promise<Response> : never;
40
40
 
41
+ /**
42
+ * Represents a location in a Source.
43
+ */
44
+ interface SourceLocation {
45
+ readonly line: number;
46
+ readonly column: number;
47
+ }
48
+
49
+ /**
50
+ * See: https://spec.graphql.org/draft/#sec-Errors
51
+ */
52
+ interface GraphQLFormattedError {
53
+ /**
54
+ * A short, human-readable summary of the problem that **SHOULD NOT** change
55
+ * from occurrence to occurrence of the problem, except for purposes of
56
+ * localization.
57
+ */
58
+ readonly message: string;
59
+ /**
60
+ * If an error can be associated to a particular point in the requested
61
+ * GraphQL document, it should contain a list of locations.
62
+ */
63
+ readonly locations?: ReadonlyArray<SourceLocation>;
64
+ /**
65
+ * If an error can be associated to a particular field in the GraphQL result,
66
+ * it _must_ contain an entry with the key `path` that details the path of
67
+ * the response field which experienced the error. This allows clients to
68
+ * identify whether a null result is intentional or caused by a runtime error.
69
+ */
70
+ readonly path?: ReadonlyArray<string | number>;
71
+ /**
72
+ * Reserved for implementors to extend the protocol however they see fit,
73
+ * and hence there are no additional restrictions on its contents.
74
+ */
75
+ readonly extensions?: {
76
+ [key: string]: unknown;
77
+ };
78
+ }
79
+
41
80
  type Headers = {
42
81
  Authorization: string;
43
82
  } & Record<string, string>;
@@ -72,11 +111,31 @@ type Descriptors = RESTFunctionDescriptor | AmbassadorFunctionDescriptor | HostM
72
111
  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 : {
73
112
  [Key in keyof T]: T[Key] extends Descriptors ? AssertHostMatches<T[Key], H> : T[Key];
74
113
  };
114
+ type TypedQueryInput<Result = {
115
+ [key: string]: any;
116
+ }, Variables = {
117
+ [key: string]: any;
118
+ }> = {
119
+ /**
120
+ * Type to support `@graphql-typed-document-node/core`
121
+ * @internal
122
+ */
123
+ __apiType?: (variables: Variables) => Result;
124
+ /**
125
+ * Type to support `TypedQueryDocumentNode` from `graphql`
126
+ * @internal
127
+ */
128
+ __ensureTypesOfVariablesAndResultMatching?: (variables: Variables) => Result;
129
+ };
75
130
  type WixClient<H extends Host<any> | undefined = undefined, Z extends AuthenticationStrategy<H> = AuthenticationStrategy<H>, T extends Descriptors = Descriptors> = {
76
131
  setHeaders(headers: Headers): void;
77
132
  auth: Z;
78
133
  fetch(relativeUrl: string, options: RequestInit): Promise<Response>;
79
134
  use<R extends Descriptors = EmptyObject>(modules: H extends Host<any> ? AssertHostMatches<R, H> : R): BuildDescriptors<R, H>;
135
+ graphql<Result, Variables>(query: string | (string & TypedQueryInput<Result, Variables>), variables?: Variables): Promise<{
136
+ data: Result;
137
+ errors?: GraphQLFormattedError[];
138
+ }>;
80
139
  } & BuildDescriptors<T, H>;
81
140
  declare function createClient<H extends Host<any> | undefined = undefined, Z extends AuthenticationStrategy<H> = AuthenticationStrategy<H>, T extends Descriptors = EmptyObject>(config: {
82
141
  modules?: H extends Host<any> ? AssertHostMatches<T, H> : T;
package/build/index.js CHANGED
@@ -230,6 +230,42 @@ var errorBuilder = (code, description, details, data) => {
230
230
  };
231
231
  };
232
232
 
233
+ // src/fetch-error.ts
234
+ var FetchErrorResponse = class extends Error {
235
+ constructor(message, response) {
236
+ super(message);
237
+ this.message = message;
238
+ this.response = response;
239
+ }
240
+ async details() {
241
+ const dataError = await this.response.json();
242
+ return errorBuilder2(
243
+ this.response.status,
244
+ dataError?.message,
245
+ dataError?.details,
246
+ {
247
+ requestId: this.response.headers.get("X-Wix-Request-Id"),
248
+ details: dataError
249
+ }
250
+ );
251
+ }
252
+ };
253
+ var errorBuilder2 = (code, description, details, data) => {
254
+ return {
255
+ details: {
256
+ ...!details?.validationError && {
257
+ applicationError: {
258
+ description,
259
+ code,
260
+ data
261
+ }
262
+ },
263
+ ...details
264
+ },
265
+ message: description
266
+ };
267
+ };
268
+
233
269
  // src/wixClient.ts
234
270
  function createClient(config) {
235
271
  const _headers = config.headers || { Authorization: "" };
@@ -291,6 +327,23 @@ function createClient(config) {
291
327
  finalUrl.host = API_URL;
292
328
  finalUrl.protocol = "https";
293
329
  return boundFetch(finalUrl, options);
330
+ },
331
+ async graphql(query, variables) {
332
+ const res = await boundFetch(`https://${API_URL}/graphql`, {
333
+ method: "POST",
334
+ headers: {
335
+ "Content-Type": "application/json"
336
+ },
337
+ body: JSON.stringify({ query, variables })
338
+ });
339
+ if (res.status !== 200) {
340
+ throw new FetchErrorResponse(
341
+ `GraphQL request failed with status ${res.status}`,
342
+ res
343
+ );
344
+ }
345
+ const { data, errors } = await res.json();
346
+ return { data: data ?? {}, errors };
294
347
  }
295
348
  };
296
349
  }
package/build/index.mjs CHANGED
@@ -184,6 +184,42 @@ var errorBuilder = (code, description, details, data) => {
184
184
  };
185
185
  };
186
186
 
187
+ // src/fetch-error.ts
188
+ var FetchErrorResponse = class extends Error {
189
+ constructor(message, response) {
190
+ super(message);
191
+ this.message = message;
192
+ this.response = response;
193
+ }
194
+ async details() {
195
+ const dataError = await this.response.json();
196
+ return errorBuilder2(
197
+ this.response.status,
198
+ dataError?.message,
199
+ dataError?.details,
200
+ {
201
+ requestId: this.response.headers.get("X-Wix-Request-Id"),
202
+ details: dataError
203
+ }
204
+ );
205
+ }
206
+ };
207
+ var errorBuilder2 = (code, description, details, data) => {
208
+ return {
209
+ details: {
210
+ ...!details?.validationError && {
211
+ applicationError: {
212
+ description,
213
+ code,
214
+ data
215
+ }
216
+ },
217
+ ...details
218
+ },
219
+ message: description
220
+ };
221
+ };
222
+
187
223
  // src/wixClient.ts
188
224
  function createClient(config) {
189
225
  const _headers = config.headers || { Authorization: "" };
@@ -245,6 +281,23 @@ function createClient(config) {
245
281
  finalUrl.host = API_URL;
246
282
  finalUrl.protocol = "https";
247
283
  return boundFetch(finalUrl, options);
284
+ },
285
+ async graphql(query, variables) {
286
+ const res = await boundFetch(`https://${API_URL}/graphql`, {
287
+ method: "POST",
288
+ headers: {
289
+ "Content-Type": "application/json"
290
+ },
291
+ body: JSON.stringify({ query, variables })
292
+ });
293
+ if (res.status !== 200) {
294
+ throw new FetchErrorResponse(
295
+ `GraphQL request failed with status ${res.status}`,
296
+ res
297
+ );
298
+ }
299
+ const { data, errors } = await res.json();
300
+ return { data: data ?? {}, errors };
248
301
  }
249
302
  };
250
303
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@wix/sdk",
3
- "version": "1.4.1",
3
+ "version": "1.5.0",
4
4
  "license": "UNLICENSED",
5
5
  "author": {
6
6
  "name": "Ronny Ringel",
@@ -35,22 +35,26 @@
35
35
  "@wix/image-kit": "^1.37.0",
36
36
  "@wix/metro-runtime": "^1.1529.0",
37
37
  "@wix/redirects": "^1.0.24",
38
- "@wix/sdk-types": "1.4.0",
38
+ "@wix/sdk-types": "1.5.0",
39
39
  "pkce-challenge": "^3.1.0",
40
40
  "querystring": "^0.2.1",
41
41
  "type-fest": "^3.13.1"
42
42
  },
43
+ "optionalDependencies": {
44
+ "graphql": "^0.8.0 || ^0.9.0 || ^0.10.0 || ^0.11.0 || ^0.12.0 || ^0.13.0 || ^14.0.0 || ^15.0.0 || ^16.0.0 || ^17.0.0"
45
+ },
43
46
  "devDependencies": {
44
47
  "@swc/core": "^1.3.83",
45
48
  "@swc/jest": "^0.2.29",
46
49
  "@types/jest": "^27.5.2",
47
50
  "@types/node": "^16.18.50",
48
51
  "@types/node-fetch": "^2.6.4",
49
- "@wix/ecom": "^1.0.351",
52
+ "@wix/ecom": "^1.0.353",
50
53
  "@wix/events": "^1.0.117",
51
54
  "@wix/metro": "^1.0.67",
52
55
  "eslint": "^7.32.0",
53
56
  "eslint-config-sdk": "0.0.0",
57
+ "graphql": "16.8.0",
54
58
  "is-ci": "^3.0.1",
55
59
  "jest": "^27.5.1",
56
60
  "jest-teamcity": "^1.11.0",
@@ -83,5 +87,5 @@
83
87
  "wallaby": {
84
88
  "autoDetect": true
85
89
  },
86
- "falconPackageHash": "9b6b240d14e5fef75a1df39838bcd3c97317d7c97d86f43073bb4306"
90
+ "falconPackageHash": "979b294b107b16bf496a7f4a836bf08aae79eb2ceba6528b9424a8a6"
87
91
  }