feat(router): expose HEAD and OPTIONS decorators
Signed-off-by: Diego Resendez <diego.resendez@zero-oneit.com>
This commit is contained in:
6 files changed
+30
-5
No files matched your search
+1
-1
@@ -413,7 +413,7 @@ function resolveTransformer(
|
||||
|
||||
if (route.transport !== 'buffer' || (route.method !== 'GET' && route.method !== 'POST')) {
|
||||
throw new Error(
|
||||
`@Html on ${route.method} ${pattern} is not allowed — @Html only supports buffered GET/POST routes (not SSE/WS/PUT/PATCH/DELETE)`,
|
||||
`@Html on ${route.method} ${pattern} is not allowed — @Html only supports buffered GET/POST routes (not SSE/WS/HEAD/PUT/PATCH/DELETE/OPTIONS)`,
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
@@ -27,6 +27,7 @@ export {
|
||||
Step,
|
||||
Route,
|
||||
Get,
|
||||
Head,
|
||||
Module,
|
||||
Transformer,
|
||||
Sse,
|
||||
@@ -36,6 +37,7 @@ export {
|
||||
Put,
|
||||
Patch,
|
||||
Delete,
|
||||
Options,
|
||||
Html,
|
||||
} from './metadata';
|
||||
export type { Transport, HttpMethod } from './metadata';
|
||||
|
||||
+5
-1
@@ -25,7 +25,7 @@ export interface StepMeta {
|
||||
export: boolean;
|
||||
}
|
||||
/** HTTP verbs supported by the route decorators. */
|
||||
export type HttpMethod = 'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE';
|
||||
export type HttpMethod = 'GET' | 'HEAD' | 'POST' | 'PUT' | 'PATCH' | 'DELETE' | 'OPTIONS';
|
||||
/** Wire transport a route responds with. */
|
||||
export type Transport = 'buffer' | 'sse' | 'ndjson' | 'negotiate' | 'ws';
|
||||
/** Metadata attached by a route decorator to a single handler method. */
|
||||
@@ -124,6 +124,8 @@ function routeDecorator(method: HttpMethod, transport: Transport) {
|
||||
|
||||
/** Declares a buffered `GET` route. */
|
||||
export const Get = routeDecorator('GET', 'buffer');
|
||||
/** Declares a buffered `HEAD` route. */
|
||||
export const Head = routeDecorator('HEAD', 'buffer');
|
||||
/** Declares a buffered `POST` route. */
|
||||
export const Post = routeDecorator('POST', 'buffer');
|
||||
/** Declares a buffered `PUT` route. */
|
||||
@@ -132,6 +134,8 @@ export const Put = routeDecorator('PUT', 'buffer');
|
||||
export const Patch = routeDecorator('PATCH', 'buffer');
|
||||
/** Declares a buffered `DELETE` route. */
|
||||
export const Delete = routeDecorator('DELETE', 'buffer');
|
||||
/** Declares a buffered `OPTIONS` route. */
|
||||
export const Options = routeDecorator('OPTIONS', 'buffer');
|
||||
/** Declares a `GET` route streamed as Server-Sent Events. */
|
||||
export const Sse = routeDecorator('GET', 'sse');
|
||||
/** Declares a `GET` route whose transport is content-negotiated at request time. */
|
||||
|
||||
+1
-1
@@ -62,7 +62,7 @@ export class TransportMismatchError extends HttpError {
|
||||
const expected = got === 'stream' ? 'return a value' : 'return an AsyncIterable';
|
||||
const fix =
|
||||
got === 'stream'
|
||||
? 'buffered routes (@Get/@Post/@Put/@Patch/@Delete) must return a value — to stream, declare @Sse, @Stream, or @Ws'
|
||||
? 'buffered routes (@Get/@Head/@Post/@Put/@Patch/@Delete/@Options) must return a value — to stream, declare @Sse, @Stream, or @Ws'
|
||||
: 'streaming routes (@Sse/@Ws) must return an AsyncIterable (e.g. an async generator or channel())';
|
||||
super(500, `Transport '${transport}'${where} must ${expected}, but the handler returned a ${got}. ${fix}.`);
|
||||
}
|
||||
|
||||
+13
-1
@@ -3,7 +3,7 @@ import { expect, test, describe, it } from 'vitest';
|
||||
import {
|
||||
Provider, Step, Route, Get, Module,
|
||||
getProviderMeta, getStepMeta, getRoutes, getModuleMeta,
|
||||
Sse, Ws, Stream, Post,
|
||||
Sse, Ws, Stream, Post, Head, Options,
|
||||
} from '../src/metadata';
|
||||
|
||||
@Provider({ provides: 'db', needs: ['config'] })
|
||||
@@ -54,6 +54,18 @@ describe('stream route decorators', () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe('HEAD and OPTIONS decorators', () => {
|
||||
it('records both as buffered methods', () => {
|
||||
class Methods {
|
||||
@Head('/resource') head() {}
|
||||
@Options('/resource') options() {}
|
||||
}
|
||||
const byName = Object.fromEntries(getRoutes(Methods).map((route) => [route.handlerName, route]));
|
||||
expect(byName.head).toMatchObject({ method: 'HEAD', transport: 'buffer', path: '/resource' });
|
||||
expect(byName.options).toMatchObject({ method: 'OPTIONS', transport: 'buffer', path: '/resource' });
|
||||
});
|
||||
});
|
||||
|
||||
describe('mesh export flags', () => {
|
||||
it('providers/steps carry export (default false)', () => {
|
||||
@Provider({ provides: 'config', export: true }) class C { provide() { return {}; } }
|
||||
|
||||
+8
-1
@@ -1,6 +1,13 @@
|
||||
import { expect, test } from 'vitest';
|
||||
import { VERSION } from '../src/index';
|
||||
import { Head, Options, VERSION, type HttpMethod } from '../src/index';
|
||||
|
||||
test('package loads and exposes a CalVer VERSION', () => {
|
||||
expect(VERSION).toMatch(/^\d{2}\.\d{1,2}\.\d+(-beta\.\d+)?$/);
|
||||
});
|
||||
|
||||
test('public barrel exports HEAD and OPTIONS route decorators', () => {
|
||||
const methods: HttpMethod[] = ['HEAD', 'OPTIONS'];
|
||||
expect(Head).toBeTypeOf('function');
|
||||
expect(Options).toBeTypeOf('function');
|
||||
expect(methods).toEqual(['HEAD', 'OPTIONS']);
|
||||
});
|
||||
Reference in new issue
Block a user