Ikkan 一貫

Getting started

How to write your first endpoint with Ikkan

There is no obligations about how you should structure your files, but here is a recommended structure:

config.ts
route.ts
bridge.ts

config.ts

This file is used to define how your endpoint will behave. It should export IkkanConfig objects, that will be used to configure actual implementations of your endpoint and client-side/server-side functions.\

import { ikkanConfig } from "@ikkan/core";
import { z } from "zod";
 
export const config = ikkanConfig({
	endpoint: () => "/api/greeting",
	method: "GET",
	schema: z.string(),
	fn: async (_req, name) => {
		return 'Hello, ' + name + '!';
	},
});

route.ts

It is the file that Next.js uses to define the route of your endpoint.
It should export a function that will be used when calling to that specific endpoint. You don't have to write much in this file, as Ikkan will take care of the boilerplate for you.
You can export multiple functions, if you endpoint supports multiple methods.

import { ikkanHandler } from "@ikkan/server";
import { config } from "./config";
 
export const { GET } = await ikkanHandler(config);
 

bridge.ts

This file is used to define the client-side or server-side function that will call your endpoint.
It is you responsibility as a developer to export relevant functions (server-side, client-side without SWR, client-side with SWR) that will call the endpoint.

import { ikkanClientBridge } from "@ikkan/server";
import { config } from "./config";
 
export const { hook: useGetGreeting, action: getGreeting } = ikkanClientBridge(config);
 

On this page