raiton 6.1.0-beta.4 → 6.1.1-beta.5

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.
@@ -4817,7 +4817,7 @@ var {
4817
4817
  Help
4818
4818
  } = import__.default;
4819
4819
  // package.json
4820
- var version = "6.1.0-beta.4";
4820
+ var version = "6.1.1-beta.5";
4821
4821
 
4822
4822
  // source/bin/cli.ts
4823
4823
  var CLI = new Command;
@@ -6362,6 +6362,21 @@ class ThrowableResponse extends Error {
6362
6362
  };
6363
6363
  }
6364
6364
  }
6365
+ // source/framework/responses/helpers.ts
6366
+ function RaitonResponses(message, data, statusCode, metadata) {
6367
+ if (metadata) {
6368
+ if (metadata.error === undefined) {
6369
+ metadata.error = typeof data?.error === "boolean" ? data?.error : false;
6370
+ }
6371
+ metadata.errorStack = metadata.errorStack instanceof Error ? metadata.errorStack : Raiton.thread?.builder?.options?.serve ? Array.isArray(metadata.errorStack) ? metadata.errorStack : [] : undefined;
6372
+ }
6373
+ return {
6374
+ ...metadata,
6375
+ statusCode,
6376
+ message,
6377
+ data
6378
+ };
6379
+ }
6365
6380
  // source/core/controller/metadata.ts
6366
6381
  var import_reflect_metadata = __toESM(require_Reflect2(), 1);
