@codex-data/sdk 2.3.42 → 2.4.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/edge.d.ts +2 -0
- package/dist/edge.js +8 -0
- package/dist/edge.mjs +1 -0
- package/dist/index.d.ts +1 -0
- package/dist/sdk/Mutation.d.ts +2 -1
- package/dist/sdk/Mutation.js +1 -0
- package/dist/sdk/Mutation.mjs +2 -1
- package/dist/sdk/Query.d.ts +4 -2
- package/dist/sdk/Query.js +3 -1
- package/dist/sdk/Query.mjs +4 -2
- package/dist/sdk/Subscribe.d.ts +32 -30
- package/dist/sdk/Subscribe.js +74 -69
- package/dist/sdk/Subscribe.mjs +74 -69
- package/dist/sdk/edge.d.ts +24 -0
- package/dist/sdk/edge.js +111 -0
- package/dist/sdk/edge.mjs +95 -0
- package/dist/sdk/edgeSubscription.d.ts +3 -0
- package/dist/sdk/edgeSubscription.js +76 -0
- package/dist/sdk/edgeSubscription.mjs +72 -0
- package/dist/sdk/generated/gql.d.ts +86 -66
- package/dist/sdk/generated/gql.js +37 -33
- package/dist/sdk/generated/gql.mjs +37 -33
- package/dist/sdk/generated/graphql.d.ts +5395 -61
- package/dist/sdk/generated/graphql.js +265 -40
- package/dist/sdk/generated/graphql.mjs +260 -35
- package/dist/sdk/index.d.ts +6 -1
- package/dist/sdk/index.js +36 -18
- package/dist/sdk/index.mjs +40 -20
- package/package.json +39 -24
- package/schema.graphql +1874 -906
package/dist/sdk/index.d.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { TypedDocumentNode } from "@graphql-typed-document-node/core";
|
|
2
2
|
import { Variables } from "graphql-request";
|
|
3
3
|
import { ExecutionResult, Sink } from "graphql-ws";
|
|
4
|
+
import { SubscriptionOptions } from "./edge";
|
|
4
5
|
import { Mutation } from "./Mutation";
|
|
5
6
|
import { Query } from "./Query";
|
|
6
7
|
import { Subscribe } from "./Subscribe";
|
|
@@ -8,6 +9,7 @@ export type CleanupFunction = () => void;
|
|
|
8
9
|
export type ApiConfig = {
|
|
9
10
|
apiUrl: string;
|
|
10
11
|
apiRealtimeUrl: string;
|
|
12
|
+
apiEdgeUrl?: string;
|
|
11
13
|
ws?: boolean;
|
|
12
14
|
headers?: Record<string, string>;
|
|
13
15
|
wsHeaders?: Record<string, string>;
|
|
@@ -19,6 +21,8 @@ export declare class Codex {
|
|
|
19
21
|
private apiConfig?;
|
|
20
22
|
private client;
|
|
21
23
|
private wsClient;
|
|
24
|
+
private edgeWsClient;
|
|
25
|
+
private disposed;
|
|
22
26
|
queries: Query;
|
|
23
27
|
mutations: Mutation;
|
|
24
28
|
subscriptions: Subscribe;
|
|
@@ -29,6 +33,7 @@ export declare class Codex {
|
|
|
29
33
|
constructor(apiKey: string, apiConfig?: Partial<ApiConfig> | undefined);
|
|
30
34
|
private parseConfig;
|
|
31
35
|
private createGraphQLClient;
|
|
36
|
+
private websocketOptions;
|
|
32
37
|
private createWebsocketClient;
|
|
33
38
|
private getRequestHeaders;
|
|
34
39
|
/**
|
|
@@ -72,5 +77,5 @@ export declare class Codex {
|
|
|
72
77
|
* @param sink - The sink to receive the results.
|
|
73
78
|
* @returns A cleanup function to unsubscribe from the subscription.
|
|
74
79
|
*/
|
|
75
|
-
subscribe<TResults, TVars extends Record<string, unknown> = Record<string, never>>(doc: string, args: TVars, sink: Sink<ExecutionResult<TResults
|
|
80
|
+
subscribe<TResults, TVars extends Record<string, unknown> = Record<string, never>>(doc: string, args: TVars, sink: Sink<ExecutionResult<TResults>>, options?: SubscriptionOptions): CleanupFunction;
|
|
76
81
|
}
|
package/dist/sdk/index.js
CHANGED
|
@@ -16,6 +16,7 @@ exports.Codex = void 0;
|
|
|
16
16
|
const graphql_request_1 = require("graphql-request");
|
|
17
17
|
const graphql_ws_1 = require("graphql-ws");
|
|
18
18
|
const isomorphic_ws_1 = __importDefault(require("isomorphic-ws"));
|
|
19
|
+
const edge_1 = require("./edge");
|
|
19
20
|
const invariant_1 = require("./invariant");
|
|
20
21
|
const Mutation_1 = require("./Mutation");
|
|
21
22
|
const Query_1 = require("./Query");
|
|
@@ -34,6 +35,7 @@ class Codex {
|
|
|
34
35
|
var _a, _b;
|
|
35
36
|
this.apiKey = apiKey;
|
|
36
37
|
this.apiConfig = apiConfig;
|
|
38
|
+
this.disposed = false;
|
|
37
39
|
const config = this.parseConfig((_a = this.apiConfig) !== null && _a !== void 0 ? _a : {});
|
|
38
40
|
this.queries = new Query_1.Query(this);
|
|
39
41
|
this.mutations = new Mutation_1.Mutation(this);
|
|
@@ -55,15 +57,16 @@ class Codex {
|
|
|
55
57
|
fetch: config.fetch,
|
|
56
58
|
});
|
|
57
59
|
}
|
|
60
|
+
websocketOptions(config) {
|
|
61
|
+
return {
|
|
62
|
+
webSocketImpl: isomorphic_ws_1.default,
|
|
63
|
+
keepAlive: 10000, // ping server every 10 seconds
|
|
64
|
+
url: config.apiRealtimeUrl,
|
|
65
|
+
connectionParams: Object.assign({ Authorization: this.apiKey }, config.wsHeaders),
|
|
66
|
+
};
|
|
67
|
+
}
|
|
58
68
|
createWebsocketClient(config) {
|
|
59
|
-
return config.ws
|
|
60
|
-
? (0, graphql_ws_1.createClient)({
|
|
61
|
-
webSocketImpl: isomorphic_ws_1.default,
|
|
62
|
-
keepAlive: 10000, // ping server every 10 seconds
|
|
63
|
-
url: config.apiRealtimeUrl,
|
|
64
|
-
connectionParams: Object.assign({ Authorization: this.apiKey }, config.wsHeaders),
|
|
65
|
-
})
|
|
66
|
-
: undefined;
|
|
69
|
+
return config.ws ? (0, graphql_ws_1.createClient)(this.websocketOptions(config)) : undefined;
|
|
67
70
|
}
|
|
68
71
|
getRequestHeaders() {
|
|
69
72
|
return __awaiter(this, void 0, void 0, function* () {
|
|
@@ -83,8 +86,11 @@ class Codex {
|
|
|
83
86
|
updateConfig(incomingConfig) {
|
|
84
87
|
var _a, _b, _c;
|
|
85
88
|
// If we have an existing websocket connection, dispose of it.
|
|
86
|
-
if (this.wsClient)
|
|
87
|
-
this.dispose();
|
|
89
|
+
if (this.wsClient || this.edgeWsClient)
|
|
90
|
+
void this.dispose();
|
|
91
|
+
this.edgeWsClient = undefined;
|
|
92
|
+
this.disposed = false;
|
|
93
|
+
this.apiConfig = Object.assign(Object.assign({}, this.apiConfig), incomingConfig);
|
|
88
94
|
// Merge the incoming config with the existing config.
|
|
89
95
|
const config = this.parseConfig(Object.assign({}, this.apiConfig, incomingConfig));
|
|
90
96
|
// Create a new websocket connection with the new headers.
|
|
@@ -108,8 +114,9 @@ class Codex {
|
|
|
108
114
|
*/
|
|
109
115
|
dispose() {
|
|
110
116
|
return __awaiter(this, void 0, void 0, function* () {
|
|
111
|
-
var _a;
|
|
112
|
-
|
|
117
|
+
var _a, _b;
|
|
118
|
+
this.disposed = true;
|
|
119
|
+
yield Promise.all([(_a = this.wsClient) === null || _a === void 0 ? void 0 : _a.dispose(), (_b = this.edgeWsClient) === null || _b === void 0 ? void 0 : _b.dispose()]);
|
|
113
120
|
});
|
|
114
121
|
}
|
|
115
122
|
/**
|
|
@@ -155,15 +162,26 @@ class Codex {
|
|
|
155
162
|
* @param sink - The sink to receive the results.
|
|
156
163
|
* @returns A cleanup function to unsubscribe from the subscription.
|
|
157
164
|
*/
|
|
158
|
-
subscribe(doc, args, sink) {
|
|
165
|
+
subscribe(doc, args, sink, options) {
|
|
166
|
+
var _a;
|
|
159
167
|
if (!this.wsClient) {
|
|
160
168
|
throw new Error("Websocket is not enabled in the config");
|
|
161
169
|
}
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
170
|
+
if (this.disposed)
|
|
171
|
+
throw new Error("SDK has been disposed");
|
|
172
|
+
const payload = { query: doc, variables: args };
|
|
173
|
+
if (!(options === null || options === void 0 ? void 0 : options.useEdge))
|
|
174
|
+
return this.wsClient.subscribe(payload, sink);
|
|
175
|
+
// Validate before creating the edge transport or starting an operation.
|
|
176
|
+
const snapshot = (0, edge_1.prepareEdgeSubscription)(payload);
|
|
177
|
+
if (!this.edgeWsClient) {
|
|
178
|
+
const config = this.parseConfig((_a = this.apiConfig) !== null && _a !== void 0 ? _a : {});
|
|
179
|
+
this.edgeWsClient = (0, edge_1.createEdgeClient)(Object.assign(Object.assign({}, this.websocketOptions(config)), { url: config.apiEdgeUrl }));
|
|
180
|
+
this.edgeWsClient.on("error", (error) => {
|
|
181
|
+
console.error("Websocket error: ", error);
|
|
182
|
+
});
|
|
183
|
+
}
|
|
184
|
+
return this.edgeWsClient.subscribe(snapshot, sink);
|
|
167
185
|
}
|
|
168
186
|
}
|
|
169
187
|
exports.Codex = Codex;
|
package/dist/sdk/index.mjs
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { GraphQLClient } from "graphql-request";
|
|
2
2
|
import { createClient, } from "graphql-ws";
|
|
3
3
|
import WebSocket from "isomorphic-ws";
|
|
4
|
+
import { createEdgeClient, prepareEdgeSubscription, } from "./edge";
|
|
4
5
|
import { invariant } from "./invariant";
|
|
5
6
|
import { Mutation } from "./Mutation";
|
|
6
7
|
import { Query } from "./Query";
|
|
@@ -18,6 +19,7 @@ export class Codex {
|
|
|
18
19
|
constructor(apiKey, apiConfig) {
|
|
19
20
|
this.apiKey = apiKey;
|
|
20
21
|
this.apiConfig = apiConfig;
|
|
22
|
+
this.disposed = false;
|
|
21
23
|
const config = this.parseConfig(this.apiConfig ?? {});
|
|
22
24
|
this.queries = new Query(this);
|
|
23
25
|
this.mutations = new Mutation(this);
|
|
@@ -44,18 +46,19 @@ export class Codex {
|
|
|
44
46
|
fetch: config.fetch,
|
|
45
47
|
});
|
|
46
48
|
}
|
|
49
|
+
websocketOptions(config) {
|
|
50
|
+
return {
|
|
51
|
+
webSocketImpl: WebSocket,
|
|
52
|
+
keepAlive: 10000, // ping server every 10 seconds
|
|
53
|
+
url: config.apiRealtimeUrl,
|
|
54
|
+
connectionParams: {
|
|
55
|
+
Authorization: this.apiKey,
|
|
56
|
+
...config.wsHeaders,
|
|
57
|
+
},
|
|
58
|
+
};
|
|
59
|
+
}
|
|
47
60
|
createWebsocketClient(config) {
|
|
48
|
-
return config.ws
|
|
49
|
-
? createClient({
|
|
50
|
-
webSocketImpl: WebSocket,
|
|
51
|
-
keepAlive: 10000, // ping server every 10 seconds
|
|
52
|
-
url: config.apiRealtimeUrl,
|
|
53
|
-
connectionParams: {
|
|
54
|
-
Authorization: this.apiKey,
|
|
55
|
-
...config.wsHeaders,
|
|
56
|
-
},
|
|
57
|
-
})
|
|
58
|
-
: undefined;
|
|
61
|
+
return config.ws ? createClient(this.websocketOptions(config)) : undefined;
|
|
59
62
|
}
|
|
60
63
|
async getRequestHeaders() {
|
|
61
64
|
// Get any dynamic headers
|
|
@@ -74,8 +77,11 @@ export class Codex {
|
|
|
74
77
|
*/
|
|
75
78
|
updateConfig(incomingConfig) {
|
|
76
79
|
// If we have an existing websocket connection, dispose of it.
|
|
77
|
-
if (this.wsClient)
|
|
78
|
-
this.dispose();
|
|
80
|
+
if (this.wsClient || this.edgeWsClient)
|
|
81
|
+
void this.dispose();
|
|
82
|
+
this.edgeWsClient = undefined;
|
|
83
|
+
this.disposed = false;
|
|
84
|
+
this.apiConfig = { ...this.apiConfig, ...incomingConfig };
|
|
79
85
|
// Merge the incoming config with the existing config.
|
|
80
86
|
const config = this.parseConfig(Object.assign({}, this.apiConfig, incomingConfig));
|
|
81
87
|
// Create a new websocket connection with the new headers.
|
|
@@ -98,7 +104,8 @@ export class Codex {
|
|
|
98
104
|
* It's useful for when you need to modify some of the configuration, or when you're done with the class.
|
|
99
105
|
*/
|
|
100
106
|
async dispose() {
|
|
101
|
-
this.
|
|
107
|
+
this.disposed = true;
|
|
108
|
+
await Promise.all([this.wsClient?.dispose(), this.edgeWsClient?.dispose()]);
|
|
102
109
|
}
|
|
103
110
|
/**
|
|
104
111
|
* This is the preferred way to execute queries.
|
|
@@ -137,14 +144,27 @@ export class Codex {
|
|
|
137
144
|
* @param sink - The sink to receive the results.
|
|
138
145
|
* @returns A cleanup function to unsubscribe from the subscription.
|
|
139
146
|
*/
|
|
140
|
-
subscribe(doc, args, sink) {
|
|
147
|
+
subscribe(doc, args, sink, options) {
|
|
141
148
|
if (!this.wsClient) {
|
|
142
149
|
throw new Error("Websocket is not enabled in the config");
|
|
143
150
|
}
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
151
|
+
if (this.disposed)
|
|
152
|
+
throw new Error("SDK has been disposed");
|
|
153
|
+
const payload = { query: doc, variables: args };
|
|
154
|
+
if (!options?.useEdge)
|
|
155
|
+
return this.wsClient.subscribe(payload, sink);
|
|
156
|
+
// Validate before creating the edge transport or starting an operation.
|
|
157
|
+
const snapshot = prepareEdgeSubscription(payload);
|
|
158
|
+
if (!this.edgeWsClient) {
|
|
159
|
+
const config = this.parseConfig(this.apiConfig ?? {});
|
|
160
|
+
this.edgeWsClient = createEdgeClient({
|
|
161
|
+
...this.websocketOptions(config),
|
|
162
|
+
url: config.apiEdgeUrl,
|
|
163
|
+
});
|
|
164
|
+
this.edgeWsClient.on("error", (error) => {
|
|
165
|
+
console.error("Websocket error: ", error);
|
|
166
|
+
});
|
|
167
|
+
}
|
|
168
|
+
return this.edgeWsClient.subscribe(snapshot, sink);
|
|
149
169
|
}
|
|
150
170
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@codex-data/sdk",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.4.1",
|
|
4
4
|
"engines": {
|
|
5
5
|
"node": ">=17.5.0"
|
|
6
6
|
},
|
|
@@ -13,16 +13,46 @@
|
|
|
13
13
|
"import": "./dist/index.mjs",
|
|
14
14
|
"require": "./dist/index.js"
|
|
15
15
|
},
|
|
16
|
-
"./schema.graphql": "./schema.graphql"
|
|
16
|
+
"./schema.graphql": "./schema.graphql",
|
|
17
|
+
"./edge": {
|
|
18
|
+
"types": "./dist/edge.d.ts",
|
|
19
|
+
"import": "./dist/edge.mjs",
|
|
20
|
+
"require": "./dist/edge.js"
|
|
21
|
+
}
|
|
17
22
|
},
|
|
18
23
|
"sideEffects": false,
|
|
24
|
+
"scripts": {
|
|
25
|
+
"prebuild": "pnpm run clean && pnpm run generate:configs",
|
|
26
|
+
"clean": "rm -rf dist dist-esm",
|
|
27
|
+
"build": "pnpm run copy:schema && pnpm run generate:graphql && pnpm run build:sdk && pnpm run codegen && pnpm run lint ./src/sdk --fix && pnpm run build:types",
|
|
28
|
+
"build:types": "pnpm run build:cjs && pnpm run build:esm && pnpm run build:rename-esm",
|
|
29
|
+
"build:cjs": "tsc",
|
|
30
|
+
"build:esm": "tsc -p tsconfig.esm.json",
|
|
31
|
+
"build:rename-esm": "find dist-esm -name '*.js' -exec sh -c 'mv \"$1\" \"${1%.js}.mjs\"' _ {} \\; && cp -r dist-esm/* dist/ && rm -rf dist-esm",
|
|
32
|
+
"fetch:schema": "curl -s https://graph.codex.io/schema/latest.graphql --output src/resources/schema.graphql && cp src/resources/schema.graphql schema.graphql",
|
|
33
|
+
"copy:schema": "cp src/resources/schema.graphql schema.graphql",
|
|
34
|
+
"schema:diff": "tsx src/scripts/schemaDiff.ts",
|
|
35
|
+
"generate:configs": "tsx src/scripts/generateNetworkConfigs.ts",
|
|
36
|
+
"generate:graphql": "tsx src/scripts/generateGraphql.ts",
|
|
37
|
+
"build:sdk": "tsx src/scripts/buildSdk.ts",
|
|
38
|
+
"codegen": "graphql-codegen --config codegen.ts",
|
|
39
|
+
"lint": "eslint --ext .ts src",
|
|
40
|
+
"test": "jest",
|
|
41
|
+
"format": "prettier --write .",
|
|
42
|
+
"prepublishOnly": "pnpm run build",
|
|
43
|
+
"publish:beta": "pnpm publish --tag beta --access public",
|
|
44
|
+
"publish:latest": "pnpm publish --access public"
|
|
45
|
+
},
|
|
19
46
|
"files": [
|
|
20
47
|
"dist/index.js",
|
|
21
48
|
"dist/index.mjs",
|
|
22
49
|
"dist/index.d.ts",
|
|
23
50
|
"dist/sdk/",
|
|
24
51
|
"schema.graphql",
|
|
25
|
-
"README.md"
|
|
52
|
+
"README.md",
|
|
53
|
+
"dist/edge.js",
|
|
54
|
+
"dist/edge.mjs",
|
|
55
|
+
"dist/edge.d.ts"
|
|
26
56
|
],
|
|
27
57
|
"keywords": [
|
|
28
58
|
"crypto",
|
|
@@ -80,25 +110,10 @@
|
|
|
80
110
|
"tsx": "^4.19.1",
|
|
81
111
|
"typescript": "^5.2.2"
|
|
82
112
|
},
|
|
83
|
-
"
|
|
84
|
-
|
|
85
|
-
"
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
"build:cjs": "tsc",
|
|
89
|
-
"build:esm": "tsc -p tsconfig.esm.json",
|
|
90
|
-
"build:rename-esm": "find dist-esm -name '*.js' -exec sh -c 'mv \"$1\" \"${1%.js}.mjs\"' _ {} \\; && cp -r dist-esm/* dist/ && rm -rf dist-esm",
|
|
91
|
-
"fetch:schema": "curl -s https://graph.codex.io/schema/latest.graphql --output src/resources/schema.graphql && cp src/resources/schema.graphql schema.graphql",
|
|
92
|
-
"copy:schema": "cp src/resources/schema.graphql schema.graphql",
|
|
93
|
-
"schema:diff": "tsx src/scripts/schemaDiff.ts",
|
|
94
|
-
"generate:configs": "tsx src/scripts/generateNetworkConfigs.ts",
|
|
95
|
-
"generate:graphql": "tsx src/scripts/generateGraphql.ts",
|
|
96
|
-
"build:sdk": "tsx src/scripts/buildSdk.ts",
|
|
97
|
-
"codegen": "graphql-codegen --config codegen.ts",
|
|
98
|
-
"lint": "eslint --ext .ts src",
|
|
99
|
-
"test": "jest",
|
|
100
|
-
"format": "prettier --write .",
|
|
101
|
-
"publish:beta": "pnpm publish --tag beta --access public",
|
|
102
|
-
"publish:latest": "pnpm publish --access public"
|
|
113
|
+
"packageManager": "pnpm@10.16.0",
|
|
114
|
+
"pnpm": {
|
|
115
|
+
"overrides": {
|
|
116
|
+
"graphql": "^16.9.0"
|
|
117
|
+
}
|
|
103
118
|
}
|
|
104
|
-
}
|
|
119
|
+
}
|