graphlit-client 1.0.20240418008 → 1.0.20240418009

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/client.d.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  import { ApolloClient, NormalizedCacheObject } from '@apollo/client/core';
2
2
  import * as Types from './generated/graphql-types';
3
3
  declare class Graphlit {
4
- client: ApolloClient<NormalizedCacheObject>;
4
+ client: ApolloClient<NormalizedCacheObject> | undefined;
5
5
  private apiUri;
6
6
  private environmentId;
7
7
  private organizationId;
@@ -10,6 +10,7 @@ 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;
13
14
  createAlert(alert: Types.AlertInput): Promise<Types.Alert>;
14
15
  updateAlert(alert: Types.AlertUpdateInput): Promise<Types.Alert>;
15
16
  deleteAlert(id: string): Promise<Types.Alert>;
package/dist/client.js CHANGED
@@ -33,9 +33,9 @@ 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
37
  const core_1 = require("@apollo/client/core");
37
38
  const Documents = __importStar(require("./generated/graphql-documents"));
38
- const jwt = __importStar(require("jsonwebtoken"));
39
39
  const dotenv = __importStar(require("dotenv"));
40
40
  // Initialize dotenv to use environment variables
41
41
  dotenv.config();
@@ -50,32 +50,54 @@ class Graphlit {
50
50
  this.ownerId = ownerId || process.env.GRAPHLIT_OWNER_ID;
51
51
  // optional: for billing correlation of multiple operations
52
52
  this.correlationId = correlationId || undefined;
53
- // Set token expiration to one hour from now
54
- const expiration = Math.floor(Date.now() / 1000) + (60 * 60);
55
- const payload = {
56
- "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" }),
57
- exp: expiration,
58
- iss: "graphlit",
59
- aud: "https://portal.graphlit.io",
60
- };
53
+ if (!this.organizationId) {
54
+ throw new Error("Graphlit organization identifier is required.");
55
+ }
56
+ if (!this.environmentId) {
57
+ throw new Error("Graphlit environment identifier is required.");
58
+ }
61
59
  if (!this.jwtSecret) {
62
- throw new Error("JWT secret is required.");
60
+ throw new Error("Graphlit environment JWT secret is required.");
63
61
  }
64
- this.token = jwt.sign(payload, this.jwtSecret, { algorithm: 'HS256' });
65
- const httpLink = (0, core_1.createHttpLink)({
66
- uri: this.apiUri,
67
- });
68
- const authLink = new core_1.ApolloLink((operation, forward) => {
69
- operation.setContext({
70
- headers: {
71
- Authorization: this.token ? `Bearer ${this.token}` : "",
72
- }
62
+ this.initializeJWT().then(() => {
63
+ const httpLink = (0, core_1.createHttpLink)({
64
+ uri: this.apiUri,
65
+ });
66
+ const authLink = new core_1.ApolloLink((operation, forward) => {
67
+ operation.setContext({
68
+ headers: {
69
+ Authorization: this.token ? `Bearer ${this.token}` : "",
70
+ }
71
+ });
72
+ return forward(operation);
73
+ });
74
+ this.client = new core_1.ApolloClient({
75
+ link: authLink.concat(httpLink),
76
+ cache: new core_1.InMemoryCache(),
73
77
  });
74
- return forward(operation);
75
78
  });
76
- this.client = new core_1.ApolloClient({
77
- link: authLink.concat(httpLink),
78
- cache: new core_1.InMemoryCache(),
79
+ }
80
+ initializeJWT() {
81
+ return __awaiter(this, void 0, void 0, function* () {
82
+ if (!this.jwtSecret)
83
+ return;
84
+ const expiration = Math.floor(Date.now() / 1000) + (60 * 60); // one hour from now
85
+ const payload = {
86
+ "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" }),
87
+ exp: expiration,
88
+ iss: "graphlit",
89
+ aud: "https://portal.graphlit.io",
90
+ };
91
+ const secretKeyJWK = yield (0, jose_1.importJWK)({
92
+ kty: 'oct',
93
+ k: Buffer.from(this.jwtSecret).toString('base64'),
94
+ alg: 'HS256'
95
+ }, 'HS256');
96
+ this.token = yield new jose_1.SignJWT(payload)
97
+ .setProtectedHeader({ alg: 'HS256' })
98
+ .setIssuedAt()
99
+ .setExpirationTime('1h')
100
+ .sign(secretKeyJWK);
79
101
  });
80
102
  }
81
103
  createAlert(alert) {
@@ -381,6 +403,8 @@ class Graphlit {
381
403
  // helper functions
382
404
  mutateAndCheckError(mutation, variables) {
383
405
  return __awaiter(this, void 0, void 0, function* () {
406
+ if (!this.client)
407
+ throw new Error("Apollo Client not configured.");
384
408
  try {
385
409
  const result = yield this.client.mutate({
386
410
  mutation,
@@ -404,6 +428,8 @@ class Graphlit {
404
428
  }
405
429
  queryAndCheckError(query, variables) {
406
430
  return __awaiter(this, void 0, void 0, function* () {
431
+ if (!this.client)
432
+ throw new Error("Apollo Client not configured.");
407
433
  try {
408
434
  const result = yield this.client.query({
409
435
  query,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "graphlit-client",
3
- "version": "1.0.20240418008",
3
+ "version": "1.0.20240418009",
4
4
  "description": "Graphlit API TypeScript Client",
5
5
  "main": "dist/client.js",
6
6
  "types": "dist/client.d.ts",
@@ -27,11 +27,10 @@
27
27
  "@graphql-codegen/typescript": "^4.0.6",
28
28
  "@graphql-codegen/typescript-operations": "^4.2.0",
29
29
  "graphql": "^16.8.1",
30
- "jsonwebtoken": "^9.0.2"
30
+ "jose": "^5.2.4"
31
31
  },
32
32
  "devDependencies": {
33
33
  "@graphql-codegen/typescript-document-nodes": "^4.0.6",
34
- "@types/jsonwebtoken": "^9.0.6",
35
34
  "typescript": "^5.4.5"
36
35
  }
37
36
  }