Share types between front and and backend without trpc...

I'm working on a project using nextjs, Hono js, drizzle, turborepo . I don't want to use trpc and would like to enforce types. I want to infer the return type of the fetchMyCollection to export it and use it in the nextjs app. I tried to do type myType = ReturnType<typeof fetchAlbum> but this is only a shallow type. How can properly share the types across the backend and nextjs?
export async function fetchMyCollection(collectionItemId: string) {
const collectionItem = await myDatabase.query.collectionItems.findFirst({
where: eq(collectionItems.itemId, collectionItemId),
});
console.log("Fetched collection item: ", collectionItemId);

const resourcesCount = (
await myDatabase.select({
count: sql<number>`count(*)`,
}).from(myMediaResources).where(eq(myMediaResources.collectionItemId, collectionItemId))
)[0].count;
return { collectionItem: { ...collectionItem, resourcesCount }, resources: [] };
}
export async function fetchMyCollection(collectionItemId: string) {
const collectionItem = await myDatabase.query.collectionItems.findFirst({
where: eq(collectionItems.itemId, collectionItemId),
});
console.log("Fetched collection item: ", collectionItemId);

const resourcesCount = (
await myDatabase.select({
count: sql<number>`count(*)`,
}).from(myMediaResources).where(eq(myMediaResources.collectionItemId, collectionItemId))
)[0].count;
return { collectionItem: { ...collectionItem, resourcesCount }, resources: [] };
}
1 Reply
KyratKing
KyratKing14mo ago
Try using this website. If you paste a JSON object into it, it not only creates interfaces and enums for you, but it also gives you a set of functions to validate against that interface. This might be what you're looking for
Instantly parse JSON in any language | quicktype
Whether you're using C#, Swift, TypeScript, Go, C++ or other languages, quicktype generates models and helper code for quickly and safely reading JSON in your apps. Customize online with advanced options, or download a command-line tool.
Want results from more Discord servers?
Add your server