instant-endpoint
TypeScript icon, indicating that this package has built-in type declarations

0.2.2 • Public • Published

Instant endpoint is a factory function for nextjs for declaring routes.

  • It handles defining params required for transaction ( headers, params, body )
  • Parses unknowns
  • Returns an ednpoint function and calling function, with TypeGuard for using an endpoint in a way that will satisfy declared requirements.
const [PANCAKES_ROUTE, CALL_PANCAKES] = instantEndpoint(
    "https://localhost:3000/api/test",
    "GET",
    { headers: { "Content-Type": "application/json", pancakes: z.string() } },
    async (request, { headers }) => {
        return {
            url: request.url,
            pancakes: headers.pancaces,
        };
    }
);

Export from route.tsx at adres provided in argument to instantEndpoint

export { PANCAEES_ROUTE as GET };

Call endpoint in the client using returned call function

    export default async function PancakesComponent(){
        const res = await CALL_PANCACES({ headers : { "Content-Type": "application/json", pancakes: "with topping" }})

        return (
            { res && <p> res.pancakes </p>}
        )
    }

Readme

Keywords

Package Sidebar

Install

npm i instant-endpoint

Weekly Downloads

27

Version

0.2.2

License

none

Unpacked Size

23.1 kB

Total Files

9

Last publish

Collaborators

  • damian-chyb