@tahminator/sapling 1.5.27 → 1.5.28

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (51) hide show
  1. package/dist/index.cjs +669 -0
  2. package/dist/index.d.cts +421 -0
  3. package/dist/index.d.mts +421 -0
  4. package/dist/index.mjs +621 -0
  5. package/package.json +11 -10
  6. package/dist/eslint.config.d.ts +0 -2
  7. package/dist/eslint.config.js +0 -38
  8. package/dist/exclusions.d.ts +0 -5
  9. package/dist/exclusions.js +0 -6
  10. package/dist/index.d.ts +0 -1
  11. package/dist/index.js +0 -1
  12. package/dist/lib/weakmap.d.ts +0 -15
  13. package/dist/lib/weakmap.js +0 -77
  14. package/dist/src/__test__/first.d.ts +0 -6
  15. package/dist/src/__test__/first.js +0 -20
  16. package/dist/src/__test__/second.d.ts +0 -6
  17. package/dist/src/__test__/second.js +0 -20
  18. package/dist/src/annotation/controller.d.ts +0 -21
  19. package/dist/src/annotation/controller.js +0 -78
  20. package/dist/src/annotation/index.d.ts +0 -4
  21. package/dist/src/annotation/index.js +0 -4
  22. package/dist/src/annotation/injectable.d.ts +0 -25
  23. package/dist/src/annotation/injectable.js +0 -72
  24. package/dist/src/annotation/middleware.d.ts +0 -9
  25. package/dist/src/annotation/middleware.js +0 -11
  26. package/dist/src/annotation/route.d.ts +0 -47
  27. package/dist/src/annotation/route.js +0 -77
  28. package/dist/src/enum/http.d.ts +0 -68
  29. package/dist/src/enum/http.js +0 -71
  30. package/dist/src/enum/index.d.ts +0 -1
  31. package/dist/src/enum/index.js +0 -1
  32. package/dist/src/helper/error.d.ts +0 -10
  33. package/dist/src/helper/error.js +0 -19
  34. package/dist/src/helper/index.d.ts +0 -4
  35. package/dist/src/helper/index.js +0 -4
  36. package/dist/src/helper/redirect.d.ts +0 -14
  37. package/dist/src/helper/redirect.js +0 -19
  38. package/dist/src/helper/response.d.ts +0 -68
  39. package/dist/src/helper/response.js +0 -90
  40. package/dist/src/helper/sapling.d.ts +0 -101
  41. package/dist/src/helper/sapling.js +0 -153
  42. package/dist/src/html/404.d.ts +0 -4
  43. package/dist/src/html/404.js +0 -14
  44. package/dist/src/html/index.d.ts +0 -1
  45. package/dist/src/html/index.js +0 -1
  46. package/dist/src/index.d.ts +0 -5
  47. package/dist/src/index.js +0 -5
  48. package/dist/src/types.d.ts +0 -21
  49. package/dist/src/types.js +0 -11
  50. package/dist/vite.config.d.ts +0 -2
  51. package/dist/vite.config.js +0 -18
