@builder-builder/builder 0.0.22 → 0.0.24

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/README.md CHANGED
@@ -17,9 +17,40 @@ Where BB runs and which backing services it talks to.
17
17
  | local dev | Clerk test | local supabase (docker) |
18
18
  | Vercel production | Clerk production | Supabase cloud project |
19
19
 
20
+ ## What lives where
21
+
22
+ Knowing what's stored in Clerk vs Supabase matters because `npm run db:reset` wipes Supabase but not Clerk.
23
+
24
+ | Lives in Clerk (survives `db:reset`) | Lives in Supabase (wiped by `db:reset`) |
25
+ | ------------------------------------ | --------------------------------------- |
26
+ | Users, orgs, roles, permissions | `entities`, `entity_edits` |
27
+ | API keys (with all their claims) | `builder_versions` |
28
+ | | `variants`, `variant_edits` |
29
+ | | `entity_references` |
30
+
31
+ After a reset, you typically re-seed entities from a consumer repo. API keys keep working — re-seeding only repopulates Supabase.
32
+
20
33
  ## Builder environments
21
34
 
22
- Every entity carries an `environment` of `development`, `staging`, or `production`. Same database, different rows. `development` rows are mutable; `staging` and `production` are immutable snapshots written when promoting from `development`.
35
+ Every entity carries an `environment` of `development`, `staging`, or `production`. Same database, different tables/rows. `development` rows are mutable; `staging` and `production` are immutable snapshots written by promotion.
36
+
37
+ | Environment | `GET /api/builder/[id]` returns | `GET /api/builder/[id]/variants` returns |
38
+ | ------------- | ------------------------------- | ---------------------------------------- |
39
+ | `development` | live editable entity | live editable variants |
40
+ | `staging` | staging snapshot | staging snapshot variants |
41
+ | `production` | production snapshot | production snapshot variants |
42
+
43
+ ## Promotion
44
+
45
+ The only path from `development` (mutable working copy) into `staging` or `production` (immutable snapshots) is through the **Promote** action in BB's UI. Promotion:
46
+
47
+ 1. Runs validation on the entity and its references.
48
+ 2. If validation passes, writes a new immutable row into the target environment's snapshot table.
49
+ 3. If validation fails, the promote is rejected — nothing changes.
50
+
51
+ So `builder_versions` is, by construction, always free of validation errors. Consumers reading with `environment: production` never see broken data; consumers reading with `environment: development` may see in-flight errors and should expect to handle them.
52
+
53
+ Bulk writes that bypass the UI (like a seed script) land in `entities` only — they still need a UI promote afterwards to become visible to `production`-env consumers.
23
54
 
24
55
  ## API key scoping
25
56
 
@@ -33,15 +64,18 @@ Each API key is minted in `/settings` with three properties:
33
64
 
34
65
  Keys are scoped to the org they were minted in and never see other orgs' data.
35
66
 
36
- | Environment | `GET /api/builder/[id]` returns | `GET /api/builder/[id]/variants` returns |
37
- | ------------- | ------------------------------- | ---------------------------------------- |
38
- | `development` | live editable entity | live editable variants |
39
- | `staging` | staging snapshot | staging snapshot variants |
40
- | `production` | production snapshot | production snapshot variants |
67
+ ### Clerk permissions
68
+
69
+ API key management is gated by `org:api_keys:{create,read,update,delete}`. These are **custom organization permissions** Clerk doesn't ship them. In the Clerk dashboard:
70
+
71
+ 1. **Configure → Roles & Permissions → Permissions** — define `api_keys:create`, `api_keys:read`, `api_keys:update`, `api_keys:delete` (Clerk prefixes them with `org:` automatically).
72
+ 2. **Edit the org Admin role** — check all four, then **save** (defining a permission and granting it to a role are separate steps).
73
+
74
+ Members need to sign out/in for new role permissions to land in their session token. Setup is per-Clerk-instance, so repeat for Clerk test and Clerk production.
41
75
 
42
76
  ## Scripts
43
77
 
44
78
  - `npm run verify` — tests, check, format, lint.
45
79
  - `npm run test:integration:local` — integration tests against local Supabase.
