@arkyn/server 1.4.33 → 1.4.35

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 (46) hide show
  1. package/dist/helpers/globalErrorHandler.d.ts.map +1 -1
  2. package/dist/helpers/globalErrorHandler.js +20 -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 +6 -3
  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 +6 -3
  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 +6 -3
  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 +6 -3
  15. package/dist/httpBadResponses/serverError.d.ts.map +1 -1
  16. package/dist/httpBadResponses/serverError.js +6 -3
  17. package/dist/httpBadResponses/unauthorized.d.ts +1 -1
  18. package/dist/httpBadResponses/unauthorized.d.ts.map +1 -1
  19. package/dist/httpBadResponses/unauthorized.js +6 -3
  20. package/dist/httpBadResponses/unprocessableEntity.d.ts.map +1 -1
  21. package/dist/httpBadResponses/unprocessableEntity.js +7 -6
  22. package/dist/httpResponses/created.d.ts +5 -2
  23. package/dist/httpResponses/created.d.ts.map +1 -1
  24. package/dist/httpResponses/created.js +13 -10
  25. package/dist/httpResponses/noContent.d.ts +4 -2
  26. package/dist/httpResponses/noContent.d.ts.map +1 -1
  27. package/dist/httpResponses/noContent.js +11 -3
  28. package/dist/httpResponses/success.d.ts +5 -2
  29. package/dist/httpResponses/success.d.ts.map +1 -1
  30. package/dist/httpResponses/success.js +13 -10
  31. package/dist/httpResponses/updated.d.ts +5 -2
  32. package/dist/httpResponses/updated.d.ts.map +1 -1
  33. package/dist/httpResponses/updated.js +13 -10
  34. package/package.json +1 -1
  35. package/src/helpers/globalErrorHandler.ts +24 -6
  36. package/src/httpBadResponses/badRequest.ts +9 -6
  37. package/src/httpBadResponses/conflict.ts +9 -6
  38. package/src/httpBadResponses/forbidden.ts +9 -6
  39. package/src/httpBadResponses/notFound.ts +9 -6
  40. package/src/httpBadResponses/serverError.ts +8 -5
  41. package/src/httpBadResponses/unauthorized.ts +9 -6
  42. package/src/httpBadResponses/unprocessableEntity.ts +9 -8
  43. package/src/httpResponses/created.ts +15 -10
  44. package/src/httpResponses/noContent.ts +13 -3
  45. package/src/httpResponses/success.ts +15 -10
  46. package/src/httpResponses/updated.ts +15 -10
@@ -1 +1 @@
1
- {"version":3,"file":"globalErrorHandler.d.ts","sourceRoot":"","sources":["../../src/helpers/globalErrorHandler.ts"],"names":[],"mappings":"AAcA,QAAA,MAAM,kBAAkB,UAAW,GAAG,oCA6BrC,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,15 +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) => {
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
+ }
9
26
  const showConsoleError = process.env.NODE_ENV === "development" ||
10
27
  process.env?.SHOW_ERRORS_IN_CONSOLE === "true";
28
+ // If showConsoleError is true, log the error to the console
11
29
  if (showConsoleError)
12
30
  console.error(error);
