@codex-data/sdk 1.0.40 → 1.1.0-beta.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.
Files changed (59) hide show
  1. package/README.md +137 -39
  2. package/dist/index.d.ts +3 -3
  3. package/dist/index.js +6 -26
  4. package/dist/index.mjs +4 -0
  5. package/dist/sdk/Mutation.d.ts +3 -3
  6. package/dist/sdk/Mutation.js +2 -2
  7. package/dist/sdk/Mutation.mjs +13 -0
  8. package/dist/sdk/Query.d.ts +53 -54
  9. package/dist/sdk/Query.js +53 -54
  10. package/dist/sdk/Query.mjs +74 -0
  11. package/dist/sdk/Subscribe.d.ts +17 -18
  12. package/dist/sdk/Subscribe.js +84 -184
  13. package/dist/sdk/Subscribe.mjs +138 -0
  14. package/dist/sdk/generated/gql.d.ts +47 -43
  15. package/dist/sdk/generated/gql.js +22 -22
  16. package/dist/sdk/generated/gql.mjs +103 -0
  17. package/dist/sdk/generated/graphql.d.ts +213 -16
  18. package/dist/sdk/generated/graphql.js +33 -25
  19. package/dist/sdk/generated/graphql.mjs +1739 -0
  20. package/dist/sdk/generated/index.d.ts +0 -2
  21. package/dist/sdk/generated/index.js +0 -2
  22. package/dist/sdk/generated/index.mjs +1 -0
  23. package/dist/sdk/index.d.ts +0 -1
  24. package/dist/sdk/index.js +0 -1
  25. package/dist/sdk/index.mjs +150 -0
  26. package/dist/sdk/invariant.d.ts +0 -1
  27. package/dist/sdk/invariant.js +0 -1
  28. package/dist/sdk/invariant.mjs +26 -0
  29. package/package.json +54 -26
  30. package/dist/index.d.ts.map +0 -1
  31. package/dist/index.js.map +0 -1
  32. package/dist/resources/graphql.d.ts +0 -9875
  33. package/dist/resources/graphql.d.ts.map +0 -1
  34. package/dist/resources/graphql.js +0 -1640
  35. package/dist/resources/graphql.js.map +0 -1
  36. package/dist/sdk/Mutation.d.ts.map +0 -1
  37. package/dist/sdk/Mutation.js.map +0 -1
  38. package/dist/sdk/Query.d.ts.map +0 -1
  39. package/dist/sdk/Query.js.map +0 -1
  40. package/dist/sdk/Subscribe.d.ts.map +0 -1
  41. package/dist/sdk/Subscribe.js.map +0 -1
  42. package/dist/sdk/generated/fragment-masking.d.ts +0 -20
  43. package/dist/sdk/generated/fragment-masking.d.ts.map +0 -1
  44. package/dist/sdk/generated/fragment-masking.js +0 -22
  45. package/dist/sdk/generated/fragment-masking.js.map +0 -1
  46. package/dist/sdk/generated/gql.d.ts.map +0 -1
  47. package/dist/sdk/generated/gql.js.map +0 -1
  48. package/dist/sdk/generated/graphql.d.ts.map +0 -1
  49. package/dist/sdk/generated/graphql.js.map +0 -1
  50. package/dist/sdk/generated/index.d.ts.map +0 -1
  51. package/dist/sdk/generated/index.js.map +0 -1
  52. package/dist/sdk/index.d.ts.map +0 -1
  53. package/dist/sdk/index.js.map +0 -1
  54. package/dist/sdk/index.test.d.ts +0 -2
  55. package/dist/sdk/index.test.d.ts.map +0 -1
  56. package/dist/sdk/index.test.js +0 -321
  57. package/dist/sdk/index.test.js.map +0 -1
  58. package/dist/sdk/invariant.d.ts.map +0 -1
  59. package/dist/sdk/invariant.js.map +0 -1
@@ -1,3 +1 @@
1
- export * from "./fragment-masking";
2
1
  export * from "./gql";
3
- //# sourceMappingURL=index.d.ts.map
@@ -14,6 +14,4 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
14
  for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
15
  };
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
- __exportStar(require("./fragment-masking"), exports);
18
17
  __exportStar(require("./gql"), exports);
19
- //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ export * from "./gql";
@@ -74,4 +74,3 @@ export declare class Codex {
74
74
  */
75
75
  subscribe<TResults, TVars extends Record<string, unknown> = Record<string, never>>(doc: string, args: TVars, sink: Sink<ExecutionResult<TResults>>): CleanupFunction;
76
76
  }
