Releases: sstur/nbit
Releases · sstur/nbit
v0.13.2
v0.13.1
v0.12.1
v0.12.0
v0.11.0
In this release:
- Implement applicationOptions.serveFile
- Some updates to /examples
- Add PLATFORM constant
- Migrate tests to vitest (except for bun which uses
bun test
)
It's now possible to provide a mock serveFile when writing tests.
This also allows us to support Response.file()
in Cloudflare workers by providing a custom serveFile that will fetch the file from some remote source.
Example:
const { defineRoutes, createRequestHandler } = createApplication({
root: '/home/myapp', // Optional; defaults to process.cwd()
allowStaticFrom: ['public'],
serveFile: async ({ fullFilePath }) => {
if (fullFilePath === '/home/myapp/public/file.txt') {
const mockFileContent = 'Some mock content';
return new Response(mockFileContent, {
headers: { 'content-type': 'text/plain' },
});
}
return new Response('Invalid file path', { status: 404 });
},
});
const routes = defineRoutes((app) => [
app.get('/file', async (request) => {
return Response.file('public/file.txt');
}),
]);
const requestHandler = createRequestHandler(routes);
const request = new Request('http://localhost/file');
const response = await requestHandler(request);
expect(response.status).toBe(200);
expect(response.headers.get('content-type')).toBe('text/plain');
const body = await response.text();
expect(body).toBe('Some mock content');
Some additional details here.
v0.10.0
In this release:
- Add support for HEAD, OPTIONS and non-standard request methods, with TypeScript auto-complete suggestions
- Normalize methods to uppercase (at both the type level and runtime)
- More comprehensive tests
The following is now possible:
const routes = defineRoutes((app) => [
// Standard methods should auto-suggest in your editor using TypeScript
app.route('HEAD', '/foo', (request) => {
return new Response(...);
}),
// Non-standard methods are also allowed
app.route('MKCOL', '/webdav', (request) => {
return new Response(...);
}),
]);
v0.9.0
In this release:
- Add support for app.route(method, path, handler)
- bun: export Request (for consistency; even though it's already a global)
- bun: update tests
- bun: update dependency
bun-types
- bun: Always use ESM exports (fixes examples/bun-app)
- express: update script in package.json (fixes yarn clean)
Notes
As of this release, we now support a middleware-like wildcard route handler, which also enables custom 404 fallback. See example below.
import { attachRoutes, defineRoutes } from './application';
import * as handlers from './handlers';
const port = 3000;
const middleware = defineRoutes((app) => [
app.route('*', '/*', (request) => {
const authHeader = request.headers.get('authorization');
if (authHeader !== '123') {
throw new Error('Invalid auth');
}
}),
]);
const fallback = defineRoutes((app) => [
app.route('*', '/*', (request) => {
const message = `No resource found at path "${request.path}"`;
return new Response(message, { status: 404 });
}),
]);
Bun.serve({
port,
fetch: attachRoutes(middleware, ...Object.values(handlers), fallback),
});