通过零束包大小选项,安全地访问任何 API。
Get type-safe access to any API, with a zero-bundle size option.
If you're lucky enough to use tRPC, GraphQL, or OpenAPI, you'll be able to get type-safe access to your API - either through a type-safe RPC or codegen.
But what about the rest of us?
What do you do if your API has no types?
Enter untypeable - a first-class library for typing API's you don't control.
import type to ensure untypeable adds nothing to your bundle.npm i untypeable
…We've added a full example of typing swapi.dev.
You can set up untypeable to run in zero-bundle mode. This is great for situations where you trust the API you're calling, but it just doesn't have types.
To set up zero-bundle mode, you'll need to:
router.ts.export type MyRouter = typeof router;// router.ts
import { initUntypeable } from "untypeable";
const u = initUntypeable();
type User = {
id: string;
name: string;
};
const router = u.router({
"/user": u.input().output(),
});
export type MyRouter = typeof router;client.ts, import createTypeLevelClient from untypeable/type-level-client.// client.ts
import { createTypeLevelClient } from "untypeable/client";
import type { MyRouter } from "./router";
export const client = createTypeLevelClient(() => {
// your implementation...
});This works because createTypeLevelClient is just an identity function, which directly returns the function you pass it. Most modern bundlers are smart enough to collapse identity functions and erase type imports, so you end up with:
// client.ts
export const client = () => {
// your implementation...
};Sometimes, you just don't trust the API you're calling. In those situations, you'll often like to validate the data you get back.
untypeable offers first-class integration with Zod. You can pass a Zod schema to u.input and u.output to ensure that these values are validated with Zod.
import { initUntypeable, createSafeClient } from "untypeable";
import { z } from "zod";
const u = initUntypeable();
const router = u.router({
"/user": u
.input(
z.object({
id: z.string(),
}),
)
.output(
z.object({
id: z.string(),
name: z.string(),
}),
),
});
export const client = createSafeClient(router, () => {
// Implementation...
});Now, every call made to client will have its input and output verified by the zod schemas passed.
untypeable lets you be extremely flexible with the shape of your router.
Each level of the router corresponds to an argument that'll be passed to your client.
// A router that looks like this:
const router = u.router({
github: {
"/repos": {
GET: u.output(),
POST: u.output(),
},
},
});
const client = createTypeLevelClient(() => {});
// Will need to be called like this:
client("github", "/repos", "POST");You can set up this argument structure using the methods below:
.pushArgUsing the .pushArg method when we initUntypeable lets us add new arguments that must be passed to our client.
…You can call this as many times as you want!
const u = initUntypeable()
.pushArg()
.pushArg();
const router = u.router({
"/": {
GET: {
foo: u.output,
},
},
});.unshiftArgYou can also add an argument at the start using .unshiftArg. This is useful for when you want to add different base endpoints:
const u = initUntypeable().unshiftArg();
const router = u.router({
github: {
"/repos": u.output(),
},
});.argsUseful for when you want to set the args up manually:
const u = initUntypeable().args();
const router = u.router({
"any-string": {
"any-other-string": {
"yet-another-string": u.output(),
},
},
});.addYou can add more detail to a router, or split it over multiple calls, by using router.add.
const router = u
.router({
"/": u.output(),
})
.add({
"/user": u.output(),
});.mergeYou can merge two routers together using router.merge. This is useful for when you want to combine multiple routers (perhaps in different modules) together.
import { userRouter } from "./userRouter";
import { postRouter } from "./postRouter";
export const baseRouter = userRouter.merge(postRouter);暂无开放 Issues,或尚未同步最近议题。