diff --git a/src/app/index.ts b/src/app/index.ts index 4fd001b..0096d01 100644 --- a/src/app/index.ts +++ b/src/app/index.ts @@ -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)`, ); } diff --git a/src/index.ts b/src/index.ts index 4beab8b..b6b664a 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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'; diff --git a/src/metadata.ts b/src/metadata.ts index 530dcff..8a1f3ec 100644 --- a/src/metadata.ts +++ b/src/metadata.ts @@ -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. */ diff --git a/src/signals.ts b/src/signals.ts index c34dfc1..ef46969 100644 --- a/src/signals.ts +++ b/src/signals.ts @@ -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}.`); } diff --git a/test/metadata.test.ts b/test/metadata.test.ts index 857d40f..f41546e 100644 --- a/test/metadata.test.ts +++ b/test/metadata.test.ts @@ -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 {}; } } diff --git a/test/smoke.test.ts b/test/smoke.test.ts index d2c91ec..a188088 100644 --- a/test/smoke.test.ts +++ b/test/smoke.test.ts @@ -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']); +});