hatchet
hatchet copied to clipboard
Feat(TS): experimental middleware
Description
It is often desirable to perform transformations to the input or output of a task or all task on a client.
For example:
- payload thinning
- encryption
- 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()],
});
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 |
Closing due to inactivity. We're planning to introduce a middleware concept to SDKs soon, can reopen then.