2.3.2 • Published 8 months ago

@flowcore/sdk-transformer-core v2.3.2

Weekly downloads
-
License
MIT
Repository
github
Last release
8 months ago

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"})
2.3.2

8 months ago

2.3.1

9 months ago

2.3.0

9 months ago

2.2.1

9 months ago

2.2.3

9 months ago

2.2.2

9 months ago

2.2.0

9 months ago

2.1.1

9 months ago

2.1.0

9 months ago

2.0.3

9 months ago

2.0.5

9 months ago

2.0.4

9 months ago

2.0.6

9 months ago

2.0.2

9 months ago

2.0.1

9 months ago

2.0.0

9 months ago

1.10.1

9 months ago

1.10.0

10 months ago

1.9.2

10 months ago

1.9.1

11 months ago

1.9.0

12 months ago

1.8.0

12 months ago

1.7.1

12 months ago

1.7.0

12 months ago

1.6.2

12 months ago

1.6.1

12 months ago

1.6.0

1 year ago

1.5.0

1 year ago

1.4.0

1 year ago

1.3.2

1 year ago

1.3.1

1 year ago

1.3.0

1 year ago

1.2.0

1 year ago

1.1.0

1 year ago

1.0.1

1 year ago