Skip to content

Basic Patterns

Here are all the basic patterns that should be used with Byte.

Route splitting

Routes in different instances can be merged using app.route.

app.route('/prefix', anotherByteInstance);

You can use Byte.route() as a shorthand of new Byte().route():

const app = Byte
.route('/user', userAPIs)
.route('/channel', channelAPIs);

Code splitting

Code splitting can be done by de-coupling parts like middlewares or request handlers.

Byte.handle is a wrapper to create functions which accepts a request context as an argument.

These functions can be used as request handlers, validator methods or middlewares.

const fn = Byte.handle((ctx) => {
// Do something with the request context
});

Byte.plugin is a wrapper to create plugins.

const plugin = Byte.plugin({
plug: (app) => {
// Do something with the app instance
}
});

Chaining

Chaining should be used if you need client type inference.

Chainable methods include use, register, route, and all methods for registering request handlers.

const app = new Byte()
.use(csrf())
.prepare(cors())
.post('/echo', async (ctx) => ctx.body(await ctx.req.text()));

Middlewares should be registered before handlers for readability.

Testing

See the testing client docs here.