@palbase/backend 11.0.0 → 12.0.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.
package/dist/index.js CHANGED
@@ -843,6 +843,12 @@ function defineFlags(input) {
843
843
  var CONTROLLER_META = /* @__PURE__ */ Symbol.for("palbase.backend.controllerMeta");
844
844
  function Controller(basePath, options = {}) {
845
845
  return function(ctor) {
846
+ const normalized = basePath.replace(/\/+$/, "");
847
+ if (normalized === "/webhooks" || normalized.startsWith("/webhooks/")) {
848
+ throw new Error(
849
+ `@Controller("${basePath}") uses the reserved /webhooks path \u2014 inbound webhooks are served there`
850
+ );
851
+ }
846
852
  const carrier = ctor;
847
853
  const meta = {
848
854
  __palbase: "controller",
@@ -1234,12 +1240,81 @@ function defineWorker(config) {
1234
1240
  };
1235
1241
  }
1236
1242
 
1243
+ // src/decorators/webhook.ts
1244
+ var WEBHOOK_META = /* @__PURE__ */ Symbol.for("palbase.backend.webhookMeta");
1245
+ var WEBHOOK_EVENTS = /* @__PURE__ */ Symbol.for("palbase.backend.webhookEvents");
1246
+ function carrierOf3(ctor) {
1247
+ return ctor;
1248
+ }
1249
+ function Webhook(options) {
1250
+ return function(ctor) {
1251
+ const carrier = carrierOf3(ctor);
1252
+ Object.defineProperty(carrier, WEBHOOK_META, {
1253
+ value: options,
1254
+ enumerable: false,
1255
+ configurable: true,
1256
+ writable: false
1257
+ });
1258
+ Object.defineProperty(carrier, "__palbase", {
1259
+ value: "webhook",
1260
+ enumerable: false,
1261
+ configurable: true,
1262
+ writable: false
1263
+ });
1264
+ return ctor;
1265
+ };
1266
+ }
1267
+ function On(event) {
1268
+ return function(target, fnName) {
1269
+ const carrier = carrierOf3(target.constructor);
1270
+ const existing = carrier[WEBHOOK_EVENTS];
1271
+ const entries = existing ? [...existing] : [];
1272
+ entries.push({ event, fnName: String(fnName) });
1273
+ Object.defineProperty(carrier, WEBHOOK_EVENTS, {
1274
+ value: entries,
1275
+ enumerable: false,
1276
+ configurable: true,
1277
+ writable: false
1278
+ });
1279
+ };
1280
+ }
1281
+ function getWebhookConfig(ctor) {
1282
+ const carrier = carrierOf3(ctor);
1283
+ const meta = carrier[WEBHOOK_META];
1284
+ const entries = carrier[WEBHOOK_EVENTS] ?? [];
1285
+ if (!meta) {
1286
+ throw new Error(
1287
+ `@On used on a class that is not decorated with @Webhook (${ctor.name ?? "anonymous"})`
1288
+ );
1289
+ }
1290
+ if (!meta.provider && !meta.signature) {
1291
+ throw new Error(
1292
+ "@Webhook requires either a `provider` preset or an explicit `signature` \u2014 an endpoint with no verification would accept forged deliveries"
1293
+ );
1294
+ }
1295
+ if (!meta.secret?.env) {
1296
+ throw new Error('@Webhook requires `secret: { env: "VAR_NAME" }`');
1297
+ }
1298
+ if (entries.length === 0) {
1299
+ throw new Error("@Webhook requires at least one @On handler");
1300
+ }
1301
+ const instance = new ctor();
1302
+ const events = /* @__PURE__ */ Object.create(null);
1303
+ for (const entry of entries) {
1304
+ if (Object.prototype.hasOwnProperty.call(events, entry.event)) {
1305
+ throw new Error(`@On("${entry.event}") declared twice on the same webhook`);
1306
+ }
1307
+ events[entry.event] = (event, metaArg) => instance[entry.fnName].call(instance, event, metaArg);
1308
+ }
1309
+ return {
1310
+ ...meta.provider ? { provider: meta.provider } : {},
1311
+ ...meta.signature ? { signature: meta.signature } : {},
1312
+ secret: meta.secret,
1313
+ events
1314
+ };
1315
+ }
1316
+
1237
1317
  // src/job.ts
1238
- var VALID_JOB_NAME = /^[a-zA-Z0-9_-]+$/;
1239
- var MAX_TIMEOUT_SECONDS = 300;
1240
- var JOB_DEFAULTS = {
1241
- timeout: 30
1242
- };
1243
1318
  function validateCronExpression(expression) {
1244
1319
  const trimmed = expression.trim();
1245
1320
  if (trimmed === "") {
@@ -1305,115 +1380,56 @@ function validateCronField(field, name, min, max) {
1305
1380
  }
1306
1381
  return null;
1307
1382
  }
1308
- function defineJob(config) {
1309
- if (!config.name || config.name.trim() === "") {
1310
- throw new Error("Job name is required");
1311
- }
1312
- if (!VALID_JOB_NAME.test(config.name)) {
1313
- throw new Error(
1314
- `Invalid job name "${config.name}": must match [a-zA-Z0-9_-]+`
1315
- );
1316
- }
1317
- if (!config.schedule || config.schedule.trim() === "") {
1318
- throw new Error("Job schedule is required");
1319
- }
1320
- const cronError = validateCronExpression(config.schedule);
1321
- if (cronError !== null) {
1322
- throw new Error(cronError);
1323
- }
1324
- if (!config.handler) {
1325
- throw new Error("Job handler is required");
1326
- }
1327
- if (config.timeout !== void 0 && config.timeout <= 0) {
1328
- throw new Error("Job timeout must be a positive number");
1329
- }
1330
- if (config.timeout !== void 0 && !Number.isInteger(config.timeout)) {
1331
- throw new Error("Job timeout must be an integer");
1332
- }
1333
- if (config.timeout !== void 0 && config.timeout > MAX_TIMEOUT_SECONDS) {
1334
- throw new Error(
1335
- `Job timeout ${config.timeout}s exceeds maximum ${MAX_TIMEOUT_SECONDS}s`
1336
- );
1337
- }
1338
- return {
1339
- name: config.name,
1340
- schedule: config.schedule.trim(),
1341
- timeout: config.timeout ?? JOB_DEFAULTS.timeout,
1342
- handler: config.handler
1383
+
1384
+ // src/decorators/job.ts
1385
+ var DEFAULT_TIMEOUT_SECONDS = 30;
1386
+ var MAX_TIMEOUT_SECONDS = 300;
1387
+ var JOB_META = /* @__PURE__ */ Symbol.for("palbase.backend.jobMeta");
1388
+ function Job(options) {
1389
+ return function(ctor) {
1390
+ const carrier = ctor;
1391
+ Object.defineProperty(carrier, JOB_META, {
1392
+ value: options,
1393
+ enumerable: false,
1394
+ configurable: true,
1395
+ writable: false
1396
+ });
1397
+ Object.defineProperty(carrier, "__palbase", {
1398
+ value: "job",
1399
+ enumerable: false,
1400
+ configurable: true,
1401
+ writable: false
1402
+ });
1403
+ return ctor;
1343
1404
  };
1344
1405
  }
1345
-
1346
- // src/webhook.ts
1347
- var VALID_WEBHOOK_PATH = /^\/[a-zA-Z0-9/_-]+$/;
1348
- function defineWebhook(config) {
1349
- if ("provider" in config) {
1350
- return validateProviderWebhook(config);
1351
- }
1352
- return validateCustomWebhook(config);
1353
- }
1354
- function validateProviderWebhook(config) {
1355
- if (!config.provider) {
1356
- throw new Error("Webhook provider is required");
1357
- }
1358
- const validProviders = [
1359
- "stripe",
1360
- "github",
1361
- "twilio",
1362
- "sendgrid",
1363
- "slack",
1364
- "discord",
1365
- "livekit"
1366
- ];
1367
- if (!validProviders.includes(config.provider)) {
1406
+ function getJobConfig(ctor) {
1407
+ const meta = ctor[JOB_META];
1408
+ if (!meta) {
1368
1409
  throw new Error(
1369
- `Invalid webhook provider "${config.provider}": must be one of ${validProviders.join(", ")}`
1410
+ `getJobConfig on a class with no @Job decorator (${ctor.name ?? "anonymous"})`
1370
1411
  );
1371
1412
  }
1372
- if (!config.secret) {
1373
- throw new Error('Webhook secret is required (use { env: "SECRET_NAME" })');
1413
+ if (!meta.schedule || meta.schedule.trim() === "") {
1414
+ throw new Error("@Job requires a `schedule` cron expression");
1374
1415
  }
1375
- if (typeof config.secret.env !== "string" || config.secret.env.trim() === "") {
1376
- throw new Error("Webhook secret env name must be a non-empty string");
1416
+ const cronError = validateCronExpression(meta.schedule);
1417
+ if (cronError) {
1418
+ throw new Error(`@Job has an invalid cron schedule: ${cronError}`);
1377
1419
  }
1378
- if (!config.events || Object.keys(config.events).length === 0) {
1379
- throw new Error("At least one event handler is required");
1420
+ const timeout = meta.timeout ?? DEFAULT_TIMEOUT_SECONDS;
1421
+ if (!Number.isInteger(timeout) || timeout <= 0) {
1422
+ throw new Error("@Job `timeout` must be a positive whole number of seconds");
1380
1423
  }
1381
- for (const [eventName, handler] of Object.entries(config.events)) {
1382
- if (typeof handler !== "function") {
1383
- throw new Error(`Event handler for "${eventName}" must be a function`);
1384
- }
1424
+ if (timeout > MAX_TIMEOUT_SECONDS) {
1425
+ throw new Error(`@Job \`timeout\` exceeds the ${MAX_TIMEOUT_SECONDS}s sandbox ceiling`);
1385
1426
  }
1386
- return {
1387
- type: "provider",
1388
- provider: config.provider,
1389
- secret: config.secret,
1390
- events: config.events
1391
- };
1392
- }
1393
- function validateCustomWebhook(config) {
1394
- if (!config.path || config.path.trim() === "") {
1395
- throw new Error("Webhook path is required");
1396
- }
1397
- if (!VALID_WEBHOOK_PATH.test(config.path)) {
1398
- throw new Error(
1399
- `Invalid webhook path "${config.path}": must start with / and contain only alphanumeric, hyphen, underscore, slash`
1400
- );
1401
- }
1402
- if (!config.handler) {
1403
- throw new Error("Webhook handler is required");
1427
+ const instance = new ctor();
1428
+ if (typeof instance.run !== "function") {
1429
+ throw new Error("@Job class must declare an async run() method");
1404
1430
  }
1405
- if (typeof config.handler !== "function") {
1406
- throw new Error("Webhook handler must be a function");
1407
- }
1408
- if (config.verify !== void 0 && typeof config.verify !== "function") {
1409
- throw new Error("Webhook verify must be a function");
1410
- }
1411
- return {
1412
- type: "custom",
1413
- path: config.path,
1414
- verify: config.verify,
1415
- handler: config.handler
1416
- };
1431
+ const run = instance.run.bind(instance);
1432
+ return { schedule: meta.schedule, timeout, handler: run };
1417
1433
  }
1418
1434
 
1419
1435
  // src/resource.ts
@@ -1517,10 +1533,12 @@ export {
1517
1533
  Get,
1518
1534
  Headers,
1519
1535
  HttpError,
1536
+ Job,
1520
1537
  Log,
1521
1538
  NOTIFICATIONS_CONFIG_KIND,
1522
1539
  NotFound,
1523
1540
  Notifications,
1541
+ On,
1524
1542
  OptionalUser,
1525
1543
  PALBASE_EXTENSIONS,
1526
1544
  PROVIDER_CATALOG,
@@ -1552,6 +1570,7 @@ export {
1552
1570
  Upload,
1553
1571
  UploadedObject,
1554
1572
  User,
1573
+ Webhook,
1555
1574
  __getRuntime,
1556
1575
  __registerResource,
1557
1576
  __requestALS,
@@ -1568,20 +1587,20 @@ export {
1568
1587
  defineEgress,
1569
1588
  defineError,
1570
1589
  defineFlags,
1571
- defineJob,
1572
1590
  defineMiddleware,
1573
1591
  defineNotifications,
1574
1592
  defineSchema,
1575
1593
  defineStorage,
1576
1594
  defineTestUsers,
1577
- defineWebhook,
1578
1595
  defineWorker,
1579
1596
  documents,
1580
1597
  entitlementFor,
1581
1598
  enumType,
1582
1599
  flag,
1583
1600
  getErrorRegistry,
1601
+ getJobConfig,
1584
1602
  getRoutes,
1603
+ getWebhookConfig,
1585
1604
  inc,
1586
1605
  integer,
1587
1606
  isPalbaseExtension,