6367
6382
  function getControllerMetadata(target) {
@@ -7280,8 +7295,7 @@ var secureRateLimit = (opts = {}) => definePlugin((scope2) => {
7280
7295
  entry.count++;
7281
7296
  hits.set(ip, entry);
7282
7297
  if (entry.count > max) {
7283
- context.reply.status(429);
7284
- return context.send({ error: "Too many requests" });
7298
+ return context.send(RaitonResponses("Too many requests", null, 429 /* TOO_MANY_REQUESTS */, { error: true }));
7285
7299
  }
7286
7300
  await next();
7287
7301
  });
@@ -7292,8 +7306,7 @@ var secureBodyLimit = (maxBytes = 1e6) => definePlugin((scope2) => {
7292
7306
  scope2.use(async ({ context, next }) => {
7293
7307
  const len = Number(context.req.headers.get("content-length") ?? 0);
7294
7308
  if (len > maxBytes) {
7295
- context.reply.status(413);
7296
- return context.send({ error: "Payload too large" });
7309
+ return context.send(RaitonResponses("Payload too large", null, 413 /* PAYLOAD_TOO_LARGE */, { error: true }));
7297
7310
  }
7298
7311
  await next();
7299
7312
  });
@@ -7303,8 +7316,7 @@ var secureBodyLimit = (maxBytes = 1e6) => definePlugin((scope2) => {
7303
7316
  var secureMethodGuard = (allowed) => definePlugin((scope2) => {
7304
7317
  scope2.use(async ({ context, next }) => {
7305
7318
  if (!allowed.includes(context.req.method)) {
7306
- context.reply.status(405);
7307
- return context.send({ error: "Method not allowed" });
7319
+ return context.send(RaitonResponses("Method not allowed", null, 405 /* METHOD_NOT_ALLOWED */, { error: true }));
7308
7320
  }
7309
7321
  await next();
7310
7322
  });
@@ -7675,7 +7687,7 @@ class Application {
7675
7687
  Logger.warn(`Request out of application pathname: ${pathname} (expected prefix: ${this.config.pathname})`);
7676
7688
  }
7677
7689
  reply2.status(404);
7678
- return reply2.send({ error: false, statusCode: 404 });
7690
+ return reply2.send(RaitonResponses("Not Found", null, 404 /* NOT_FOUND */, { error: false }));
7679
7691
  }
7680
7692
  }
7681
7693
  const route2 = this.root.router.match(req2.method, pathname);
@@ -7684,7 +7696,7 @@ class Application {
7684
7696
  Logger.warn(`Route not found: ${req2.method} ${pathname}`);
7685
7697
  }
7686
7698
  reply2.status(404);
7687
- return reply2.send({ error: false, statusCode: 404 });
7699
+ return reply2.send(RaitonResponses("Not Found", null, 404 /* NOT_FOUND */, { error: false }));
7688
7700
  }
7689
7701
  try {
7690
7702
  context.params = route2.parameters;
@@ -7703,7 +7715,7 @@ class Application {
7703
7715
  console.error(e);
7704
7716
  }
7705
7717
  context.reply.status(500);
7706
- context.reply.send({ error: true, statusCode: 500, data: null });
7718
+ context.reply.send(RaitonResponses("Internal Server Error", null, 500 /* INTERNAL_SERVER_ERROR */, { error: true }));
7707
7719
  }
7708
7720
  };
7709
7721
  try {
@@ -7716,12 +7728,7 @@ class Application {
7716
7728
  ctx.reply.status(err.statusCode ?? 500);
7717
7729
  return ctx.reply.send(err.render());
7718
7730
  }
7719
- ctx.reply.send({
7720
- statusCode: 500,
7721
- error: true,
7722
- message: err.message ?? err,
7723
- data: null
7724
- });
7731
+ ctx.reply.send(RaitonResponses(err.message ?? err, null, 500 /* INTERNAL_SERVER_ERROR */, { error: true }));
7725
7732
  }
7726
7733
  }
7727
7734
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "raiton",
3
- "version": "6.1.0-beta.4",
3
+ "version": "6.1.1-beta.5",
4
4
  "private": false,
5
5
  "type": "module",
6
6
  "description": "Modular TypeScript Backend Framework with Dependency Injection, Plugins, Controllers, Middleware and Hot Reloading.",
@@ -2,7 +2,7 @@ import {Security, bodyParserPlugin} from "../framework/plugins";
2
2
  import {PluginScope} from './plugins/scope'
3
3
  import {RequestContext} from './context'
4
4
  import {ApplicationConfigInterface, ApplicationInterface} from "../types/application";
5
- import {HttpException, HttpMethod, ThrowableResponse} from "../framework";
5
+ import {HttpException, HttpMethod, ThrowableResponse, RaitonResponses, HttpStatus} from "../framework";
6
6
  import {RouteHandlerCallable} from "../types";
7
7
  import {Logger} from "@protorians/logger";
8
8
  import {RaitonConfig} from "./config";
@@ -141,7 +141,7 @@ export class Application implements ApplicationInterface {
141
141
  Logger.warn(`Request out of application pathname: ${pathname} (expected prefix: ${this.config.pathname})`)
142
142
  }
143
143
  reply.status(404)
144
- return reply.send({error: false, statusCode: 404})
144
+ return reply.send(RaitonResponses('Not Found', null, HttpStatus.NOT_FOUND, {error: false}))
145
145
  }
146
146
  }
147
147
 
@@ -155,7 +155,7 @@ export class Application implements ApplicationInterface {
155
155
  Logger.warn(`Route not found: ${req.method} ${pathname}`)
156
156
  }
157
157
  reply.status(404)
158
- return reply.send({error: false, statusCode: 404})
158
+ return reply.send(RaitonResponses('Not Found', null, HttpStatus.NOT_FOUND, {error: false}))
159
159
  }
160
160
 
161
161
  try {
@@ -177,7 +177,7 @@ export class Application implements ApplicationInterface {
177
177
  console.error(e)
178
178
  }
179
179
  context.reply.status(500)
180
- context.reply.send({error: true, statusCode: 500, data: null})
180
+ context.reply.send(RaitonResponses('Internal Server Error', null, HttpStatus.INTERNAL_SERVER_ERROR, {error: true}))
181
181
  }
182
182
  }
183
183
 
@@ -191,12 +191,7 @@ export class Application implements ApplicationInterface {
191
191
  ctx.reply.status(err.statusCode ?? 500)
192
192
  return ctx.reply.send(err.render())
193
193
  }
