@ikkan/server
Server-side functionalities for Ikkan
This package handles the server-side part of Ikkan. It is designed to be used with Next.js, and it will take care of the boilerplate code for you, so you can focus on writing the logic of your API.
This package only exposes two functions, that you will use on a well-defined config
object of type IkkanConfig.
Functions
ikkanHandler
This function will take a IkkanConfig object and return an object than can be used by Next.js to handle the request.
This code must be placed in a route.ts
file, that will be used by Next.js to define the route of your endpoint.
Some considerations:
- Error handling: The error handling is done automatically by Ikkan. If an error is thrown in your function, it will be wrapped in a
SerializedError
object, and sent to the client. - Schema validation: The schema validation is done automatically by Ikkan. If the data sent by the client does not match the schema, a 400 error will be sent to the client.
- Params:
- In the case of a method using search params (such as
GET
), the params will be passed in a single search params, namedparams
. You can access them usingreq.query.params
. - In the case of a method using a body (such as
POST
), the body will be passed in the body, as JSON. You can access them usingreq.body
.
- In the case of a method using search params (such as
ikkanServerBridge
This function will take a IkkanConfig object and return a function that will call the endpoint, intended to be used on the server-side.
This function first will take the params of the endpoint and return a fetcher function that will call the endpoint with params and return the result.