Svetch.ts

Typesafety, Minus the typing 😉

BORN TO FETCH

WORLD IS CORS

鬼神 Type Em All 1991

I am typesafe man

2,023.952k DEAD BUGS

Live Stats

Files Processsed
10450
Much process, such time saved
Codelines Generated
2023952
Code less, sleep more?

Methods Processed

GET
1334
POST
548
PUT
241
PATCH
240
DELETE
260

Server Code

            install from npm
            npm install svetch.ts
            // use
            npx svetch.ts
          

By writing your endpoints as you usually do 👇

Server Code


    // 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),
        );
    };

You get intellisense on all your API parameters 👌

Client Code


    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'
}
        }
    )

The return type will be inferred from the API without having to manually add types

All your APIs will have generated types 👇

/lib/api/api.ts


    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: {};
		};
	};
}

And zod schemas, which the client can automatically validate on any request 👇

/lib/api/zod.ts


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({})
		})
	})
});

It will also auto generate BEAUTIFUL and functional API docs for testing & documentation

Sample Docs