@arkyn/server 1.4.32 → 1.4.34

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 (40) hide show
  1. package/dist/helpers/globalErrorHandler.d.ts.map +1 -1
  2. package/dist/helpers/globalErrorHandler.js +24 -3
  3. package/dist/httpBadResponses/badRequest.d.ts +1 -1
  4. package/dist/httpBadResponses/badRequest.d.ts.map +1 -1
  5. package/dist/httpBadResponses/badRequest.js +0 -1
  6. package/dist/httpBadResponses/conflict.d.ts +1 -1
  7. package/dist/httpBadResponses/conflict.d.ts.map +1 -1
  8. package/dist/httpBadResponses/conflict.js +0 -1
  9. package/dist/httpBadResponses/forbidden.d.ts +1 -1
  10. package/dist/httpBadResponses/forbidden.d.ts.map +1 -1
  11. package/dist/httpBadResponses/forbidden.js +0 -1
  12. package/dist/httpBadResponses/notFound.d.ts +1 -1
  13. package/dist/httpBadResponses/notFound.d.ts.map +1 -1
  14. package/dist/httpBadResponses/notFound.js +0 -1
  15. package/dist/httpBadResponses/unauthorized.d.ts +1 -1
  16. package/dist/httpBadResponses/unauthorized.d.ts.map +1 -1
  17. package/dist/httpBadResponses/unauthorized.js +0 -1
  18. package/dist/httpResponses/created.d.ts +5 -2
  19. package/dist/httpResponses/created.d.ts.map +1 -1
  20. package/dist/httpResponses/created.js +9 -10
  21. package/dist/httpResponses/noContent.d.ts +4 -2
  22. package/dist/httpResponses/noContent.d.ts.map +1 -1
  23. package/dist/httpResponses/noContent.js +7 -3
  24. package/dist/httpResponses/success.d.ts +5 -2
  25. package/dist/httpResponses/success.d.ts.map +1 -1
  26. package/dist/httpResponses/success.js +9 -10
  27. package/dist/httpResponses/updated.d.ts +5 -2
  28. package/dist/httpResponses/updated.d.ts.map +1 -1
  29. package/dist/httpResponses/updated.js +9 -10
  30. package/package.json +1 -1
  31. package/src/helpers/globalErrorHandler.ts +29 -5
  32. package/src/httpBadResponses/badRequest.ts +1 -2
  33. package/src/httpBadResponses/conflict.ts +1 -2
  34. package/src/httpBadResponses/forbidden.ts +1 -2
  35. package/src/httpBadResponses/notFound.ts +1 -2
  36. package/src/httpBadResponses/unauthorized.ts +1 -2
  37. package/src/httpResponses/created.ts +11 -10
  38. package/src/httpResponses/noContent.ts +9 -3
  39. package/src/httpResponses/success.ts +11 -10
  40. package/src/httpResponses/updated.ts +11 -10
@@ -1 +1 @@
1
- {"version":3,"file":"globalErrorHandler.d.ts","sourceRoot":"","sources":["../../src/helpers/globalErrorHandler.ts"],"names":[],"mappings":"AAcA,QAAA,MAAM,kBAAkB,UAAW,GAAG,oCAuBrC,CAAC;AAEF,OAAO,EAAE,kBAAkB,EAAE,CAAC"}
1
+ {"version":3,"file":"globalErrorHandler.d.ts","sourceRoot":"","sources":["../../src/helpers/globalErrorHandler.ts"],"names":[],"mappings":"AAkBA,QAAA,MAAM,kBAAkB,UAAW,GAAG,oCA2CrC,CAAC;AAEF,OAAO,EAAE,kBAAkB,EAAE,CAAC"}
@@ -3,11 +3,32 @@ import { conflict, ConflictError } from "../httpBadResponses/conflict";
3
3
  import { forbidden, ForbiddenError } from "../httpBadResponses/forbidden";
4
4
  import { notFound, NotFoundError } from "../httpBadResponses/notFound";
5
5
  import { serverError } from "../httpBadResponses/serverError";
6
- import { UnprocessableEntityError, unprocessableEntity, } from "../httpBadResponses/unprocessableEntity";
7
6
  import { unauthorized, UnauthorizedError, } from "../httpBadResponses/unauthorized";
