farrow-deno-api
A deno server middleware.
Installation
- npm
- Yarn
npm install farrow-deno-api
yarn add farrow-deno-api
Usage
// server
import { Http } from 'farrow-http'
import { DenoService } from from 'farrow-deno-api'
// api definition
...
const entries = {
getCount,
setCount,
triggerError,
}
const CounterService = DenoService({
entries,
})
const http = Http()
const server = http.server()
http.route('/counter').use(CounterService)
http.listen(3000)
// client
import { api } from "http://localhost:3000/counter/client.ts";
api.getCount({}).then(console.log);
Options
type CreateDenoServiceOptions = {
entries: ApiEntries;
namespace?: string;
codegen?: CodegenOptions;
transform?: (source: string) => string;
format?: (source: string) => string;
};
entries
entries: ApiEntries;
Set the api entries of all services.
import { DenoService } from from 'farrow-deno-api'
const CounterService = DenoService({
entries,
})
namespace
namespace: string = "client";
It affect the path of file in client. Like:
import { api } from "http://localhost:3000/counter/client.ts";
// server
const CounterService = DenoService({
entries,
namespace: "todo",
});
// client
import { api } from "http://localhost:3000/counter/todo.ts";
codegen
codegen?: CodegenOptions
Set the code generation option.
transform
transform?: (source: string) => string
Transform source code received from server, it's useful when need to attach custom code snippet
// server
const CounterService = DenoService({
entries,
namespace: "todo",
transform: (code) => (code += `\nexport const test = 'test success'`),
});
// client
import { api, test } from "http://localhost:3000/counter/todo.ts";
test; // 'test success'
format
format?: (source: string) => string
Format source code via codegen. Could format with prettier.
Learn more
Relative Module
- farrow-api: Schema-based Api builder.
- farrow-api-server: farrow-api adapter for farrow-http.
- farrow-api-client: an api-client for
farrow-api-server
. - farrow-http: A Type-Friendly Web Framework.