@trackunit/serverside-utils 0.4.77 → 0.5.0

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
@@ -9,9 +9,60 @@ For more info and a full guide on Iris App SDK Development, please visit our [De
9
9
  ## Installation
10
10
 
11
11
  ```bash
12
- yarn add @trackunit/serverside-utils
12
+ npm install @trackunit/serverside-utils
13
13
  ```
14
14
 
15
+ ## Authenticated Trackunit public GraphQL
16
+
17
+ `createTrackunitGraphqlClient` is only for the Trackunit public GraphQL API.
18
+
19
+ Set up GraphQL and define one named operation per `.graphql` file:
20
+
21
+ ```bash
22
+ npx nx generate @trackunit/react-graphql-tools:add-graphql --project=<project>
23
+ npx nx run <project>:graphql-hooks
24
+ ```
25
+
26
+ ```graphql
27
+ # src/graphql/asset-by-id.graphql
28
+ query AssetById($id: ID!) {
29
+ asset(id: $id) {
30
+ id
31
+ name
32
+ }
33
+ }
34
+ ```
35
+
36
+ Run `graphql-hooks` after changing an operation, then use the generated document:
37
+
38
+ ```typescript
39
+ import { createTrackunitGraphqlClient } from "@trackunit/serverside-utils";
40
+ import { Hono } from "hono";
41
+ import { AssetByIdDocument } from "./generated/graphql-api/graphql";
42
+
43
+ const app = new Hono();
44
+
45
+ app.get("/assets/:id", async c => {
46
+ const client = createTrackunitGraphqlClient({ context: c });
47
+ const data = await client.request({
48
+ document: AssetByIdDocument,
49
+ variables: { id: c.req.param("id") },
50
+ signal: c.req.raw.signal,
51
+ });
52
+
53
+ return c.json(data);
54
+ });
55
+
56
+ export default app.fetch;
57
+ ```
58
+
59
+ ### Behavior
60
+
61
+ - The Hono context supplies the requesting user's authentication. Missing authentication throws `Missing user token` before any request.
62
+ - `SDK_ENVIRONMENT` selects the Trackunit public endpoint for `DEV`, `STAGE`, or `PROD`; unset defaults to `PROD`.
63
+ - `request({ document, variables, signal? })` infers data and variable types from the generated `TypedDocumentNode`. Generated types do not validate responses at runtime. `signal` cancels the request.
64
+ - Transport, HTTP, and GraphQL errors propagate from the shared client (e.g. `graphql-request`'s `ClientError`).
65
+
15
66
  ## Trackunit
16
67
 
17
68
  This package was developed by Trackunit ApS.
package/index.cjs.js CHANGED
@@ -1,5 +1,6 @@
1
1
  'use strict';
2
2
 
3
+ var serverGraphqlClient = require('@trackunit/server-graphql-client');
3
4
  var zod = require('zod');
4
5
  var jwtDecode = require('jwt-decode');
5
6
 
@@ -161,6 +162,22 @@ const getUserToken = ({ context }) => {
161
162
  return undefined;
162
163
  };
163
164
 
165
+ /**
166
+ * Creates a typed public Trackunit GraphQL client authenticated as the requesting user.
167
+ */
168
+ const createTrackunitGraphqlClient = ({ context }) => {
169
+ const userToken = getUserToken({ context });
170
+ if (!userToken) {
171
+ throw new Error("Missing user token");
172
+ }
173
+ return serverGraphqlClient.createServerGraphqlClient({
174
+ baseUrl: getGraphqlUrl(),
175
+ name: "trackunit-graphql-client",
176
+ headers: { Authorization: `Bearer ${userToken}` },
177
+ resolveUrl: serverGraphqlClient.operationNameUrlResolver,
178
+ });
179
+ };
180
+
164
181
  const JwtAccountPayloadSchema = zod.z.object({
165
182
  accountId: zod.z.string().optional(),
166
183
  assumedAccountId: zod.z.string().optional(),
@@ -504,6 +521,7 @@ const requireScopes = (scopes, options = {}) => {
504
521
 
505
522
  exports.ACCOUNT_ADMIN_IRIS_APP_TOKEN_HEADER = ACCOUNT_ADMIN_IRIS_APP_TOKEN_HEADER;
506
523
  exports.FORWARDED_AUTHORIZATION_HEADER = FORWARDED_AUTHORIZATION_HEADER;
524
+ exports.createTrackunitGraphqlClient = createTrackunitGraphqlClient;
507
525
  exports.executePublicQuery = executePublicQuery;
508
526
  exports.getAccountId = getAccountId;
509
527
  exports.getDatabricksAccessToken = getDatabricksAccessToken;
package/index.esm.js CHANGED
@@ -1,3 +1,4 @@
1
+ import { createServerGraphqlClient, operationNameUrlResolver } from '@trackunit/server-graphql-client';
1
2
  import { z } from 'zod';
2
3
  import jwtDecode from 'jwt-decode';
3
4
 
@@ -159,6 +160,22 @@ const getUserToken = ({ context }) => {
159
160
  return undefined;
160
161
  };
161
162
 
163
+ /**
164
+ * Creates a typed public Trackunit GraphQL client authenticated as the requesting user.
165
+ */
166
+ const createTrackunitGraphqlClient = ({ context }) => {
167
+ const userToken = getUserToken({ context });
168
+ if (!userToken) {
169
+ throw new Error("Missing user token");
170
+ }
171
+ return createServerGraphqlClient({
172
+ baseUrl: getGraphqlUrl(),
173
+ name: "trackunit-graphql-client",
174
+ headers: { Authorization: `Bearer ${userToken}` },
175
+ resolveUrl: operationNameUrlResolver,
176
+ });
177
+ };
178
+
162
179
  const JwtAccountPayloadSchema = z.object({
163
180
  accountId: z.string().optional(),
164
181
  assumedAccountId: z.string().optional(),
@@ -500,4 +517,4 @@ const requireScopes = (scopes, options = {}) => {
500
517
  };
501
518
  };
502
519
 
503
- export { ACCOUNT_ADMIN_IRIS_APP_TOKEN_HEADER, FORWARDED_AUTHORIZATION_HEADER, executePublicQuery, getAccountId, getDatabricksAccessToken, getGraphqlUrl, getSecret, getUserToken, hasScopes, requireScopes };
520
+ export { ACCOUNT_ADMIN_IRIS_APP_TOKEN_HEADER, FORWARDED_AUTHORIZATION_HEADER, createTrackunitGraphqlClient, executePublicQuery, getAccountId, getDatabricksAccessToken, getGraphqlUrl, getSecret, getUserToken, hasScopes, requireScopes };
package/package.json CHANGED
@@ -1,16 +1,18 @@
1
1
  {
2
2
  "name": "@trackunit/serverside-utils",
3
- "version": "0.4.77",
3
+ "version": "0.5.0",
4
4
  "repository": "https://github.com/Trackunit/manager",
5
5
  "license": "SEE LICENSE IN LICENSE.txt",
6
6
  "engines": {
7
7
  "node": ">=24.x"
8
8
  },
9
9
  "dependencies": {
10
+ "@trackunit/server-graphql-client": "0.0.1",
10
11
  "jwt-decode": "^3.1.2",
11
12
  "zod": "^3.25.76"
12
13
  },
13
14
  "peerDependencies": {
15
+ "graphql": "^16.10.0",
14
16
  "hono": "^4.12.12"
15
17
  },
16
18
  "migrations": "./migrations.json",
@@ -0,0 +1,10 @@
1
+ import { type ServerGraphqlClient } from "@trackunit/server-graphql-client";
2
+ import type { Context } from "hono";
3
+ export type CreateTrackunitGraphqlClientProps = {
4
+ readonly context: Context;
5
+ };
6
+ export type TrackunitGraphqlClient = ServerGraphqlClient;
7
+ /**
8
+ * Creates a typed public Trackunit GraphQL client authenticated as the requesting user.
9
+ */
10
+ export declare const createTrackunitGraphqlClient: ({ context }: CreateTrackunitGraphqlClientProps) => TrackunitGraphqlClient;
package/src/index.d.ts CHANGED
@@ -1,3 +1,4 @@
1
+ export { createTrackunitGraphqlClient, type CreateTrackunitGraphqlClientProps, type TrackunitGraphqlClient, } from "./create-trackunit-graphql-client";
1
2
  export { executePublicQuery, getGraphqlUrl, type ExecutePublicQueryProps } from "./execute-public-query";
2
3
  export { getAccountId } from "./get-account-id";
3
4
  export { getDatabricksAccessToken, type GetDatabricksAccessTokenProps } from "./get-databricks-access-token";