Skip to main content

farrow-deno-api

A deno server middleware.

Installation

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;

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

CodegenOptions

Set the code generation option.

transform

transform?: (source: string) => string

transform

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

Format source code via codegen. Could format with prettier.

Learn more

Relative Module