graphlit-client 1.0.20240418015 → 1.0.20240418016

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 CHANGED
@@ -1,12 +1,13 @@
1
- # JavaScript Client for Graphlit Platform
1
+ # Node.js Client for Graphlit Platform
2
2
  ## Overview
3
3
 
4
- The Graphlit Client for JavaScript enables straightforward interactions with the Graphlit API, allowing developers to execute GraphQL queries and mutations against the Graphlit service. This document outlines the setup process and provides a basic example of using the client.
4
+ The Graphlit Client for Node.js enables straightforward interactions with the Graphlit API, allowing developers to execute GraphQL queries and mutations against the Graphlit service. This document outlines the setup process and provides a basic example of using the client.
5
5
 
6
6
  ## Prerequisites
7
7
 
8
8
  Before you begin, ensure you have the following:
9
9
 
10
+ - Node.js installed on your system (recommended version 14.x or higher).
10
11
  - An active account on the [Graphlit Platform](https://portal.graphlit.dev) with access to the API settings dashboard.
11
12
 
12
13
  ## Installation
package/dist/client.d.ts CHANGED
@@ -10,7 +10,6 @@ declare class Graphlit {
10
10
  private correlationId;
11
11
  private token;
12
12
  constructor(organizationId?: string, environmentId?: string, jwtSecret?: string, ownerId?: string, apiUri?: string, correlationId?: string);
13
- private initializeJWT;
14
13
  createAlert(alert: Types.AlertInput): Promise<Types.Alert>;
15
14
  updateAlert(alert: Types.AlertUpdateInput): Promise<Types.Alert>;
16
15
  deleteAlert(id: string): Promise<Types.Alert>;
package/dist/client.js CHANGED
@@ -33,7 +33,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
33
33
  };
34
34
  Object.defineProperty(exports, "__esModule", { value: true });
35
35
  exports.Types = exports.Graphlit = void 0;
36
- const jose_1 = require("jose");
36
+ const jwt = __importStar(require("jsonwebtoken"));
37
37
  const core_1 = require("@apollo/client/core");
38
38
  const Documents = __importStar(require("./generated/graphql-documents"));
39
39
  const dotenv = __importStar(require("dotenv"));
@@ -67,55 +67,31 @@ class Graphlit {
67
67
  if (!this.jwtSecret) {
68
68
  throw new Error("Graphlit environment JWT secret is required.");
69
69
  }
70
- this.initializeJWT().then(() => {
71
- const httpLink = (0, core_1.createHttpLink)({
72
- uri: this.apiUri,
73
- });
74
- const authLink = new core_1.ApolloLink((operation, forward) => {
75
- operation.setContext({
76
- headers: {
77
- Authorization: this.token ? `Bearer ${this.token}` : "",
78
- }
79
- });
80
- return forward(operation);
81
- });
82
- this.client = new core_1.ApolloClient({
83
- link: authLink.concat(httpLink),
84
- cache: new core_1.InMemoryCache(),
70
+ if (!this.jwtSecret) {
71
+ throw new Error("JWT secret is required.");
72
+ }
73
+ const expiration = Math.floor(Date.now() / 1000) + (60 * 60); // one hour from now
74
+ const payload = {
75
+ "https://graphlit.io/jwt/claims": Object.assign(Object.assign({ "x-graphlit-environment-id": this.environmentId, "x-graphlit-organization-id": this.organizationId }, (this.ownerId && { "x-graphlit-owner-id": this.ownerId })), { "x-graphlit-role": "Owner" }),
76
+ exp: expiration,
77
+ iss: "graphlit",
78
+ aud: "https://portal.graphlit.io",
79
+ };
80
+ this.token = jwt.sign(payload, this.jwtSecret, { algorithm: 'HS256' });
81
+ const httpLink = (0, core_1.createHttpLink)({
82
+ uri: this.apiUri,
83
+ });
84
+ const authLink = new core_1.ApolloLink((operation, forward) => {
85
+ operation.setContext({
86
+ headers: {
87
+ Authorization: this.token ? `Bearer ${this.token}` : "",
88
+ }
85
89
  });
90
+ return forward(operation);
86
91
  });
87
- }
88
- initializeJWT() {
89
- return __awaiter(this, void 0, void 0, function* () {
90
- if (!this.jwtSecret)
91
- return;
92
- const expiration = Math.floor(Date.now() / 1000) + (60 * 60); // one hour from now
93
- const payload = {
94
- "https://graphlit.io/jwt/claims": Object.assign(Object.assign({ "x-graphlit-environment-id": this.environmentId, "x-graphlit-organization-id": this.organizationId }, (this.ownerId && { "x-graphlit-owner-id": this.ownerId })), { "x-graphlit-role": "Owner" }),
95
- exp: expiration,
96
- iss: "graphlit",
97
- aud: "https://portal.graphlit.io",
98
- };
99
- function uint8ArrayToBase64(buffer) {
100
- var binary = '';
101
- var bytes = new Uint8Array(buffer);
102
- var len = bytes.byteLength;
103
- for (var i = 0; i < len; i++) {
104
- binary += String.fromCharCode(bytes[i]);
105
- }
106
- return window.btoa(binary);
107
- }
108
- // NOTE: not using Buffer, so we don't require Node.js
109
- const secretKeyJWK = yield (0, jose_1.importJWK)({
110
- kty: 'oct',
111
- k: uint8ArrayToBase64(new TextEncoder().encode(this.jwtSecret)),
112
- alg: 'HS256'
113
- }, 'HS256');
114
- this.token = yield new jose_1.SignJWT(payload)
115
- .setProtectedHeader({ alg: 'HS256' })
116
- .setIssuedAt()
117
- .setExpirationTime('1h')
118
- .sign(secretKeyJWK);
92
+ this.client = new core_1.ApolloClient({
93
+ link: authLink.concat(httpLink),
94
+ cache: new core_1.InMemoryCache(),
119
95
  });
120
96
  }
121
97
  createAlert(alert) {
package/package.json CHANGED
@@ -1,9 +1,19 @@
1
1
  {
2
2
  "name": "graphlit-client",
3
- "version": "1.0.20240418015",
3
+ "version": "1.0.20240418016",
4
4
  "description": "Graphlit API TypeScript Client",
5
5
  "main": "dist/client.js",
6
6
  "types": "dist/client.d.ts",
7
+ "repository": {
8
+ "type": "git",
9
+ "url": "https://github.com/graphlit/graphlit-client-typescript"
10
+ },
11
+ "contributors": [
12
+ "Kirk Marple (https://github.com/kirk-marple)"
13
+ ],
14
+ "engines": {
15
+ "node": ">=14.0.0"
16
+ },
7
17
  "scripts": {
8
18
  "generate": "graphql-codegen --config codegen.yml",
9
19
  "build": "tsc -p tsconfig.json"
@@ -27,10 +37,11 @@
27
37
  "@graphql-codegen/typescript": "^4.0.6",
28
38
  "@graphql-codegen/typescript-operations": "^4.2.0",
29
39
  "graphql": "^16.8.1",
30
- "jose": "^5.2.4"
40
+ "jsonwebtoken": "^9.0.2"
31
41
  },
32
42
  "devDependencies": {
33
43
  "@graphql-codegen/typescript-document-nodes": "^4.0.6",
44
+ "@types/jsonwebtoken": "^9.0.6",
34
45
  "typescript": "^5.4.5"
35
46
  }
36
47
  }