Last active
May 16, 2025 16:53
-
-
Save marcus-sa/05c54e665b9c83c24c8f9e08491d9ddd to your computer and use it in GitHub Desktop.
custom deepkit http decorator
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import {http as _http, HttpAction, httpAction, HttpActionDecorator, httpClass} from "@deepkit/http"; | |
import { | |
createPropertyDecoratorContext, | |
DualDecorator, | |
mergeDecorator, | |
PropertyDecoratorFn, | |
ReceiveType | |
} from "@deepkit/type"; | |
import {ClassType,UnionToIntersection} from "@deepkit/core"; | |
export enum Role { | |
ADMIN = 'admin', | |
USER = 'user', | |
} | |
class HttpAuthActionDecorator extends HttpActionDecorator { | |
t = new HttpAction(); | |
roles(...roles: Role[]) { | |
this.t.data.set('roles', roles); | |
} | |
} | |
export const httpAuthAction = createPropertyDecoratorContext(HttpAuthActionDecorator); | |
//this workaround is necessary since generic functions are lost during a mapped type and changed ReturnType | |
type HttpMerge<U> = { [K in keyof U]: K extends 'response' ? <T2>(statusCode: number, description?: string, type?: ReceiveType<T2>) => PropertyDecoratorFn & U : U[K] extends ((...a: infer A) => infer R) ? R extends DualDecorator ? (...a: A) => PropertyDecoratorFn & R & U : (...a: A) => R : never }; | |
type MergedHttp<T extends any[]> = HttpMerge<Omit<UnionToIntersection<T[number]>, '_fetch' | 't'>> | |
export const http = mergeDecorator(httpClass, httpAuthAction) as any as MergedHttp<[typeof httpClass, typeof httpAuthAction]>; | |
@http.controller('/test') | |
class TestController { | |
@(http.POST('test')).roles(Role.ADMIN) | |
test() { | |
} | |
} | |
console.log(httpClass._fetch(TestController)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
It logs