feat: add metadata headers to response

This commit is contained in:
shiftinv 2022-09-04 15:34:19 +02:00
parent 61a49ceb32
commit 4d2eeb52b3
2 changed files with 21 additions and 0 deletions

View File

@ -17,4 +17,7 @@ export default {
maxWebhookRetries: parseInt(get("MAX_RETRIES", "3")),
maxWebhookRetryMs: parseInt(get("MAX_RETRY_MS", "30000")),
mainRedirect: get("MAIN_REDIRECT", null),
// set by deno deploy
deployId: get("DENO_DEPLOYMENT_ID", "<unknown>"),
};

View File

@ -47,5 +47,23 @@ export async function sendWebhook(
log.warning(`retrying after ${resetms}ms (retry ${retries})`);
await sleep(resetms);
} while (true);
// clone response to make headers mutable
res = new Response(res.body, res);
// set metadata headers
const meta: Record<string, string> = {
"deploy": config.deployId,
};
if (retries) meta["retries"] = retries.toString();
setMetadata(res, meta);
return res;
}
function setMetadata(res: Response, meta: Record<string, string>): void {
for (const [key, value] of Object.entries(meta)) {
res.headers.set(`x-webhook-filter-${key}`, value);
}
}