46
80
  - `npm run db:types:local` — regenerate `database.types.ts` from local schema.
47
- - `npm run db:reset` — wipe local Supabase, re-diff migrations, regenerate types.
81
+ - `npm run db:reset` — wipe local Supabase, re-diff migrations, regenerate types. **Does not** touch Clerk.
package/dist/bb.js CHANGED
@@ -75,7 +75,7 @@ function bbFactory(environment, references = []) {
75
75
  return result;
76
76
  }
77
77
  if (errors.length > 0) {
78
- throw new BuilderException(errors);
78
+ throw new BuilderException({ category: 'validation', errors });
79
79
  }
80
80
  return [entity, []];
81
81
  }
@@ -2,15 +2,25 @@ import * as v from 'valibot';
2
2
  import { BuilderException } from '../errors/index.js';
3
3
  import { BuilderBuilderClientOptionsSchema, BuilderBuilderGetResponseSchema } from './schema.js';
4
4
  export function client(options) {
5
- const { url, apiKey } = v.parse(BuilderBuilderClientOptionsSchema, options);
5
+ const { url, apiKey, headers } = v.parse(BuilderBuilderClientOptionsSchema, options);
6
6
  return {
7
7
  builder: {
8
8
  get: async (id) => {
9
- const response = await fetch(`${url}/api/builder/${id}`, {
10
- headers: { 'X-Builder-Builder-Key': apiKey }
9
+ const requestUrl = `${url}/api/builder/${id}`;
10
+ const response = await fetch(requestUrl, {
11
+ headers: {
12
+ ...headers,
13
+ 'X-Builder-Builder-Key': apiKey
14
+ }
11
15
  });
12
16
  if (!response.ok) {
13
- throw new BuilderException([]);
17
+ throw new BuilderException({
18
+ category: 'request',
19
+ url: requestUrl,
20
+ status: response.status,
21
+ statusText: response.statusText,
22
+ body: await response.text()
23
+ });
14
24
  }
15
25
  return v.parse(BuilderBuilderGetResponseSchema, await response.json());
16
26
  }
@@ -2,6 +2,7 @@ import * as v from 'valibot';
2
2
  export declare const BuilderBuilderClientOptionsSchema: v.ObjectSchema<{
3
3
  readonly url: v.StringSchema<undefined>;
4
4
  readonly apiKey: v.StringSchema<undefined>;
5
+ readonly headers: v.OptionalSchema<v.RecordSchema<v.StringSchema<undefined>, v.StringSchema<undefined>, undefined>, undefined>;
5
6
  }, undefined>;
6
7
  export type BuilderBuilderClientOptions = v.InferOutput<typeof BuilderBuilderClientOptionsSchema>;
7
8
  export declare const BuilderBuilderGetResponseSchema: v.ObjectSchema<{
@@ -3,7 +3,8 @@ import { BuilderReferencesSchema, BuilderSerialisedSchema } from '../entities/in
3
3
  import { BuilderVariantsSchema } from '../instance.js';
4
4
  export const BuilderBuilderClientOptionsSchema = v.object({
5
5
  url: v.string(),
6
- apiKey: v.string()
6
+ apiKey: v.string(),
7
+ headers: v.optional(v.record(v.string(), v.string()))
7
8
  });
8
9
  export const BuilderBuilderGetResponseSchema = v.object({
9
10
  name: v.string(),
@@ -1,7 +1,7 @@
1
1
  import * as v from 'valibot';
2
2
  declare class Check {
3
- truthy<Input>(input: Input, message?: `${string}! ❌`): asserts input is Exclude<Input, null | undefined | '' | 0 | false>;
4
- falsy(input: unknown, message?: `${string}! ❌`): void;
3
+ truthy<Input>(input: Input, message: `${string}! ❌`): asserts input is Exclude<Input, null | undefined | '' | 0 | false>;
4
+ falsy(input: unknown, message: `${string}! ❌`): void;
5
5
  is<const Schema extends v.BaseSchema<unknown, unknown, v.BaseIssue<unknown>>>(schema: Schema, input: unknown): input is v.InferOutput<Schema>;
6
6
  assert<const Schema extends v.BaseSchema<unknown, unknown, v.BaseIssue<unknown>>>(schema: Schema, input: unknown, message?: `${string}! ❌`): asserts input is v.InferOutput<Schema>;
7
7
  }
@@ -1,24 +1,29 @@
1
1
  import * as v from 'valibot';
2
- import { BuilderException } from './exception.js';
3
- const DEFAULT_MESSAGE = 'Unexpected value! ❌';
2
+ import { BuilderException, formatSchemaIssues } from './exception.js';
4
3
  class Check {
5
- truthy(input, message = DEFAULT_MESSAGE) {
4
+ truthy(input, message) {
6
5
  if (!input) {
7
- throw new BuilderException([], message);
6
+ throw new BuilderException({ category: 'assertion', summary: message });
8
7
  }
9
8
  }
10
- falsy(input, message = DEFAULT_MESSAGE) {
9
+ falsy(input, message) {
11
10
  if (input) {
12
- throw new BuilderException([], message);
11
+ throw new BuilderException({ category: 'assertion', summary: message });
13
12
  }
14
13
  }
15
14
  is(schema, input) {
16
15
  return v.is(schema, input);
17
16
  }
18
- assert(schema, input, message = DEFAULT_MESSAGE) {
19
- if (!v.is(schema, input)) {
20
- throw new BuilderException([], message);
17
+ assert(schema, input, message) {
18
+ const result = v.safeParse(schema, input);
19
+ if (result.success) {
20
+ return;
21
21
  }
22
+ throw new BuilderException({
23
+ category: 'assertion',
24
+ summary: message ?? formatSchemaIssues(result.issues),
25
+ issues: result.issues
26
+ });
22
27
  }
23
28
  }
24
29
  export const check = new Check();
@@ -1,5 +1,22 @@
1
1
  import type { BuilderErrors } from './errors';
2
- export declare class BuilderException extends globalThis.Error {
2
+ import * as v from 'valibot';
3
+ export type BuilderExceptionPayload = {
4
+ readonly category: 'validation';
3
5
  readonly errors: BuilderErrors;
4
- constructor(errors: BuilderErrors, message?: string);
6
+ } | {
7
+ readonly category: 'assertion';
8
+ readonly summary: string;
9
+ readonly issues?: ReadonlyArray<v.BaseIssue<unknown>>;
10
+ } | {
11
+ readonly category: 'request';
12
+ readonly url: string;
13
+ readonly status: number;
14
+ readonly statusText: string;
15
+ readonly body: string;
16
+ };
17
+ export declare class BuilderException extends globalThis.Error {
18
+ readonly payload: BuilderExceptionPayload;
19
+ constructor(payload: BuilderExceptionPayload);
20
+ get errors(): BuilderErrors;
5
21
  }
22
+ export declare function formatSchemaIssues(issues: ReadonlyArray<v.BaseIssue<unknown>>): `${string}! ❌`;
@@ -1,7 +1,77 @@
1
+ import * as v from 'valibot';
1
2
  export class BuilderException extends globalThis.Error {
2
- errors;
3
- constructor(errors, message = '') {
4
- super(`BuilderBuilder exception 🏗️${message ? `: ${message}` : ''}`);
5
- this.errors = errors;
3
+ payload;
4
+ constructor(payload) {
5
+ super(formatMessage(payload));
6
+ this.payload = payload;
6
7
  }
8
+ get errors() {
9
+ return this.payload.category === 'validation' ? this.payload.errors : [];
10
+ }
11
+ }
12
+ export function formatSchemaIssues(issues) {
13
+ const [first, ...rest] = issues;
14
+ if (!first) {
15
+ return 'Schema validation failed! ❌';
16
+ }
17
+ const path = formatIssuePath(first);
18
+ const location = path ? ` at ${path}` : '';
19
+ const more = rest.length > 0 ? ` (+${rest.length} more)` : '';
20
+ return `${first.message}${location}${more}! ❌`;
21
+ }
22
+ const HEADER = 'BuilderBuilder error 🏗️';
23
+ function formatMessage(payload) {
24
+ if (payload.category === 'validation') {
25
+ return formatValidation(payload.errors);
26
+ }
27
+ if (payload.category === 'assertion') {
28
+ return `${HEADER} — ${payload.summary}`;
29
+ }
30
+ return formatRequest(payload);
31
+ }
32
+ function formatValidation(errors) {
33
+ if (errors.length === 0) {
34
+ return `${HEADER} — validation failed`;
35
+ }
36
+ const noun = errors.length === 1 ? 'issue' : 'issues';
37
+ const lines = errors.map((error) => ` • ${formatBuilderError(error)}`);
38
+ return `${HEADER} — ${errors.length} validation ${noun}:\n${lines.join('\n')}`;
39
+ }
40
+ function formatBuilderError(error) {
41
+ const { kind, location, ...rest } = error;
42
+ const locationLabel = location.length > 0 ? ` at ${location.join('.')}` : '';
43
+ const detailEntries = Object.entries(rest);
44
+ if (detailEntries.length === 0) {
45
+ return `${kind}${locationLabel}`;
46
+ }
47
+ const details = detailEntries.map(([key, value]) => `${key}=${formatValue(value)}`).join(', ');
48
+ return `${kind}${locationLabel}: ${details}`;
49
+ }
50
+ function formatRequest(payload) {
51
+ const { url, status, statusText, body } = payload;
52
+ const statusLine = statusText ? `${status} ${statusText}` : `${status}`;
53
+ return `${HEADER} — Builder API request failed: ${statusLine}\n url: ${url}\n body: ${body}`;
54
+ }
55
+ function formatIssuePath(issue) {
56
+ if (!issue.path) {
57
+ return '';
58
+ }
59
+ return issue.path.map((segment) => segment.key).join('.');
60
+ }
61
+ function formatValue(value) {
62
+ if (typeof value === 'string') {
63
+ return JSON.stringify(value);
64
+ }
65
+ if (value === null || value === undefined) {
66
+ return String(value);
67
+ }
68
+ if (typeof value === 'object') {
69
+ try {
70
+ return JSON.stringify(value);
71
+ }
72
+ catch {
73
+ return String(value);
74
+ }
75
+ }
76
+ return String(value);
7
77
  }
@@ -1,4 +1,5 @@
1
1
  export type { BuilderError, BuilderErrorKind, BuilderErrorLocation, BuilderErrors } from './errors';
2
+ export type { BuilderExceptionPayload } from './exception.js';
2
3
  export { check } from './check.js';
3
4
  export { BuilderValidateErrors } from './errors.js';
4
5
  export { BuilderException } from './exception.js';
@@ -8,6 +8,6 @@ export function assertValidated(value) {
8
8
  if (typeof value !== 'object' || value === null || !VALIDATED_ENTITIES.has(value)) {
9
9
  const errors = new BuilderValidateErrors();
10
10
  errors.unvalidated(value);
11
- throw new BuilderException(errors.errors);
11
+ throw new BuilderException({ category: 'validation', errors: errors.errors });
12
12
  }
13
13
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@builder-builder/builder",
3
- "version": "0.0.22",
3
+ "version": "0.0.24",
4
4
  "type": "module",
5
5
  "engines": {
6
6
  "node": ">=24"
@@ -39,7 +39,7 @@
39
39
  "lint": "prettier --check . && eslint .",
40
40
  "test": "npm run test:unit -- --run",
41
41
  "test:unit": "npm run messages && vitest --project client --project server",
42
- "test:integration:local": "npm run messages && dotenv -e .env.development.local -- vitest run --project integration",
42
+ "test:integration:local": "npm run messages && dotenv -e .env.development.local -- sh -c 'npx tsx scripts/integration-clean.ts && vitest run --project integration'",
43
43
  "verify": "npm run test && npm run check && npm run lint",
44
44
  "db:types": "dotenv -e .env -- sh -c 'supabase gen types typescript --project-id \"$SUPABASE_DEV_PROJECT_ID\" --schema public > ./src/lib/server/db/database.types.ts'",
45
45
  "db:types:local": "supabase gen types typescript --local > ./src/lib/server/db/database.types.ts",