hatchet icon indicating copy to clipboard operation
hatchet copied to clipboard

Feat(TS): experimental middleware

Open grutt opened this issue 8 months ago • 1 comments

Description

It is often desirable to perform transformations to the input or output of a task or all task on a client.

For example:

  1. payload thinning
  2. encryption
  3. enrichment with no-serializable data

This experimental feature exposes a middleware concept that is applied to the inputs and outputs at runtime and on run method bindings.

It is available on the 1.2.0-alpha tag.

Type of change

  • [x] New feature (non-breaking change which adds functionality)

Example usage:

/* eslint-disable max-classes-per-file */
/* eslint-disable max-classes-per-file */
import { JsonObject } from '@hatchet/v1/types';
import { Serializable, Middleware } from '@hatchet/v1/next';
import { HatchetClient } from '../../client/client';

class EncodeSerializer implements Serializable<unknown> {
  deserialize(input: JsonObject) {
    console.log('client-encode-deserialize', input);

    if (input.encoded && typeof input.encoded === 'string') {
      console.warn('WARNING THIS IS NOT REAL ENCRYPTION');
      const decoded = Buffer.from(input.encoded, 'base64').toString('utf-8');
      return JSON.parse(decoded);
    }

    return input;
  }

  serialize(input: unknown) {
    console.warn('WARNING THIS IS NOT REAL ENCRYPTION');
    const encoded = Buffer.from(JSON.stringify(input)).toString('base64');
    console.log('client-encode-serialize', input);
    return {
      encoded,
    };
  }
}

class EncodeMiddleware implements Middleware {
  input = new EncodeSerializer();
  output = new EncodeSerializer();
}

export const hatchet = HatchetClient.init({
  middleware: [new EncodeMiddleware()],
});

grutt avatar Apr 14 '25 16:04 grutt

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
hatchet-docs ✅ Ready (Inspect) Visit Preview 💬 Add feedback Apr 14, 2025 4:36pm
hatchet-v0-docs ✅ Ready (Inspect) Visit Preview 💬 Add feedback Apr 14, 2025 4:36pm

vercel[bot] avatar Apr 14 '25 16:04 vercel[bot]

Closing due to inactivity. We're planning to introduce a middleware concept to SDKs soon, can reopen then.

abelanger5 avatar Sep 05 '25 19:09 abelanger5