7
+ import { unprocessableEntity, UnprocessableEntityError, } from "../httpBadResponses/unprocessableEntity";
8
+ import { created, Created } from "../httpResponses/created";
9
+ import { noContent, NoContent } from "../httpResponses/noContent";
10
+ import { success, Success } from "../httpResponses/success";
11
+ import { updated, Updated } from "../httpResponses/updated";
8
12
  const globalErrorHandler = (error) => {
9
- if (error instanceof Response)
10
- throw error;
13
+ // If the error is not an instance of Error, return the response
14
+ switch (true) {
15
+ case error instanceof Response:
16
+ return error;
17
+ case error instanceof Created:
18
+ return created(error.body, error.init);
19
+ case error instanceof Updated:
20
+ return updated(error.body, error.init);
21
+ case error instanceof Success:
22
+ return success(error.body, error.init);
23
+ case error instanceof NoContent:
24
+ return noContent(error.init);
25
+ }
26
+ const showConsoleError = process.env.NODE_ENV === "development" ||
27
+ process.env?.SHOW_ERRORS_IN_CONSOLE === "true";
28
+ // If showConsoleError is true, log the error to the console
29
+ if (showConsoleError)
30
+ console.error(error);
31
+ // If the error is an instance of BadRequestError, ForbiddenError, ConflictError, UnauthorizedError, NotFoundError, or UnprocessableEntityError, return the error
11
32
  switch (true) {
12
33
  case error instanceof BadRequestError:
13
34
  return badRequest(error);
@@ -1,4 +1,4 @@
1
- declare function badRequest(error: Error): import("undici-types").Response;
1
+ declare function badRequest(error: BadRequestError): import("undici-types").Response;
2
2
  declare class BadRequestError {
3
3
  name: string;
4
4
  message: string;
@@ -1 +1 @@
1
- {"version":3,"file":"badRequest.d.ts","sourceRoot":"","sources":["../../src/httpBadResponses/badRequest.ts"],"names":[],"mappings":"AAAA,iBAAS,UAAU,CAAC,KAAK,EAAE,KAAK,mCAW/B;AAED,cAAM,eAAe;IACnB,IAAI,EAAE,MAAM,CAAC;IACb,OAAO,EAAE,MAAM,CAAC;gBAEJ,OAAO,EAAE,MAAM;CAI5B;AAED,OAAO,EAAE,UAAU,EAAE,eAAe,EAAE,CAAC"}
1
+ {"version":3,"file":"badRequest.d.ts","sourceRoot":"","sources":["../../src/httpBadResponses/badRequest.ts"],"names":[],"mappings":"AAAA,iBAAS,UAAU,CAAC,KAAK,EAAE,eAAe,mCAUzC;AAED,cAAM,eAAe;IACnB,IAAI,EAAE,MAAM,CAAC;IACb,OAAO,EAAE,MAAM,CAAC;gBAEJ,OAAO,EAAE,MAAM;CAI5B;AAED,OAAO,EAAE,UAAU,EAAE,eAAe,EAAE,CAAC"}
@@ -4,7 +4,6 @@ function badRequest(error) {
4
4
  success: false,
5
5
  name: error.name,
6
6
  message: error.message,
7
- cause: error.cause,
8
7
  }, { status: 400 });
9
8
  }
10
9
  class BadRequestError {
@@ -1,4 +1,4 @@
1
- declare function conflict(error: Error): import("undici-types").Response;
1
+ declare function conflict(error: ConflictError): import("undici-types").Response;
2
2
  declare class ConflictError {
3
3
  name: string;
4
4
  message: string;
@@ -1 +1 @@
1
- {"version":3,"file":"conflict.d.ts","sourceRoot":"","sources":["../../src/httpBadResponses/conflict.ts"],"names":[],"mappings":"AAAA,iBAAS,QAAQ,CAAC,KAAK,EAAE,KAAK,mCAW7B;AAED,cAAM,aAAa;IACjB,IAAI,EAAE,MAAM,CAAC;IACb,OAAO,EAAE,MAAM,CAAC;gBAEJ,OAAO,EAAE,MAAM;CAI5B;AAED,OAAO,EAAE,QAAQ,EAAE,aAAa,EAAE,CAAC"}
1
+ {"version":3,"file":"conflict.d.ts","sourceRoot":"","sources":["../../src/httpBadResponses/conflict.ts"],"names":[],"mappings":"AAAA,iBAAS,QAAQ,CAAC,KAAK,EAAE,aAAa,mCAUrC;AAED,cAAM,aAAa;IACjB,IAAI,EAAE,MAAM,CAAC;IACb,OAAO,EAAE,MAAM,CAAC;gBAEJ,OAAO,EAAE,MAAM;CAI5B;AAED,OAAO,EAAE,QAAQ,EAAE,aAAa,EAAE,CAAC"}
@@ -4,7 +4,6 @@ function conflict(error) {
4
4
  success: false,
5
5
  name: error.name,
6
6
  message: error.message,
7
- cause: error.cause,
8
7
  }, { status: 409 });
9
8
  }
10
9
  class ConflictError {
@@ -1,4 +1,4 @@
1
- declare function forbidden(error: Error): import("undici-types").Response;
1
+ declare function forbidden(error: ForbiddenError): import("undici-types").Response;
2
2
  declare class ForbiddenError {
3
3
  name: string;
4
4
  message: string;
@@ -1 +1 @@
1
- {"version":3,"file":"forbidden.d.ts","sourceRoot":"","sources":["../../src/httpBadResponses/forbidden.ts"],"names":[],"mappings":"AAAA,iBAAS,SAAS,CAAC,KAAK,EAAE,KAAK,mCAW9B;AAED,cAAM,cAAc;IAClB,IAAI,EAAE,MAAM,CAAC;IACb,OAAO,EAAE,MAAM,CAAC;gBAEJ,OAAO,EAAE,MAAM;CAI5B;AAED,OAAO,EAAE,SAAS,EAAE,cAAc,EAAE,CAAC"}
1
+ {"version":3,"file":"forbidden.d.ts","sourceRoot":"","sources":["../../src/httpBadResponses/forbidden.ts"],"names":[],"mappings":"AAAA,iBAAS,SAAS,CAAC,KAAK,EAAE,cAAc,mCAUvC;AAED,cAAM,cAAc;IAClB,IAAI,EAAE,MAAM,CAAC;IACb,OAAO,EAAE,MAAM,CAAC;gBAEJ,OAAO,EAAE,MAAM;CAI5B;AAED,OAAO,EAAE,SAAS,EAAE,cAAc,EAAE,CAAC"}
@@ -4,7 +4,6 @@ function forbidden(error) {
4
4
  success: false,
5
5
  name: error.name,
6
6
  message: error.message,
7
- cause: error.cause,
8
7
  }, { status: 403 });
9
8
  }
10
9
  class ForbiddenError {
@@ -1,4 +1,4 @@
1
- declare function notFound(error: Error): import("undici-types").Response;
1
+ declare function notFound(error: NotFoundError): import("undici-types").Response;
2
2
  declare class NotFoundError {
3
3
  name: string;
4
4
  message: string;
@@ -1 +1 @@
1
- {"version":3,"file":"notFound.d.ts","sourceRoot":"","sources":["../../src/httpBadResponses/notFound.ts"],"names":[],"mappings":"AAAA,iBAAS,QAAQ,CAAC,KAAK,EAAE,KAAK,mCAW7B;AAED,cAAM,aAAa;IACjB,IAAI,EAAE,MAAM,CAAC;IACb,OAAO,EAAE,MAAM,CAAC;gBAEJ,OAAO,EAAE,MAAM;CAI5B;AAED,OAAO,EAAE,QAAQ,EAAE,aAAa,EAAE,CAAC"}
1
+ {"version":3,"file":"notFound.d.ts","sourceRoot":"","sources":["../../src/httpBadResponses/notFound.ts"],"names":[],"mappings":"AAAA,iBAAS,QAAQ,CAAC,KAAK,EAAE,aAAa,mCAUrC;AAED,cAAM,aAAa;IACjB,IAAI,EAAE,MAAM,CAAC;IACb,OAAO,EAAE,MAAM,CAAC;gBAEJ,OAAO,EAAE,MAAM;CAI5B;AAED,OAAO,EAAE,QAAQ,EAAE,aAAa,EAAE,CAAC"}
@@ -4,7 +4,6 @@ function notFound(error) {
4
4
  success: false,
5
5
  name: error.name,
6
6
  message: error.message,
7
- cause: error.cause,
8
7
  }, { status: 404 });
9
8
  }
10
9
  class NotFoundError {
@@ -1,4 +1,4 @@
1
- declare function unauthorized(error: Error): import("undici-types").Response;
1
+ declare function unauthorized(error: UnauthorizedError): import("undici-types").Response;
2
2
  declare class UnauthorizedError {
3
3
  name: string;
4
4
  message: string;
@@ -1 +1 @@
1
- {"version":3,"file":"unauthorized.d.ts","sourceRoot":"","sources":["../../src/httpBadResponses/unauthorized.ts"],"names":[],"mappings":"AAAA,iBAAS,YAAY,CAAC,KAAK,EAAE,KAAK,mCAWjC;AAED,cAAM,iBAAiB;IACrB,IAAI,EAAE,MAAM,CAAC;IACb,OAAO,EAAE,MAAM,CAAC;gBAEJ,OAAO,EAAE,MAAM;CAI5B;AAED,OAAO,EAAE,YAAY,EAAE,iBAAiB,EAAE,CAAC"}
1
+ {"version":3,"file":"unauthorized.d.ts","sourceRoot":"","sources":["../../src/httpBadResponses/unauthorized.ts"],"names":[],"mappings":"AAAA,iBAAS,YAAY,CAAC,KAAK,EAAE,iBAAiB,mCAU7C;AAED,cAAM,iBAAiB;IACrB,IAAI,EAAE,MAAM,CAAC;IACb,OAAO,EAAE,MAAM,CAAC;gBAEJ,OAAO,EAAE,MAAM;CAI5B;AAED,OAAO,EAAE,YAAY,EAAE,iBAAiB,EAAE,CAAC"}
@@ -4,7 +4,6 @@ function unauthorized(error) {
4
4
  success: false,
5
5
  name: error.name,
6
6
  message: error.message,
7
- cause: error.cause,
8
7
  }, { status: 401 });
9
8
  }
10
9
  class UnauthorizedError {
@@ -1,5 +1,8 @@
1
- declare class Created extends Response {
1
+ declare function created(body: any, init?: ResponseInit): Response;
2
+ declare class Created {
3
+ body: any;
4
+ init: ResponseInit;
2
5
  constructor(body: any, init?: ResponseInit);
3
6
  }
4
- export { Created };
7
+ export { created, Created };
5
8
  //# sourceMappingURL=created.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"created.d.ts","sourceRoot":"","sources":["../../src/httpResponses/created.ts"],"names":[],"mappings":"AAAA,cAAM,OAAQ,SAAQ,QAAQ;gBAChB,IAAI,EAAE,GAAG,EAAE,IAAI,CAAC,EAAE,YAAY;CAU3C;AAED,OAAO,EAAE,OAAO,EAAE,CAAC"}
1
+ {"version":3,"file":"created.d.ts","sourceRoot":"","sources":["../../src/httpResponses/created.ts"],"names":[],"mappings":"AAAA,iBAAS,OAAO,CAAC,IAAI,EAAE,GAAG,EAAE,IAAI,CAAC,EAAE,YAAY,GAAG,QAAQ,CAEzD;AAED,cAAM,OAAO;IACX,IAAI,EAAE,GAAG,CAAC;IACV,IAAI,EAAE,YAAY,CAAC;gBAEP,IAAI,EAAE,GAAG,EAAE,IAAI,CAAC,EAAE,YAAY;CAI3C;AAED,OAAO,EAAE,OAAO,EAAE,OAAO,EAAE,CAAC"}
@@ -1,13 +1,12 @@
1
- class Created extends Response {
1
+ function created(body, init) {
2
+ return Response.json(body, { ...init, status: 201 });
3
+ }
4
+ class Created {
5
+ body;
6
+ init;
2
7
  constructor(body, init) {
3
- super(JSON.stringify(body), {
4
- ...init,
5
- status: 201,
6
- headers: {
7
- "Content-Type": "application/json",
8
- ...init?.headers,
9
- },
10
- });
8
+ this.body = body;
9
+ this.init = init || {};
11
10
  }
12
11
  }
13
- export { Created };
12
+ export { created, Created };
@@ -1,5 +1,7 @@
1
- declare class NoContent extends Response {
1
+ declare function noContent(init?: ResponseInit): Response;
2
+ declare class NoContent {
3
+ init: ResponseInit;
2
4
  constructor(init?: ResponseInit);
3
5
  }
4
- export { NoContent };
6
+ export { noContent, NoContent };
5
7
  //# sourceMappingURL=noContent.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"noContent.d.ts","sourceRoot":"","sources":["../../src/httpResponses/noContent.ts"],"names":[],"mappings":"AAAA,cAAM,SAAU,SAAQ,QAAQ;gBAClB,IAAI,CAAC,EAAE,YAAY;CAGhC;AAED,OAAO,EAAE,SAAS,EAAE,CAAC"}
1
+ {"version":3,"file":"noContent.d.ts","sourceRoot":"","sources":["../../src/httpResponses/noContent.ts"],"names":[],"mappings":"AAAA,iBAAS,SAAS,CAAC,IAAI,CAAC,EAAE,YAAY,GAAG,QAAQ,CAEhD;AAED,cAAM,SAAS;IACb,IAAI,EAAE,YAAY,CAAC;gBAEP,IAAI,CAAC,EAAE,YAAY;CAGhC;AAED,OAAO,EAAE,SAAS,EAAE,SAAS,EAAE,CAAC"}
@@ -1,6 +1,10 @@
1
- class NoContent extends Response {
1
+ function noContent(init) {
2
+ return new Response(null, { ...init, status: 200 });
3
+ }
4
+ class NoContent {
5
+ init;
2
6
  constructor(init) {
3
- super(null, init);
7
+ this.init = init || {};
4
8
  }
5
9
  }
6
- export { NoContent };
10
+ export { noContent, NoContent };
@@ -1,5 +1,8 @@
1
- declare class Success extends Response {
1
+ declare function success(body: any, init?: ResponseInit): Response;
2
+ declare class Success {
3
+ body: any;
4
+ init: ResponseInit;
2
5
  constructor(body: any, init?: ResponseInit);
3
6
  }
4
- export { Success };
7
+ export { success, Success };
5
8
  //# sourceMappingURL=success.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"success.d.ts","sourceRoot":"","sources":["../../src/httpResponses/success.ts"],"names":[],"mappings":"AAAA,cAAM,OAAQ,SAAQ,QAAQ;gBAChB,IAAI,EAAE,GAAG,EAAE,IAAI,CAAC,EAAE,YAAY;CAU3C;AAED,OAAO,EAAE,OAAO,EAAE,CAAC"}
1
+ {"version":3,"file":"success.d.ts","sourceRoot":"","sources":["../../src/httpResponses/success.ts"],"names":[],"mappings":"AAAA,iBAAS,OAAO,CAAC,IAAI,EAAE,GAAG,EAAE,IAAI,CAAC,EAAE,YAAY,GAAG,QAAQ,CAEzD;AAED,cAAM,OAAO;IACX,IAAI,EAAE,GAAG,CAAC;IACV,IAAI,EAAE,YAAY,CAAC;gBAEP,IAAI,EAAE,GAAG,EAAE,IAAI,CAAC,EAAE,YAAY;CAI3C;AAED,OAAO,EAAE,OAAO,EAAE,OAAO,EAAE,CAAC"}
@@ -1,13 +1,12 @@
1
- class Success extends Response {
1
+ function success(body, init) {
2
+ return Response.json(body, { ...init, status: 200 });
3
+ }
4
+ class Success {
5
+ body;
6
+ init;
2
7
  constructor(body, init) {
3
- super(JSON.stringify(body), {
4
- ...init,
5
- status: 200,
6
- headers: {
7
- "Content-Type": "application/json",
8
- ...init?.headers,
9
- },
10
- });
8
+ this.body = body;
9
+ this.init = init || {};
11
10
  }
12
11
  }
13
- export { Success };
12
+ export { success, Success };
@@ -1,5 +1,8 @@
1
- declare class Updated extends Response {
1
+ declare function updated(body: any, init?: ResponseInit): Response;
2
+ declare class Updated {
3
+ body: any;
4
+ init: ResponseInit;
2
5
  constructor(body: any, init?: ResponseInit);
3
6
  }
4
- export { Updated };
7
+ export { updated, Updated };
5
8
  //# sourceMappingURL=updated.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"updated.d.ts","sourceRoot":"","sources":["../../src/httpResponses/updated.ts"],"names":[],"mappings":"AAAA,cAAM,OAAQ,SAAQ,QAAQ;gBAChB,IAAI,EAAE,GAAG,EAAE,IAAI,CAAC,EAAE,YAAY;CAU3C;AAED,OAAO,EAAE,OAAO,EAAE,CAAC"}
1
+ {"version":3,"file":"updated.d.ts","sourceRoot":"","sources":["../../src/httpResponses/updated.ts"],"names":[],"mappings":"AAAA,iBAAS,OAAO,CAAC,IAAI,EAAE,GAAG,EAAE,IAAI,CAAC,EAAE,YAAY,GAAG,QAAQ,CAEzD;AAED,cAAM,OAAO;IACX,IAAI,EAAE,GAAG,CAAC;IACV,IAAI,EAAE,YAAY,CAAC;gBAEP,IAAI,EAAE,GAAG,EAAE,IAAI,CAAC,EAAE,YAAY;CAI3C;AAED,OAAO,EAAE,OAAO,EAAE,OAAO,EAAE,CAAC"}
@@ -1,13 +1,12 @@
1
- class Updated extends Response {
1
+ function updated(body, init) {
2
+ return Response.json(body, { ...init, status: 200 });
3
+ }
4
+ class Updated {
5
+ body;
6
+ init;
2
7
  constructor(body, init) {
3
- super(JSON.stringify(body), {
4
- ...init,
5
- status: 200,
6
- headers: {
7
- "Content-Type": "application/json",
8
- ...init?.headers,
9
- },
10
- });
8
+ this.body = body;
9
+ this.init = init || {};
11
10
  }
12
11
  }
13
- export { Updated };
12
+ export { updated, Updated };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@arkyn/server",
3
- "version": "1.4.32",
3
+ "version": "1.4.34",
4
4
  "main": "./dist/bundle.js",
5
5
  "module": "./src/index.ts",
6
6
  "type": "module",
@@ -3,18 +3,42 @@ import { conflict, ConflictError } from "../httpBadResponses/conflict";
3
3
  import { forbidden, ForbiddenError } from "../httpBadResponses/forbidden";
4
4
  import { notFound, NotFoundError } from "../httpBadResponses/notFound";
5
5
  import { serverError } from "../httpBadResponses/serverError";
6
- import {
7
- UnprocessableEntityError,
8
- unprocessableEntity,
9
- } from "../httpBadResponses/unprocessableEntity";
10
6
  import {
11
7
  unauthorized,
12
8
  UnauthorizedError,
13
9
  } from "../httpBadResponses/unauthorized";
10
+ import {
11
+ unprocessableEntity,
12
+ UnprocessableEntityError,
13
+ } from "../httpBadResponses/unprocessableEntity";
14
+ import { created, Created } from "../httpResponses/created";
15
+ import { noContent, NoContent } from "../httpResponses/noContent";
16
+ import { success, Success } from "../httpResponses/success";
17
+ import { updated, Updated } from "../httpResponses/updated";
14
18
 
15
19
  const globalErrorHandler = (error: any) => {
16
- if (error instanceof Response) throw error;
20
+ // If the error is not an instance of Error, return the response
21
+ switch (true) {
22
+ case error instanceof Response:
23
+ return error;
24
+ case error instanceof Created:
25
+ return created(error.body, error.init);
26
+ case error instanceof Updated:
27
+ return updated(error.body, error.init);
28
+ case error instanceof Success:
29
+ return success(error.body, error.init);
30
+ case error instanceof NoContent:
31
+ return noContent(error.init);
32
+ }
33
+
34
+ const showConsoleError =
35
+ process.env.NODE_ENV === "development" ||
36
+ process.env?.SHOW_ERRORS_IN_CONSOLE === "true";
37
+
38
+ // If showConsoleError is true, log the error to the console
39
+ if (showConsoleError) console.error(error);
17
40
 
41
+ // If the error is an instance of BadRequestError, ForbiddenError, ConflictError, UnauthorizedError, NotFoundError, or UnprocessableEntityError, return the error
18
42
  switch (true) {
19
43
  case error instanceof BadRequestError:
20
44
  return badRequest(error);
@@ -1,11 +1,10 @@
1
- function badRequest(error: Error) {
1
+ function badRequest(error: BadRequestError) {
2
2
  return Response.json(
3
3
  {
4
4
  status: 400,
5
5
  success: false,
6
6
  name: error.name,
7
7
  message: error.message,
8
- cause: error.cause,
9
8
  },
10
9
  { status: 400 }
11
10
  );
@@ -1,11 +1,10 @@
1
- function conflict(error: Error) {
1
+ function conflict(error: ConflictError) {
2
2
  return Response.json(
3
3
  {
4
4
  status: 409,
5
5
  success: false,
6
6
  name: error.name,
7
7
  message: error.message,
8
- cause: error.cause,
9
8
  },
10
9
  { status: 409 }
11
10
  );
@@ -1,11 +1,10 @@
1
- function forbidden(error: Error) {
1
+ function forbidden(error: ForbiddenError) {
2
2
  return Response.json(
3
3
  {
4
4
  status: 403,
5
5
  success: false,
6
6
  name: error.name,
7
7
  message: error.message,
8
- cause: error.cause,
9
8
  },
10
9
  { status: 403 }
11
10
  );
@@ -1,11 +1,10 @@
1
- function notFound(error: Error) {
1
+ function notFound(error: NotFoundError) {
2
2
  return Response.json(
3
3
  {
4
4
  status: 404,
5
5
  success: false,
6
6
  name: error.name,
7
7
  message: error.message,
8
- cause: error.cause,
9
8
  },
10
9
  { status: 404 }
11
10
  );
@@ -1,11 +1,10 @@
1
- function unauthorized(error: Error) {
1
+ function unauthorized(error: UnauthorizedError) {
2
2
  return Response.json(
3
3
  {
4
4
  status: 401,
5
5
  success: false,
6
6
  name: error.name,
7
7
  message: error.message,
8
- cause: error.cause,
9
8
  },
10
9
  { status: 401 }
11
10
  );
@@ -1,14 +1,15 @@
1
- class Created extends Response {
1
+ function created(body: any, init?: ResponseInit): Response {
2
+ return Response.json(body, { ...init, status: 201 });
3
+ }
4
+
5
+ class Created {
6
+ body: any;
7
+ init: ResponseInit;
8
+
2
9
  constructor(body: any, init?: ResponseInit) {
3
- super(JSON.stringify(body), {
4
- ...init,
5
- status: 201,
6
- headers: {
7
- "Content-Type": "application/json",
8
- ...init?.headers,
9
- },
10
- });
10
+ this.body = body;
11
+ this.init = init || {};
11
12
  }
12
13
  }
13
14
 
14
- export { Created };
15
+ export { created, Created };
@@ -1,7 +1,13 @@
1
- class NoContent extends Response {
1
+ function noContent(init?: ResponseInit): Response {
2
+ return new Response(null, { ...init, status: 200 });
3
+ }
4
+
5
+ class NoContent {
6
+ init: ResponseInit;
7
+
2
8
  constructor(init?: ResponseInit) {
3
- super(null, init);
9
+ this.init = init || {};
4
10
  }
5
11
  }
6
12
 
7
- export { NoContent };
13
+ export { noContent, NoContent };
@@ -1,14 +1,15 @@
1
- class Success extends Response {
1
+ function success(body: any, init?: ResponseInit): Response {
2
+ return Response.json(body, { ...init, status: 200 });
3
+ }
4
+
5
+ class Success {
6
+ body: any;
7
+ init: ResponseInit;
8
+
2
9
  constructor(body: any, init?: ResponseInit) {
3
- super(JSON.stringify(body), {
4
- ...init,
5
- status: 200,
6
- headers: {
7
- "Content-Type": "application/json",
8
- ...init?.headers,
9
- },
10
- });
10
+ this.body = body;
11
+ this.init = init || {};
11
12
  }
12
13
  }
13
14
 
14
- export { Success };
15
+ export { success, Success };
@@ -1,14 +1,15 @@
1
- class Updated extends Response {
1
+ function updated(body: any, init?: ResponseInit): Response {
2
+ return Response.json(body, { ...init, status: 200 });
3
+ }
4
+
5
+ class Updated {
6
+ body: any;
7
+ init: ResponseInit;
8
+
2
9
  constructor(body: any, init?: ResponseInit) {
3
- super(JSON.stringify(body), {
4
- ...init,
5
- status: 200,
6
- headers: {
7
- "Content-Type": "application/json",
8
- ...init?.headers,
9
- },
10
- });
10
+ this.body = body;
11
+ this.init = init || {};
11
12
  }
12
13
  }
13
14
 
14
- export { Updated };
15
+ export { updated, Updated };