@spoosh/plugin-transform
v0.8.2
Published
Data transformation plugin for Spoosh with sync/async support
Downloads
581
Maintainers
Readme
@spoosh/plugin-transform
Transform response data with full type inference.
Documentation · Requirements: TypeScript >= 5.0 · Peer Dependencies: @spoosh/core
Installation
npm install @spoosh/plugin-transformUsage
Response Transforms
Response transforms produce a separate transformedData field in meta while preserving the original data:
import { Spoosh } from "@spoosh/core";
import { transformPlugin } from "@spoosh/plugin-transform";
const spoosh = new Spoosh<ApiSchema, Error>("/api").use([transformPlugin()]);
const { data, meta } = useRead((api) => api("posts").GET(), {
transform: (posts) => ({
count: posts.length,
hasMore: posts.length >= 10,
ids: posts.map((p) => p.id),
}),
});
// data = Post[] (original response, preserved)
// meta.transformedData = { count: number, hasMore: boolean, ids: number[] } | undefinedAsync Transforms
Transform functions support async operations:
const { data, meta } = useRead((api) => api("posts").GET(), {
transform: async (posts) => {
const enriched = await enrichPostsWithMetadata(posts);
return {
count: enriched.length,
titles: enriched.map((p) => p.title),
};
},
});Features
- ✅ Full type inference for transforms
- ✅ Async transform functions supported
- ✅ Per-request transforms (no global config needed)
- ✅ Return
undefinedto remove data entirely
useWrite with Transform
With the hook-level options pattern, transform is now passed as a second argument to useWrite, enabling full type inference:
type TransformedPost = {
success: boolean;
postId: number;
};
const { trigger, meta } = useWrite((api) => api("posts").POST(), {
transform: (post) => ({
success: true,
postId: post.id,
}),
});
await trigger({ body: { title: "New Post" } });
// meta.transformedData is now properly typed!
const typed = meta.transformedData; // Type: TransformedPost | undefined