Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error upon token option instead of logging #1

Merged
merged 4 commits into from
Jan 21, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 9 additions & 8 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,16 +45,17 @@ type QueryHandlerOptions = Parameters<typeof createFactory>[0];
*/
export const ronin = (options: QueryHandlerOptions = {}) =>
createMiddleware<Env>(async (c, next) => {
if (!c.env.RONIN_TOKEN)
throw new Error('Missing `RONIN_TOKEN` in environment variables');

const userOptions = typeof options === 'function' ? options() : options;
if (userOptions.token) {
console.warn(
'The `token` option is ignored in favor of `c.env.RONIN_TOKEN` when using the `ronin` middleware.',
if (!c.env.RONIN_TOKEN) {
throw new Error(
'A `RONIN_TOKEN` environment variable must be provided for the RONIN middleware',
);
}

const { token: userToken, ...userOptions } =
typeof options === 'function' ? options() : options;

delete userOptions.token;
if (userToken) {
throw new Error('The RONIN middleware does not support a `token` option');
}

const client = createFactory({
Expand Down
83 changes: 41 additions & 42 deletions tests/integration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,33 +13,6 @@ import.meta.env.RONIN_TOKEN = 'secret-token';
describe('use `ronin` middleware', () => {
const factory = createFactory<{ Bindings: Bindings; Variables: Variables }>();

it('with a missing `RONIN_TOKEN`', async () => {
let receivedError: Error | undefined;

const app = factory
.createApp()
.use('*', ronin({ fetch: fetcher }))
.get('/', async (c) => {
expect(c.var.ronin).toBeDefined();

const posts = (await c.var.ronin.get.blogPosts()) as Array<unknown>;

return c.json(posts);
})
.onError((err) => {
receivedError = err;
return new Response();
});

await testClient(app, {
RONIN_TOKEN: null,
}).index.$get();

expect(receivedError).toMatchObject({
message: 'Missing `RONIN_TOKEN` in environment variables',
});
});

it('with default options', async () => {
const app = factory
.createApp()
Expand Down Expand Up @@ -155,32 +128,58 @@ describe('use `ronin` middleware', () => {
expect(json).toHaveLength(1);
});

it('with a ignored `token` option', async () => {
it('with a missing `RONIN_TOKEN`', async () => {
let receivedError: Error | undefined;

const app = factory
.createApp()
.use('*', ronin({ fetch: fetcher, token: crypto.randomUUID() }))
.use('*', ronin({ fetch: fetcher }))
.get('/', async (c) => {
expect(c.var.ronin).toBeDefined();

const users = await c.var.ronin.get.users();

expect(users).toBeDefined();
expect(users).toBeInstanceOf(Array);
const posts = (await c.var.ronin.get.blogPosts()) as Array<unknown>;

return c.json(users);
return c.json(posts);
})
.onError((err) => {
receivedError = err;
return new Response();
});

const response = await testClient(app, {
RONIN_TOKEN: import.meta.env.RONIN_TOKEN,
await testClient(app, {
RONIN_TOKEN: null,
}).index.$get();

expect(response.ok).toBe(true);
expect(response.status).toBe(200);
expect(receivedError).toMatchObject({
message:
'A `RONIN_TOKEN` environment variable must be provided for the RONIN middleware',
});
});

const json = await response.json();
it('with a `token` option', async () => {
let receivedError: Error | undefined;

expect(json).toBeDefined();
expect(json).toBeInstanceOf(Array);
expect(json).toHaveLength(0);
const app = factory
.createApp()
.use('*', ronin({ fetch: fetcher, token: crypto.randomUUID() }))
.get('/', async (c) => {
expect(c.var.ronin).toBeDefined();

const posts = (await c.var.ronin.get.blogPosts()) as Array<unknown>;

return c.json(posts);
})
.onError((err) => {
receivedError = err;
return new Response();
});

await testClient(app, {
RONIN_TOKEN: import.meta.env.RONIN_TOKEN,
}).index.$get();

expect(receivedError).toMatchObject({
message: 'The RONIN middleware does not support a `token` option',
});
});
});
Loading