@contello/sdk-client 8.22.0 → 8.22.1

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/dist/index.cjs CHANGED
@@ -1,9 +1,7 @@
1
1
  "use strict";
2
- var __create = Object.create;
3
2
  var __defProp = Object.defineProperty;
4
3
  var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
4
  var __getOwnPropNames = Object.getOwnPropertyNames;
6
- var __getProtoOf = Object.getPrototypeOf;
7
5
  var __hasOwnProp = Object.prototype.hasOwnProperty;
8
6
  var __export = (target, all) => {
9
7
  for (var name in all)
@@ -17,14 +15,6 @@ var __copyProps = (to, from, except, desc) => {
17
15
  }
18
16
  return to;
19
17
  };
20
- var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
- // If the importer is in node compatibility mode or this is not an ESM
22
- // file that has been converted to a CommonJS file using a Babel-
23
- // compatible transform (i.e. "__esModule" has not been set), then set
24
- // "default" to the CommonJS "module.exports" for node compatibility.
25
- isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
- mod
27
- ));
28
18
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
19
 
30
20
  // src/index.ts
@@ -38,61 +28,6 @@ module.exports = __toCommonJS(index_exports);
38
28
  var import_graphql_ws = require("graphql-ws");
39
29
  var import_rxjs2 = require("rxjs");
40
30
 
41
- // src/diagnostics.ts
42
- var import_node_diagnostics_channel = __toESM(require("diagnostics_channel"), 1);
43
- var channels = {
44
- start: "contello:sdk.start",
45
- end: "contello:sdk.end",
46
- error: "contello:sdk.error",
47
- /** subscribe to decorate outgoing GraphQL WebSocket messages (e.g. inject traceparent) */
48
- message: "contello:sdk.message"
49
- };
50
- var onStart = import_node_diagnostics_channel.default.channel(channels.start);
51
- var onEnd = import_node_diagnostics_channel.default.channel(channels.end);
52
- var onError = import_node_diagnostics_channel.default.channel(channels.error);
53
- var onMessage = import_node_diagnostics_channel.default.channel(channels.message);
54
- function hasOperationSubscribers() {
55
- return onStart.hasSubscribers || onEnd.hasSubscribers || onError.hasSubscribers;
56
- }
57
- function getWrap() {
58
- if (!hasOperationSubscribers()) {
59
- return void 0;
60
- }
61
- return (name, fn) => {
62
- const start = performance.now();
63
- onStart.publish({ name });
64
- let result;
65
- try {
66
- result = fn();
67
- } catch (error) {
68
- onError.publish({ name, error, durationMs: performance.now() - start });
69
- throw error;
70
- }
71
- if (result instanceof Promise) {
72
- return result.then(
73
- (r) => {
74
- onEnd.publish({ name, durationMs: performance.now() - start });
75
- return r;
76
- },
77
- (error) => {
78
- onError.publish({ name, error, durationMs: performance.now() - start });
79
- throw error;
80
- }
81
- );
82
- }
83
- onEnd.publish({ name, durationMs: performance.now() - start });
84
- return result;
85
- };
86
- }
87
- function decorateMessage(message) {
88
- if (!onMessage.hasSubscribers) {
89
- return message;
90
- }
91
- const msg = { message };
92
- onMessage.publish(msg);
93
- return msg.message;
94
- }
95
-
96
31
  // src/pool.ts