194
- ctx.reply.send({
195
- statusCode: 500,
196
- error: true,
197
- message: err.message ?? err,
198
- data: null
199
- })
194
+ ctx.reply.send(RaitonResponses(err.message ?? err, null, HttpStatus.INTERNAL_SERVER_ERROR, {error: true}))
200
195
  }
201
196
  }
202
197
  }
@@ -4,6 +4,7 @@ import {Raiton} from "..";
4
4
  import {HttpException} from "../../framework/exceptions";
5
5
  import {ThrowableResponse} from "../../framework/responses/http-throwable";
6
6
  import {collectRouteArguments, validateDtoArguments, validateResponse, runMiddlewares} from "./handler.utils";
7
+ import {RaitonResponses, HttpStatus} from "../../framework";
7
8
 
8
9
  export function createHandler(
9
10
  instance: any,
@@ -33,16 +34,18 @@ export function createHandler(
33
34
  return err.render()
34
35
  }
35
36
 
36
- return {
37
- statusCode: 500,
38
- error: true,
39
- message: err.message ?? err,
40
- data: null,
41
- stack: isDevelopment
42
- ? (typeof err.stack === 'string' ? err.stack.split('\n')
43
- : [String(err.stack || err.toString() || err.message || err.name || 'Unknown error')])
44
- .map((l: any) => typeof l === 'string' ? l.trim() : String(l)) : undefined
45
- };
37
+ return RaitonResponses(
38
+ err.message ?? err,
39
+ null,
40
+ HttpStatus.INTERNAL_SERVER_ERROR,
41
+ {
42
+ error: true,
43
+ errorStack: isDevelopment
44
+ ? (typeof err.stack === 'string' ? err.stack.split('\n')
45
+ : [String(err.stack || err.toString() || err.message || err.name || 'Unknown error')])
46
+ .map((l: any) => typeof l === 'string' ? l.trim() : String(l)) : undefined
47
+ }
48
+ );
46
49
  }
47
50
  };
48
51
 
@@ -1,7 +1,7 @@
1
1
  import {GuardOptions} from "../../types";
2
2
  import {Middleware} from "./middleware.decorator";
3
3
  import {RaitonGuards} from "../../core/guards";
4
- import {HttpStatus} from "../enums";
4
+ import {HttpStatus, RaitonResponses} from "..";
5
5
 
6
6
  export function createGuardDecoration({name, handler}: GuardOptions) {
7
7
  return Middleware(async ({next, context}) => {
@@ -15,9 +15,6 @@ export function createGuardDecoration({name, handler}: GuardOptions) {
15
15
  if (response) return next()
16
16
 
17
17
  context.reply.status(HttpStatus.FORBIDDEN)
18
- return context.reply.send({
19
- error: true,
20
- message: 'Forbidden'
21
- });
18
+ return context.reply.send(RaitonResponses('Forbidden', null, HttpStatus.FORBIDDEN, {error: true}));
22
19
  });
23
20
  }
@@ -1,5 +1,6 @@
1
1
  import {definePlugin} from "../../../core/plugins";
2
2
  import {MiddlewareParametersInterface} from "../../../types";
3
+ import {RaitonResponses, HttpStatus} from "../..";
3
4
 
4
5
 
5
6
  export const secureBodyLimit = (maxBytes = 1_000_000) =>
@@ -10,8 +11,7 @@ export const secureBodyLimit = (maxBytes = 1_000_000) =>
10
11
  )
11
12
 
12
13
  if (len > maxBytes) {
13
- context.reply.status(413)
14
- return context.send({ error: 'Payload too large' })
14
+ return context.send(RaitonResponses('Payload too large', null, HttpStatus.PAYLOAD_TOO_LARGE, {error: true}))
15
15
  }
16
16
 
17
17
  await next()
@@ -1,13 +1,13 @@
1
1
  import {definePlugin} from "../../../core/plugins";
