Skip to content

v0.11.0

Compare
Choose a tag to compare
@sstur sstur released this 11 Aug 01:36
· 42 commits to main since this release

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.