@gitterm/sdk 0.6.0 → 0.6.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.
package/README.md CHANGED
@@ -118,12 +118,14 @@ without a GitTerm server, is described at the [end](#direct-provider-mode).
118
118
  `createGittermClient()` resolves its server and token in this order: constructor options →
119
119
  `GITTERM_SERVER_URL` / `GITTERM_API_TOKEN` → the CLI config at `~/.config/gitterm/cli.json`
120
120
  written by `gitterm login`. The default server is the hosted API at `https://api.gitterm.dev`;
121
- self-hosted instances use the same SDK with their own `serverUrl`:
121
+ self-hosted instances use the same SDK with their own `serverUrl`. Behind the bundled
122
+ path-routing proxy, point it at the `/api` path (for example
123
+ `GITTERM_SERVER_URL=https://<host>/api`); a server reached directly uses its origin:
122
124
 
123
125
  ```ts
124
126
  const hosted = createGittermClient({ token: process.env.GITTERM_API_TOKEN });
125
127
  const selfHosted = createGittermClient({
126
- serverUrl: "https://gitterm.example.com", // or http://localhost:3000
128
+ serverUrl: "https://gitterm.example.com/api", // or http://localhost:3000
127
129
  token: process.env.GITTERM_API_TOKEN,
128
130
  });
129
131
  const fromCli = createGittermClient(); // env vars, then the CLI's saved login
package/dist/index.js CHANGED
@@ -112,6 +112,23 @@ function normalizeServerUrl(value) {
112
112
  url.search = "";
113
113
  return url.toString().replace(/\/$/, "");
114
114
  }
115
+ function withPath(serverUrl, pathname) {
116
+ const url = new URL(serverUrl);
117
+ url.pathname = pathname;
118
+ return url.toString();
119
+ }
120
+ function basePath(serverUrl) {
121
+ return new URL(serverUrl).pathname.replace(/\/+$/, "");
122
+ }
123
+ function trpcEndpoint(serverUrl) {
124
+ const base = basePath(serverUrl);
125
+ return withPath(serverUrl, base.endsWith("/trpc") ? base : `${base}/trpc`);
126
+ }
127
+ function apiEndpoint(serverUrl, route) {
128
+ const base = basePath(serverUrl);
129
+ const apiBase = base.endsWith("/api") ? base : `${base}/api`;
130
+ return withPath(serverUrl, `${apiBase}/${route.replace(/^\/+/, "")}`);
131
+ }
115
132
  function createNoRedirectFetch(fetchImpl = fetch) {
116
133
  return async (input, init) => {
117
134
  const response = await fetchImpl(input, { ...init, redirect: "manual" });
@@ -286,9 +303,6 @@ function resolveCredentials(options) {
286
303
  }
287
304
  return { serverUrl: normalizeServerUrl(serverUrl), token };
288
305
  }
289
- function toTrpcUrl(serverUrl) {
290
- return new URL("/trpc", serverUrl).toString();
291
- }
292
306
  function toIso(value) {
293
307
  if (!value)
294
308
  return null;
@@ -492,14 +506,14 @@ function createGittermClient(options = {}) {
492
506
  splitLink({
493
507
  condition: (operation) => operation.type === "subscription",
494
508
  true: httpSubscriptionLink({
495
- url: toTrpcUrl(credentials.serverUrl),
509
+ url: trpcEndpoint(credentials.serverUrl),
496
510
  EventSource,
497
511
  eventSourceOptions: {
498
512
  fetch: (url, init) => fetchImpl(url, { ...init, headers: { ...init.headers, authorization } })
499
513
  }
500
514
  }),
501
515
  false: httpBatchLink({
502
- url: toTrpcUrl(credentials.serverUrl),
516
+ url: trpcEndpoint(credentials.serverUrl),
503
517
  fetch: fetchImpl,
504
518
  headers: () => ({ authorization })
505
519
  })
@@ -747,7 +761,7 @@ function sleep2(ms) {
747
761
  async function loginWithDeviceCode(serverUrl, options = {}) {
748
762
  const normalizedServerUrl = normalizeServerUrl(serverUrl);
749
763
  const fetchImpl = createNoRedirectFetch(options.fetch);
750
- const codeRes = await fetchImpl(new URL("/api/device/code", normalizedServerUrl), {
764
+ const codeRes = await fetchImpl(apiEndpoint(normalizedServerUrl, "device/code"), {
751
765
  method: "POST",
752
766
  headers: { "content-type": "application/json" },
753
767
  body: JSON.stringify({ clientName: options.clientName ?? "gitterm" })
@@ -763,7 +777,7 @@ async function loginWithDeviceCode(serverUrl, options = {}) {
763
777
  });
764
778
  const deadline = Date.now() + codeJson.expiresInSeconds * 1000;
765
779
  while (Date.now() < deadline) {
766
- const tokenRes = await fetchImpl(new URL("/api/device/token", normalizedServerUrl), {
780
+ const tokenRes = await fetchImpl(apiEndpoint(normalizedServerUrl, "device/token"), {
767
781
  method: "POST",
768
782
  headers: { "content-type": "application/json" },
769
783
  body: JSON.stringify({ deviceCode: codeJson.deviceCode })
@@ -802,10 +816,6 @@ function errorCode(code) {
802
816
  return code;
803
817
  return "SERVER_ERROR";
804
818
  }
805
- function toWorkspaceTrpcUrl(serverUrl) {
806
- const url = new URL(serverUrl);
807
- return url.pathname.endsWith("/trpc") ? serverUrl : new URL("/trpc", url).toString();
808
- }
809
819
  function createGittermWorkspaceClient(options = {}) {
810
820
  const detected = options.serverUrl && options.token && options.workspaceId ? null : getWorkspaceEnvironment();
811
821
  const rawServerUrl = options.serverUrl ?? detected?.serverUrl;
@@ -818,7 +828,7 @@ function createGittermWorkspaceClient(options = {}) {
818
828
  const trpc = createTRPCClient2({
819
829
  links: [
820
830
  httpBatchLink2({
821
- url: toWorkspaceTrpcUrl(serverUrl),
831
+ url: trpcEndpoint(serverUrl),
822
832
  fetch: createNoRedirectFetch(options.fetch),
823
833
  headers: () => ({ authorization: `Bearer ${token}` })
824
834
  })
@@ -1,2 +1,9 @@
1
1
  export declare function normalizeServerUrl(value: string): string;
2
+ /**
3
+ * Resolve the tRPC endpoint relative to the server URL's path, so a base like
4
+ * `https://host/api` (path-routing proxy) maps to `https://host/api/trpc`.
5
+ */
6
+ export declare function trpcEndpoint(serverUrl: string): string;
7
+ /** Resolve a server `/api/*` route, adding `/api` only when the base doesn't already end in it. */
8
+ export declare function apiEndpoint(serverUrl: string, route: string): string;
2
9
  export declare function createNoRedirectFetch(fetchImpl?: typeof fetch): typeof fetch;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gitterm/sdk",
3
- "version": "0.6.0",
3
+ "version": "0.6.1",
4
4
  "files": [
5
5
  "dist"
6
6
  ],