farrow-api-server
farrow-api adapter for farrow-http.
Installation
- npm
- Yarn
npm install farrow-api-server
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
- farrow-schema: Powerful and extensible schema builder library. Create Schema like type of TypeSccript.
- farrow-api: Schema-based Api builder.
- farrow-api-client: An client for farrow-api-server.
- farrow-deno-api: A deno server middleware.
- farrow-federation: A aggregation tool for farrow-api.
Sample
- farrow-sample/06-deno-api: Sample of deno api.
- farrow-sample/13-api-federation: Sample for api federation.
- farrow-sample/14-api-todo-app: Todo App with farrow-api.
- farrow-sample/23-api-todo: A template for farrow with rpc api.