13
- if (error instanceof Response)
14
- throw error;
31
+ // If the error is an instance of BadRequestError, ForbiddenError, ConflictError, UnauthorizedError, NotFoundError, or UnprocessableEntityError, return the error
15
32
  switch (true) {
16
33
  case error instanceof BadRequestError:
17
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,mCAczC;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,11 +1,14 @@
1
1
  function badRequest(error) {
2
- return Response.json({
2
+ return new Response(JSON.stringify({
3
3
  status: 400,
4
4
  success: false,
5
5
  name: error.name,
6
6
  message: error.message,
7
- cause: error.cause,
8
- }, { status: 400 });
7
+ }), {
8
+ status: 400,
9
+ statusText: "Bad Request",
10
+ headers: { "Content-Type": "application/json" },
11
+ });
9
12
  }
10
13
  class BadRequestError {
11
14
  name;
@@ -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,mCAcrC;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,11 +1,14 @@
1
1
  function conflict(error) {
2
- return Response.json({
2
+ return new Response(JSON.stringify({
3
3
  status: 409,
4
4
  success: false,
5
5
  name: error.name,
6
6
  message: error.message,
7
- cause: error.cause,
8
- }, { status: 409 });
7
+ }), {
8
+ status: 409,
9
+ statusText: "Bad Request",
10
+ headers: { "Content-Type": "application/json" },
11
+ });
9
12
  }
10
13
  class ConflictError {
11
14
  name;
@@ -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,mCAcvC;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,11 +1,14 @@
1
1
  function forbidden(error) {
2
- return Response.json({
2
+ return new Response(JSON.stringify({
3
3
  status: 403,
4
4
  success: false,
5
5
  name: error.name,
6
6
  message: error.message,
7
- cause: error.cause,
8
- }, { status: 403 });
7
+ }), {
8
+ status: 403,
9
+ statusText: "Bad Request",
10
+ headers: { "Content-Type": "application/json" },
11
+ });
9
12
  }
10
13
  class ForbiddenError {
11
14
  name;
@@ -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,mCAcrC;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,11 +1,14 @@
1
1
  function notFound(error) {
2
- return Response.json({
2
+ return new Response(JSON.stringify({
3
3
  status: 404,
4
4
  success: false,
5
5
  name: error.name,
6
6
  message: error.message,
7
- cause: error.cause,
8
- }, { status: 404 });
7
+ }), {
8
+ status: 404,
9
+ statusText: "Bad Request",
10
+ headers: { "Content-Type": "application/json" },
11
+ });
9
12
  }
10
13
  class NotFoundError {
11
14
  name;
@@ -1 +1 @@
1
- {"version":3,"file":"serverError.d.ts","sourceRoot":"","sources":["../../src/httpBadResponses/serverError.ts"],"names":[],"mappings":"AAAA,iBAAS,WAAW,CAAC,KAAK,EAAE,KAAK,mCAWhC;AAED,cAAM,WAAW;IACf,IAAI,EAAE,MAAM,CAAC;IACb,OAAO,EAAE,MAAM,CAAC;gBAEJ,OAAO,EAAE,MAAM;CAI5B;AAED,OAAO,EAAE,WAAW,EAAE,WAAW,EAAE,CAAC"}
1
+ {"version":3,"file":"serverError.d.ts","sourceRoot":"","sources":["../../src/httpBadResponses/serverError.ts"],"names":[],"mappings":"AAAA,iBAAS,WAAW,CAAC,KAAK,EAAE,KAAK,mCAchC;AAED,cAAM,WAAW;IACf,IAAI,EAAE,MAAM,CAAC;IACb,OAAO,EAAE,MAAM,CAAC;gBAEJ,OAAO,EAAE,MAAM;CAI5B;AAED,OAAO,EAAE,WAAW,EAAE,WAAW,EAAE,CAAC"}
@@ -1,11 +1,14 @@
1
1
  function serverError(error) {
2
- return Response.json({
2
+ return new Response(JSON.stringify({
3
3
  status: 500,
4
4
  success: false,
5
5
  name: error.name,
6
6
  message: error.message,
7
- cause: error.cause,
8
- }, { status: 500 });
7
+ }), {
8
+ status: 500,
9
+ statusText: "Bad Request",
10
+ headers: { "Content-Type": "application/json" },
11
+ });
9
12
  }
10
13
  class ServerError {
11
14
  name;
@@ -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,mCAc7C;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,11 +1,14 @@
1
1
  function unauthorized(error) {
2
- return Response.json({
2
+ return new Response(JSON.stringify({
3
3
  status: 401,
4
4
  success: false,
5
5
  name: error.name,
6
6
  message: error.message,
7
- cause: error.cause,
8
- }, { status: 401 });
7
+ }), {
8
+ status: 401,
9
+ statusText: "Bad Request",
10
+ headers: { "Content-Type": "application/json" },
11
+ });
9
12
  }
10
13
  class UnauthorizedError {
11
14
  name;
@@ -1 +1 @@
1
- {"version":3,"file":"unprocessableEntity.d.ts","sourceRoot":"","sources":["../../src/httpBadResponses/unprocessableEntity.ts"],"names":[],"mappings":"AAAA,KAAK,6BAA6B,GAAG;IACnC,IAAI,CAAC,EAAE,GAAG,CAAC;IACX,WAAW,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IACrC,MAAM,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IAChC,OAAO,CAAC,EAAE,MAAM,CAAC;CAClB,CAAC;AAEF,iBAAS,mBAAmB,CAAC,KAAK,EAAE,wBAAwB,mCAa3D;AAED,cAAM,wBAAwB;IAC5B,IAAI,EAAE,MAAM,CAAC;IACb,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,WAAW,EAAE,GAAG,CAAC;IACjB,MAAM,EAAE,GAAG,CAAC;IACZ,IAAI,EAAE,GAAG,CAAC;gBAEE,IAAI,EAAE,6BAA6B;CAOhD;AAED,OAAO,EAAE,mBAAmB,EAAE,wBAAwB,EAAE,CAAC"}
1
+ {"version":3,"file":"unprocessableEntity.d.ts","sourceRoot":"","sources":["../../src/httpBadResponses/unprocessableEntity.ts"],"names":[],"mappings":"AAAA,KAAK,6BAA6B,GAAG;IACnC,IAAI,CAAC,EAAE,GAAG,CAAC;IACX,WAAW,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IACrC,MAAM,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IAChC,OAAO,CAAC,EAAE,MAAM,CAAC;CAClB,CAAC;AAEF,iBAAS,mBAAmB,CAAC,KAAK,EAAE,wBAAwB,mCAc3D;AAED,cAAM,wBAAwB;IAC5B,IAAI,EAAE,MAAM,CAAC;IACb,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,WAAW,EAAE,GAAG,CAAC;IACjB,MAAM,EAAE,GAAG,CAAC;IACZ,IAAI,EAAE,GAAG,CAAC;gBAEE,IAAI,EAAE,6BAA6B;CAOhD;AAED,OAAO,EAAE,mBAAmB,EAAE,wBAAwB,EAAE,CAAC"}
@@ -1,13 +1,14 @@
1
1
  function unprocessableEntity(error) {
2
- return Response.json({
3
- status: 400,
2
+ return new Response(JSON.stringify({
3
+ status: 422,
4
4
  success: false,
5
5
  name: error.name,
6
6
  message: error.message,
7
- data: error.data || null,
8
- fieldErrors: error.fieldErrors || null,
9
- fields: error.fields || null,
10
- }, { status: 400 });
7
+ }), {
8
+ status: 422,
9
+ statusText: "Bad Request",
10
+ headers: { "Content-Type": "application/json" },
11
+ });
11
12
  }
12
13
  class UnprocessableEntityError {
13
14
  name;
@@ -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,CAMzD;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,16 @@
1
- class Created extends Response {
1
+ function created(body, init) {
2
+ return new Response(JSON.stringify(body), {
3
+ ...init,
4
+ status: 201,
5
+ headers: { "Content-Type": "application/json", ...init?.headers },
6
+ });
7
+ }
8
+ class Created {
9
+ body;
10
+ init;
2
11
  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
- });
12
+ this.body = body;
13
+ this.init = init || {};
11
14
  }
12
15
  }
13
- export { Created };
16
+ 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,CAMhD;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,14 @@
1
- class NoContent extends Response {
1
+ function noContent(init) {
2
+ return new Response(null, {
3
+ ...init,
4
+ status: 200,
5
+ headers: { "Content-Type": "application/json", ...init?.headers },
6
+ });
7
+ }
8
+ class NoContent {
9
+ init;
2
10
  constructor(init) {
3
- super(null, init);
11
+ this.init = init || {};
4
12
  }
5
13
  }
6
- export { NoContent };
14
+ 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,CAMzD;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,16 @@
1
- class Success extends Response {
1
+ function success(body, init) {
2
+ return new Response(JSON.stringify(body), {
3
+ ...init,
4
+ status: 200,
5
+ headers: { "Content-Type": "application/json", ...init?.headers },
6
+ });
7
+ }
8
+ class Success {
9
+ body;
10
+ init;
2
11
  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
- });
12
+ this.body = body;
13
+ this.init = init || {};
11
14
  }
12
15
  }
13
- export { Success };
16
+ 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,CAMzD;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,16 @@
1
- class Updated extends Response {
1
+ function updated(body, init) {
2
+ return new Response(JSON.stringify(body), {
3
+ ...init,
4
+ status: 200,
5
+ headers: { "Content-Type": "application/json", ...init?.headers },
6
+ });
7
+ }
8
+ class Updated {
9
+ body;
10
+ init;
2
11
  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
- });
12
+ this.body = body;
13
+ this.init = init || {};
11
14
  }
12
15
  }
13
- export { Updated };
16
+ export { updated, Updated };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@arkyn/server",
3
- "version": "1.4.33",
3
+ "version": "1.4.35",
4
4
  "main": "./dist/bundle.js",
5
5
  "module": "./src/index.ts",
6
6
  "type": "module",
@@ -3,24 +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) => {
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
+
16
34
  const showConsoleError =
17
35
  process.env.NODE_ENV === "development" ||
18
36
  process.env?.SHOW_ERRORS_IN_CONSOLE === "true";
19
37
 
38
+ // If showConsoleError is true, log the error to the console
20
39
  if (showConsoleError) console.error(error);
21
40
 
22
- if (error instanceof Response) throw error;
23
-
41
+ // If the error is an instance of BadRequestError, ForbiddenError, ConflictError, UnauthorizedError, NotFoundError, or UnprocessableEntityError, return the error
24
42
  switch (true) {
25
43
  case error instanceof BadRequestError:
26
44
  return badRequest(error);
@@ -1,13 +1,16 @@
1
- function badRequest(error: Error) {
2
- return Response.json(
3
- {
1
+ function badRequest(error: BadRequestError) {
2
+ return new Response(
3
+ JSON.stringify({
4
4
  status: 400,
5
5
  success: false,
6
6
  name: error.name,
7
7
  message: error.message,
8
- cause: error.cause,
9
- },
10
- { status: 400 }
8
+ }),
9
+ {
10
+ status: 400,
11
+ statusText: "Bad Request",
12
+ headers: { "Content-Type": "application/json" },
13
+ }
11
14
  );
12
15
  }
13
16
 
@@ -1,13 +1,16 @@
1
- function conflict(error: Error) {
2
- return Response.json(
3
- {
1
+ function conflict(error: ConflictError) {
2
+ return new Response(
3
+ JSON.stringify({
4
4
  status: 409,
5
5
  success: false,
6
6
  name: error.name,
7
7
  message: error.message,
8
- cause: error.cause,
9
- },
10
- { status: 409 }
8
+ }),
9
+ {
10
+ status: 409,
11
+ statusText: "Bad Request",
12
+ headers: { "Content-Type": "application/json" },
13
+ }
11
14
  );
12
15
  }
13
16
 
@@ -1,13 +1,16 @@
1
- function forbidden(error: Error) {
2
- return Response.json(
3
- {
1
+ function forbidden(error: ForbiddenError) {
2
+ return new Response(
3
+ JSON.stringify({
4
4
  status: 403,
5
5
  success: false,
6
6
  name: error.name,
7
7
  message: error.message,
8
- cause: error.cause,
9
- },
10
- { status: 403 }
8
+ }),
9
+ {
10
+ status: 403,
11
+ statusText: "Bad Request",
12
+ headers: { "Content-Type": "application/json" },
13
+ }
11
14
  );
12
15
  }
13
16
 
@@ -1,13 +1,16 @@
1
- function notFound(error: Error) {
2
- return Response.json(
3
- {
1
+ function notFound(error: NotFoundError) {
2
+ return new Response(
3
+ JSON.stringify({
4
4
  status: 404,
5
5
  success: false,
6
6
  name: error.name,
7
7
  message: error.message,
8
- cause: error.cause,
9
- },
10
- { status: 404 }
8
+ }),
9
+ {
10
+ status: 404,
11
+ statusText: "Bad Request",
12
+ headers: { "Content-Type": "application/json" },
13
+ }
11
14
  );
12
15
  }
13
16
 
@@ -1,13 +1,16 @@
1
1
  function serverError(error: Error) {
2
- return Response.json(
3
- {
2
+ return new Response(
3
+ JSON.stringify({
4
4
  status: 500,
5
5
  success: false,
6
6
  name: error.name,
7
7
  message: error.message,
8
- cause: error.cause,
9
- },
10
- { status: 500 }
8
+ }),
9
+ {
10
+ status: 500,
11
+ statusText: "Bad Request",
12
+ headers: { "Content-Type": "application/json" },
13
+ }
11
14
  );
12
15
  }
13
16
 
@@ -1,13 +1,16 @@
1
- function unauthorized(error: Error) {
2
- return Response.json(
3
- {
1
+ function unauthorized(error: UnauthorizedError) {
2
+ return new Response(
3
+ JSON.stringify({
4
4
  status: 401,
5
5
  success: false,
6
6
  name: error.name,
7
7
  message: error.message,
8
- cause: error.cause,
9
- },
10
- { status: 401 }
8
+ }),
9
+ {
10
+ status: 401,
11
+ statusText: "Bad Request",
12
+ headers: { "Content-Type": "application/json" },
13
+ }
11
14
  );
12
15
  }
13
16
 
@@ -6,17 +6,18 @@ type UnprocessableEntityErrorProps = {
6
6
  };
7
7
 
8
8
  function unprocessableEntity(error: UnprocessableEntityError) {
9
- return Response.json(
10
- {
11
- status: 400,
9
+ return new Response(
10
+ JSON.stringify({
11
+ status: 422,
12
12
  success: false,
13
13
  name: error.name,
14
14
  message: error.message,
15
- data: error.data || null,
16
- fieldErrors: error.fieldErrors || null,
17
- fields: error.fields || null,
18
- },
19
- { status: 400 }
15
+ }),
16
+ {
17
+ status: 422,
18
+ statusText: "Bad Request",
19
+ headers: { "Content-Type": "application/json" },
20
+ }
20
21
  );
21
22
  }
22
23
 
@@ -1,14 +1,19 @@
1
- class Created extends Response {
1
+ function created(body: any, init?: ResponseInit): Response {
2
+ return new Response(JSON.stringify(body), {
3
+ ...init,
4
+ status: 201,
5
+ headers: { "Content-Type": "application/json", ...init?.headers },
6
+ });
7
+ }
8
+
9
+ class Created {
10
+ body: any;
11
+ init: ResponseInit;
12
+
2
13
  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
- });
14
+ this.body = body;
15
+ this.init = init || {};
11
16
  }
12
17
  }
13
18
 
14
- export { Created };
19
+ export { created, Created };
@@ -1,7 +1,17 @@
1
- class NoContent extends Response {
1
+ function noContent(init?: ResponseInit): Response {
2
+ return new Response(null, {
3
+ ...init,
4
+ status: 200,
5
+ headers: { "Content-Type": "application/json", ...init?.headers },
6
+ });
7
+ }
8
+
9
+ class NoContent {
10
+ init: ResponseInit;
11
+
2
12
  constructor(init?: ResponseInit) {
3
- super(null, init);
13
+ this.init = init || {};
4
14
  }
5
15
  }
6
16
 
7
- export { NoContent };
17
+ export { noContent, NoContent };
@@ -1,14 +1,19 @@
1
- class Success extends Response {
1
+ function success(body: any, init?: ResponseInit): Response {
2
+ return new Response(JSON.stringify(body), {
3
+ ...init,
4
+ status: 200,
5
+ headers: { "Content-Type": "application/json", ...init?.headers },
6
+ });
7
+ }
8
+
9
+ class Success {
10
+ body: any;
11
+ init: ResponseInit;
12
+
2
13
  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
- });
14
+ this.body = body;
15
+ this.init = init || {};
11
16
  }
12
17
  }
13
18
 
14
- export { Success };
19
+ export { success, Success };
@@ -1,14 +1,19 @@
1
- class Updated extends Response {
1
+ function updated(body: any, init?: ResponseInit): Response {
2
+ return new Response(JSON.stringify(body), {
3
+ ...init,
4
+ status: 200,
5
+ headers: { "Content-Type": "application/json", ...init?.headers },
6
+ });
7
+ }
8
+
9
+ class Updated {
10
+ body: any;
11
+ init: ResponseInit;
12
+
2
13
  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
- });
14
+ this.body = body;
15
+ this.init = init || {};
11
16
  }
12
17
  }
13
18
 
14
- export { Updated };
19
+ export { updated, Updated };