@brickkit/core 0.14.0 → 0.16.0-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.
@@ -1,4 +1,4 @@
1
- import { Q as BrickSocketConnectOptions, St as StandardErrorCode, X as BrickSocketClient, Y as BrickClientSocket, c as ActionExecutionError, s as ActionErrorDefinition, t as Action, wr as HttpMethod } from "./types.js";
1
+ import { $ as BrickSocketClient, It as StandardErrorCode, Q as BrickClientSocket, c as ActionExecutionError, ri as HttpMethod, s as ActionErrorDefinition, t as Action, tt as BrickSocketConnectOptions } from "./types.js";
2
2
  import { Static, TSchema } from "@sinclair/typebox";
3
3
  /**
4
4
  * The URL segment for a declared name: `orderBilling` becomes `order-billing`,
@@ -21,6 +21,10 @@ declare const ATTR: {
21
21
  readonly urlPath: "url.path";
22
22
  readonly service: "brick.service";
23
23
  readonly action: "brick.action";
24
+ /** A webhook delivery: the webhook's name, its event id and what became of it. Never the body or the signature. */
25
+ readonly webhook: "brick.webhook";
26
+ readonly webhookEventId: "brick.webhook.event_id";
27
+ readonly webhookOutcome: "brick.webhook.outcome";
24
28
  readonly traceId: "brick.trace_id";
25
29
  readonly errorCode: "brick.error.code";
26
30
  readonly errorMessage: "brick.error.message";
@@ -45,6 +49,8 @@ declare const GLOBAL_SYMBOL: {
45
49
  readonly logsRecord: "brickkit.logs.record";
46
50
  /** The set of running event hubs, so the CLI can stop them all. */
47
51
  readonly eventsStarted: "brickkit.events.started";
52
+ /** The key the dashboard signs `/app/*` identities with. */
53
+ readonly dashboardKey: "brickkit.dashboard.key";
48
54
  };
49
55
  /**
50
56
  * Eden/tRPC-style typed HTTP client inferred from Brick-TS application contracts.
@@ -268,5 +274,4 @@ declare function createBrickClient<TApp extends Record<string, any>>(options: Br
268
274
  contract: TApp;
269
275
  }): BrickClient<TApp>;
270
276
  declare function createBrickClient<TApp extends Record<string, any>>(options: BrickClientOptions): BrickClient<TApp>;
271
- export { GLOBAL_SYMBOL as _, BrickClientOptions as a, BrickTransportError as c, InferActionInput as d, InferActionOutput as f, ATTR as g, isBrickTransportError as h, BrickClientAction as i, InferActionErrorCodes as l, isBrickServerError as m, BrickCallOptions as n, BrickServerError as o, createBrickClient as p, BrickClient as r, BrickServices as s, BrickActionCaller as t, InferActionFailure as u, SPAN as v, kebabCase as y };
272
- //# sourceMappingURL=client.d.ts.map
277
+ export { GLOBAL_SYMBOL as _, BrickClientOptions as a, BrickTransportError as c, InferActionInput as d, InferActionOutput as f, ATTR as g, isBrickTransportError as h, BrickClientAction as i, InferActionErrorCodes as l, isBrickServerError as m, BrickCallOptions as n, BrickServerError as o, createBrickClient as p, BrickClient as r, BrickServices as s, BrickActionCaller as t, InferActionFailure as u, SPAN as v, kebabCase as y };
@@ -1,6 +1,13 @@
1
1
  import { Kind, TransformKind } from "@sinclair/typebox";
2
2
  import { Check } from "@sinclair/typebox/value";
3
3
  // src/types.ts
4
+ function withoutRejectedValue({ path, message, type }) {
5
+ return {
6
+ path,
7
+ message,
8
+ type
9
+ };
10
+ }
4
11
  var ValidationError = class extends Error {
5
12
  code = "VALIDATION_FAILED";
6
13
  status;
@@ -9,7 +16,7 @@ var ValidationError = class extends Error {
9
16
  super(message);
10
17
  this.name = "ValidationError";
11
18
  this.status = status;
12
- this.errors = errors;
19
+ this.errors = errors.map(withoutRejectedValue);
13
20
  }
14
21
  toJSON() {
15
22
  return {
@@ -70,6 +77,10 @@ const ATTR = {
70
77
  urlPath: "url.path",
71
78
  service: "brick.service",
72
79
  action: "brick.action",
80
+ /** A webhook delivery: the webhook's name, its event id and what became of it. Never the body or the signature. */
81
+ webhook: "brick.webhook",
82
+ webhookEventId: "brick.webhook.event_id",
83
+ webhookOutcome: "brick.webhook.outcome",
73
84
  traceId: "brick.trace_id",
74
85
  errorCode: "brick.error.code",
75
86
  errorMessage: "brick.error.message",
@@ -93,7 +104,9 @@ const GLOBAL_SYMBOL = {
93
104
  /** Core's function that records a log line on the running span. */
94
105
  logsRecord: "brickkit.logs.record",
95
106
  /** The set of running event hubs, so the CLI can stop them all. */
96
- eventsStarted: "brickkit.events.started"
107
+ eventsStarted: "brickkit.events.started",
108
+ /** The key the dashboard signs `/app/*` identities with. */
109
+ dashboardKey: "brickkit.dashboard.key"
97
110
  };
