๐ง Wrap everything, without breaking types ๐ฅฒ
June 19, 2025 ยท View on GitHub
๐ Huge thanks to the sponsors who help me maintain this repo:
๐ง Wrap everything, without breaking types ๐ฅฒ
OnionJS is a type-safe and ultra-lightweight (2KB) library to design and apply wrappers, based on HotScript high-order types.
In particular, it's awesome for building and using type-safe middlewares (see the dedicated section).
Table of content
- ๐ฌ Installation
- ๐ Layers
- ๐ง
Onion.wrap - โป๏ธ
Onion.produce - ๐ Building Middlewares
- ๐๏ธ Composing Layers
- ๐ช Customizable Layers
๐ฌ Installation
# npm
npm install @onion.js/core
# yarn
yarn add @onion.js/core
๐ Layers
In OnionJS, Layers are functions that transform Subjects from a before to an after state:
For instance, let's define a layer that JSON.stringifies the 'body' property of an object:
import type { Layer } from '@onion.js/core'
import type { Objects } from 'hotscript'
const jsonStringifyBody: Layer<
Record<string, unknown>, // subject type
Objects.Update<'body', string>, // outward HO Type
Objects.Update<'body', unknown> // inward HO Type
> = before => {
const after = {
...before,
body: JSON.stringify(before.body)
}
return after
}
๐ง
Onion.wrap
We can now apply this layer to any object with Onion.wrap:
import { Onion } from '@onion.js/core'
const before = {
headers: null,
body: { foo: 'bar' }
}
const after = Onion.wrap(before).with(jsonStringifyBody)
// ^? { headers: null; body: string } ๐
Notice how the after type is correctly inferred thanks to Hotscript high-order types!
...And why stop here? Let's add more layers:
import type { Identity } from 'hotscript'
// Logs the object
const logObject: Layer<
Record<string, unknown>,
Identity,
Identity
> = before => {
console.log(before)
return before
}
// Layers are gracefully composed ๐
const after = Onion.wrap(before).with(
logObject, // 1st layer
jsonStringifyBody, // 2nd layer etc.
...
)
โป๏ธ Onion.produce<TYPE>
But wait, that's not all! Layers can also work inward ๐คฏ
Given an after type and some layers, OnionJS can infer the expected before type:
For instance, we can reuse jsonStringifyBody to produce the same result as above with Onion.produce:
const after = Onion.produce<{ headers: null; body: string }>()
.with(
jsonStringifyBody, // last layer
logObject, // 2nd to last etc.
...
)
.from({ headers: null, body: { foo: 'bar' } })
// ^? ({ headers: null; body: unknown }) => { headers: null; body: string } ๐
โ๏ธ Note that layers are applied in reverse for improved readability.
๐ Building Middlewares
OnionJS really shines when wrapping functions with middlewares.
In this case, layers receive before functions and return after functions (hence the "high-order function" name):
For instance, let's apply jsonStringifyBody to the output of a function:
import type { Layer } from '@onion.js/core'
import type { Functions, Objects } from 'hotscript'
const jsonStringifyRespBody: Layer<
(...params: unknown[]) => Record<string, unknown>,
Functions.MapReturnType<Objects.Update<'body', string>>,
Functions.MapReturnType<Objects.Update<'body', unknown>>
> = before => {
function after(...params: unknown[]) {
return jsonStringifyBody(before(...params))
}
return after
}
Now we can use this layer to wrap and produce functions ๐ With literally the same code as above:
import { Onion } from '@onion.js/core'
const before = () => ({ body: { foo: 'bar' } })
const after = Onion.wrap(before).with(jsonStringifyRespBody)
// ^? () => { body: string } ๐
const produced = Onion.produce<() => { body: string }>()
.with(jsonStringifyRespBody)
.from(before)
// ^? (before: () => { body: unknown }) ๐
๐๏ธ Composing Layers
You can create new layers from existing ones with composeDown and composeUp:
import { compose, Onion } from '@onion.js/core'
const composedLayer = composeDown(
logObject, // 1st layer
jsonStringifyBody, // 2nd layer etc.
...
)
const after = Onion.wrap(before).with(composedLayer)
// Similar to:
const after = Onion.wrap(before).with(
logObject,
jsonStringifyBody,
...
)
It is advised to use
composeDownwhen wrapping, andcomposeUpwhen producing for better readability.
๐ช Customizable Layers
Layers can accept parameters to allow for customization. But make sure to use generics if needed!
For instance, let's define a jsonStringifyProp layer that JSON.stringifies any property you want:
type JSONStringifyPropLayer<KEY extends string> = Layer<
Record<string, unknown>,
Objects.Update<KEY, string>,
Objects.Update<KEY, unknown>
>
const jsonStringifyProp =
<KEY extends string>(key: KEY): JSONStringifyPropLayer<KEY> =>
before => {
const after = {
...before,
[key]: JSON.stringify(before[key])
}
return after
}
const after = Onion.wrap({ yolo: { foo: 'bar' } })
// ^? { yolo: string } ๐
.with(jsonStringifyProp('yolo'))
We can even compose customizable layers by making good use of the ComposeUpLayers and ComposeDownLayers type:
import type { ComposeDownLayers } from '@onion.js/core'
type LogAndStringifyPropLayer<KEY extends string> = ComposeDownLayers<[
LogObjectLayer,
JSONStringifyPropLayer<KEY>
]>
const logAndStringifyProp = <KEY extends string>(
key: KEY
): JSONStringifyPropLayer<KEY> => composeDown(logOject, jsonStringifyProp(key))
const after = Onion.wrap({ yolo: { foo: 'bar' } })
// ^? { yolo: string } ๐
.with(jsonStringifyProp('yolo'))






