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>
codestringRequired

JavaScript that defines transform(request), not empty, and at most 64 KB. See Transformations for what it gets and what it returns.

options.eventIdstring

The ID of an event to run it on. When you leave it out, it runs on a sample: a POST with the header content-type: application/json and the body {"hello":"world"}.

options.signalAbortSignal

Cancels the call when it aborts. The promise then rejects with aborted.

options.timeoutMsnumber

The longest each request may take, in milliseconds. The client’s timeoutMs when 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 | null

What would be sent, before the destination’s authentication and signing headers are added: headers, a list of { name, value }, body, and bodyEncoding, text or base64. null when the transformation failed.

errorstring | null

Why the transformation failed, such as a syntax error, a thrown error, a wrong shape, or running out of time or memory. null when it worked.

logsstring[]

The lines it wrote with console.log.

durationMsinteger

How long it ran, in milliseconds. A delivery stops it after 100 ms.

CodeWhen
validation_failedThe code is empty or longer than 64 KB, or eventId is not 26 lowercase letters and digits. Nothing is sent
not_foundThe project has no event with that eventId
unavailableWe could not run the transformation this time. Try again
validate-transformation.ts
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