77
- //# sourceMappingURL=index.d.ts.map
package/dist/sdk/index.js CHANGED
@@ -167,4 +167,3 @@ class Codex {
167
167
  }
168
168
  }
169
169
  exports.Codex = Codex;
170
- //# sourceMappingURL=index.js.map
@@ -0,0 +1,150 @@
1
+ import { GraphQLClient } from "graphql-request";
2
+ import { createClient, } from "graphql-ws";
3
+ import WebSocket from "isomorphic-ws";
4
+ import { invariant } from "./invariant";
5
+ import { Mutation } from "./Mutation";
6
+ import { Query } from "./Query";
7
+ import { Subscribe } from "./Subscribe";
8
+ const defaultConfig = {
9
+ apiUrl: `https://graph.codex.io/graphql`,
10
+ apiRealtimeUrl: `wss://graph.codex.io/graphql`,
11
+ ws: true,
12
+ };
13
+ export class Codex {
14
+ /**
15
+ * @param apiKey - The api key to use for the requests.
16
+ * @param apiConfig - The configuration to use for the requests.
17
+ */
18
+ constructor(apiKey, apiConfig) {
19
+ this.apiKey = apiKey;
20
+ this.apiConfig = apiConfig;
21
+ const config = this.parseConfig(this.apiConfig ?? {});
22
+ this.queries = new Query(this);
23
+ this.mutations = new Mutation(this);
24
+ this.subscriptions = new Subscribe(this);
25
+ this.client = this.createGraphQLClient(config);
26
+ this.wsClient = this.createWebsocketClient(config);
27
+ this.wsClient?.on("error", (error) => {
28
+ console.error("Websocket error: ", error);
29
+ });
30
+ }
31
+ parseConfig(config) {
32
+ invariant(this.apiKey, "apiKey must be defined");
33
+ return Object.assign({}, defaultConfig, config);
34
+ }
35
+ createGraphQLClient(config) {
36
+ return new GraphQLClient(config.apiUrl, {
37
+ method: "POST",
38
+ headers: new Headers({
39
+ "Content-Type": "application/json",
40
+ Authorization: this.apiKey,
41
+ "X-Apollo-Operation-Name": "query",
42
+ ...config.headers,
43
+ }),
44
+ fetch: config.fetch,
45
+ });
46
+ }
47
+ 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;
59
+ }
60
+ async getRequestHeaders() {
61
+ // Get any dynamic headers
62
+ const dynamicHeaders = await this.apiConfig?.applyHeaders?.();
63
+ const staticHeaders = this.apiConfig?.headers;
64
+ const headers = {
65
+ ...staticHeaders,
66
+ ...dynamicHeaders,
67
+ };
68
+ return headers;
69
+ }
70
+ /**
71
+ * Need to update the headers on the websocket connection.
72
+ * This is useful if the api key changes, or you need to modify the connection init params at all.
73
+ * NOTE: it will dispose of the old connection, so all existing subscriptions will be closed.
74
+ */
75
+ updateConfig(incomingConfig) {
76
+ // If we have an existing websocket connection, dispose of it.
77
+ if (this.wsClient)
78
+ this.dispose();
79
+ // Merge the incoming config with the existing config.
80
+ const config = this.parseConfig(Object.assign({}, this.apiConfig, incomingConfig));
81
+ // Create a new websocket connection with the new headers.
82
+ this.wsClient = this.createWebsocketClient(config);
83
+ this.client = this.createGraphQLClient(config);
84
+ this.wsClient?.on("error", (error) => {
85
+ console.error("Websocket error: ", error);
86
+ });
87
+ this.wsClient?.on("closed", (er) => {
88
+ console.log("Websocket closed", er);
89
+ });
90
+ this.wsClient?.on("connecting", () => {
91
+ console.log("Websocket connecting");
92
+ });
93
+ }
94
+ /**
95
+ * Call this to dispose of the websocket connection, and make this class ready for garbage collection.
96
+ * This will also close any open subscriptions.
97
+ *
98
+ * It's useful for when you need to modify some of the configuration, or when you're done with the class.
99
+ */
100
+ async dispose() {
101
+ this.wsClient?.dispose();
102
+ }
103
+ /**
104
+ * This is the preferred way to execute queries.
105
+ * @param doc - The graphql document node to execute.
106
+ * @param args - The variables to pass to the document.
107
+ * @returns The result of the query.
108
+ */
109
+ async query(doc, args = {}) {
110
+ const res = await this.client.request(doc, args, await this.getRequestHeaders());
111
+ return res;
112
+ }
113
+ /**
114
+ * This is functionally the same thing as the `query` method, but it's more expressive.
115
+ * @param doc - The graphql document node to execute.
116
+ * @param args - The variables to pass to the document.
117
+ * @returns The result of the mutation.
118
+ */
119
+ async mutation(doc, args = {}) {
120
+ const res = await this.client.request(doc, args, await this.getRequestHeaders());
121
+ return res;
122
+ }
123
+ /**
124
+ * This should be used when you don't want to use graphql libaries to create a document node,
125
+ * and would rather just pass in a string.
126
+ * @param gqlString - The graphql document string to execute.
127
+ * @param args - The variables to pass to the document.
128
+ * @returns The result of the mutation.
129
+ */
130
+ async send(gqlString, args = {}) {
131
+ const res = await this.client.request(gqlString, args, await this.getRequestHeaders());
132
+ return res;
133
+ }
134
+ /**
135
+ * @param doc - The graphql document string to execute.
136
+ * @param args - The variables to pass to the document.
137
+ * @param sink - The sink to receive the results.
138
+ * @returns A cleanup function to unsubscribe from the subscription.
139
+ */
140
+ subscribe(doc, args, sink) {
141
+ if (!this.wsClient) {
142
+ throw new Error("Websocket is not enabled in the config");
143
+ }
144
+ const cleanup = this.wsClient.subscribe({
145
+ query: doc,
146
+ variables: args,
147
+ }, sink);
148
+ return cleanup;
149
+ }
150
+ }
@@ -1,2 +1 @@
1
1
  export declare function invariant(condition: unknown, message?: string | (() => string)): asserts condition;
