@contello/sdk-client 8.21.0 → 8.22.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.
- package/README.md +124 -0
- package/dist/index.cjs +250 -0
- package/dist/index.d.cts +49 -0
- package/dist/index.d.ts +49 -53
- package/dist/index.js +176 -972
- package/package.json +19 -24
- package/dist/index.umd.cjs +0 -3
package/README.md
ADDED
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
# @contello/sdk-client
|
|
2
|
+
|
|
3
|
+
GraphQL SDK client for Contello CMS with WebSocket transport and connection pooling.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install @contello/sdk-client rxjs graphql graphql-ws
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Usage
|
|
12
|
+
|
|
13
|
+
### Basic setup
|
|
14
|
+
|
|
15
|
+
```ts
|
|
16
|
+
import { ContelloSdkClient } from '@contello/sdk-client';
|
|
17
|
+
import { getSdk } from './generated-sdk';
|
|
18
|
+
|
|
19
|
+
const client = new ContelloSdkClient(getSdk, {
|
|
20
|
+
url: 'https://example.com',
|
|
21
|
+
project: 'myProjectId',
|
|
22
|
+
token: 'app-a123b456c7890d123456789',
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
await client.connect();
|
|
26
|
+
|
|
27
|
+
const result = await client.sdk.someQuery({ id: '123' });
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
### Connection pooling
|
|
31
|
+
|
|
32
|
+
By default, the client maintains a pool of 5 WebSocket connections and round-robins requests across them.
|
|
33
|
+
|
|
34
|
+
```ts
|
|
35
|
+
const client = new ContelloSdkClient(getSdk, {
|
|
36
|
+
url: 'https://example.com',
|
|
37
|
+
project: 'myProjectId',
|
|
38
|
+
token: 'token',
|
|
39
|
+
pooling: {
|
|
40
|
+
size: 10,
|
|
41
|
+
},
|
|
42
|
+
});
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
To disable pooling:
|
|
46
|
+
|
|
47
|
+
```ts
|
|
48
|
+
pooling: {
|
|
49
|
+
enabled: false;
|
|
50
|
+
}
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
### Middleware
|
|
54
|
+
|
|
55
|
+
Middlewares can intercept requests and outgoing WebSocket messages.
|
|
56
|
+
|
|
57
|
+
```ts
|
|
58
|
+
import type { ContelloSdkClientMiddleware } from '@contello/sdk-client';
|
|
59
|
+
|
|
60
|
+
const loggingMiddleware: ContelloSdkClientMiddleware = {
|
|
61
|
+
onRequest(request, next) {
|
|
62
|
+
console.log(`${request.kind}: ${request.operationName}`);
|
|
63
|
+
|
|
64
|
+
return next();
|
|
65
|
+
},
|
|
66
|
+
};
|
|
67
|
+
|
|
68
|
+
const client = new ContelloSdkClient(getSdk, {
|
|
69
|
+
url: 'https://example.com',
|
|
70
|
+
project: 'myProjectId',
|
|
71
|
+
token: 'token',
|
|
72
|
+
middlewares: [loggingMiddleware],
|
|
73
|
+
});
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
### Client events
|
|
77
|
+
|
|
78
|
+
```ts
|
|
79
|
+
const client = new ContelloSdkClient(getSdk, {
|
|
80
|
+
url: 'https://example.com',
|
|
81
|
+
project: 'myProjectId',
|
|
82
|
+
token: 'token',
|
|
83
|
+
client: {
|
|
84
|
+
retryAttempts: 5,
|
|
85
|
+
onConnected: (ctx) => console.log(`connected: ${ctx.connectionId}`),
|
|
86
|
+
onError: (ctx, error) => console.error(`error on ${ctx.connectionId}:`, error),
|
|
87
|
+
onClosed: (ctx) => console.log(`closed: ${ctx.connectionId}`),
|
|
88
|
+
},
|
|
89
|
+
});
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
### Disconnect
|
|
93
|
+
|
|
94
|
+
```ts
|
|
95
|
+
await client.disconnect();
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
> `connect()` resolves once all WebSocket connections in the pool have been acknowledged by the server. The underlying transport retries automatically on transient failures, so the promise will stay pending until every connection succeeds. If retries are exhausted the promise will remain unresolved.
|
|
99
|
+
|
|
100
|
+
## API
|
|
101
|
+
|
|
102
|
+
### `ContelloSdkClient`
|
|
103
|
+
|
|
104
|
+
| Option | Type | Default | Description |
|
|
105
|
+
| ---------------------- | ------------------------------- | ------- | ------------------------------------ |
|
|
106
|
+
| `url` | `string` | — | Base URL of the Contello server |
|
|
107
|
+
| `project` | `string` | — | Project identifier |
|
|
108
|
+
| `token` | `string` | — | Authentication token |
|
|
109
|
+
| `middlewares` | `ContelloSdkClientMiddleware[]` | `[]` | Request/message middlewares |
|
|
110
|
+
| `pooling.enabled` | `boolean` | `true` | Enable connection pooling |
|
|
111
|
+
| `pooling.size` | `number` | `5` | Number of WebSocket connections |
|
|
112
|
+
| `client.retryAttempts` | `number` | `3` | Retry attempts on connection failure |
|
|
113
|
+
|
|
114
|
+
### Methods
|
|
115
|
+
|
|
116
|
+
| Method | Returns | Description |
|
|
117
|
+
| -------------- | --------------- | -------------------------- |
|
|
118
|
+
| `sdk` | `T` | The generated SDK instance |
|
|
119
|
+
| `connect()` | `Promise<void>` | Open WebSocket connections |
|
|
120
|
+
| `disconnect()` | `Promise<void>` | Close all connections |
|
|
121
|
+
|
|
122
|
+
## License
|
|
123
|
+
|
|
124
|
+
MIT
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,250 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __export = (target, all) => {
|
|
9
|
+
for (var name in all)
|
|
10
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
11
|
+
};
|
|
12
|
+
var __copyProps = (to, from, except, desc) => {
|
|
13
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
+
for (let key of __getOwnPropNames(from))
|
|
15
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
16
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
17
|
+
}
|
|
18
|
+
return to;
|
|
19
|
+
};
|
|
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
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
|
+
|
|
30
|
+
// src/index.ts
|
|
31
|
+
var index_exports = {};
|
|
32
|
+
__export(index_exports, {
|
|
33
|
+
ContelloSdkClient: () => ContelloSdkClient
|
|
34
|
+
});
|
|
35
|
+
module.exports = __toCommonJS(index_exports);
|
|
36
|
+
|
|
37
|
+
// src/client.ts
|
|
38
|
+
var import_graphql_ws = require("graphql-ws");
|
|
39
|
+
var import_rxjs2 = require("rxjs");
|
|
40
|
+
|
|
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
|
+
// src/pool.ts
|
|
97
|
+
var ConnectionPool = class {
|
|
98
|
+
constructor(createClient2, poolSize) {
|
|
99
|
+
this.createClient = createClient2;
|
|
100
|
+
this.poolSize = poolSize;
|
|
101
|
+
}
|
|
102
|
+
clients = [];
|
|
103
|
+
currentIndex = 0;
|
|
104
|
+
async connect() {
|
|
105
|
+
const connected = [];
|
|
106
|
+
for (let i = 0; i < this.poolSize; i++) {
|
|
107
|
+
const client = this.createClient(`${i + 1}`);
|
|
108
|
+
this.clients.push(client);
|
|
109
|
+
connected.push(
|
|
110
|
+
new Promise((resolve) => {
|
|
111
|
+
const unsubscribe = client.on("connected", () => {
|
|
112
|
+
unsubscribe();
|
|
113
|
+
resolve();
|
|
114
|
+
});
|
|
115
|
+
})
|
|
116
|
+
);
|
|
117
|
+
}
|
|
118
|
+
await Promise.all(connected);
|
|
119
|
+
}
|
|
120
|
+
get() {
|
|
121
|
+
if (this.clients.length === 0) {
|
|
122
|
+
throw new Error("Connection pool is empty. Please call connect() first.");
|
|
123
|
+
}
|
|
124
|
+
const client = this.clients[this.currentIndex];
|
|
125
|
+
this.currentIndex = (this.currentIndex + 1) % this.poolSize;
|
|
126
|
+
if (!client) {
|
|
127
|
+
throw new Error("No available WebSocket client");
|
|
128
|
+
}
|
|
129
|
+
return client;
|
|
130
|
+
}
|
|
131
|
+
async disconnect() {
|
|
132
|
+
const closed = this.clients.map(
|
|
133
|
+
(client) => new Promise((resolve) => {
|
|
134
|
+
const unsubscribe = client.on("closed", () => {
|
|
135
|
+
unsubscribe();
|
|
136
|
+
resolve();
|
|
137
|
+
});
|
|
138
|
+
client.dispose();
|
|
139
|
+
})
|
|
140
|
+
);
|
|
141
|
+
this.clients = [];
|
|
142
|
+
await Promise.all(closed);
|
|
143
|
+
}
|
|
144
|
+
};
|
|
145
|
+
|
|
146
|
+
// src/sdk.ts
|
|
147
|
+
var import_rxjs = require("rxjs");
|
|
148
|
+
var createSdk = (client, getSdk) => {
|
|
149
|
+
return getSdk((doc, vars, options) => {
|
|
150
|
+
if (options) {
|
|
151
|
+
console.warn("options are not supported yet");
|
|
152
|
+
}
|
|
153
|
+
const query = doc.loc?.source.body;
|
|
154
|
+
if (!query) {
|
|
155
|
+
throw new Error("No query provided");
|
|
156
|
+
}
|
|
157
|
+
if (vars && typeof vars !== "object") {
|
|
158
|
+
throw new Error("Variables must be an object");
|
|
159
|
+
}
|
|
160
|
+
const operationDef = doc.definitions.find((def) => def.kind === "OperationDefinition");
|
|
161
|
+
if (!operationDef) {
|
|
162
|
+
throw new Error("No operation definition found");
|
|
163
|
+
}
|
|
164
|
+
const kind = operationDef.operation;
|
|
165
|
+
const execute = () => {
|
|
166
|
+
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;
|
|
169
|
+
};
|
|
170
|
+
const wrapFn = getWrap();
|
|
171
|
+
if (wrapFn && kind !== "subscription") {
|
|
172
|
+
return wrapFn(`sdk:${operationDef.name?.value ?? ""}`, execute);
|
|
173
|
+
}
|
|
174
|
+
return execute();
|
|
175
|
+
});
|
|
176
|
+
};
|
|
177
|
+
|
|
178
|
+
// src/client.ts
|
|
179
|
+
var ContelloSdkClient = class {
|
|
180
|
+
_pool;
|
|
181
|
+
_sdk;
|
|
182
|
+
constructor(getSdk, params) {
|
|
183
|
+
const { url, project, token, client, pooling } = params;
|
|
184
|
+
const websocketUrl = `${url}/graphql/projects/${project}`.replace(/^http/i, "ws");
|
|
185
|
+
this._pool = new ConnectionPool(
|
|
186
|
+
(id) => {
|
|
187
|
+
const context = Object.freeze({ connectionId: id, websocketUrl });
|
|
188
|
+
return (0, import_graphql_ws.createClient)({
|
|
189
|
+
url: websocketUrl,
|
|
190
|
+
connectionParams: { authorization: `Bearer ${token}` },
|
|
191
|
+
lazy: false,
|
|
192
|
+
keepAlive: 3e4,
|
|
193
|
+
retryAttempts: client?.retryAttempts ?? 3,
|
|
194
|
+
shouldRetry: () => true,
|
|
195
|
+
jsonMessageReplacer: (key, value) => {
|
|
196
|
+
if (!key) {
|
|
197
|
+
return decorateMessage(value);
|
|
198
|
+
}
|
|
199
|
+
return value;
|
|
200
|
+
},
|
|
201
|
+
...client?.onError ? { onNonLazyError: (e) => client.onError(context, e) } : {},
|
|
202
|
+
on: {
|
|
203
|
+
...client?.onError ? { error: (e) => client.onError(context, e) } : {},
|
|
204
|
+
...client?.onConnected ? { connected: () => client.onConnected(context) } : {},
|
|
205
|
+
...client?.onClosed ? { closed: () => client.onClosed(context) } : {},
|
|
206
|
+
...client?.onConnecting ? { connecting: () => client.onConnecting(context) } : {},
|
|
207
|
+
...client?.onOpened ? { opened: () => client.onOpened(context) } : {},
|
|
208
|
+
...client?.onMessage ? { message: (m) => client.onMessage(context, m) } : {},
|
|
209
|
+
...client?.onPing ? { ping: () => client.onPing(context) } : {},
|
|
210
|
+
...client?.onPong ? { pong: () => client.onPong(context) } : {}
|
|
211
|
+
}
|
|
212
|
+
});
|
|
213
|
+
},
|
|
214
|
+
pooling?.enabled === false ? 1 : pooling?.size ?? 5
|
|
215
|
+
);
|
|
216
|
+
this._sdk = { sdk: createSdk(() => this._pool.get(), getSdk) };
|
|
217
|
+
}
|
|
218
|
+
get sdk() {
|
|
219
|
+
return this._sdk.sdk;
|
|
220
|
+
}
|
|
221
|
+
async connect() {
|
|
222
|
+
await this._pool.connect();
|
|
223
|
+
}
|
|
224
|
+
async disconnect() {
|
|
225
|
+
await this._pool.disconnect();
|
|
226
|
+
}
|
|
227
|
+
/**
|
|
228
|
+
* Subscribes to a raw GraphQL operation against the connection pool.
|
|
229
|
+
* Returns an Observable that emits each result's data.
|
|
230
|
+
*/
|
|
231
|
+
subscribe(query, variables) {
|
|
232
|
+
const wsClient = this._pool.get();
|
|
233
|
+
return new import_rxjs2.Observable((obs) => wsClient.subscribe({ query, variables }, obs)).pipe(
|
|
234
|
+
(0, import_rxjs2.map)((r) => r.data)
|
|
235
|
+
);
|
|
236
|
+
}
|
|
237
|
+
/**
|
|
238
|
+
* Executes a raw GraphQL query/mutation and returns the first result.
|
|
239
|
+
* Convenience wrapper around subscribe + firstValueFrom.
|
|
240
|
+
*/
|
|
241
|
+
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();
|
|
245
|
+
}
|
|
246
|
+
};
|
|
247
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
248
|
+
0 && (module.exports = {
|
|
249
|
+
ContelloSdkClient
|
|
250
|
+
});
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import { Observable } from 'rxjs';
|
|
2
|
+
import { DocumentNode, ExecutionResult } from 'graphql';
|
|
3
|
+
|
|
4
|
+
type Requester<C = any, E = unknown> = <R, V>(doc: DocumentNode, vars?: V, options?: C) => Promise<ExecutionResult<R, E>> | Observable<ExecutionResult<R, E>>;
|
|
5
|
+
|
|
6
|
+
type ClientEventContext = {
|
|
7
|
+
connectionId: string;
|
|
8
|
+
websocketUrl: string;
|
|
9
|
+
};
|
|
10
|
+
type ContelloSdkClientParams = {
|
|
11
|
+
url: string;
|
|
12
|
+
project: string;
|
|
13
|
+
token: string;
|
|
14
|
+
pooling?: {
|
|
15
|
+
enabled?: boolean | undefined;
|
|
16
|
+
size?: number | undefined;
|
|
17
|
+
} | undefined;
|
|
18
|
+
client?: {
|
|
19
|
+
retryAttempts?: number | undefined;
|
|
20
|
+
onError?: (context: ClientEventContext, error: unknown) => void;
|
|
21
|
+
onConnected?: (context: ClientEventContext) => void;
|
|
22
|
+
onClosed?: (context: ClientEventContext) => void;
|
|
23
|
+
onConnecting?: (context: ClientEventContext) => void;
|
|
24
|
+
onOpened?: (context: ClientEventContext) => void;
|
|
25
|
+
onMessage?: (context: ClientEventContext, message: any) => void;
|
|
26
|
+
onPing?: (context: ClientEventContext) => void;
|
|
27
|
+
onPong?: (context: ClientEventContext) => void;
|
|
28
|
+
} | undefined;
|
|
29
|
+
};
|
|
30
|
+
declare class ContelloSdkClient<T> {
|
|
31
|
+
private _pool;
|
|
32
|
+
private _sdk;
|
|
33
|
+
constructor(getSdk: <C, E>(requester: Requester<C, E>) => T, params: ContelloSdkClientParams);
|
|
34
|
+
get sdk(): T;
|
|
35
|
+
connect(): Promise<void>;
|
|
36
|
+
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
|
+
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
|
+
execute<TData>(query: string, variables?: Record<string, unknown> | undefined): Promise<TData>;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
export { ContelloSdkClient, type ContelloSdkClientParams };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,53 +1,49 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { ExecutionResult } from 'graphql';
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
};
|
|
50
|
-
|
|
51
|
-
declare type Requester<C = any, E = unknown> = <R, V>(doc: DocumentNode, vars?: V, options?: C) => Promise<ExecutionResult<R, E>> | Observable<ExecutionResult<R, E>>;
|
|
52
|
-
|
|
53
|
-
export { }
|
|
1
|
+
import { Observable } from 'rxjs';
|
|
2
|
+
import { DocumentNode, ExecutionResult } from 'graphql';
|
|
3
|
+
|
|
4
|
+
type Requester<C = any, E = unknown> = <R, V>(doc: DocumentNode, vars?: V, options?: C) => Promise<ExecutionResult<R, E>> | Observable<ExecutionResult<R, E>>;
|
|
5
|
+
|
|
6
|
+
type ClientEventContext = {
|
|
7
|
+
connectionId: string;
|
|
8
|
+
websocketUrl: string;
|
|
9
|
+
};
|
|
10
|
+
type ContelloSdkClientParams = {
|
|
11
|
+
url: string;
|
|
12
|
+
project: string;
|
|
13
|
+
token: string;
|
|
14
|
+
pooling?: {
|
|
15
|
+
enabled?: boolean | undefined;
|
|
16
|
+
size?: number | undefined;
|
|
17
|
+
} | undefined;
|
|
18
|
+
client?: {
|
|
19
|
+
retryAttempts?: number | undefined;
|
|
20
|
+
onError?: (context: ClientEventContext, error: unknown) => void;
|
|
21
|
+
onConnected?: (context: ClientEventContext) => void;
|
|
22
|
+
onClosed?: (context: ClientEventContext) => void;
|
|
23
|
+
onConnecting?: (context: ClientEventContext) => void;
|
|
24
|
+
onOpened?: (context: ClientEventContext) => void;
|
|
25
|
+
onMessage?: (context: ClientEventContext, message: any) => void;
|
|
26
|
+
onPing?: (context: ClientEventContext) => void;
|
|
27
|
+
onPong?: (context: ClientEventContext) => void;
|
|
28
|
+
} | undefined;
|
|
29
|
+
};
|
|
30
|
+
declare class ContelloSdkClient<T> {
|
|
31
|
+
private _pool;
|
|
32
|
+
private _sdk;
|
|
33
|
+
constructor(getSdk: <C, E>(requester: Requester<C, E>) => T, params: ContelloSdkClientParams);
|
|
34
|
+
get sdk(): T;
|
|
35
|
+
connect(): Promise<void>;
|
|
36
|
+
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
|
+
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
|
+
execute<TData>(query: string, variables?: Record<string, unknown> | undefined): Promise<TData>;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
export { ContelloSdkClient, type ContelloSdkClientParams };
|