@squadbase/server 0.2.1 → 0.2.2-beta.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/dist/index.cjs CHANGED
@@ -20,6 +20,7 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
20
20
  // src/index.ts
21
21
  var index_exports = {};
22
22
  __export(index_exports, {
23
+ createConnectionClient: () => createConnectionClient,
23
24
  createServerClient: () => createServerClient
24
25
  });
25
26
  module.exports = __toCommonJS(index_exports);
@@ -33,6 +34,7 @@ var PREVIEW_SESSION_COOKIE_NAME = "squadbase-preview-session";
33
34
  var APP_BASE_DOMAIN = "squadbase.app";
34
35
  var PREVIEW_BASE_DOMAIN = "preview.app.squadbase.dev";
35
36
  var SANDBOX_ID_ENV_NAME = "INTERNAL_SQUADBASE_SANDBOX_ID";
37
+ var MACHINE_CREDENTIAL_ENV_NAME = "INTERNAL_SQUADBASE_OAUTH_MACHINE_CREDENTIAL";
36
38
 
37
39
  // src/types.ts
38
40
  var import_zod = require("zod");
@@ -115,7 +117,83 @@ var ServerClient = class {
115
117
  }
116
118
  };
117
119
  var createServerClient = (options) => new ServerClient(options);
120
+
121
+ // src/connection-client.ts
122
+ var import_cookie2 = require("cookie");
123
+ var ConnectionClient = class {
124
+ constructor(options) {
125
+ this.options = options;
126
+ }
127
+ async fetch(url, fetchOptions) {
128
+ const proxyUrl = this.resolveProxyUrl();
129
+ const authHeaders = await this.resolveAuthHeaders();
130
+ const response = await fetch(proxyUrl, {
131
+ method: "POST",
132
+ headers: {
133
+ "Content-Type": "application/json",
134
+ ...authHeaders
135
+ },
136
+ body: JSON.stringify({
137
+ url,
138
+ method: fetchOptions?.method,
139
+ headers: fetchOptions?.headers,
140
+ body: fetchOptions?.body,
141
+ timeoutMs: fetchOptions?.timeoutMs
142
+ })
143
+ });
144
+ if (!response.ok) {
145
+ throw new Error(
146
+ `Connection proxy request failed with status ${response.status}`
147
+ );
148
+ }
149
+ return await response.json();
150
+ }
151
+ get projectIdOrThrow() {
152
+ const projectId = this.options.projectId ?? process.env["SQUADBASE_PROJECT_ID"];
153
+ if (!projectId) {
154
+ throw new Error(
155
+ "Project ID is required. Please set SQUADBASE_PROJECT_ID environment variable or provide projectId in ConnectionClient options."
156
+ );
157
+ }
158
+ return projectId;
159
+ }
160
+ get connectionPath() {
161
+ return `/_sqcore/connections/${this.options.connectionId}/request`;
162
+ }
163
+ resolveProxyUrl() {
164
+ const sandboxId = process.env[SANDBOX_ID_ENV_NAME];
165
+ if (sandboxId) {
166
+ const baseDomain2 = this.options._internal?.preview_base_domain ?? PREVIEW_BASE_DOMAIN;
167
+ return `https://${sandboxId}.${baseDomain2}${this.connectionPath}`;
168
+ }
169
+ const baseDomain = this.options._internal?.app_base_domain ?? APP_BASE_DOMAIN;
170
+ return `https://${this.projectIdOrThrow}.${baseDomain}${this.connectionPath}`;
171
+ }
172
+ async resolveAuthHeaders() {
173
+ const machineCredential = process.env[MACHINE_CREDENTIAL_ENV_NAME];
174
+ if (machineCredential) {
175
+ return { Authorization: `Bearer ${machineCredential}` };
176
+ }
177
+ const cookieString = await this.options.cookieOptions.getCookie() ?? "";
178
+ const cookie = (0, import_cookie2.parse)(cookieString);
179
+ const previewSessionToken = cookie[PREVIEW_SESSION_COOKIE_NAME];
180
+ if (previewSessionToken) {
181
+ return {
182
+ Cookie: `${PREVIEW_SESSION_COOKIE_NAME}=${previewSessionToken}`
183
+ };
184
+ }
185
+ const appSessionToken = cookie[APP_SESSION_COOKIE_NAME];
186
+ if (appSessionToken) {
187
+ return { Authorization: `Bearer ${appSessionToken}` };
188
+ }
189
+ throw new Error(
190
+ "No authentication method available for connection proxy. Expected one of: INTERNAL_SQUADBASE_OAUTH_MACHINE_CREDENTIAL env var, preview session cookie, or app session cookie."
191
+ );
192
+ }
193
+ };
194
+ var createConnectionClient = (options) => new ConnectionClient(options);
118
195
  // Annotate the CommonJS export names for ESM import in node:
