Skip to main content

farrow-api-server

farrow-api adapter for farrow-http.

Installation

yarn add farrow-api-server

Usage

farrow-api just defining API, it's not directly bind to a server.

farrow-api-server can convert an api-entries to a router of farrow-http.

In servier-side, we define api/service via farrow-api, and attach them to farrow-http.

Integrate api:

// /src/api/todo.ts
import { ApiService } from "farrow-api-server";

// assuming addTodo/removeTodo is defined

// combine all api to an object/entries
export const entries = {
addTodo,
removeTodo,
};

// create service router
export const service = ApiService({
entries,
});

Attach to farrow-http:

// /src/api/index.ts
import { Response, Router } from "farrow-http";
// import todo-service
import { service as TodoService } from "./todo";

export const services = Router();

// capture all json response and do something if needed
services.capture("json", (body) => {
if (typeof body.value === "object") {
return Response.json({
...body.value,
// ...others
});
}
return Response.json(body.value);
});

// attach todo api to services
services.route("/api/todo").use(TodoService);

// attach user api to services if existed
// services.route('/api/user').use(UserService)
import path from "path";
import { Http } from "farrow-http";
import { vite } from "farrow-vite";

import { services } from "./api";

let http = Http();

// attach services to http
http.use(services);

http.listen(3000, () => {
console.log("server started at http://localhost:3002");
});

In client-side, for consuming data we need to use farrow-api-client.

// import file codegened by farrow
import { api as TodoApi } from "../api/todo";

const main = async () => {
// invoke api
let result = await TodoApi.addTodo({
content: "todo content",
});
};

API

createApiService

Type Signature:

const createApiService: (options: CreateApiServiceOptions) => ApiServiceType;
type CreateApiServiceOptions = {
entries: ApiEntries;
errorStack?: boolean;
introspection?: boolean;
};

Example Usage:

import { ApiService } from "farrow-api-server";

// combine all api to an object/entries
export const entries = {
addTodo,
removeTodo,
};

const service = ApiService({
entries,
});

Options:

  • entries: ApiEntries;

    Api entries.

  • errorStack?: boolean;

    Should display error.stack or not. It will be true if process.env.NODE_ENV === 'production' by default.

  • introspection?: boolean;

    Should open introspection feature or not. It will be true if process.env.NODE_ENV === 'production' by default.

Learn more

Relative Module
Sample