@squadbase/server 0.0.3-beta.0 → 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
@@ -28,8 +28,12 @@ module.exports = __toCommonJS(index_exports);
28
28
  var import_cookie = require("cookie");
29
29
 
30
30
  // src/constants.ts
31
- var SESSION_COOKIE_NAME = "__Host-squadbase-session";
31
+ var APP_SESSION_COOKIE_NAME = "__Host-squadbase-session";
32
+ var PREVIEW_SESSION_COOKIE_NAME = "squadbase-preview-session";
32
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";
33
37
 
34
38
  // ../../node_modules/.pnpm/zod@3.24.2/node_modules/zod/lib/index.mjs
35
39
  var util;
@@ -4110,8 +4114,9 @@ var ServerClient = class {
4110
4114
  const cookie = (0, import_cookie.parse)(
4111
4115
  await this.options.cookieOptions.getCookie() ?? ""
4112
4116
  );
4113
- const sessionToken = cookie[SESSION_COOKIE_NAME];
4114
- if (!sessionToken) {
4117
+ const appSessionToken = cookie[APP_SESSION_COOKIE_NAME];
4118
+ const previewSessionToken = cookie[PREVIEW_SESSION_COOKIE_NAME];
4119
+ if (!appSessionToken && !previewSessionToken) {
4115
4120
  if (!this.options.mockUser) {
4116
4121
  throw new Error(
4117
4122
  "No session token or mock user provided. Please provide one of them to use getUser in local environment."
@@ -4119,15 +4124,7 @@ var ServerClient = class {
4119
4124
  }
4120
4125
  return this.options.mockUser;
4121
4126
  }
4122
- const request = new Request(
4123
- `https://${this.options.projectId}.${this.options._internal?.app_base_domain ?? APP_BASE_DOMAIN}/_sqcore/auth`,
4124
- {
4125
- method: "POST",
4126
- headers: {
4127
- Authorization: `Bearer ${sessionToken}`
4128
- }
4129
- }
4130
- );
4127
+ const request = appSessionToken ? this.getUserWithAppSessionRequest(appSessionToken) : this.getUserWithPreviewSession(previewSessionToken);
4131
4128
  const response = await fetch(request);
4132
4129
  if (!response.ok) {
4133
4130
  throw new Error("Failed to get user");
@@ -4136,6 +4133,46 @@ var ServerClient = class {
4136
4133
  const user = zUser.parse(body);
4137
4134
  return user;
4138
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
+ }
4139
4176
  };
4140
4177
  var createServerClient = (options) => new ServerClient(options);
4141
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
@@ -2,8 +2,12 @@
2
2
  import { parse as parseCookie } from "cookie";
3
3
 
4
4
  // src/constants.ts
5
- var SESSION_COOKIE_NAME = "__Host-squadbase-session";
5
+ var APP_SESSION_COOKIE_NAME = "__Host-squadbase-session";
6
+ var PREVIEW_SESSION_COOKIE_NAME = "squadbase-preview-session";
6
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";
7
11
 
8
12
  // ../../node_modules/.pnpm/zod@3.24.2/node_modules/zod/lib/index.mjs
9
13
  var util;
@@ -4084,8 +4088,9 @@ var ServerClient = class {
4084
4088
  const cookie = parseCookie(
4085
4089
  await this.options.cookieOptions.getCookie() ?? ""
4086
4090
  );
4087
- const sessionToken = cookie[SESSION_COOKIE_NAME];
4088
- if (!sessionToken) {
4091
+ const appSessionToken = cookie[APP_SESSION_COOKIE_NAME];
4092
+ const previewSessionToken = cookie[PREVIEW_SESSION_COOKIE_NAME];
4093
+ if (!appSessionToken && !previewSessionToken) {
4089
4094
  if (!this.options.mockUser) {
4090
4095
  throw new Error(
4091
4096
  "No session token or mock user provided. Please provide one of them to use getUser in local environment."
@@ -4093,15 +4098,7 @@ var ServerClient = class {
4093
4098
  }
4094
4099
  return this.options.mockUser;
4095
4100
  }
4096
- const request = new Request(
4097
- `https://${this.options.projectId}.${this.options._internal?.app_base_domain ?? APP_BASE_DOMAIN}/_sqcore/auth`,
4098
- {
4099
- method: "POST",
4100
- headers: {
4101
- Authorization: `Bearer ${sessionToken}`
4102
- }
4103
- }
4104
- );
4101
+ const request = appSessionToken ? this.getUserWithAppSessionRequest(appSessionToken) : this.getUserWithPreviewSession(previewSessionToken);
4105
4102
  const response = await fetch(request);
4106
4103
  if (!response.ok) {
4107
4104
  throw new Error("Failed to get user");
@@ -4110,6 +4107,46 @@ var ServerClient = class {
4110
4107
  const user = zUser.parse(body);
4111
4108
  return user;
4112
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
+ }
4113
4150
  };
4114
4151
  var createServerClient = (options) => new ServerClient(options);
4115
4152
  export {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@squadbase/server",
3
- "version": "0.0.3-beta.0",
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",