2
- //# sourceMappingURL=invariant.d.ts.map
@@ -27,4 +27,3 @@ message) {
27
27
  const value = provided ? `${prefix}: ${provided}` : prefix;
28
28
  throw new Error(value);
29
29
  }
30
- //# sourceMappingURL=invariant.js.map
@@ -0,0 +1,26 @@
1
+ const isProduction = process.env.NODE_ENV === "production";
2
+ const prefix = "Invariant failed";
3
+ // Throw an error if the condition fails
4
+ // Strip out error messages for production
5
+ // > Not providing an inline default argument for message as the result is smaller
6
+ export function invariant(condition,
7
+ // Can provide a string, or a function that returns a string for cases where
8
+ // the message takes a fair amount of effort to compute
9
+ message) {
10
+ if (condition) {
11
+ return;
12
+ }
13
+ // Condition not passed
14
+ // In production we strip the message but still throw
15
+ if (isProduction) {
16
+ throw new Error(prefix);
17
+ }
18
+ // When not in production we allow the message to pass through
19
+ // *This block will be removed in production builds*
20
+ const provided = typeof message === "function" ? message() : message;
21
+ // Options:
22
+ // 1. message provided: `${prefix}: ${provided}`
23
+ // 2. message not provided: prefix
24
+ const value = provided ? `${prefix}: ${provided}` : prefix;
25
+ throw new Error(value);
26
+ }
package/package.json CHANGED
@@ -1,22 +1,26 @@
1
1
  {
2
2
  "name": "@codex-data/sdk",
3
- "version": "1.0.40",
3
+ "version": "1.1.0-beta.1",
4
4
  "engines": {
5
5
  "node": ">=17.5.0"
6
6
  },
7
7
  "main": "dist/index.js",
8
+ "module": "dist/index.mjs",
8
9
  "types": "dist/index.d.ts",
9
- "scripts": {
10
- "prebuild": "bun run generate:configs",
11
- "build": "bun run fetch:schema && bun run src/scripts/generateGraphql.ts && bun run src/scripts/buildSdk.ts && bun run codegen && bun run lint ./src/sdk --fix && bun run eslint --no-ignore ./src/resources/**/*.graphql --fix && bun run tsc",
12
- "fetch:schema": "curl -s https://graph.codex.io/schema/latest.graphql --output src/resources/schema.graphql",
13
- "generate:configs": "bun run src/scripts/generateNetworkConfigs.ts",
14
- "codegen": "graphql-codegen --config codegen.ts",
15
- "lint": "eslint --ext .ts src",
16
- "test": "jest"
10
+ "exports": {
11
+ ".": {
12
+ "types": "./dist/index.d.ts",
13
+ "import": "./dist/index.mjs",
14
+ "require": "./dist/index.js"
15
+ }
17
16
  },
17
+ "sideEffects": false,
18
18
  "files": [
19
- "dist"
19
+ "dist/index.js",
20
+ "dist/index.mjs",
21
+ "dist/index.d.ts",
22
+ "dist/sdk/",
23
+ "README.md"
20
24
  ],
21
25
  "keywords": [
22
26
  "crypto",
@@ -32,20 +36,13 @@
32
36
  "homepage": "https://docs.codex.io",
33
37
  "description": "The Codex SDK for JavaScript/Typescript. It provides generated types and convenient ways to access the graphql api.",
34
38
  "dependencies": {
35
- "@graphql-tools/wrap": "^10.0.5",
36
- "@types/jest": "^29.5.4",
37
- "@types/lodash": "^4.17.7",
38
- "@types/node": "^20.5.7",
39
- "gql-query-builder": "^3.8.0",
40
- "graphql": "^16.9.0",
41
- "graphql-ast-types": "^1.0.2",
39
+ "@graphql-typed-document-node/core": "^3.2.0",
42
40
  "graphql-request": "^6.1.0",
43
41
  "graphql-ws": "^5.14.0",
44
- "isomorphic-ws": "^5.0.0",
45
- "lodash": "^4.17.21",
46
- "mkdirp": "^3.0.1",
47
- "tsx": "^4.19.1",
48
- "typescript": "^5.2.2"
42
+ "isomorphic-ws": "^5.0.0"
43
+ },
44
+ "peerDependencies": {
45
+ "graphql": "^16.0.0"
49
46
  },
50
47
  "devDependencies": {
51
48
  "@graphql-codegen/cli": "5.0.0",
@@ -53,19 +50,50 @@
53
50
  "@graphql-codegen/introspection": "4.0.0",
54
51
  "@graphql-codegen/typescript": "4.0.1",
55
52
  "@graphql-eslint/eslint-plugin": "^3.20.1",
53
+ "@graphql-tools/wrap": "^10.0.5",
54
+ "@types/jest": "^29.5.4",
55
+ "@types/lodash": "^4.17.7",
56
+ "@types/node": "^20.5.7",
56
57
  "@typescript-eslint/eslint-plugin": "^6.7.3",
57
58
  "@typescript-eslint/parser": "^6.7.3",
59
+ "chalk": "^4.1.2",
60
+ "cli-spinners": "^2.9.2",
61
+ "dotenv": "^16.4.5",
58
62
  "eslint": "^8.50.0",
59
63
  "eslint-config-prettier": "^9.0.0",
60
64
  "eslint-plugin-import": "^2.28.1",
61
65
  "eslint-plugin-prettier": "^5.0.0",
62
66
  "eslint-plugin-simple-import-sort": "^10.0.0",
67
+ "gql-query-builder": "^3.8.0",
68
+ "graphql": "^16.9.0",
69
+ "graphql-tag": "^2.12.6",
63
70
  "jest": "^29.6.4",
71
+ "lodash": "^4.17.21",
64
72
  "merge": "^2.1.1",
73
+ "mkdirp": "^3.0.1",
74
+ "ora": "^5.4.1",
65
75
  "prettier": "^3.0.3",
66
- "ts-jest": "^29.1.1"
76
+ "ts-jest": "^29.1.1",
77
+ "tsx": "^4.19.1",
78
+ "typescript": "^5.2.2"
67
79
  },
68
- "resolutions": {
69
- "graphql": "^16.9.0"
80
+ "scripts": {
81
+ "prebuild": "pnpm run clean && pnpm run generate:configs",
82
+ "clean": "rm -rf dist dist-esm",
83
+ "build": "pnpm run fetch:schema && pnpm run generate:graphql && pnpm run build:sdk && pnpm run codegen && pnpm run lint ./src/sdk --fix && pnpm run build:types",
84
+ "build:types": "pnpm run build:cjs && pnpm run build:esm && pnpm run build:rename-esm",
85
+ "build:cjs": "tsc",
86
+ "build:esm": "tsc -p tsconfig.esm.json",
87
+ "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",
88
+ "fetch:schema": "curl -s https://graph.codex.io/schema/latest.graphql --output src/resources/schema.graphql",
89
+ "generate:configs": "tsx src/scripts/generateNetworkConfigs.ts",
90
+ "generate:graphql": "tsx src/scripts/generateGraphql.ts",
91
+ "build:sdk": "tsx src/scripts/buildSdk.ts",
92
+ "codegen": "graphql-codegen --config codegen.ts",
93
+ "lint": "eslint --ext .ts src",
94
+ "test": "jest",
95
+ "format": "prettier --write .",
96
+ "publish:beta": "pnpm publish --tag beta --access public",
97
+ "publish:latest": "pnpm publish --access public"
70
98
  }
71
- }
99
+ }
@@ -1 +0,0 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,OAAO,MAAM,qBAAqB,CAAC;AAC/C,cAAc,OAAO,CAAC"}
package/dist/index.js.map DELETED
@@ -1 +0,0 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,+DAA+C;AAC/C,wCAAsB"}