space.hooks.transformations
Try a transformation on a webhook before you save it on a connection.
A transformation is JavaScript on a connection that changes each webhook before it is sent. Try it here first, on a webhook you received or on a sample, and see what it would send.
Every call below can also fail with the codes shared by all of space, such as missing_project, forbidden, timeout, or network_error. See SpaceError.
space.hooks.transformations.validate()
Runs a transformation once, the way a delivery would, and returns what it would send. Nothing is saved and nothing is sent to a destination.
space.hooks.transformations.validate(
code: string,
options?: {
eventId?: string
signal?: AbortSignal
timeoutMs?: number
}
): Promise<TransformationResult>codestringJavaScript that defines
transform(request), not empty, and at most 64 KB. See Transformations for what it gets and what it returns.options.eventIdstringThe ID of an event to run it on. When you leave it out, it runs on a sample: a
POSTwith the headercontent-type: application/jsonand the body{"hello":"world"}.options.signalAbortSignalCancels the call when it aborts. The promise then rejects with
aborted.options.timeoutMsnumberThe longest each request may take, in milliseconds. The client’s
timeoutMswhen you leave it out, 30,000 by default.
Returns a promise of a TransformationResult. A transformation that fails does not reject the promise: the reason is in error.
outputSentRequest | nullWhat would be sent, before the destination’s authentication and signing headers are added:
headers, a list of{ name, value },body, andbodyEncoding,textorbase64.nullwhen the transformation failed.errorstring | nullWhy the transformation failed, such as a syntax error, a thrown error, a wrong shape, or running out of time or memory.
nullwhen it worked.logsstring[]The lines it wrote with
console.log.durationMsintegerHow long it ran, in milliseconds. A delivery stops it after 100 ms.
| Code | When |
|---|---|
validation_failed | The code is empty or longer than 64 KB, or eventId is not 26 lowercase letters and digits. Nothing is sent |
not_found | The project has no event with that eventId |
unavailable | We could not run the transformation this time. Try again |
import { space } from "digitospace"
const code = `function transform(request) {
const payment = request.body
console.log("payment", payment.reference)
return {
headers: { "x-order-id": payment.reference },
body: { orderId: payment.reference, amountTzs: payment.amount, phone: payment.msisdn },
}
}`
const result = await space.hooks.transformations.validate(code, {
eventId: "01m3my2b42vahrbx8zt4fw84wc",
})
if (result.error === null) {
console.log(result.output?.body, result.logs, `${result.durationMs} ms`)
await space.hooks.connections.update("01m3m5hezf7epksb7f8qbww4wb", {
transformation: code,
})
} else {
console.error(result.error)
}Last updated