2
2
  import {ContextInterface, MiddlewareParametersInterface, MiddlewareNextCallable} from "../../../types";
3
+ import {RaitonResponses, HttpStatus} from "../..";
3
4
 
4
5
 
5
6
  export const secureMethodGuard = (allowed: string[]) =>
6
7
  definePlugin((scope) => {
7
8
  scope.use(async ({context, next}: MiddlewareParametersInterface) => {
8
9
  if (!allowed.includes(context.req.method)) {
9
- context.reply.status(405)
10
- return context.send({ error: 'Method not allowed' })
10
+ return context.send(RaitonResponses('Method not allowed', null, HttpStatus.METHOD_NOT_ALLOWED, {error: true}))
11
11
  }
12
12
  await next()
13
13
  })
@@ -1,5 +1,6 @@
1
1
  import {definePlugin} from "../../../core/plugins";
2
2
  import {ContextInterface, MiddlewareParametersInterface, MiddlewareNextCallable} from "../../../types";
3
+ import {RaitonResponses, HttpStatus} from "../..";
3
4
 
4
5
 
5
6
  export interface RateLimitOptions {
@@ -33,8 +34,7 @@ export const secureRateLimit = (
33
34
  hits.set(ip, entry)
34
35
 
35
36
  if (entry.count > max) {
36
- context.reply.status(429)
37
- return context.send({error: 'Too many requests'})
37
+ return context.send(RaitonResponses('Too many requests', null, HttpStatus.TOO_MANY_REQUESTS, {error: true}))
38
38
  }
39
39
 
40
40
  await next()
@@ -12,7 +12,9 @@ export function RaitonResponses(
12
12
  ) {
13
13
 
14
14
  if (metadata) {
15
- metadata.error = typeof data?.error === 'boolean' ? data?.error : false
15
+ if (metadata.error === undefined) {
16
+ metadata.error = typeof data?.error === 'boolean' ? data?.error : false
17
+ }
16
18
  metadata.errorStack = (metadata.errorStack instanceof Error)
17
19
  ? metadata.errorStack
18
20
  : (Raiton.thread?.builder?.options?.serve
@@ -6,6 +6,7 @@ import {Logger} from "@protorians/logger";
6
6
  import {getRealIp} from "../../utilities";
7
7
  import {findSocketForPath} from "../../../core/socket";
8
8
  import {Injection} from "../../../core/injection";
9
+ import {RaitonResponses, HttpStatus} from "../..";
9
10
 
10
11
  export const bunRuntime: RuntimeAdapterInterface = {
11
12
  createServer(handler, options?: RuntimeServerOptionsInterface) {
@@ -50,11 +51,11 @@ export const bunRuntime: RuntimeAdapterInterface = {
50
51
  : entry.metadata.events.find((item: any) => item.type === 'message')
51
52
 
52
53
  if (!event) {
53
- ws.send(JSON.stringify({
54
- statusCode: 404,
55
- message: `Socket event "${eventName ?? 'message'}" not found`,
56
- data: null
57
- }))
54
+ ws.send(JSON.stringify(RaitonResponses(
55
+ `Socket event "${eventName ?? 'message'}" not found`,
56
+ null,
57
+ HttpStatus.NOT_FOUND
58
+ )))
58
59
  return
59
60
  }
60
61
 
@@ -64,11 +65,11 @@ export const bunRuntime: RuntimeAdapterInterface = {
64
65
  }
65
66
  } catch (e: any) {
66
67
  Logger.error('Socket message failed', e.message ?? e)
67
- ws.send(JSON.stringify({
68
- statusCode: 500,
69
- message: 'Internal server error',
70
- data: null
71
- }))
68
+ ws.send(JSON.stringify(RaitonResponses(
69
+ 'Internal server error',
70
+ null,
71
+ HttpStatus.INTERNAL_SERVER_ERROR
72
+ )))
72
73
  }
73
74
  },
74
75
  async close(ws: any) {