@squadbase/server 0.2.2-beta.0 → 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 +77 -78
- package/dist/index.d.cts +29 -35
- package/dist/index.d.ts +29 -35
- package/dist/index.js +77 -77
- package/package.json +1 -1
package/dist/index.cjs
CHANGED
|
@@ -20,13 +20,12 @@ 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,
|
|
24
23
|
createServerClient: () => createServerClient
|
|
25
24
|
});
|
|
26
25
|
module.exports = __toCommonJS(index_exports);
|
|
27
26
|
|
|
28
27
|
// src/client.ts
|
|
29
|
-
var
|
|
28
|
+
var import_cookie2 = require("cookie");
|
|
30
29
|
|
|
31
30
|
// src/constants.ts
|
|
32
31
|
var APP_SESSION_COOKIE_NAME = "__Host-squadbase-session";
|
|
@@ -36,6 +35,74 @@ var PREVIEW_BASE_DOMAIN = "preview.app.squadbase.dev";
|
|
|
36
35
|
var SANDBOX_ID_ENV_NAME = "INTERNAL_SQUADBASE_SANDBOX_ID";
|
|
37
36
|
var MACHINE_CREDENTIAL_ENV_NAME = "INTERNAL_SQUADBASE_OAUTH_MACHINE_CREDENTIAL";
|
|
38
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
|
+
};
|
|
105
|
+
|
|
39
106
|
// src/types.ts
|
|
40
107
|
var import_zod = require("zod");
|
|
41
108
|
var zUser = import_zod.z.object({
|
|
@@ -53,7 +120,7 @@ var ServerClient = class {
|
|
|
53
120
|
this.options = options;
|
|
54
121
|
}
|
|
55
122
|
async getUser() {
|
|
56
|
-
const cookie = (0,
|
|
123
|
+
const cookie = (0, import_cookie2.parse)(
|
|
57
124
|
await this.options.cookieOptions.getCookie() ?? ""
|
|
58
125
|
);
|
|
59
126
|
const appSessionToken = cookie[APP_SESSION_COOKIE_NAME];
|
|
@@ -115,85 +182,17 @@ var ServerClient = class {
|
|
|
115
182
|
}
|
|
116
183
|
);
|
|
117
184
|
}
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
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
|
-
})
|
|
185
|
+
connection(connectionId) {
|
|
186
|
+
return new ConnectionClient({
|
|
187
|
+
connectionId,
|
|
188
|
+
projectId: this.options.projectId,
|
|
189
|
+
cookieOptions: this.options.cookieOptions,
|
|
190
|
+
_internal: this.options._internal
|
|
143
191
|
});
|
|
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
192
|
}
|
|
193
193
|
};
|
|
194
|
-
var
|
|
194
|
+
var createServerClient = (options) => new ServerClient(options);
|
|
195
195
|
// Annotate the CommonJS export names for ESM import in node:
|
|
196
196
|
0 && (module.exports = {
|
|
197
|
-
createConnectionClient,
|
|
198
197
|
createServerClient
|
|
199
198
|
});
|
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,41 +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
|
-
type
|
|
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 };
|
|
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,41 +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
|
-
type
|
|
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 };
|
|
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
|
|
2
|
+
import { parse as parseCookie2 } from "cookie";
|
|
3
3
|
|
|
4
4
|
// src/constants.ts
|
|
5
5
|
var APP_SESSION_COOKIE_NAME = "__Host-squadbase-session";
|
|
@@ -9,6 +9,74 @@ var PREVIEW_BASE_DOMAIN = "preview.app.squadbase.dev";
|
|
|
9
9
|
var SANDBOX_ID_ENV_NAME = "INTERNAL_SQUADBASE_SANDBOX_ID";
|
|
10
10
|
var MACHINE_CREDENTIAL_ENV_NAME = "INTERNAL_SQUADBASE_OAUTH_MACHINE_CREDENTIAL";
|
|
11
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
|
+
};
|
|
79
|
+
|
|
12
80
|
// src/types.ts
|
|
13
81
|
import { z } from "zod";
|
|
14
82
|
var zUser = z.object({
|
|
@@ -26,7 +94,7 @@ var ServerClient = class {
|
|
|
26
94
|
this.options = options;
|
|
27
95
|
}
|
|
28
96
|
async getUser() {
|
|
29
|
-
const cookie =
|
|
97
|
+
const cookie = parseCookie2(
|
|
30
98
|
await this.options.cookieOptions.getCookie() ?? ""
|
|
31
99
|
);
|
|
32
100
|
const appSessionToken = cookie[APP_SESSION_COOKIE_NAME];
|
|
@@ -88,84 +156,16 @@ var ServerClient = class {
|
|
|
88
156
|
}
|
|
89
157
|
);
|
|
90
158
|
}
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
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
|
-
})
|
|
159
|
+
connection(connectionId) {
|
|
160
|
+
return new ConnectionClient({
|
|
161
|
+
connectionId,
|
|
162
|
+
projectId: this.options.projectId,
|
|
163
|
+
cookieOptions: this.options.cookieOptions,
|
|
164
|
+
_internal: this.options._internal
|
|
116
165
|
});
|
|
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
|
}
|
|
166
167
|
};
|
|
167
|
-
var
|
|
168
|
+
var createServerClient = (options) => new ServerClient(options);
|
|
168
169
|
export {
|
|
169
|
-
createConnectionClient,
|
|
170
170
|
createServerClient
|
|
171
171
|
};
|