97
32
  var ConnectionPool = class {
98
33
  constructor(createClient2, poolSize) {
@@ -145,7 +80,7 @@ var ConnectionPool = class {
145
80
 
146
81
  // src/sdk.ts
147
82
  var import_rxjs = require("rxjs");
148
- var createSdk = (client, getSdk) => {
83
+ var createSdk = (client, middlewares, getSdk) => {
149
84
  return getSdk((doc, vars, options) => {
150
85
  if (options) {
151
86
  console.warn("options are not supported yet");
@@ -162,16 +97,28 @@ var createSdk = (client, getSdk) => {
162
97
  throw new Error("No operation definition found");
163
98
  }
164
99
  const kind = operationDef.operation;
165
- const execute = () => {
100
+ const executeRequest = () => {
166
101
  const wsClient = client();
167
- const res = new import_rxjs.Observable((obs) => wsClient.subscribe({ query, variables: vars }, obs));
168
- return kind !== "subscription" ? (0, import_rxjs.firstValueFrom)(res) : res;
102
+ return new import_rxjs.Observable((obs) => wsClient.subscribe({ query, variables: vars }, obs));
169
103
  };
170
- const wrapFn = getWrap();
171
- if (wrapFn && kind !== "subscription") {
172
- return wrapFn(`sdk:${operationDef.name?.value ?? ""}`, execute);
173
- }
174
- return execute();
104
+ const request = {
105
+ kind,
106
+ operationName: operationDef.name?.value ?? "",
107
+ query,
108
+ variables: vars || {}
109
+ };
110
+ const executeWithMiddlewares = (mws, index) => {
111
+ if (index >= mws.length) {
112
+ return executeRequest();
113
+ }
114
+ const middleware = mws[index];
115
+ if (middleware?.onRequest) {
116
+ return middleware.onRequest(request, () => executeWithMiddlewares(mws, index + 1));
117
+ }
118
+ return executeWithMiddlewares(mws, index + 1);
119
+ };
120
+ const res = executeWithMiddlewares(middlewares, 0);
121
+ return kind !== "subscription" ? (0, import_rxjs.firstValueFrom)(res) : res;
175
122
  });
176
123
  };
177
124
 
@@ -194,7 +141,13 @@ var ContelloSdkClient = class {
194
141
  shouldRetry: () => true,
195
142
  jsonMessageReplacer: (key, value) => {
196
143
  if (!key) {
197
- return decorateMessage(value);
144
+ let message = value;
145
+ for (const middleware of params.middlewares ?? []) {
146
+ if (middleware.onOutgoingMessage) {
147
+ message = middleware.onOutgoingMessage(message);
148
+ }
149
+ }
150
+ return message;
198
151
  }
199
152
  return value;
200
153
  },
@@ -213,7 +166,7 @@ var ContelloSdkClient = class {
213
166
  },
214
167
  pooling?.enabled === false ? 1 : pooling?.size ?? 5
215
168
  );
216
- this._sdk = { sdk: createSdk(() => this._pool.get(), getSdk) };
169
+ this._sdk = { sdk: createSdk(() => this._pool.get(), params.middlewares ?? [], getSdk) };
217
170
  }
218
171
  get sdk() {
219
172
  return this._sdk.sdk;
@@ -224,24 +177,14 @@ var ContelloSdkClient = class {
224
177
  async disconnect() {
225
178
  await this._pool.disconnect();
226
179
  }
227
- /**
228
- * Subscribes to a raw GraphQL operation against the connection pool.
229
- * Returns an Observable that emits each result's data.
230
- */
231
180
  subscribe(query, variables) {
232
181
  const wsClient = this._pool.get();
233
182
  return new import_rxjs2.Observable((obs) => wsClient.subscribe({ query, variables }, obs)).pipe(
234
183
  (0, import_rxjs2.map)((r) => r.data)
235
184
  );
236
185
  }
237
- /**
238
- * Executes a raw GraphQL query/mutation and returns the first result.
239
- * Convenience wrapper around subscribe + firstValueFrom.
240
- */
241
186
  execute(query, variables) {
242
- const fn = () => (0, import_rxjs2.firstValueFrom)(this.subscribe(query, variables));
243
- const wrap = getWrap();
244
- return wrap ? wrap("sdk:execute", fn) : fn();
187
+ return (0, import_rxjs2.firstValueFrom)(this.subscribe(query, variables));
245
188
  }
246
189
  };
247
190
  // Annotate the CommonJS export names for ESM import in node:
package/dist/index.d.cts CHANGED
@@ -1,6 +1,16 @@
1
1
  import { Observable } from 'rxjs';
2
2
  import { DocumentNode, ExecutionResult } from 'graphql';
3
3
 
4
+ type ContelloSdkClientMiddleware = {
5
+ onRequest?: (request: {
6
+ kind: 'query' | 'mutation' | 'subscription';
7
+ operationName: string;
8
+ query: string;
9
+ variables: Record<string, any>;
10
+ }, next: () => Observable<any>) => Observable<any>;
11
+ onOutgoingMessage?: (message: any) => any;
12
+ };
13
+
4
14
  type Requester<C = any, E = unknown> = <R, V>(doc: DocumentNode, vars?: V, options?: C) => Promise<ExecutionResult<R, E>> | Observable<ExecutionResult<R, E>>;
5
15
 
6
16
  type ClientEventContext = {
@@ -11,6 +21,7 @@ type ContelloSdkClientParams = {
11
21
  url: string;
12
22
  project: string;
13
23
  token: string;
24
+ middlewares?: ContelloSdkClientMiddleware[] | undefined;
14
25
  pooling?: {
15
26
  enabled?: boolean | undefined;
16
27
  size?: number | undefined;
@@ -34,16 +45,8 @@ declare class ContelloSdkClient<T> {
34
45
  get sdk(): T;
35
46
  connect(): Promise<void>;
36
47
  disconnect(): Promise<void>;
37
- /**
38
- * Subscribes to a raw GraphQL operation against the connection pool.
39
- * Returns an Observable that emits each result's data.
40
- */
41
48
  subscribe<TData>(query: string, variables?: Record<string, unknown> | undefined): Observable<TData>;
42
- /**
43
- * Executes a raw GraphQL query/mutation and returns the first result.
44
- * Convenience wrapper around subscribe + firstValueFrom.
45
- */
46
49
  execute<TData>(query: string, variables?: Record<string, unknown> | undefined): Promise<TData>;
47
50
  }
48
51
 
49
- export { ContelloSdkClient, type ContelloSdkClientParams };
52
+ export { ContelloSdkClient, type ContelloSdkClientMiddleware, type ContelloSdkClientParams };
package/dist/index.d.ts CHANGED
@@ -1,6 +1,16 @@
1
1
  import { Observable } from 'rxjs';
2
2
  import { DocumentNode, ExecutionResult } from 'graphql';
3
3
 
4
+ type ContelloSdkClientMiddleware = {
5
+ onRequest?: (request: {
6
+ kind: 'query' | 'mutation' | 'subscription';
7
+ operationName: string;
8
+ query: string;
9
+ variables: Record<string, any>;
10
+ }, next: () => Observable<any>) => Observable<any>;
11
+ onOutgoingMessage?: (message: any) => any;
12
+ };
13
+
4
14
  type Requester<C = any, E = unknown> = <R, V>(doc: DocumentNode, vars?: V, options?: C) => Promise<ExecutionResult<R, E>> | Observable<ExecutionResult<R, E>>;
5
15
 
6
16
  type ClientEventContext = {
@@ -11,6 +21,7 @@ type ContelloSdkClientParams = {
11
21
  url: string;
12
22
  project: string;
13
23
  token: string;
24
+ middlewares?: ContelloSdkClientMiddleware[] | undefined;
14
25
  pooling?: {
15
26
  enabled?: boolean | undefined;
16
27
  size?: number | undefined;
@@ -34,16 +45,8 @@ declare class ContelloSdkClient<T> {
34
45
  get sdk(): T;
35
46
  connect(): Promise<void>;
36
47
  disconnect(): Promise<void>;
37
- /**
38
- * Subscribes to a raw GraphQL operation against the connection pool.
39
- * Returns an Observable that emits each result's data.
40
- */
41
48
  subscribe<TData>(query: string, variables?: Record<string, unknown> | undefined): Observable<TData>;
42
- /**
43
- * Executes a raw GraphQL query/mutation and returns the first result.
44
- * Convenience wrapper around subscribe + firstValueFrom.
45
- */
46
49
  execute<TData>(query: string, variables?: Record<string, unknown> | undefined): Promise<TData>;
47
50
  }
48
51
 
49
- export { ContelloSdkClient, type ContelloSdkClientParams };
52
+ export { ContelloSdkClient, type ContelloSdkClientMiddleware, type ContelloSdkClientParams };
package/dist/index.js CHANGED
@@ -2,61 +2,6 @@
2
2
  import { createClient } from "graphql-ws";
3
3
  import { Observable as Observable2, firstValueFrom as firstValueFrom2, map } from "rxjs";
4
4
 
5
- // src/diagnostics.ts
6
- import dc from "diagnostics_channel";
7
- var channels = {
8
- start: "contello:sdk.start",
9
- end: "contello:sdk.end",
10
- error: "contello:sdk.error",
11
- /** subscribe to decorate outgoing GraphQL WebSocket messages (e.g. inject traceparent) */
12
- message: "contello:sdk.message"
13
- };
14
- var onStart = dc.channel(channels.start);
15
- var onEnd = dc.channel(channels.end);
16
- var onError = dc.channel(channels.error);
17
- var onMessage = dc.channel(channels.message);
18
- function hasOperationSubscribers() {
19
- return onStart.hasSubscribers || onEnd.hasSubscribers || onError.hasSubscribers;
20
- }
21
- function getWrap() {
22
- if (!hasOperationSubscribers()) {
23
- return void 0;
24
- }
25
- return (name, fn) => {
26
- const start = performance.now();
27
- onStart.publish({ name });
28
- let result;
29
- try {
30
- result = fn();
31
- } catch (error) {
32
- onError.publish({ name, error, durationMs: performance.now() - start });
33
- throw error;
34
- }
35
- if (result instanceof Promise) {
36
- return result.then(
37
- (r) => {
38
- onEnd.publish({ name, durationMs: performance.now() - start });
39
- return r;
40
- },
41
- (error) => {
42
- onError.publish({ name, error, durationMs: performance.now() - start });
43
- throw error;
44
- }
45
- );
46
- }
47
- onEnd.publish({ name, durationMs: performance.now() - start });
48
- return result;
49
- };
50
- }
51
- function decorateMessage(message) {
52
- if (!onMessage.hasSubscribers) {
53
- return message;
54
- }
55
- const msg = { message };
56
- onMessage.publish(msg);
57
- return msg.message;
58
- }
59
-
60
5
  // src/pool.ts
61
6
  var ConnectionPool = class {
62
7
  constructor(createClient2, poolSize) {
@@ -109,7 +54,7 @@ var ConnectionPool = class {
109
54
 
110
55
  // src/sdk.ts
111
56
  import { Observable, firstValueFrom } from "rxjs";
112
- var createSdk = (client, getSdk) => {
57
+ var createSdk = (client, middlewares, getSdk) => {
113
58
  return getSdk((doc, vars, options) => {
114
59
  if (options) {
115
60
  console.warn("options are not supported yet");
@@ -126,16 +71,28 @@ var createSdk = (client, getSdk) => {
126
71
  throw new Error("No operation definition found");
127
72
  }
128
73
  const kind = operationDef.operation;
129
- const execute = () => {
74
+ const executeRequest = () => {
130
75
  const wsClient = client();
131
- const res = new Observable((obs) => wsClient.subscribe({ query, variables: vars }, obs));
132
- return kind !== "subscription" ? firstValueFrom(res) : res;
76
+ return new Observable((obs) => wsClient.subscribe({ query, variables: vars }, obs));
133
77
  };
134
- const wrapFn = getWrap();
135
- if (wrapFn && kind !== "subscription") {
136
- return wrapFn(`sdk:${operationDef.name?.value ?? ""}`, execute);
137
- }
138
- return execute();
78
+ const request = {
79
+ kind,
80
+ operationName: operationDef.name?.value ?? "",
81
+ query,
82
+ variables: vars || {}
83
+ };
84
+ const executeWithMiddlewares = (mws, index) => {
85
+ if (index >= mws.length) {
86
+ return executeRequest();
87
+ }
88
+ const middleware = mws[index];
89
+ if (middleware?.onRequest) {
90
+ return middleware.onRequest(request, () => executeWithMiddlewares(mws, index + 1));
91
+ }
92
+ return executeWithMiddlewares(mws, index + 1);
93
+ };
94
+ const res = executeWithMiddlewares(middlewares, 0);
95
+ return kind !== "subscription" ? firstValueFrom(res) : res;
139
96
  });
140
97
  };
141
98
 
@@ -158,7 +115,13 @@ var ContelloSdkClient = class {
158
115
  shouldRetry: () => true,
159
116
  jsonMessageReplacer: (key, value) => {
160
117
  if (!key) {
161
- return decorateMessage(value);
118
+ let message = value;
119
+ for (const middleware of params.middlewares ?? []) {
120
+ if (middleware.onOutgoingMessage) {
121
+ message = middleware.onOutgoingMessage(message);
122
+ }
123
+ }
124
+ return message;
162
125
  }
163
126
  return value;
164
127
  },
@@ -177,7 +140,7 @@ var ContelloSdkClient = class {
177
140
  },
178
141
  pooling?.enabled === false ? 1 : pooling?.size ?? 5
179
142
  );
180
- this._sdk = { sdk: createSdk(() => this._pool.get(), getSdk) };
143
+ this._sdk = { sdk: createSdk(() => this._pool.get(), params.middlewares ?? [], getSdk) };
181
144
  }
182
145
  get sdk() {
183
146
  return this._sdk.sdk;
@@ -188,24 +151,14 @@ var ContelloSdkClient = class {
188
151
  async disconnect() {
189
152
  await this._pool.disconnect();
190
153
  }
191
- /**
192
- * Subscribes to a raw GraphQL operation against the connection pool.
193
- * Returns an Observable that emits each result's data.
194
- */
195
154
  subscribe(query, variables) {
196
155
  const wsClient = this._pool.get();
197
156
  return new Observable2((obs) => wsClient.subscribe({ query, variables }, obs)).pipe(
198
157
  map((r) => r.data)
199
158
  );
200
159
  }
201
- /**
202
- * Executes a raw GraphQL query/mutation and returns the first result.
203
- * Convenience wrapper around subscribe + firstValueFrom.
204
- */
205
160
  execute(query, variables) {
206
- const fn = () => firstValueFrom2(this.subscribe(query, variables));
207
- const wrap = getWrap();
208
- return wrap ? wrap("sdk:execute", fn) : fn();
161
+ return firstValueFrom2(this.subscribe(query, variables));
209
162
  }
210
163
  };
211
164
  export {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@contello/sdk-client",
3
- "version": "8.22.0",
3
+ "version": "8.22.1",
4
4
  "description": "GraphQL SDK client for Contello CMS with WebSocket transport and connection pooling",
5
5
  "type": "module",
6
6
  "license": "MIT",
@@ -17,7 +17,7 @@
17
17
  "typecheck": "tsc --noEmit"
18
18
  },
19
19
  "peerDependencies": {
20
- "rxjs": ">=5.0.0",
20
+ "rxjs": "^7.0.0",
21
21
  "graphql": ">=16.0.0",
22
22
  "graphql-ws": ">=5.0.0"
23
23
  },