@bonniernews/b0rker
Advanced tools
Comparing version 10.2.1 to 10.3.0
@@ -37,2 +37,8 @@ import { logger } from "lu-logger"; | ||
if (err.validation) { | ||
logger.error(`Validation error: ${err.message}`); | ||
await sendToDlx(req, `Validation error: ${err.message}`); | ||
return res.status(200).send({ type: "validation_error", message: err.message }); | ||
} | ||
logger.error(`Unexpected error ${err.message}: ${err.stack}`); | ||
@@ -39,0 +45,0 @@ |
@@ -15,3 +15,3 @@ import joi from "joi"; | ||
const sequenceSchema = joi.object().keys({ | ||
namespace: joi.string().valid("event", "action", "sequence", "sub-sequence").required(), | ||
namespace: joi.string().valid("action", "sequence", "sub-sequence").required(), | ||
name: joi | ||
@@ -26,11 +26,4 @@ .string() | ||
.items(joi.object().length(1)), | ||
executionDelay: joi.alternatives().conditional("namespace", { | ||
is: "sub-sequence", | ||
then: joi | ||
.number() | ||
.min(0) | ||
.max(60 * 60 * 1000), | ||
}), | ||
unrecoverable: joi.array().items(joi.object().length(1)), | ||
useParentCorrelationId: joi.boolean().default(false), | ||
schema: joi.object().schema().optional(), | ||
}); | ||
@@ -37,0 +30,0 @@ |
@@ -5,10 +5,11 @@ import { Router as expressRouter } from "express"; | ||
import buildContext from "./context.js"; | ||
import { sendToDlx } from "./dlx.js"; | ||
import { errorHandler } from "./error-handler.js"; | ||
import jobStorage from "./job-storage/index.js"; | ||
import { router as middlewareRouter } from "./middleware/index.js"; | ||
import { errorHandler } from "./error-handler.js"; | ||
import { publishTask, publishTasksBulk } from "./publish-task.js"; | ||
import resend from "./resend.js"; | ||
import { appendData, buildNextKeyMapper, buildUrl, keyToUrl, sequenceIterator } from "./utils/sequences.js"; | ||
import buildContext from "./context.js"; | ||
import jobStorage from "./job-storage/index.js"; | ||
import { sendToDlx } from "./dlx.js"; | ||
import resend from "./resend.js"; | ||
import validate from "./validator.js"; | ||
@@ -40,5 +41,5 @@ // Ugly hack to make lu-logger accept the same interface as pino. | ||
function buildCloudTaskSequenceRoutes(router, { namespace, name, sequence, unrecoverable }, nextKeyMapper) { | ||
function buildCloudTaskSequenceRoutes(router, { namespace, name, sequence, unrecoverable, schema }, nextKeyMapper) { | ||
for (const { key, func } of sequenceIterator(sequence)) { | ||
router.post(buildUrl(namespace, name, key), messageHandler(func, nextKeyMapper(`${namespace}.${name}.${key}`))); | ||
router.post(buildUrl(namespace, name, key), messageHandler(func, nextKeyMapper(`${namespace}.${name}.${key}`), schema)); | ||
} | ||
@@ -67,4 +68,6 @@ // Allow to start a sequence/sub-sequence by posting to the sequence name | ||
function messageHandler(func, { nextKey, queue } = {}) { | ||
function messageHandler(func, { nextKey, queue } = {}, schema) { | ||
return async (req, res) => { | ||
const body = schema ? validate(req.body, schema) : req.body; | ||
const { | ||
@@ -81,5 +84,5 @@ key, | ||
const result = await func(req.body, context); | ||
const result = await func(body, context); | ||
const nextBody = appendData(req.body, result); | ||
const nextBody = appendData(body, result); | ||
@@ -141,3 +144,3 @@ if (result?.type === "trigger") { | ||
} | ||
} else if ([ "sequence", "event" ].some((s) => result.key.startsWith(s))) { | ||
} else if (result.key.startsWith("sequence")) { | ||
// If we trigger another main sequence, we fire-and-forget and continue with the main sequence immediately | ||
@@ -144,0 +147,0 @@ await triggerOtherSequences(body, result, { |
{ | ||
"name": "@bonniernews/b0rker", | ||
"version": "10.2.1", | ||
"version": "10.3.0", | ||
"engines": { | ||
@@ -5,0 +5,0 @@ "node": ">=18" |
183995
66
4680