119
196
  0 && (module.exports = {
197
+ createConnectionClient,
120
198
  createServerClient
121
199
  });
package/dist/index.d.cts CHANGED
@@ -54,4 +54,38 @@ declare class ServerClient {
54
54
  }
55
55
  declare const createServerClient: (options: ServerClientOptions) => ServerClient;
56
56
 
57
- export { type GetCookie, ServerClient, type ServerClientOptions, type User, createServerClient };
57
+ type ConnectionClientOptions = {
58
+ connectionId: string;
59
+ projectId?: string;
60
+ cookieOptions: {
61
+ getCookie: GetCookie;
62
+ };
63
+ _internal?: {
64
+ app_base_domain?: string;
65
+ preview_base_domain?: string;
66
+ };
67
+ };
68
+ type ConnectionFetchOptions = {
69
+ method?: "GET" | "POST" | "PUT" | "PATCH" | "DELETE";
70
+ headers?: Record<string, string>;
71
+ body?: unknown;
72
+ timeoutMs?: number;
73
+ };
74
+ type ConnectionFetchResponse = {
75
+ status: number;
76
+ headers: Record<string, string>;
77
+ body: unknown;
78
+ isBase64Encoded: boolean;
79
+ };
80
+ declare class ConnectionClient {
81
+ private readonly options;
82
+ constructor(options: ConnectionClientOptions);
83
+ fetch(url: string, fetchOptions?: ConnectionFetchOptions): Promise<ConnectionFetchResponse>;
84
+ private get projectIdOrThrow();
85
+ private get connectionPath();
86
+ private resolveProxyUrl;
87
+ private resolveAuthHeaders;
88
+ }
89
+ declare const createConnectionClient: (options: ConnectionClientOptions) => ConnectionClient;
90
+
91
+ export { ConnectionClient, type ConnectionClientOptions, type ConnectionFetchOptions, type ConnectionFetchResponse, type GetCookie, ServerClient, type ServerClientOptions, type User, createConnectionClient, createServerClient };
package/dist/index.d.ts CHANGED
@@ -54,4 +54,38 @@ declare class ServerClient {
54
54
  }
55
55
  declare const createServerClient: (options: ServerClientOptions) => ServerClient;
56
56
 
57
- export { type GetCookie, ServerClient, type ServerClientOptions, type User, createServerClient };
57
+ type ConnectionClientOptions = {
58
+ connectionId: string;
59
+ projectId?: string;
60
+ cookieOptions: {
61
+ getCookie: GetCookie;
62
+ };
63
+ _internal?: {
64
+ app_base_domain?: string;
65
+ preview_base_domain?: string;
66
+ };
67
+ };
68
+ type ConnectionFetchOptions = {
69
+ method?: "GET" | "POST" | "PUT" | "PATCH" | "DELETE";
70
+ headers?: Record<string, string>;
71
+ body?: unknown;
72
+ timeoutMs?: number;
73
+ };
74
+ type ConnectionFetchResponse = {
75
+ status: number;
76
+ headers: Record<string, string>;
77
+ body: unknown;
78
+ isBase64Encoded: boolean;
79
+ };
80
+ declare class ConnectionClient {
81
+ private readonly options;
82
+ constructor(options: ConnectionClientOptions);
83
+ fetch(url: string, fetchOptions?: ConnectionFetchOptions): Promise<ConnectionFetchResponse>;
84
+ private get projectIdOrThrow();
85
+ private get connectionPath();
86
+ private resolveProxyUrl;
87
+ private resolveAuthHeaders;
88
+ }
89
+ declare const createConnectionClient: (options: ConnectionClientOptions) => ConnectionClient;
90
+
91
+ export { ConnectionClient, type ConnectionClientOptions, type ConnectionFetchOptions, type ConnectionFetchResponse, type GetCookie, ServerClient, type ServerClientOptions, type User, createConnectionClient, createServerClient };
package/dist/index.js CHANGED
@@ -7,6 +7,7 @@ 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";
10
11
 