98
111
  // src/protocol.ts
99
112
  /** The codes every action, listener and socket handler can raise, with their status and default message. */
@@ -118,6 +131,10 @@ const STANDARD_ERRORS = {
118
131
  status: 409,
119
132
  message: "Conflict"
120
133
  },
134
+ RATE_LIMITED: {
135
+ status: 429,
136
+ message: "Too Many Requests"
137
+ },
121
138
  INTERNAL_SERVER_ERROR: {
122
139
  status: 500,
123
140
  message: "Internal Server Error"
@@ -127,7 +144,7 @@ const STANDARD_ERRORS = {
127
144
  function decodeError(body, status) {
128
145
  if (body === null || typeof body !== "object") return void 0;
129
146
  const record = body;
130
- if (record.name === "ValidationError") return new ValidationError(typeof record.message === "string" ? record.message : "Validation failed", Array.isArray(record.errors) ? record.errors : [], typeof record.status === "number" ? record.status : status);
147
+ if (record.name === "ValidationError") return new ValidationError(typeof record.message === "string" ? record.message : "Validation failed", Array.isArray(record.errors) ? record.errors.filter((item) => item !== null && typeof item === "object") : [], typeof record.status === "number" ? record.status : status);
131
148
  if (typeof record.code !== "string") return void 0;
132
149
  return new ActionExecutionError(record.code, typeof record.message === "string" ? record.message : record.code, typeof record.status === "number" ? record.status : status, record.details ?? void 0);
133
150
  }
@@ -308,6 +325,9 @@ function getPluginActionRoute(action, prefix = DEFAULT_PREFIX) {
308
325
  function getActionRoute(action, prefix = DEFAULT_PREFIX) {
309
326
  return actionRoute(action, action.serviceName ?? "", prefix);
310
327
  }
328
+ function actionMethod(action) {
329
+ return actionMeta(action).route?.method ?? action.config.method ?? "POST";
330
+ }
311
331
  /** `getActionRoute` for an action that may not be registered yet, under `service`. */
312
332
  function actionRoute(action, service, prefix) {
313
333
  const plugin = getPluginActionRoute(action, prefix);
@@ -316,7 +336,7 @@ function actionRoute(action, service, prefix) {
316
336
  method: plugin.method,
317
337
  path: plugin.path
318
338
  };
319
- const method = action.config.method ?? "POST";
339
+ const method = actionMethod(action);
320
340
  const { resourceAction } = actionMeta(action);
321
341
  if (resourceAction) {
322
342
  const base = ownerPath(prefix, service, resourceAction.resource);
@@ -404,20 +424,20 @@ const kinds = /* @__PURE__ */ new Set([
404
424
  "Union"
405
425
  ]);
406
426
  /** Keep validators browser-safe and reject schemas whose behavior cannot cross JSON. */
407
- function serializeSocketSchema(schema) {
408
- const visit = (value) => {
409
- if (Array.isArray(value)) return value.map(visit);
427
+ function serializeSocketSchema(schema, path = "schema") {
428
+ const visit = (value, at) => {
429
+ if (Array.isArray(value)) return value.map((child, i) => visit(child, `${at}[${i}]`));
410
430
  if (!value || typeof value !== "object") return value;
411
431
  if (TransformKind in value) throw new Error("Socket schemas cannot contain transforms");
412
- const entries = Object.entries(value).map(([key, child]) => [key, visit(child)]);
432
+ const entries = Object.entries(value).map(([key, child]) => [key, visit(child, `${at}.${key}`)]);
413
433
  if (Kind in value) {
414
- if (typeof value[Kind] !== "string" || !kinds.has(value[Kind])) throw new Error("Socket schemas must describe JSON values");
434
+ if (typeof value[Kind] !== "string" || !kinds.has(value[Kind])) throw new Error(`Socket schemas must describe JSON values (got kind "${String(value[Kind])}" at ${at}); use t.Json() for any JSON value`);
415
435
  if ("format" in value || "__brick_kind" in value) throw new Error("Socket schemas cannot contain formats or reserved __brick_kind annotations");
416
436
  entries.push(["__brick_kind", value[Kind]]);
417
437
  }
418
438
  return Object.fromEntries(entries);
419
439
  };
420
- return visit(schema);
440
+ return visit(schema, path);
421
441
  }
422
442
  function restoreSocketSchema(value) {
423
443
  const visit = (node) => {
@@ -864,21 +884,28 @@ function normalizePrefix(prefix) {
864
884
  const withLeading = prefix.startsWith("/") ? prefix : `/${prefix}`;
865
885
  return withLeading.length > 1 ? withLeading.replace(/\/+$/, "") : withLeading;
866
886
  }
887
+ function appendFields(target, input) {
888
+ for (const [key, value] of Object.entries(input)) for (const item of Array.isArray(value) ? value : [value]) {
889
+ if (item === void 0 || item === null) continue;
890
+ if (item instanceof Blob) target.append(key, item);
891
+ else target.append(key, typeof item === "object" ? JSON.stringify(item) : String(item));
892
+ }
893
+ }
867
894
  function toQueryString(input) {
868
895
  if (input == null || typeof input !== "object") return "";
869
896
  const params = new URLSearchParams();
870
- for (const [key, value] of Object.entries(input)) {
871
- if (value === void 0 || value === null) continue;
872
- if (Array.isArray(value)) for (const item of value) {
873
- if (item === void 0 || item === null) continue;
874
- params.append(key, typeof item === "object" ? JSON.stringify(item) : String(item));
875
- }
876
- else if (typeof value === "object") params.append(key, JSON.stringify(value));
877
- else params.append(key, String(value));
878
- }
897
+ appendFields(params, input);
879
898
  const qs = params.toString();
880
899
  return qs ? `?${qs}` : "";
881
900
  }
901
+ function formDataIfFiles(body) {
902
+ if (body == null || typeof body !== "object") return void 0;
903
+ const isFile = (value) => value instanceof Blob || Array.isArray(value) && value.some((item) => item instanceof Blob);
904
+ if (!Object.values(body).some(isFile)) return void 0;
905
+ const form = new FormData();
906
+ appendFields(form, body);
907
+ return form;
908
+ }
882
909
  function isActionValue(value) {
883
910
  return typeof value === "function" && !!value.config && typeof value.config.execute === "function";
884
911
  }
@@ -941,8 +968,13 @@ async function dispatch(resolved, path, target, input, callOptions) {
941
968
  };
942
969
  let payload;
943
970
  if (hasBody) {
944
- headers["content-type"] = "application/json";
945
- payload = JSON.stringify(body ?? null);
971
+ payload = formDataIfFiles(body);
972
+ if (payload) {
973
+ for (const key of Object.keys(headers)) if (key.toLowerCase() === "content-type") delete headers[key];
974
+ } else {
975
+ headers["content-type"] = "application/json";
976
+ payload = JSON.stringify(body ?? null);
977
+ }
946
978
  }
947
979
  const timeoutMs = callOptions?.timeoutMs ?? resolved.timeoutMs;
948
980
  let timeoutSignal;
@@ -1016,6 +1048,6 @@ function createBrickClient(options) {
1016
1048
  timeoutMs: options.timeoutMs
1017
1049
  }, [], options.contract);
1018
1050
  }
1019
- export { byteLength as A, splitSeq as B, SOCKET_BUFFER_BYTES as C, SOCKET_PROTOCOL as D, SOCKET_MAX_BYTES as E, isSocketRoom as F, DuplicateNameError as G, GLOBAL_SYMBOL as H, isSocketSeq as I, ValidationError as K, replayFrame as L, encodeFrame as M, isControlEvent as N, SOCKET_TOKEN_PROTOCOL as O, isSocketEnvelope as P, replayHeadroom as R, actionMeta as S, SOCKET_CONTROL as T, SPAN as U, ATTR as V, ActionExecutionError as W, pathParamNames as _, isBrickTransportError as a, toOpenApiPath as b, DEFAULT_PREFIX as c, getActionRoute as d, getPluginActionRoute as f, ownerPath as g, joinRoute as h, isBrickServerError as i, compareSocketSeq as j, STANDARD_ERRORS as k, actionOwners as l, isReadLike as m, BrickTransportError as n, BrickSocketError as o, inputSource as p, createBrickClient as r, serializeSocketSchema as s, BrickServerError as t, checkSegments as u, pluginRoutePath as v, SOCKET_CLOSE as w, kebabCase as x, routeKey as y, signSeq as z };
1051
+ export { STANDARD_ERRORS as A, signSeq as B, actionMeta as C, SOCKET_MAX_BYTES as D, SOCKET_CONTROL as E, isSocketEnvelope as F, ActionExecutionError as G, ATTR as H, isSocketRoom as I, DuplicateNameError as K, isSocketSeq as L, compareSocketSeq as M, encodeFrame as N, SOCKET_PROTOCOL as O, isControlEvent as P, replayFrame as R, kebabCase as S, SOCKET_CLOSE as T, GLOBAL_SYMBOL as U, splitSeq as V, SPAN as W, ownerPath as _, isBrickTransportError as a, routeKey as b, DEFAULT_PREFIX as c, checkSegments as d, getActionRoute as f, joinRoute as g, isReadLike as h, isBrickServerError as i, byteLength as j, SOCKET_TOKEN_PROTOCOL as k, actionMethod as l, inputSource as m, BrickTransportError as n, BrickSocketError as o, getPluginActionRoute as p, ValidationError as q, createBrickClient as r, serializeSocketSchema as s, BrickServerError as t, actionOwners as u, pathParamNames as v, SOCKET_BUFFER_BYTES as w, toOpenApiPath as x, pluginRoutePath as y, replayHeadroom as z };
1020
1052
 
1021
1053
  //# sourceMappingURL=client.js.map