install from npmnpm install svetch.ts
// usenpx svetch.ts
// src/foo/[organization_id]/server.ts
export const POST: RequestHandler = async ({request, params, url}) => {
const premium = url.searchParams.get('premium')
const payload = await request.json() as {name: string, age: number, email: string}
const user = {id: 1, ...payload}
return new Response(
JSON.stringify(user),
);
};
import {Svetch} from 'src/lib/client'
const svetch = new Svetch()
const user = svetch.post('foo',
{
path: {
organization_id: 1
},
query: {
premium: true
},
body: {
name: 'foo',
age: 12,
email: 'foo@bar.com'
}
}
)
export interface APIPaths {
'foo/:organization_id': {
GET: {
parameters: {
path: { organization_id: string };
query?: never;
};
responses: {};
errors: {};
};
POST: {
parameters: {
path: { organization_id: string };
body: {
name: string;
age: number;
email: string;
};
query: { premium: string };
};
responses: {
200: string;
};
errors: {};
};
};
}
import { z } from 'zod';
export const schema = z.object({
'foo/:organization_id': z.object({
GET: z.object({
parameters: z.object({ path: z.object({ organization_id: z.string() }) }),
responses: z.object({}),
errors: z.object({})
}),
POST: z.object({
parameters: z.object({
path: z.object({ organization_id: z.string() }),
body: z.object({ name: z.string(), age: z.number(), email: z.string() }),
query: z.object({ premium: z.string() })
}),
responses: z.object({ '200': z.string() }),
errors: z.object({})
})
})
});