@@ -0,0 +1,421 @@
1
+ import e, { NextFunction, Request, Response, Router } from "express";
2
+
3
+ //#region src/html/404.d.ts
4
+ /**
5
+ * Default Express.js 404 error page, as a string.
6
+ */
7
+ declare const Html404ErrorPage: (error: string) => string;
8
+ //#endregion
9
+ //#region src/types.d.ts
10
+ type ExpressRouterMethodKey = "GET" | "POST" | "PUT" | "DELETE" | "PATCH" | "OPTIONS" | "HEAD" | "USE";
11
+ type ExpressRouterMethods = Lowercase<ExpressRouterMethodKey>;
12
+ declare const methodResolve: Record<ExpressRouterMethodKey, ExpressRouterMethods>;
13
+ type RouteDefinition = {
14
+ /**
15
+ * Express.js HTTP method.
16
+ */
17
+ method: ExpressRouterMethodKey;
18
+ /**
19
+ * The path to define the route on. Can be a string or RegExp.
20
+ */
21
+ path: string | RegExp;
22
+ /**
23
+ * The name of the function the `@Route` annotation was applied on.
24
+ */
25
+ fnName: string;
26
+ };
27
+ type Class<T> = new (...args: any[]) => T;
28
+ type HttpHeaders = Record<string, string>;
29
+ type ExpressMiddlewareFn = ($1: Request, $2: Response, $3: NextFunction) => void;
30
+ //#endregion
31
+ //#region src/annotation/controller.d.ts
32
+ declare const _ControllerRegistry: WeakMap<Function, Router>;
33
+ type ControllerProps = {
34
+ /**
35
+ * Optional URL prefix applied to all routes in the controller. Defaults to "".
36
+ */
37
+ prefix?: string;
38
+ /**
39
+ * Optional array of dependencies to be injected into the constructor that are `@Injectable`
40
+ */
41
+ deps?: Array<Class<any>>;
42
+ } | undefined;
43
+ /**
44
+ * Registers a class as an HTTP controller and registers its routes.
45
+ *
46
+ * @param [prefix] Optional URL prefix applied to all routes in the controller. Defaults to "".
47
+ * @param [deps] Optional array of dependencies to be injected into the constructor that are `@Injectable`
48
+ */
49
+ declare function Controller({
50
+ prefix,
51
+ deps
52
+ }?: ControllerProps): ClassDecorator;
53
+ //#endregion
54
+ //#region lib/weakmap.d.ts
55
+ /**
56
+ * WeakMap that is iterable.
57
+ */
58
+ declare class IterableWeakMap<K extends object, V> {
59
+ #private;
60
+ constructor(iterable?: Iterable<[K, V]>);
61
+ set(key: K, value: V): this;
62
+ get(key: K): V | undefined;
63
+ delete(key: K): boolean;
64
+ [Symbol.iterator](): IterableIterator<[K, V]>;
65
+ entries(): IterableIterator<[K, V]>;
66
+ keys(): IterableIterator<K>;
67
+ values(): IterableIterator<V>;
68
+ forEach(callback: (value: V, key: K, map: this) => void, thisArg?: any): void;
69
+ }
70
+ //#endregion
71
+ //#region src/annotation/injectable.d.ts
72
+ declare const _InjectableRegistry: WeakMap<Class<any>, any>;
73
+ declare const _InjectableDeps: IterableWeakMap<Class<any>, Class<any>[]>;
74
+ /**
75
+ * Mark the class as an injectable to be handled by Sapling. The class can now be
76
+ * be injected into other classes, as well as allow the class to inject other `@Injectable` classes.
77
+ *
78
+ * @argument deps - An optional array to define any dependencies that this class may require.
79
+ */
80
+ declare function Injectable(deps?: Array<Class<any>>): ClassDecorator;
81
+ /**
82
+ * Resolves and instantiates a class along with all of it's transitive dependencies.
83
+ *
84
+ * Uses topological sort (Kahn's algorithm) to ensure that the dependency graph is created
85
+ * in a correct order.
86
+ *
87
+ * When `resolve` is first called (usually during controller registration),
88
+ * it will compute the dependency graph of all `@Injectable` classes and instantiates
89
+ * them in the correct order.
90
+ *
91
+ * Subsequent calls to dependencies that have already been resolved are cached, so they will
92
+ * re-use the created singletons instead of re-instantiation.
93
+ */
94
+ declare function _resolve<T>(ctor: Class<T>): T;
95
+ //#endregion
96
+ //#region src/annotation/route.d.ts
97
+ type OptionalStrOrRegExp = string | RegExp | undefined;
98
+ /**
99
+ * Custom annotation that will store all routes inside of a map,
100
+ * which can then be used to initialize all the routes to the router.
101
+ */
102
+ declare function _Route({
103
+ method,
104
+ path
105
+ }: {
106
+ method: ExpressRouterMethodKey;
107
+ path: OptionalStrOrRegExp;
108
+ }): MethodDecorator;
109
+ /**
110
+ * Register GET route on the given path (default "") for the given controller.
111
+ */
112
+ declare const GET: (path?: OptionalStrOrRegExp) => MethodDecorator;
113
+ /**
114
+ * Register POST route on the given path (default "") for the given controller.
115
+ */
116
+ declare const POST: (path?: OptionalStrOrRegExp) => MethodDecorator;
117
+ /**
118
+ * Register PUT route on the given path (default "") for the given controller.
119
+ */
120
+ declare const PUT: (path?: OptionalStrOrRegExp) => MethodDecorator;
121
+ /**
122
+ * Register DELETE route on the given path (default "") for the given controller.
123
+ */
124
+ declare const DELETE: (path?: OptionalStrOrRegExp) => MethodDecorator;
125
+ /**
126
+ * Register OPTIONS route on the given path (default "") for the given controller.
127
+ */
128
+ declare const OPTIONS: (path?: OptionalStrOrRegExp) => MethodDecorator;
129
+ /**
130
+ * Register PATCH route on the given path (default "") for the given controller.
131
+ */
132
+ declare const PATCH: (path?: OptionalStrOrRegExp) => MethodDecorator;
133
+ /**
134
+ * Register HEAD route on the given path (default "") for the given controller.
135
+ */
136
+ declare const HEAD: (path?: OptionalStrOrRegExp) => MethodDecorator;
137
+ /**
138
+ * Register a middleware route on the given path (default "") for the given controller.
139
+ */
140
+ declare const Middleware: (path?: OptionalStrOrRegExp) => MethodDecorator;
141
+ /**
142
+ * Given a class constructor, fetch all the routes attached.
143
+ */
144
+ declare function _getRoutes(ctor: Function): readonly RouteDefinition[];
145
+ //#endregion
146
+ //#region src/annotation/middleware.d.ts
147
+ /**
148
+ * Used to define a middleware-only class.
149
+ *
150
+ * __NOTE:__ `@MiddlewareClass` works exactly the same as `@Controller`. As such, you
151
+ * can still register `@Route` and `@Middleware` methods, though you very well should not
152
+ * for the sake of semantics.
153
+ */
154
+ declare function MiddlewareClass(...args: Parameters<typeof Controller>): ClassDecorator;
155
+ //#endregion
156
+ //#region src/helper/redirect.d.ts
157
+ /**
158
+ * Generic HTTP redirect wrapped modeled after Spring's `RedirectView`.
159
+ *
160
+ * You can either return `new RedirectView(url)` or `RedirectView.redirect(url)` inside of a controller method.
161
+ */
162
+ declare class RedirectView {
163
+ _url: string;
164
+ constructor(url: string);
165
+ getUrl(): string;
166
+ /**
167
+ * Instantiate `RedirectView` with the given `url`.
168
+ */
169
+ static redirect(url: string): RedirectView;
170
+ }
171
+ //#endregion
172
+ //#region src/helper/response.d.ts
173
+ /**
174
+ * Generic HTTP response wrapper modeled after Spring's `ResponseEntity`.
175
+ *
176
+ * Provides status code, headers, and an optional typed body.
177
+ * The body is serialized through `Sapling.serialize`.
178
+ *
179
+ * @typeParam T - the type of the response body
180
+ */
181
+ declare class ResponseEntity<T> {
182
+ private readonly _statusCode;
183
+ private readonly _headers;
184
+ private readonly _body;
185
+ constructor(body: T, headers?: HttpHeaders, statusCode?: number);
186
+ /**
187
+ * Create a builder with a 200 status code.
188
+ *
189
+ * @example```ts
190
+ * return ResponseEntity.ok().body({ success: true });
191
+ * ```
192
+ */
193
+ static ok(): ResponseEntityBuilder;
194
+ /**
195
+ * Create a builder with a custom status code.
196
+ *
197
+ * @example```ts
198
+ * return ResponseEntity.status(HttpStatus.BAD_REQUEST).body({ success: false });
199
+ * ```
200
+ *
201
+ * @see {@link HttpStatus}
202
+ */
203
+ static status(statusCode: number): ResponseEntityBuilder;
204
+ /**
205
+ * Return status code.
206
+ */
207
+ getStatusCode(): number;
208
+ /**
209
+ * Return headers.
210
+ */
211
+ getHeaders(): HttpHeaders;
212
+ /**
213
+ * Return the response body.
214
+ */
215
+ getBody(): T;
216
+ }
217
+ /**
218
+ * Builder for {@link ResponseEntity}.
219
+ *
220
+ * Forces the status code to be set first, then headers and body,
221
+ * ensuring type safety when constructing the response.
222
+ */
223
+ declare class ResponseEntityBuilder {
224
+ private readonly _statusCode;
225
+ private _headers;
226
+ constructor(statusCode: number);
227
+ /**
228
+ * Set all headers as an object with keys and values.
229
+ */
230
+ headers(headers: HttpHeaders): this;
231
+ /**
232
+ * Add/override a single key and value to the headers.
233
+ */
234
+ setHeader(key: string, value: string): this;
235
+ /**
236
+ * Set the response body.
237
+ */
238
+ body<T>(body: T): ResponseEntity<T>;
239
+ }
240
+ //#endregion
241
+ //#region src/enum/http.d.ts
242
+ /**
243
+ * Enum of every valid HTTP status code mapped to a specific enum member.
244
+ *
245
+ * @see {@link ResponseEntity}
246
+ */
247
+ declare enum HttpStatus {
248
+ CONTINUE = 100,
249
+ SWITCHING_PROTOCOLS = 101,
250
+ PROCESSING = 102,
251
+ EARLY_HINTS = 103,
252
+ OK = 200,
253
+ CREATED = 201,
254
+ ACCEPTED = 202,
255
+ NON_AUTHORITATIVE_INFORMATION = 203,
256
+ NO_CONTENT = 204,
257
+ RESET_CONTENT = 205,
258
+ PARTIAL_CONTENT = 206,
259
+ MULTI_STATUS = 207,
260
+ ALREADY_REPORTED = 208,
261
+ IM_USED = 226,
262
+ MULTIPLE_CHOICES = 300,
263
+ MOVED_PERMANENTLY = 301,
264
+ FOUND = 302,
265
+ SEE_OTHER = 303,
266
+ NOT_MODIFIED = 304,
267
+ TEMPORARY_REDIRECT = 307,
268
+ PERMANENT_REDIRECT = 308,
269
+ BAD_REQUEST = 400,
270
+ UNAUTHORIZED = 401,
271
+ PAYMENT_REQUIRED = 402,
272
+ FORBIDDEN = 403,
273
+ NOT_FOUND = 404,
274
+ METHOD_NOT_ALLOWED = 405,
275
+ NOT_ACCEPTABLE = 406,
276
+ PROXY_AUTHENTICATION_REQUIRED = 407,
277
+ REQUEST_TIMEOUT = 408,
278
+ CONFLICT = 409,
279
+ GONE = 410,
280
+ LENGTH_REQUIRED = 411,
281
+ PRECONDITION_FAILED = 412,
282
+ PAYLOAD_TOO_LARGE = 413,
283
+ URI_TOO_LONG = 414,
284
+ UNSUPPORTED_MEDIA_TYPE = 415,
285
+ REQUESTED_RANGE_NOT_SATISFIABLE = 416,
286
+ EXPECTATION_FAILED = 417,
287
+ I_AM_A_TEAPOT = 418,
288
+ UNPROCESSABLE_ENTITY = 422,
289
+ LOCKED = 423,
290
+ FAILED_DEPENDENCY = 424,
291
+ TOO_EARLY = 425,
292
+ UPGRADE_REQUIRED = 426,
293
+ PRECONDITION_REQUIRED = 428,
294
+ TOO_MANY_REQUESTS = 429,
295
+ REQUEST_HEADER_FIELDS_TOO_LARGE = 431,
296
+ UNAVAILABLE_FOR_LEGAL_REASONS = 451,
297
+ INTERNAL_SERVER_ERROR = 500,
298
+ NOT_IMPLEMENTED = 501,
299
+ BAD_GATEWAY = 502,
300
+ SERVICE_UNAVAILABLE = 503,
301
+ GATEWAY_TIMEOUT = 504,
302
+ HTTP_VERSION_NOT_SUPPORTED = 505,
303
+ VARIANT_ALSO_NEGOTIATES = 506,
304
+ INSUFFICIENT_STORAGE = 507,
305
+ LOOP_DETECTED = 508,
306
+ BANDWIDTH_LIMIT_EXCEEDED = 509,
307
+ NOT_EXTENDED = 510,
308
+ NETWORK_AUTHENTICATION_REQUIRED = 511
309
+ }
310
+ //#endregion
311
+ //#region src/helper/error.d.ts
312
+ /**
313
+ * Ensure that you define a middleware that can handle this error.
314
+ *
315
+ * @see {@link Sapling.loadResponseStatusErrorMiddleware}
316
+ */
317
+ declare class ResponseStatusError extends Error {
318
+ readonly status: HttpStatus;
319
+ constructor(status: HttpStatus, message?: string);
320
+ }
321
+ //#endregion
322
+ //#region src/helper/sapling.d.ts
323
+ /**
324
+ * Collection of utility functions which are essential for Sapling to function.
325
+ */
326
+ declare class Sapling {
327
+ /**
328
+ * If you would prefer to manually resolve your controllers instead, call resolve
329
+ * on the controller class.
330
+ *
331
+ * @example```ts
332
+ * import { Sapling } from "@tahminator/sapling";
333
+ * import TestController from "./path/to/test.controller";
334
+ *
335
+ * const app = express();
336
+ *
337
+ * const router = Sapling.resolve(TestController);
338
+ * app.use(router);
339
+ * ```
340
+ */
341
+ static resolve<TClass>(this: void, clazz: Class<TClass>): Router;
342
+ /**
343
+ * Register this function as a middleware in order to utilize Sapling's `deserialize` function.
344
+ *
345
+ * @example```ts
346
+ * import { Sapling } from "@tahminator/sapling";
347
+ * import express from "express";
348
+ *
349
+ * const app = express();
350
+ *
351
+ * app.use(Sapling.json());
352
+ * ```
353
+ */
354
+ static json(this: void): ExpressMiddlewareFn;
355
+ /**
356
+ * Register your application with all the necessary middlewares and logics for Sapling to function.
357
+ *
358
+ * @example```ts
359
+ * import { Sapling } from "@tahminator/sapling";
360
+ * import express from "express";
361
+ *
362
+ * const app = express();
363
+ *
364
+ * app.registerApp(app);
365
+ * ```
366
+ */
367
+ static registerApp(app: e.Express): void;
368
+ /**
369
+ * Register a middleware that will handle {@link ResponseStatusError}.
370
+ *
371
+ * This middleware will chain to the next middleware if it does not catch {@link ResponseStatusError}.
372
+ * You may still define middleware to handle all other errors in a separate `app.use` call.
373
+ *
374
+ * @example
375
+ * ```ts
376
+ * import express from "express";
377
+ * import { Sapling } from "@tahminator/sapling";
378
+ *
379
+ * const app = express();
380
+ *
381
+ * Sapling.loadResponseStatusErrorMiddleware(app, (err, req, res, next) => {
382
+ * // `err` is guaranteed to be of type ResponseStatusError
383
+ * res.status(err.status).json({
384
+ * success: false,
385
+ * message: err.message,
386
+ * });
387
+ * });
388
+ * ```
389
+ */
390
+ static loadResponseStatusErrorMiddleware(this: void, app: e.Express, fn: (err: ResponseStatusError, request: e.Request, response: e.Response, next: e.NextFunction) => void): void;
391
+ /**
392
+ * Serialize a value into a JSON string.
393
+ *
394
+ * This function is used in {@link ResponseEntity} to serialize the `body`.
395
+ *
396
+ * Use `setSerializeFn` to override underlying implementation.
397
+ *
398
+ * @defaultValue `JSON.stringify`
399
+ */
400
+ static serialize(this: void, value: any): string;
401
+ /**
402
+ * Replace the function used for `serialize`.
403
+ */
404
+ static setSerializeFn(this: void, fn: (value: any) => string): void;
405
+ /**
406
+ * De-serialize a JSON string back to a JavaScript object.
407
+ *
408
+ * This function is used to de-serialize a string into a `body`.
409
+ *
410
+ * Use `setDeserializeFn` to override underlying implementation.
411
+ *
412
+ * @defaultValue `JSON.parse`
413
+ */
414
+ static deserialize<T = any>(this: void, value: string): T;
415
+ /**
416
+ * Replace the function used for `deserialize`
417
+ */
418
+ static setDeserializeFn(this: void, fn: (value: string) => any): void;
419
+ }
420
+ //#endregion
421
+ export { Class, Controller, DELETE, ExpressMiddlewareFn, ExpressRouterMethodKey, ExpressRouterMethods, GET, HEAD, Html404ErrorPage, HttpHeaders, HttpStatus, Injectable, Middleware, MiddlewareClass, OPTIONS, PATCH, POST, PUT, RedirectView, ResponseEntity, ResponseEntityBuilder, ResponseStatusError, RouteDefinition, Sapling, _ControllerRegistry, _InjectableDeps, _InjectableRegistry, _Route, _getRoutes, _resolve, methodResolve };