@squadbase/server 0.2.1 → 0.2.2-beta.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/dist/index.cjs CHANGED
@@ -25,7 +25,7 @@ __export(index_exports, {
25
25
  module.exports = __toCommonJS(index_exports);
26
26
 
27
27
  // src/client.ts
28
- var import_cookie = require("cookie");
28
+ var import_cookie2 = require("cookie");
29
29
 
30
30
  // src/constants.ts
31
31
  var APP_SESSION_COOKIE_NAME = "__Host-squadbase-session";
@@ -33,6 +33,75 @@ var PREVIEW_SESSION_COOKIE_NAME = "squadbase-preview-session";
33
33
  var APP_BASE_DOMAIN = "squadbase.app";
34
34
  var PREVIEW_BASE_DOMAIN = "preview.app.squadbase.dev";
35
35
  var SANDBOX_ID_ENV_NAME = "INTERNAL_SQUADBASE_SANDBOX_ID";
36
+ var MACHINE_CREDENTIAL_ENV_NAME = "INTERNAL_SQUADBASE_OAUTH_MACHINE_CREDENTIAL";
37
+
38
+ // src/connection-client.ts
39
+ var import_cookie = require("cookie");
40
+ var ConnectionClient = class {
41
+ constructor(options) {
42
+ this.options = options;
43
+ }
44
+ async fetch(url, fetchOptions) {
45
+ const proxyUrl = this.resolveProxyUrl();
46
+ const authHeaders = await this.resolveAuthHeaders();
47
+ return await fetch(proxyUrl, {
48
+ method: "POST",
49
+ headers: {
50
+ "Content-Type": "application/json",
51
+ ...authHeaders
52
+ },
53
+ body: JSON.stringify({
54
+ url,
55
+ method: fetchOptions?.method,
56
+ headers: fetchOptions?.headers,
57
+ body: fetchOptions?.body,
58
+ timeoutMs: fetchOptions?.timeoutMs
59
+ })
60
+ });
61
+ }
62
+ get projectIdOrThrow() {
63
+ const projectId = this.options.projectId ?? process.env["SQUADBASE_PROJECT_ID"];
64
+ if (!projectId) {
65
+ throw new Error(
66
+ "Project ID is required. Please set SQUADBASE_PROJECT_ID environment variable or provide projectId in ConnectionClient options."
67
+ );
68
+ }
69
+ return projectId;
70
+ }
71
+ get connectionPath() {
72
+ return `/_sqcore/connections/${this.options.connectionId}/request`;
73
+ }
74
+ resolveProxyUrl() {
75
+ const sandboxId = process.env[SANDBOX_ID_ENV_NAME];
76
+ if (sandboxId) {
77
+ const baseDomain2 = this.options._internal?.preview_base_domain ?? PREVIEW_BASE_DOMAIN;
78
+ return `https://${sandboxId}.${baseDomain2}${this.connectionPath}`;
79
+ }
80
+ const baseDomain = this.options._internal?.app_base_domain ?? APP_BASE_DOMAIN;
81
+ return `https://${this.projectIdOrThrow}.${baseDomain}${this.connectionPath}`;
82
+ }
83
+ async resolveAuthHeaders() {
84
+ const machineCredential = process.env[MACHINE_CREDENTIAL_ENV_NAME];
85
+ if (machineCredential) {
86
+ return { Authorization: `Bearer ${machineCredential}` };
87
+ }
88
+ const cookieString = await this.options.cookieOptions.getCookie() ?? "";
89
+ const cookie = (0, import_cookie.parse)(cookieString);
90
+ const previewSessionToken = cookie[PREVIEW_SESSION_COOKIE_NAME];
91
+ if (previewSessionToken) {
92
+ return {
93
+ Cookie: `${PREVIEW_SESSION_COOKIE_NAME}=${previewSessionToken}`
94
+ };
95
+ }
96
+ const appSessionToken = cookie[APP_SESSION_COOKIE_NAME];
97
+ if (appSessionToken) {
98
+ return { Authorization: `Bearer ${appSessionToken}` };
99
+ }
100
+ throw new Error(
101
+ "No authentication method available for connection proxy. Expected one of: INTERNAL_SQUADBASE_OAUTH_MACHINE_CREDENTIAL env var, preview session cookie, or app session cookie."
102
+ );
103
+ }
104
+ };
36
105
 
37
106
  // src/types.ts
38
107
  var import_zod = require("zod");
@@ -51,7 +120,7 @@ var ServerClient = class {
51
120
  this.options = options;
52
121
  }
53
122
  async getUser() {
54
- const cookie = (0, import_cookie.parse)(
123
+ const cookie = (0, import_cookie2.parse)(
55
124
  await this.options.cookieOptions.getCookie() ?? ""
56
125
  );
57
126
  const appSessionToken = cookie[APP_SESSION_COOKIE_NAME];
@@ -113,6 +182,14 @@ var ServerClient = class {
113
182
  }
114
183
  );
115
184
  }
185
+ connection(connectionId) {
186
+ return new ConnectionClient({
187
+ connectionId,
188
+ projectId: this.options.projectId,
189
+ cookieOptions: this.options.cookieOptions,
190
+ _internal: this.options._internal
191
+ });
192
+ }
116
193
  };
117
194
  var createServerClient = (options) => new ServerClient(options);
118
195
  // Annotate the CommonJS export names for ESM import in node:
package/dist/index.d.cts CHANGED
@@ -1,5 +1,32 @@
1
1
  import { z } from 'zod';
2
2
 
3
+ type ConnectionClientOptions = {
4
+ connectionId: string;
5
+ projectId?: string;
6
+ cookieOptions: {
7
+ getCookie: GetCookie;
8
+ };
9
+ _internal?: {
10
+ app_base_domain?: string;
11
+ preview_base_domain?: string;
12
+ };
13
+ };
14
+ type ConnectionFetchOptions = {
15
+ method?: "GET" | "POST" | "PUT" | "PATCH" | "DELETE";
16
+ headers?: Record<string, string>;
17
+ body?: unknown;
18
+ timeoutMs?: number;
19
+ };
20
+ declare class ConnectionClient {
21
+ private readonly options;
22
+ constructor(options: ConnectionClientOptions);
23
+ fetch(url: string, fetchOptions?: ConnectionFetchOptions): Promise<Response>;
24
+ private get projectIdOrThrow();
25
+ private get connectionPath();
26
+ private resolveProxyUrl;
27
+ private resolveAuthHeaders;
28
+ }
29
+
3
30
  declare const zUser: z.ZodObject<{
4
31
  username: z.ZodString;
5
32
  email: z.ZodString;
@@ -51,7 +78,8 @@ declare class ServerClient {
51
78
  private get sandboxIdOrThrow();
52
79
  private getUserWithAppSessionRequest;
53
80
  private getUserWithPreviewSession;
81
+ connection(connectionId: string): ConnectionClient;
54
82
  }
55
83
  declare const createServerClient: (options: ServerClientOptions) => ServerClient;
56
84
 
57
- export { type GetCookie, ServerClient, type ServerClientOptions, type User, createServerClient };
85
+ export { ConnectionClient, type ConnectionFetchOptions, type GetCookie, ServerClient, type ServerClientOptions, type User, createServerClient };
package/dist/index.d.ts CHANGED
@@ -1,5 +1,32 @@
1
1
  import { z } from 'zod';
2
2
 
3
+ type ConnectionClientOptions = {
4
+ connectionId: string;
5
+ projectId?: string;
6
+ cookieOptions: {
7
+ getCookie: GetCookie;
8
+ };
9
+ _internal?: {
10
+ app_base_domain?: string;
11
+ preview_base_domain?: string;
12
+ };
13
+ };
14
+ type ConnectionFetchOptions = {
15
+ method?: "GET" | "POST" | "PUT" | "PATCH" | "DELETE";
16
+ headers?: Record<string, string>;
17
+ body?: unknown;
18
+ timeoutMs?: number;
19
+ };
20
+ declare class ConnectionClient {
21
+ private readonly options;
22
+ constructor(options: ConnectionClientOptions);
23
+ fetch(url: string, fetchOptions?: ConnectionFetchOptions): Promise<Response>;
24
+ private get projectIdOrThrow();
25
+ private get connectionPath();
26
+ private resolveProxyUrl;
27
+ private resolveAuthHeaders;
28
+ }
29
+
3
30
  declare const zUser: z.ZodObject<{
4
31
  username: z.ZodString;
5
32
  email: z.ZodString;
@@ -51,7 +78,8 @@ declare class ServerClient {
51
78
  private get sandboxIdOrThrow();
52
79
  private getUserWithAppSessionRequest;
53
80
  private getUserWithPreviewSession;
81
+ connection(connectionId: string): ConnectionClient;
54
82
  }
55
83
  declare const createServerClient: (options: ServerClientOptions) => ServerClient;
56
84
 
57
- export { type GetCookie, ServerClient, type ServerClientOptions, type User, createServerClient };
85
+ export { ConnectionClient, type ConnectionFetchOptions, type GetCookie, ServerClient, type ServerClientOptions, type User, createServerClient };
package/dist/index.js CHANGED
@@ -1,5 +1,5 @@
1
1
  // src/client.ts
2
- import { parse as parseCookie } from "cookie";
2
+ import { parse as parseCookie2 } from "cookie";
3
3
 
4
4
  // src/constants.ts
5
5
  var APP_SESSION_COOKIE_NAME = "__Host-squadbase-session";
@@ -7,6 +7,75 @@ var PREVIEW_SESSION_COOKIE_NAME = "squadbase-preview-session";
7
7
  var APP_BASE_DOMAIN = "squadbase.app";
8
8
  var PREVIEW_BASE_DOMAIN = "preview.app.squadbase.dev";
9
9
  var SANDBOX_ID_ENV_NAME = "INTERNAL_SQUADBASE_SANDBOX_ID";
10
+ var MACHINE_CREDENTIAL_ENV_NAME = "INTERNAL_SQUADBASE_OAUTH_MACHINE_CREDENTIAL";
11
+
12
+ // src/connection-client.ts
13
+ import { parse as parseCookie } from "cookie";
14
+ var ConnectionClient = class {
15
+ constructor(options) {
16
+ this.options = options;
17
+ }
18
+ async fetch(url, fetchOptions) {
19
+ const proxyUrl = this.resolveProxyUrl();
20
+ const authHeaders = await this.resolveAuthHeaders();
21
+ return await fetch(proxyUrl, {
22
+ method: "POST",
23
+ headers: {
24
+ "Content-Type": "application/json",
25
+ ...authHeaders
26
+ },
27
+ body: JSON.stringify({
28
+ url,
29
+ method: fetchOptions?.method,
30
+ headers: fetchOptions?.headers,
31
+ body: fetchOptions?.body,
32
+ timeoutMs: fetchOptions?.timeoutMs
33
+ })
34
+ });
35
+ }
36
+ get projectIdOrThrow() {
37
+ const projectId = this.options.projectId ?? process.env["SQUADBASE_PROJECT_ID"];
38
+ if (!projectId) {
39
+ throw new Error(
40
+ "Project ID is required. Please set SQUADBASE_PROJECT_ID environment variable or provide projectId in ConnectionClient options."
41
+ );
42
+ }
43
+ return projectId;
44
+ }
45
+ get connectionPath() {
46
+ return `/_sqcore/connections/${this.options.connectionId}/request`;
47
+ }
48
+ resolveProxyUrl() {
49
+ const sandboxId = process.env[SANDBOX_ID_ENV_NAME];
50
+ if (sandboxId) {
51
+ const baseDomain2 = this.options._internal?.preview_base_domain ?? PREVIEW_BASE_DOMAIN;
52
+ return `https://${sandboxId}.${baseDomain2}${this.connectionPath}`;
53
+ }
54
+ const baseDomain = this.options._internal?.app_base_domain ?? APP_BASE_DOMAIN;
55
+ return `https://${this.projectIdOrThrow}.${baseDomain}${this.connectionPath}`;
56
+ }
57
+ async resolveAuthHeaders() {
58
+ const machineCredential = process.env[MACHINE_CREDENTIAL_ENV_NAME];
59
+ if (machineCredential) {
60
+ return { Authorization: `Bearer ${machineCredential}` };
61
+ }
62
+ const cookieString = await this.options.cookieOptions.getCookie() ?? "";
63
+ const cookie = parseCookie(cookieString);
64
+ const previewSessionToken = cookie[PREVIEW_SESSION_COOKIE_NAME];
65
+ if (previewSessionToken) {
66
+ return {
67
+ Cookie: `${PREVIEW_SESSION_COOKIE_NAME}=${previewSessionToken}`
68
+ };
69
+ }
70
+ const appSessionToken = cookie[APP_SESSION_COOKIE_NAME];
71
+ if (appSessionToken) {
72
+ return { Authorization: `Bearer ${appSessionToken}` };
73
+ }
74
+ throw new Error(
75
+ "No authentication method available for connection proxy. Expected one of: INTERNAL_SQUADBASE_OAUTH_MACHINE_CREDENTIAL env var, preview session cookie, or app session cookie."
76
+ );
77
+ }
78
+ };
10
79
 
11
80
  // src/types.ts
12
81
  import { z } from "zod";
@@ -25,7 +94,7 @@ var ServerClient = class {
25
94
  this.options = options;
26
95
  }
27
96
  async getUser() {
28
- const cookie = parseCookie(
97
+ const cookie = parseCookie2(
29
98
  await this.options.cookieOptions.getCookie() ?? ""
30
99
  );
31
100
  const appSessionToken = cookie[APP_SESSION_COOKIE_NAME];
@@ -87,6 +156,14 @@ var ServerClient = class {
87
156
  }
88
157
  );
89
158
  }
159
+ connection(connectionId) {
160
+ return new ConnectionClient({
161
+ connectionId,
162
+ projectId: this.options.projectId,
163
+ cookieOptions: this.options.cookieOptions,
164
+ _internal: this.options._internal
165
+ });
166
+ }
90
167
  };
91
168
  var createServerClient = (options) => new ServerClient(options);
92
169
  export {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@squadbase/server",
3
- "version": "0.2.1",
3
+ "version": "0.2.2-beta.1",
4
4
  "description": "Server-side SDK for Squadbase",
5
5
  "main": "./dist/index.js",
6
6
  "types": "./dist/index.d.ts",