feat: Sse/Ws/Stream/Post route decorators with transport metadata
Signed-off-by: Diego Resendez <diego.resendez@zero-oneit.com>
This commit is contained in:
2 files changed
+39
-10
No files matched your search
+20
-8
@@ -5,7 +5,9 @@ export type TransformerFn = (value: unknown) => { status?: number; headers?: Rec
|
||||
|
||||
export interface ProviderMeta { provides: string; needs: string[]; optional: boolean }
|
||||
export interface StepMeta { provides: string; needs: string[]; optional: boolean }
|
||||
export interface RouteMeta { method: 'GET'; path: string; handlerName: string }
|
||||
export type HttpMethod = 'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE';
|
||||
export type Transport = 'buffer' | 'sse' | 'ndjson' | 'negotiate' | 'ws';
|
||||
export interface RouteMeta { method: HttpMethod; path: string; handlerName: string; transport: Transport }
|
||||
export interface ModuleOptions {
|
||||
mountpoint: string; providers?: Ctor[]; steps?: Ctor[]; controllers?: Ctor[];
|
||||
}
|
||||
@@ -37,15 +39,25 @@ export function Route(prefix: string): ClassDecorator {
|
||||
return (target) => { Reflect.defineMetadata(K.routePrefix, prefix, target); };
|
||||
}
|
||||
|
||||
export function Get(path: string): MethodDecorator {
|
||||
return (target, propertyKey) => {
|
||||
const ctor = target.constructor;
|
||||
const routes = (Reflect.getMetadata(K.routes, ctor) as RouteMeta[]) ?? [];
|
||||
routes.push({ method: 'GET', path, handlerName: String(propertyKey) });
|
||||
Reflect.defineMetadata(K.routes, routes, ctor);
|
||||
};
|
||||
function routeDecorator(method: HttpMethod, transport: Transport) {
|
||||
return (path: string): MethodDecorator =>
|
||||
(target, propertyKey) => {
|
||||
const ctor = target.constructor;
|
||||
const routes = (Reflect.getMetadata(K.routes, ctor) as RouteMeta[]) ?? [];
|
||||
routes.push({ method, path, handlerName: String(propertyKey), transport });
|
||||
Reflect.defineMetadata(K.routes, routes, ctor);
|
||||
};
|
||||
}
|
||||
|
||||
export const Get = routeDecorator('GET', 'buffer');
|
||||
export const Post = routeDecorator('POST', 'buffer');
|
||||
export const Put = routeDecorator('PUT', 'buffer');
|
||||
export const Patch = routeDecorator('PATCH', 'buffer');
|
||||
export const Delete = routeDecorator('DELETE', 'buffer');
|
||||
export const Sse = routeDecorator('GET', 'sse');
|
||||
export const Stream = routeDecorator('GET', 'negotiate');
|
||||
export const Ws = routeDecorator('GET', 'ws');
|
||||
|
||||
export function Transformer(fn: TransformerFn): MethodDecorator {
|
||||
return (target, propertyKey) => {
|
||||
Reflect.defineMetadata(K.transformer, fn, target.constructor, String(propertyKey));
|
||||
|
||||
+19
-2
@@ -1,8 +1,9 @@
|
||||
import 'reflect-metadata';
|
||||
import { expect, test } from 'vitest';
|
||||
import { expect, test, describe, it } from 'vitest';
|
||||
import {
|
||||
Provider, Step, Route, Get, Module,
|
||||
getProviderMeta, getStepMeta, getRoutes, getModuleMeta,
|
||||
Sse, Ws, Stream, Post,
|
||||
} from '../src/metadata';
|
||||
|
||||
@Provider({ provides: 'db', needs: ['config'] })
|
||||
@@ -26,7 +27,7 @@ test('step metadata is readable', () => {
|
||||
});
|
||||
|
||||
test('routes carry method, path and handler name', () => {
|
||||
expect(getRoutes(Ctl)).toEqual([{ method: 'GET', path: '/users/:id', handlerName: 'getUser' }]);
|
||||
expect(getRoutes(Ctl)).toEqual([{ method: 'GET', path: '/users/:id', handlerName: 'getUser', transport: 'buffer' }]);
|
||||
});
|
||||
|
||||
test('module metadata lists members', () => {
|
||||
@@ -36,3 +37,19 @@ test('module metadata lists members', () => {
|
||||
expect(m.steps).toEqual([Auth]);
|
||||
expect(m.controllers).toEqual([Ctl]);
|
||||
});
|
||||
|
||||
describe('stream route decorators', () => {
|
||||
it('records transport + method per decorator', () => {
|
||||
class StreamCtl {
|
||||
@Sse('/feed') feed() {}
|
||||
@Ws('/chat') chat() {}
|
||||
@Stream('/either') either() {}
|
||||
@Post('/make') make() {}
|
||||
}
|
||||
const byName = Object.fromEntries(getRoutes(StreamCtl).map((r) => [r.handlerName, r]));
|
||||
expect(byName.feed).toMatchObject({ method: 'GET', transport: 'sse', path: '/feed' });
|
||||
expect(byName.chat).toMatchObject({ method: 'GET', transport: 'ws', path: '/chat' });
|
||||
expect(byName.either).toMatchObject({ method: 'GET', transport: 'negotiate', path: '/either' });
|
||||
expect(byName.make).toMatchObject({ method: 'POST', transport: 'buffer', path: '/make' });
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user