sstur / nbit

A zero-dependency, strongly-typed web framework for Bun, Node and Cloudflare workers
65 stars 4 forks source link

`@nbit/bun` has no exported member `Request` #3

Closed ImLunaHey closed 1 year ago

ImLunaHey commented 1 year ago

When switching to @nbit/bun I get this error Module '"@nbit/bun"' has no exported member 'Request'.

import { createApplication, Request } from '@nbit/node';
import { expect, test } from 'bun:test';

const { defineRoutes, createRequestHandler } = createApplication();

const routes = defineRoutes((app) => [
    app.get('/', (request) => {
        return { hello: 'world' };
    }),
]);

const requestHandler = createRequestHandler(routes);

test('should handle a request', async () => {
    const request = new Request('/');
    const response = await requestHandler(request);
    expect(response.status).toBe(200);
    const data = await response.json();
    expect(data).toEqual({ hello: 'world' });
});
sstur commented 1 year ago

Good catch! This might be a bug. I'll look into it

sstur commented 1 year ago

Ok, I've looked into this. In bun, Request is a global, so you don't need to import it. In the next release I'll see about exporting Request just for consistency with the node package.

Your code should look like this:

import { createApplication } from '@nbit/bun';
import { expect, test } from 'bun:test';

const { defineRoutes, createRequestHandler } = createApplication();

const routes = defineRoutes((app) => [
    app.get('/', (request) => {
        return { hello: 'world' };
    }),
]);

const requestHandler = createRequestHandler(routes);

test('should handle a request', async () => {
    const request = new Request('http://localhost/');
    const response = await requestHandler(request);
    expect(response.status).toBe(200);
    const data = await response.json();
    expect(data).toEqual({ hello: 'world' });
});

Note: You need to use a fully-qualified url for new Request(url) or else it will show:

Failed to construct 'Request': Invalid URL (missing a hostname)
sstur commented 1 year ago

In the next release I'll see about exporting Request just for consistency with the node package

Done in v0.9