@flowcore/sdk-transformer-core

中文文档
2.5.1发布于 2025/06/03MITTypeScriptCommonJS近 30 天下载 2.8万

A core library that facilitates writing transformers and business logic services that utilise the flowcore paradigm

原始使用文档
文档目录7 节
  1. Installation
  2. Usage
  3. Development
  4. Transformer builder
  5. Webhook builder
  6. Returns and Exception handling
  7. Metadata factory

sdk-transformer-core

A core library that facilitates writing transformers and business logic services that utilise the flowcore paradigm

Installation

install with npm:

npm install @flowcore/refine-data-provider

or yarn:

yarn add @flowcore/refine-data-provider

Usage

Development

yarn install

or with npm:

npm install

Transformer builder

Usage exampe

new TransformerBuilder("flow-type.0")
    .withSecret("SUPER_SECRET_STRING")
    // Execute after succsessfully handling and responding to an event
    .withSuccessHandler(async (event: FlowcoreEvent) => {
        // Handle success
    })
    .onEventType(
        "event-type.0",
        EventTypePayloadSchema,
        async (event: FlowcoreEvent, payload: Static<typeof EventTypePayloadSchema>) => {
            // Handle event
        }
    )
    // ... (add more onEventType)
    // Get a generic handler method
    .getHandler()
    // Returns
    // (event: unknown, secret?: string) => Promise<{status: "ok" | "error", statusCode: number, message?: string, errors?: Record<string, string>}>

// You can extend the builder to build for specific routers

// Elysia example
export class ElysiaTransformerBuilder extends TransformerBuilder {
  public getRouter() {
    return new Elysia().post(
      `/${this.flowType}`,
      async ({ body, headers, set }) => {
        const response = await this.handleEvent(body, headers["x-secret"])
        set.status = response.statusCode
        return response
      },
      {
        body: FlowcoreEventSchema,
        headers: TransformerHeadersSchema,
        response: TransformerResponseSchema,
      },
    )
  }
}

Webhook builder

Usage example

const webhookFactory = new WebhookBuilder({
  // Base URL of webhook ingestion (default: "https://webhook.api.flowcore.io")
  baseUrl: "https://webhook.api.flowcore.io",
  // Required: flowcore tenant name
  tenant: "tenant",
  // Required: data core name or uuid
  dataCore: "data-core",
  // Required: flowcore api key
  apiKey: "00000000-0000-0000-0000-000000000000",
})
    // Will attempt X times to send webhook if it fails with a retryable status code
    // Retryable status codes are: 408, 429, 500, 502, 503, 504
    .withRetry({
        // Maximum number of attempts (default: 1)
        maxAttempts: 1,
        // Delay between attempts (default: 250)
        attemptDelayMs: 250,
        // Can also be a backoff method
        // attemptDelayMs: (attempt: number) => number
    })
    // Will do a predicate check after event is sent
    .withPredicate({
        // Predicate method: (eventId: string) => Promise<boolean> | boolean
        predicate: (eventId: string) => checkEventId(eventId)
        // Predicate options
        options: {
            // Maximum number of predicate attempts
            maxAttempts: 8,
            // Delay betweeen predicate attempts
            attemptDelayMs: 250,
            // Can also be a backoff method
            // attemptDelayMs: (attempt: number) => number
        }
    })
    // Build webhook method
    .buildWebhook<EventPayload extends Record<string, unknown>, MetaData extends Record<string, unknown>>("flow-type.0", "event-type.0")
    // Returns
    //{
    //    send: (payload: EventPayload, metadata?: MetaData, options?: WebhookSendOptions) => Promise<string>,
    //    sendBatch: (payload: EventPayload[], metadata?: MetaData, options?: WebhookSendBatchOptions) => Promise<string>,
    //}
    .buildFileWebhook<MetaData extends Record<string, unknown>>("flow-type.0", "event-type.0")
    // Returns
    //{
    //    send: (payload: FilePayload, metadata?: MetaData, options?: WebhookSendOptions) => Promise<string>,
    //}
    // Can also be used as a factory
    .factory()
    // Returns a method that returns a new instance of WebhookBuilder with current options
    // () => WebhookBuilder

Returns and Exception handling

webhook.send returns a single eventId (string) webhook.sendBatch and fileWebhook.send return an array of eventIds (string[])

When send methods fail you can get these exceptions WebhookSendError {message: string, response?: unknown, exception?: Error}

  • response is the return from the webhook endpoint (send failed on webhook endpoint)
  • exception is the locally thrown exception (send failed locally with an exception)

WebhookPredicateError {message: string, eventIds: string[], exception?: Error}`

  • eventIds the event ids returned from the webhook endpoint
  • exception is the locally thrown exception (predicate failed with exception)

Error

  • Whoopsy... uncaught exception

Metadata factory

const webhookWithMetadata = metadataWebhook({
  userId: "123",
})

// Will inject metadata into the sent event
const eventId: string = await webhookWithMetadata(webhook.send, {foo: "bar"})
// Will inject metadata into the sent events
const eventIds: string[] = await webhookWithMetadata(webhook.sendBatch, {foo: "bar"})
// Will inject metadata into the sent event
const eventIds: string[] = await webhookWithMetadata(fileWebhook.send, {foo: "bar"})

版本与发布记录

@flowcore/sdk-transformer-core 共发布 43 个版本,首次发布于 2024/05/29;过去 12 个月发布了 0 个版本。

版本发布日期距今
2.5.1latest2025/06/031.5 年前
2.5.02025/06/031.5 年前
2.4.02025/05/271.5 年前
2.3.72025/05/271.5 年前
2.3.62025/02/211.7 年前
2.3.52025/02/211.7 年前

关于 @flowcore/sdk-transformer-core 的常见问题

@flowcore/sdk-transformer-core 是什么?

A core library that facilitates writing transformers and business logic services that utilise the flowcore paradigm

如何安装 @flowcore/sdk-transformer-core?

在项目目录运行 npm install @flowcore/sdk-transformer-core,或使用 pnpm add / yarn add / bun add。

@flowcore/sdk-transformer-core 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@flowcore/sdk-transformer-core 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

@flowcore/sdk-transformer-core 可以商用吗?

@flowcore/sdk-transformer-core 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@flowcore/sdk-transformer-core 的下载量有多少?

根据 npm 官方下载统计,@flowcore/sdk-transformer-core 在 2026/10/15 – 2026/11/13 这 30 天内被下载 28,247 次,日均约 942 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@flowcore/sdk-transformer-core 还在维护吗?

最新版本 2.5.1 发布于 2025/06/03(1.5 年前);至今共发布 43 个版本,其中过去 12 个月发布了 0 个;首次发布于 2024/05/29。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。