UNPKG

6.39 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.Middleware = exports.IsDisabledOn = exports.AsyncVerify = exports.Verify = exports.Name = exports.MultipartForm = exports.API = exports.Body = exports.PATCH = exports.DELETE = exports.PUT = exports.POST = exports.GET = exports.Route = void 0;
4var http_verb_1 = require("./http-verb");
5var routes = [];
6/**
7 * Decorator to set the base route of a controller.
8 * @param path the route path for the controller.
9 */
10function Route(path) {
11 return function (target) {
12 target.controllerPath = path;
13 target.routes = routes;
14 routes = [];
15 };
16}
17exports.Route = Route;
18function setTarget(_, type, path, method) {
19 routes.push({
20 type: type,
21 path: path,
22 method: method,
23 body: undefined,
24 isAPI: false,
25 isMultipartForm: false,
26 verifiers: [],
27 isDisabledOn: undefined
28 });
29}
30function setBody(_, name, schema) {
31 if (!routes)
32 return;
33 routes[routes.length - 1].body = {
34 name: name,
35 schema: schema
36 };
37}
38function setApi(_) {
39 if (!routes)
40 return;
41 routes[routes.length - 1].isAPI = true;
42}
43function setMultipartForm(_) {
44 if (!routes)
45 return;
46 routes[routes.length - 1].isMultipartForm = true;
47}
48function setName(name) {
49 if (!routes)
50 return;
51 routes[routes.length - 1].name = name;
52}
53function setVerify(_, func) {
54 if (!routes)
55 return;
56 routes[routes.length - 1].verifiers.push({ fun: func, isAsync: false });
57}
58function setAsyncVerify(_, func) {
59 if (!routes)
60 return;
61 routes[routes.length - 1].verifiers.push({ fun: func, isAsync: true });
62}
63function setIsDisabledOn(_, func) {
64 if (!routes)
65 return;
66 routes[routes.length - 1].isDisabledOn = func;
67}
68/**
69 * Set the decorated method to a GET endpoint with the specified path.
70 * @param path the endpoint path
71 */
72function GET(path) {
73 return function (target, method, _) {
74 setTarget(target, http_verb_1.HttpVerb.GET, path, method);
75 };
76}
77exports.GET = GET;
78/**
79 * Set the decorated method to a POST endpoint with the specified path.
80 * @param path the endpoint path
81 */
82function POST(path) {
83 return function (target, method, _) {
84 setTarget(target, http_verb_1.HttpVerb.POST, path, method);
85 };
86}
87exports.POST = POST;
88/**
89 * Set the decorated method to a PUT endpoint with the specified path.
90 * @param path the endpoint path
91 */
92function PUT(path) {
93 return function (target, method, _) {
94 setTarget(target, http_verb_1.HttpVerb.PUT, path, method);
95 };
96}
97exports.PUT = PUT;
98/**
99 * Set the decorated method to a DELETE endpoint with the specified path.
100 * @param path the endpoint path
101 */
102function DELETE(path) {
103 return function (target, method, _) {
104 setTarget(target, http_verb_1.HttpVerb.DELETE, path, method);
105 };
106}
107exports.DELETE = DELETE;
108/**
109 * Set the decorated method to a PATH endpoint with the specified path.
110 * @param path the endpoint path
111 */
112function PATCH(path) {
113 return function (target, method, _) {
114 setTarget(target, http_verb_1.HttpVerb.PATCH, path, method);
115 };
116}
117exports.PATCH = PATCH;
118/**
119 * Add to the decorated method a body to be injected. The body will be validated
120 * using the specified schema.
121 * @param name the name of the argument in the method to map the body
122 * @param schema a JOI schema to validate the body object
123 */
124function Body(name, schema) {
125 return function (target, _, __) {
126 setBody(target, name, schema);
127 };
128}
129exports.Body = Body;
130/**
131 * Set the decorated method to an API endpoints.
132 * In this way, the returned value of the method will be encapsulated in a
133 * standard API envelope and serialized to JSON using the Lynx serialization system.
134 */
135function API() {
136 return function (target, _, __) {
137 setApi(target);
138 };
139}
140exports.API = API;
141/**
142 * Set the decorated method to accept MultipartForm requested.
143 */
144function MultipartForm() {
145 return function (target, _, __) {
146 setMultipartForm(target);
147 };
148}
149exports.MultipartForm = MultipartForm;
150/**
151 * Add to the decorated method a route name, in order to easely generate redirect
152 * or, more general, the execution of the `route` method.
153 * @param name the name of the route
154 */
155function Name(name) {
156 return function (_, __, ___) {
157 setName(name);
158 };
159}
160exports.Name = Name;
161/**
162 * Add to the decorated method a verification function that will be executed
163 * BEFORE the route. The function must NOT be an async function, and it shell
164 * return a boolean value. If true is returned, the method is then executed.
165 * This method is fundamental to implement authorization to a single endpoint.
166 * NOTE: this is the sync version of the AsyncVerify decorator.
167 * @param func the verification function to be executed. It must NOT be an async function, and return a boolean value.
168 */
169function Verify(func) {
170 return function (target, _, __) {
171 setVerify(target, func);
172 };
173}
174exports.Verify = Verify;
175/**
176 * Add to the decorated method a verification function that will be executed
177 * BEFORE the route. The function must NOT be an async function, and it shell
178 * return a boolean value. If true is returned, the method is then executed.
179 * This method is fundamental to implement authorization to a single endpoint.
180 * NOTE: this is the async version of the Verify decorator.
181 * @param func the verification function to be executed. It MUST BE an async function, and return a boolean value.
182 */
183function AsyncVerify(func) {
184 return function (target, _, __) {
185 setAsyncVerify(target, func);
186 };
187}
188exports.AsyncVerify = AsyncVerify;
189/**
190 * Add to the decorated method a "disabled" function, that is verified at startup
191 * time. If the function return a truly value, the decorated method is not registered
192 * as an endpoint.
193 * @param func the disabling function to be executed. It shall return a boolean value.
194 */
195function IsDisabledOn(func) {
196 return function (target, _, __) {
197 setIsDisabledOn(target, func);
198 };
199}
200exports.IsDisabledOn = IsDisabledOn;
201/**
202 * Add to the decorated class a path to be executed as middleware.
203 * @param path the endpoint path
204 */
205function Middleware(path) {
206 return function (target) {
207 target.middlewarePath = path;
208 };
209}
210exports.Middleware = Middleware;