graphlit-client 1.0.20240418008 → 1.0.20240418010

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,49 +33,73 @@ 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
- dotenv.config();
41
+ if (process)
42
+ dotenv.config();
42
43
  // Define the Graphlit class
43
44
  class Graphlit {
44
45
  constructor(organizationId, environmentId, jwtSecret, ownerId, apiUri, correlationId) {
46
+ var _a, _b, _c, _d;
45
47
  this.apiUri = apiUri || "https://data-scus.graphlit.io/api/v1";
46
- this.organizationId = organizationId || process.env.GRAPHLIT_ORGANIZATION_ID;
47
- this.environmentId = environmentId || process.env.GRAPHLIT_ENVIRONMENT_ID;
48
- this.jwtSecret = jwtSecret || process.env.GRAPHLIT_JWT_SECRET;
48
+ this.organizationId = organizationId || ((_a = process === null || process === void 0 ? void 0 : process.env) === null || _a === void 0 ? void 0 : _a.GRAPHLIT_ORGANIZATION_ID);
49
+ this.environmentId = environmentId || ((_b = process === null || process === void 0 ? void 0 : process.env) === null || _b === void 0 ? void 0 : _b.GRAPHLIT_ENVIRONMENT_ID);
50
+ this.jwtSecret = jwtSecret || ((_c = process === null || process === void 0 ? void 0 : process.env) === null || _c === void 0 ? void 0 : _c.GRAPHLIT_JWT_SECRET);
49
51
  // optional: for multi-tenant support
50
- this.ownerId = ownerId || process.env.GRAPHLIT_OWNER_ID;
52
+ this.ownerId = ownerId || ((_d = process === null || process === void 0 ? void 0 : process.env) === null || _d === void 0 ? void 0 : _d.GRAPHLIT_OWNER_ID);
51
53
  // optional: for billing correlation of multiple operations
52
54
  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
- };
55
+ if (!this.organizationId) {
56
+ throw new Error("Graphlit organization identifier is required.");
57
+ }
58
+ if (!this.environmentId) {
59
+ throw new Error("Graphlit environment identifier is required.");
60
+ }
61
61
  if (!this.jwtSecret) {
62
- throw new Error("JWT secret is required.");
62
+ throw new Error("Graphlit environment JWT secret is required.");
63
63
  }
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
- }
64
+ this.initializeJWT().then(() => {
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
+ }
73
+ });
74
+ return forward(operation);
75
+ });
76
+ this.client = new core_1.ApolloClient({
77
+ link: authLink.concat(httpLink),
78
+ cache: new core_1.InMemoryCache(),
73
79
  });
74
- return forward(operation);
75
80
  });
76
- this.client = new core_1.ApolloClient({
77
- link: authLink.concat(httpLink),
78
- cache: new core_1.InMemoryCache(),
81
+ }
82
+ initializeJWT() {
83
+ return __awaiter(this, void 0, void 0, function* () {
84
+ if (!this.jwtSecret)
85
+ return;
86
+ const expiration = Math.floor(Date.now() / 1000) + (60 * 60); // one hour from now
87
+ const payload = {
88
+ "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" }),
89
+ exp: expiration,
90
+ iss: "graphlit",
91
+ aud: "https://portal.graphlit.io",
92
+ };
93
+ const secretKeyJWK = yield (0, jose_1.importJWK)({
94
+ kty: 'oct',
95
+ k: Buffer.from(this.jwtSecret).toString('base64'),
96
+ alg: 'HS256'
97
+ }, 'HS256');
98
+ this.token = yield new jose_1.SignJWT(payload)
99
+ .setProtectedHeader({ alg: 'HS256' })
100
+ .setIssuedAt()
101
+ .setExpirationTime('1h')
102
+ .sign(secretKeyJWK);
79
103
  });
80
104
  }
81
105
  createAlert(alert) {
@@ -381,6 +405,8 @@ class Graphlit {
381
405
  // helper functions
382
406
  mutateAndCheckError(mutation, variables) {
383
407
  return __awaiter(this, void 0, void 0, function* () {
408
+ if (!this.client)
409
+ throw new Error("Apollo Client not configured.");
384
410
  try {
385
411
  const result = yield this.client.mutate({
386
412
  mutation,
@@ -404,6 +430,8 @@ class Graphlit {
404
430
  }
405
431
  queryAndCheckError(query, variables) {
406
432
  return __awaiter(this, void 0, void 0, function* () {
433
+ if (!this.client)
434
+ throw new Error("Apollo Client not configured.");
407
435
  try {
408
436
  const result = yield this.client.query({
409
437
  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.20240418010",
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
  }