@squadbase/server 0.0.3-beta.1 → 0.0.3-beta.2

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
@@ -29,8 +29,11 @@ var import_cookie = require("cookie");
29
29
 
30
30
  // src/constants.ts
31
31
  var APP_SESSION_COOKIE_NAME = "__Host-squadbase-session";
32
- var EDITOR_SESSION_COOKIE_NAME = "squadbase-preview-session";
32
+ var PREVIEW_SESSION_COOKIE_NAME = "squadbase-preview-session";
33
33
  var APP_BASE_DOMAIN = "squadbase.app";
34
+ var PREVIEW_BASE_DOMAIN = "preview.app.squadbase.dev";
35
+ var PROJECT_ID_ENV_NAME = "INTERNAL_SQUADBASE_PROJECT_ID";
36
+ var SANDBOX_ID_ENV_NAME = "INTERNAL_SQUADBASE_SANDBOX_ID";
34
37
 
35
38
  // ../../node_modules/.pnpm/zod@3.24.2/node_modules/zod/lib/index.mjs
36
39
  var util;
@@ -4111,8 +4114,9 @@ var ServerClient = class {
4111
4114
  const cookie = (0, import_cookie.parse)(
4112
4115
  await this.options.cookieOptions.getCookie() ?? ""
4113
4116
  );
4114
- const sessionToken = cookie[APP_SESSION_COOKIE_NAME] ?? cookie[EDITOR_SESSION_COOKIE_NAME];
4115
- if (!sessionToken) {
4117
+ const appSessionToken = cookie[APP_SESSION_COOKIE_NAME];
4118
+ const previewSessionToken = cookie[PREVIEW_SESSION_COOKIE_NAME];
4119
+ if (!appSessionToken && !previewSessionToken) {
4116
4120
  if (!this.options.mockUser) {
4117
4121
  throw new Error(
4118
4122
  "No session token or mock user provided. Please provide one of them to use getUser in local environment."
@@ -4120,15 +4124,7 @@ var ServerClient = class {
4120
4124
  }
4121
4125
  return this.options.mockUser;
4122
4126
  }
4123
- const request = new Request(
4124
- `https://${this.options.projectId}.${this.options._internal?.app_base_domain ?? APP_BASE_DOMAIN}/_sqcore/auth`,
4125
- {
4126
- method: "POST",
4127
- headers: {
4128
- Authorization: `Bearer ${sessionToken}`
4129
- }
4130
- }
4131
- );
4127
+ const request = appSessionToken ? this.getUserWithAppSessionRequest(appSessionToken) : this.getUserWithPreviewSession(previewSessionToken);
4132
4128
  const response = await fetch(request);
4133
4129
  if (!response.ok) {
4134
4130
  throw new Error("Failed to get user");
@@ -4137,6 +4133,46 @@ var ServerClient = class {
4137
4133
  const user = zUser.parse(body);
4138
4134
  return user;
4139
4135
  }
4136
+ get projectIdOrThrow() {
4137
+ const projectId = this.options.projectId ?? process.env[PROJECT_ID_ENV_NAME];
4138
+ if (!projectId) {
4139
+ throw new Error(
4140
+ `Project ID is required. Please provide projectId in ServerClient options if you are running in local development environment.`
4141
+ );
4142
+ }
4143
+ return projectId;
4144
+ }
4145
+ get sandboxIdOrThrow() {
4146
+ const sandboxId = process.env[SANDBOX_ID_ENV_NAME];
4147
+ if (!sandboxId) {
4148
+ throw new Error(
4149
+ `Sandbox ID is required. Please set ${SANDBOX_ID_ENV_NAME} environment variable.`
4150
+ );
4151
+ }
4152
+ return sandboxId;
4153
+ }
4154
+ getUserWithAppSessionRequest(appSessionToken) {
4155
+ return new Request(
4156
+ `https://${this.projectIdOrThrow}.${this.options._internal?.app_base_domain ?? APP_BASE_DOMAIN}/_sqcore/auth`,
4157
+ {
4158
+ method: "POST",
4159
+ headers: {
4160
+ Authorization: `Bearer ${appSessionToken}`
4161
+ }
4162
+ }
4163
+ );
4164
+ }
4165
+ getUserWithPreviewSession(previewSessionToken) {
4166
+ return new Request(
4167
+ `https://${this.sandboxIdOrThrow}.${this.options._internal?.preview_base_domain ?? PREVIEW_BASE_DOMAIN}/_sqcore/auth`,
4168
+ {
4169
+ method: "POST",
4170
+ headers: {
4171
+ Cookie: `${PREVIEW_SESSION_COOKIE_NAME}=${previewSessionToken}`
4172
+ }
4173
+ }
4174
+ );
4175
+ }
4140
4176
  };
4141
4177
  var createServerClient = (options) => new ServerClient(options);
4142
4178
  // Annotate the CommonJS export names for ESM import in node:
package/dist/index.d.cts CHANGED
@@ -26,13 +26,14 @@ type User = z.infer<typeof zUser>;
26
26
 
27
27
  type GetCookie = () => Promise<string | undefined> | string | undefined;
28
28
  type ServerClientOptions = {
29
- projectId: string;
29
+ projectId?: string;
30
30
  cookieOptions: {
31
31
  getCookie: GetCookie;
32
32
  };
33
33
  mockUser?: User;
34
34
  _internal?: {
35
35
  app_base_domain?: string;
36
+ preview_base_domain?: string;
36
37
  };
37
38
  };
38
39
  declare class ServerClient {
@@ -46,6 +47,10 @@ declare class ServerClient {
46
47
  iconUrl: string | null;
47
48
  roles: string[];
48
49
  }>;
50
+ private get projectIdOrThrow();
51
+ private get sandboxIdOrThrow();
52
+ private getUserWithAppSessionRequest;
53
+ private getUserWithPreviewSession;
49
54
  }
50
55
  declare const createServerClient: (options: ServerClientOptions) => ServerClient;
51
56
 
package/dist/index.d.ts CHANGED
@@ -26,13 +26,14 @@ type User = z.infer<typeof zUser>;
26
26
 
27
27
  type GetCookie = () => Promise<string | undefined> | string | undefined;
28
28
  type ServerClientOptions = {
29
- projectId: string;
29
+ projectId?: string;
30
30
  cookieOptions: {
31
31
  getCookie: GetCookie;
32
32
  };
33
33
  mockUser?: User;
34
34
  _internal?: {
35
35
  app_base_domain?: string;
36
+ preview_base_domain?: string;
36
37
  };
37
38
  };
38
39
  declare class ServerClient {
@@ -46,6 +47,10 @@ declare class ServerClient {
46
47
  iconUrl: string | null;
47
48
  roles: string[];
48
49
  }>;
50
+ private get projectIdOrThrow();
51
+ private get sandboxIdOrThrow();
52
+ private getUserWithAppSessionRequest;
53
+ private getUserWithPreviewSession;
49
54
  }
50
55
  declare const createServerClient: (options: ServerClientOptions) => ServerClient;
51
56
 
package/dist/index.js CHANGED
@@ -3,8 +3,11 @@ import { parse as parseCookie } from "cookie";
3
3
 
4
4
  // src/constants.ts
5
5
  var APP_SESSION_COOKIE_NAME = "__Host-squadbase-session";
6
- var EDITOR_SESSION_COOKIE_NAME = "squadbase-preview-session";
6
+ var PREVIEW_SESSION_COOKIE_NAME = "squadbase-preview-session";
7
7
  var APP_BASE_DOMAIN = "squadbase.app";
8
+ var PREVIEW_BASE_DOMAIN = "preview.app.squadbase.dev";
9
+ var PROJECT_ID_ENV_NAME = "INTERNAL_SQUADBASE_PROJECT_ID";
10
+ var SANDBOX_ID_ENV_NAME = "INTERNAL_SQUADBASE_SANDBOX_ID";
8
11
 
9
12
  // ../../node_modules/.pnpm/zod@3.24.2/node_modules/zod/lib/index.mjs
10
13
  var util;
@@ -4085,8 +4088,9 @@ var ServerClient = class {
4085
4088
  const cookie = parseCookie(
4086
4089
  await this.options.cookieOptions.getCookie() ?? ""
4087
4090
  );
4088
- const sessionToken = cookie[APP_SESSION_COOKIE_NAME] ?? cookie[EDITOR_SESSION_COOKIE_NAME];
4089
- if (!sessionToken) {
4091
+ const appSessionToken = cookie[APP_SESSION_COOKIE_NAME];
4092
+ const previewSessionToken = cookie[PREVIEW_SESSION_COOKIE_NAME];
4093
+ if (!appSessionToken && !previewSessionToken) {
4090
4094
  if (!this.options.mockUser) {
4091
4095
  throw new Error(
4092
4096
  "No session token or mock user provided. Please provide one of them to use getUser in local environment."
@@ -4094,15 +4098,7 @@ var ServerClient = class {
4094
4098
  }
4095
4099
  return this.options.mockUser;
4096
4100
  }
4097
- const request = new Request(
4098
- `https://${this.options.projectId}.${this.options._internal?.app_base_domain ?? APP_BASE_DOMAIN}/_sqcore/auth`,
4099
- {
4100
- method: "POST",
4101
- headers: {
4102
- Authorization: `Bearer ${sessionToken}`
4103
- }
4104
- }
4105
- );
4101
+ const request = appSessionToken ? this.getUserWithAppSessionRequest(appSessionToken) : this.getUserWithPreviewSession(previewSessionToken);
4106
4102
  const response = await fetch(request);
4107
4103
  if (!response.ok) {
4108
4104
  throw new Error("Failed to get user");
@@ -4111,6 +4107,46 @@ var ServerClient = class {
4111
4107
  const user = zUser.parse(body);
4112
4108
  return user;
4113
4109
  }
4110
+ get projectIdOrThrow() {
4111
+ const projectId = this.options.projectId ?? process.env[PROJECT_ID_ENV_NAME];
4112
+ if (!projectId) {
4113
+ throw new Error(
4114
+ `Project ID is required. Please provide projectId in ServerClient options if you are running in local development environment.`
4115
+ );
4116
+ }
4117
+ return projectId;
4118
+ }
4119
+ get sandboxIdOrThrow() {
4120
+ const sandboxId = process.env[SANDBOX_ID_ENV_NAME];
4121
+ if (!sandboxId) {
4122
+ throw new Error(
4123
+ `Sandbox ID is required. Please set ${SANDBOX_ID_ENV_NAME} environment variable.`
4124
+ );
4125
+ }
4126
+ return sandboxId;
4127
+ }
4128
+ getUserWithAppSessionRequest(appSessionToken) {
4129
+ return new Request(
4130
+ `https://${this.projectIdOrThrow}.${this.options._internal?.app_base_domain ?? APP_BASE_DOMAIN}/_sqcore/auth`,
4131
+ {
4132
+ method: "POST",
4133
+ headers: {
4134
+ Authorization: `Bearer ${appSessionToken}`
4135
+ }
4136
+ }
4137
+ );
4138
+ }
4139
+ getUserWithPreviewSession(previewSessionToken) {
4140
+ return new Request(
4141
+ `https://${this.sandboxIdOrThrow}.${this.options._internal?.preview_base_domain ?? PREVIEW_BASE_DOMAIN}/_sqcore/auth`,
4142
+ {
4143
+ method: "POST",
4144
+ headers: {
4145
+ Cookie: `${PREVIEW_SESSION_COOKIE_NAME}=${previewSessionToken}`
4146
+ }
4147
+ }
4148
+ );
4149
+ }
4114
4150
  };
4115
4151
  var createServerClient = (options) => new ServerClient(options);
4116
4152
  export {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@squadbase/server",
3
- "version": "0.0.3-beta.1",
3
+ "version": "0.0.3-beta.2",
4
4
  "description": "Server-side SDK for Squadbase",
5
5
  "main": "./dist/index.js",
6
6
  "types": "./dist/index.d.ts",