11
12
  // src/types.ts
12
13
  import { z } from "zod";
@@ -89,6 +90,82 @@ var ServerClient = class {
89
90
  }
90
91
  };
91
92
  var createServerClient = (options) => new ServerClient(options);
93
+
94
+ // src/connection-client.ts
95
+ import { parse as parseCookie2 } from "cookie";
96
+ var ConnectionClient = class {
97
+ constructor(options) {
98
+ this.options = options;
99
+ }
100
+ async fetch(url, fetchOptions) {
101
+ const proxyUrl = this.resolveProxyUrl();
102
+ const authHeaders = await this.resolveAuthHeaders();
103
+ const response = await fetch(proxyUrl, {
104
+ method: "POST",
105
+ headers: {
106
+ "Content-Type": "application/json",
107
+ ...authHeaders
108
+ },
109
+ body: JSON.stringify({
110
+ url,
111
+ method: fetchOptions?.method,
112
+ headers: fetchOptions?.headers,
113
+ body: fetchOptions?.body,
114
+ timeoutMs: fetchOptions?.timeoutMs
115
+ })
116
+ });
117
+ if (!response.ok) {
118
+ throw new Error(
119
+ `Connection proxy request failed with status ${response.status}`
120
+ );
121
+ }
122
+ return await response.json();
123
+ }
124
+ get projectIdOrThrow() {
125
+ const projectId = this.options.projectId ?? process.env["SQUADBASE_PROJECT_ID"];
126
+ if (!projectId) {
127
+ throw new Error(
128
+ "Project ID is required. Please set SQUADBASE_PROJECT_ID environment variable or provide projectId in ConnectionClient options."
129
+ );
130
+ }
131
+ return projectId;
132
+ }
133
+ get connectionPath() {
134
+ return `/_sqcore/connections/${this.options.connectionId}/request`;
135
+ }
136
+ resolveProxyUrl() {
137
+ const sandboxId = process.env[SANDBOX_ID_ENV_NAME];
138
+ if (sandboxId) {
139
+ const baseDomain2 = this.options._internal?.preview_base_domain ?? PREVIEW_BASE_DOMAIN;
140
+ return `https://${sandboxId}.${baseDomain2}${this.connectionPath}`;
141
+ }
142
+ const baseDomain = this.options._internal?.app_base_domain ?? APP_BASE_DOMAIN;
143
+ return `https://${this.projectIdOrThrow}.${baseDomain}${this.connectionPath}`;
144
+ }
145
+ async resolveAuthHeaders() {
146
+ const machineCredential = process.env[MACHINE_CREDENTIAL_ENV_NAME];
147
+ if (machineCredential) {
148
+ return { Authorization: `Bearer ${machineCredential}` };
149
+ }
150
+ const cookieString = await this.options.cookieOptions.getCookie() ?? "";
151
+ const cookie = parseCookie2(cookieString);
152
+ const previewSessionToken = cookie[PREVIEW_SESSION_COOKIE_NAME];
153
+ if (previewSessionToken) {
154
+ return {
155
+ Cookie: `${PREVIEW_SESSION_COOKIE_NAME}=${previewSessionToken}`
156
+ };
157
+ }
158
+ const appSessionToken = cookie[APP_SESSION_COOKIE_NAME];
159
+ if (appSessionToken) {
160
+ return { Authorization: `Bearer ${appSessionToken}` };
161
+ }
162
+ throw new Error(
163
+ "No authentication method available for connection proxy. Expected one of: INTERNAL_SQUADBASE_OAUTH_MACHINE_CREDENTIAL env var, preview session cookie, or app session cookie."
164
+ );
165
+ }
166
+ };
167
+ var createConnectionClient = (options) => new ConnectionClient(options);
92
168
  export {
169
+ createConnectionClient,
93
170
  createServerClient
94
171
  };
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.0",
4
4
  "description": "Server-side SDK for Squadbase",
5
5
  "main": "./dist/index.js",
6
6
  "types": "./dist/index.d.ts",