@ruiapp/rapid-core 0.1.5 → 0.1.7

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 (105) hide show
  1. package/dist/bootstrapApplicationConfig.d.ts +1 -1
  2. package/dist/core/{httpHandler.d.ts → actionHandler.d.ts} +4 -5
  3. package/dist/core/pluginManager.d.ts +4 -1
  4. package/dist/core/request.d.ts +2 -1
  5. package/dist/core/server.d.ts +19 -17
  6. package/dist/helpers/runCollectionEntityActionHandler.d.ts +5 -0
  7. package/dist/index.d.ts +8 -6
  8. package/dist/index.js +334 -264
  9. package/dist/plugins/auth/{mod.d.ts → AuthPlugin.d.ts} +5 -3
  10. package/dist/plugins/auth/actionHandlers/createSession.d.ts +8 -0
  11. package/dist/plugins/auth/actionHandlers/deleteSession.d.ts +4 -0
  12. package/dist/plugins/auth/actionHandlers/getMyProfile.d.ts +4 -0
  13. package/dist/plugins/auth/routes/getMyProfile.d.ts +1 -1
  14. package/dist/plugins/auth/routes/index.d.ts +2 -2
  15. package/dist/plugins/auth/routes/signin.d.ts +1 -1
  16. package/dist/plugins/auth/routes/signout.d.ts +1 -1
  17. package/dist/plugins/dataManage/{mod.d.ts → DataManagePlugin.d.ts} +1 -1
  18. package/dist/plugins/dataManage/actionHandlers/addEntityRelations.d.ts +5 -0
  19. package/dist/plugins/dataManage/actionHandlers/countCollectionEntities.d.ts +5 -0
  20. package/dist/plugins/dataManage/actionHandlers/createCollectionEntitiesBatch.d.ts +5 -0
  21. package/dist/plugins/dataManage/actionHandlers/createCollectionEntity.d.ts +5 -0
  22. package/dist/plugins/dataManage/actionHandlers/deleteCollectionEntityById.d.ts +5 -0
  23. package/dist/plugins/dataManage/actionHandlers/findCollectionEntities.d.ts +5 -0
  24. package/dist/plugins/dataManage/actionHandlers/findCollectionEntityById.d.ts +5 -0
  25. package/dist/plugins/dataManage/actionHandlers/queryDatabase.d.ts +5 -0
  26. package/dist/plugins/dataManage/actionHandlers/removeEntityRelations.d.ts +5 -0
  27. package/dist/plugins/dataManage/actionHandlers/updateCollectionEntityById.d.ts +5 -0
  28. package/dist/plugins/fileManage/{mod.d.ts → FileManagePlugin.d.ts} +1 -1
  29. package/dist/plugins/fileManage/actionHandlers/downloadDocument.d.ts +4 -0
  30. package/dist/plugins/fileManage/actionHandlers/downloadFile.d.ts +4 -0
  31. package/dist/plugins/fileManage/actionHandlers/uploadFile.d.ts +4 -0
  32. package/dist/plugins/metaManage/{mod.d.ts → MetaManagePlugin.d.ts} +1 -1
  33. package/dist/plugins/metaManage/actionHandlers/getMetaModelDetail.d.ts +4 -0
  34. package/dist/plugins/metaManage/actionHandlers/listMetaModels.d.ts +4 -0
  35. package/dist/plugins/metaManage/actionHandlers/listMetaRoutes.d.ts +4 -0
  36. package/dist/plugins/routeManage/{mod.d.ts → RouteManagePlugin.d.ts} +1 -1
  37. package/dist/plugins/routeManage/actionHandlers/httpProxy.d.ts +5 -0
  38. package/dist/plugins/webhooks/{mod.d.ts → WebhooksPlugin.d.ts} +1 -1
  39. package/dist/server.d.ts +3 -3
  40. package/dist/types.d.ts +3 -3
  41. package/dist/utilities/jwtUtility.d.ts +2 -2
  42. package/package.json +1 -1
  43. package/src/bootstrapApplicationConfig.ts +6 -6
  44. package/src/core/{httpHandler.ts → actionHandler.ts} +5 -11
  45. package/src/core/pluginManager.ts +16 -3
  46. package/src/core/request.ts +16 -3
  47. package/src/core/routesBuilder.ts +4 -4
  48. package/src/core/server.ts +20 -19
  49. package/src/helpers/{runCollectionEntityHttpHandler.ts → runCollectionEntityActionHandler.ts} +5 -5
  50. package/src/index.ts +8 -6
  51. package/src/plugins/auth/{mod.ts → AuthPlugin.ts} +31 -6
  52. package/src/plugins/auth/{httpHandlers → actionHandlers}/createSession.ts +2 -2
  53. package/src/plugins/auth/{httpHandlers → actionHandlers}/deleteSession.ts +2 -2
  54. package/src/plugins/auth/{httpHandlers → actionHandlers}/getMyProfile.ts +2 -2
  55. package/src/plugins/auth/{httpHandlers → actionHandlers}/index.ts +2 -2
  56. package/src/plugins/auth/routes/getMyProfile.ts +1 -1
  57. package/src/plugins/auth/routes/signin.ts +1 -1
  58. package/src/plugins/auth/routes/signout.ts +1 -1
  59. package/src/plugins/dataManage/{mod.ts → DataManagePlugin.ts} +24 -24
  60. package/src/plugins/dataManage/{httpHandlers → actionHandlers}/addEntityRelations.ts +4 -4
  61. package/src/plugins/dataManage/{httpHandlers → actionHandlers}/countCollectionEntities.ts +6 -6
  62. package/src/plugins/dataManage/{httpHandlers → actionHandlers}/createCollectionEntitiesBatch.ts +4 -4
  63. package/src/plugins/dataManage/{httpHandlers → actionHandlers}/createCollectionEntity.ts +4 -4
  64. package/src/plugins/dataManage/{httpHandlers → actionHandlers}/deleteCollectionEntityById.ts +4 -4
  65. package/src/plugins/dataManage/{httpHandlers → actionHandlers}/findCollectionEntities.ts +6 -6
  66. package/src/plugins/dataManage/{httpHandlers → actionHandlers}/findCollectionEntityById.ts +4 -4
  67. package/src/plugins/dataManage/{httpHandlers → actionHandlers}/queryDatabase.ts +2 -2
  68. package/src/plugins/dataManage/{httpHandlers → actionHandlers}/removeEntityRelations.ts +4 -4
  69. package/src/plugins/dataManage/{httpHandlers → actionHandlers}/updateCollectionEntityById.ts +4 -4
  70. package/src/plugins/fileManage/{mod.ts → FileManagePlugin.ts} +7 -7
  71. package/src/plugins/fileManage/{httpHandlers → actionHandlers}/downloadDocument.ts +2 -2
  72. package/src/plugins/fileManage/{httpHandlers → actionHandlers}/downloadFile.ts +2 -2
  73. package/src/plugins/fileManage/{httpHandlers → actionHandlers}/uploadFile.ts +2 -2
  74. package/src/plugins/metaManage/{mod.ts → MetaManagePlugin.ts} +7 -7
  75. package/src/plugins/metaManage/{httpHandlers → actionHandlers}/getMetaModelDetail.ts +2 -2
  76. package/src/plugins/metaManage/{httpHandlers → actionHandlers}/listMetaModels.ts +2 -2
  77. package/src/plugins/metaManage/{httpHandlers → actionHandlers}/listMetaRoutes.ts +2 -2
  78. package/src/plugins/routeManage/{mod.ts → RouteManagePlugin.ts} +3 -3
  79. package/src/plugins/routeManage/{httpHandlers → actionHandlers}/httpProxy.ts +2 -2
  80. package/src/plugins/webhooks/{mod.ts → WebhooksPlugin.ts} +1 -1
  81. package/src/server.ts +11 -10
  82. package/src/types.ts +3 -3
  83. package/src/utilities/jwtUtility.ts +3 -3
  84. package/dist/helpers/runCollectionEntityHttpHandler.d.ts +0 -5
  85. package/dist/plugins/auth/httpHandlers/createSession.d.ts +0 -8
  86. package/dist/plugins/auth/httpHandlers/deleteSession.d.ts +0 -4
  87. package/dist/plugins/auth/httpHandlers/getMyProfile.d.ts +0 -4
  88. package/dist/plugins/dataManage/httpHandlers/addEntityRelations.d.ts +0 -5
  89. package/dist/plugins/dataManage/httpHandlers/countCollectionEntities.d.ts +0 -5
  90. package/dist/plugins/dataManage/httpHandlers/createCollectionEntitiesBatch.d.ts +0 -5
  91. package/dist/plugins/dataManage/httpHandlers/createCollectionEntity.d.ts +0 -5
  92. package/dist/plugins/dataManage/httpHandlers/deleteCollectionEntityById.d.ts +0 -5
  93. package/dist/plugins/dataManage/httpHandlers/findCollectionEntities.d.ts +0 -5
  94. package/dist/plugins/dataManage/httpHandlers/findCollectionEntityById.d.ts +0 -5
  95. package/dist/plugins/dataManage/httpHandlers/queryDatabase.d.ts +0 -5
  96. package/dist/plugins/dataManage/httpHandlers/removeEntityRelations.d.ts +0 -5
  97. package/dist/plugins/dataManage/httpHandlers/updateCollectionEntityById.d.ts +0 -5
  98. package/dist/plugins/fileManage/httpHandlers/downloadDocument.d.ts +0 -4
  99. package/dist/plugins/fileManage/httpHandlers/downloadFile.d.ts +0 -4
  100. package/dist/plugins/fileManage/httpHandlers/uploadFile.d.ts +0 -4
  101. package/dist/plugins/metaManage/httpHandlers/getMetaModelDetail.d.ts +0 -4
  102. package/dist/plugins/metaManage/httpHandlers/listMetaModels.d.ts +0 -4
  103. package/dist/plugins/metaManage/httpHandlers/listMetaRoutes.d.ts +0 -4
  104. package/dist/plugins/routeManage/httpHandlers/httpProxy.d.ts +0 -5
  105. /package/dist/plugins/auth/{httpHandlers → actionHandlers}/index.d.ts +0 -0
package/dist/index.js CHANGED
@@ -465,10 +465,10 @@ class PluginManager {
465
465
  }
466
466
  }
467
467
  /** 注册接口动作处理程序 */
468
- async registerHttpHandlers() {
468
+ async registerActionHandlers() {
469
469
  for (const plugin of this.#plugins) {
470
- if (plugin.registerHttpHandlers) {
471
- await plugin.registerHttpHandlers(this.#server);
470
+ if (plugin.registerActionHandlers) {
471
+ await plugin.registerActionHandlers(this.#server);
472
472
  }
473
473
  }
474
474
  }
@@ -544,6 +544,14 @@ class PluginManager {
544
544
  }
545
545
  }
546
546
  }
547
+ /** 在接收到HTTP请求,准备路由上下文时调用。 */
548
+ async onPrepareRouteContext(server, routeContext) {
549
+ for (const plugin of this.#plugins) {
550
+ if (plugin.onPrepareRouteContext) {
551
+ await plugin.onPrepareRouteContext(server, routeContext);
552
+ }
553
+ }
554
+ }
547
555
  }
548
556
 
549
557
  class EventManager {
@@ -611,8 +619,8 @@ async function buildRoutes(server, applicationConfig) {
611
619
  applicationConfig,
612
620
  input,
613
621
  };
614
- for (const handlerConfig of routeConfig.handlers) {
615
- const handler = server.getHttpHandlerByCode(handlerConfig.code);
622
+ for (const handlerConfig of routeConfig.actions) {
623
+ const handler = server.getActionHandlerByCode(handlerConfig.code);
616
624
  if (!handler) {
617
625
  throw new Error("Unknown handler: " + handlerConfig.code);
618
626
  }
@@ -769,19 +777,255 @@ const convertToNewArray = (form, key, value) => {
769
777
  form[key] = [form[key], value];
770
778
  };
771
779
 
780
+ // Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
781
+ class AssertionError extends Error {
782
+ name = "AssertionError";
783
+ constructor(message) {
784
+ super(message);
785
+ }
786
+ }
787
+
788
+ // Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
789
+ /** Make an assertion, error will be thrown if `expr` does not have truthy value. */
790
+ function assert(expr, msg = "") {
791
+ if (!expr) {
792
+ throw new AssertionError(msg);
793
+ }
794
+ }
795
+
796
+ // Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
797
+ // This module is browser compatible.
798
+ /**
799
+ * Formats the given date to IMF date time format. (Reference:
800
+ * https://tools.ietf.org/html/rfc7231#section-7.1.1.1).
801
+ * IMF is the time format to use when generating times in HTTP
802
+ * headers. The time being formatted must be in UTC for Format to
803
+ * generate the correct format.
804
+ *
805
+ * @example
806
+ * ```ts
807
+ * import { toIMF } from "https://deno.land/std@$STD_VERSION/datetime/to_imf.ts";
808
+ *
809
+ * toIMF(new Date(0)); // => returns "Thu, 01 Jan 1970 00:00:00 GMT"
810
+ * ```
811
+ * @param date Date to parse
812
+ * @return IMF date formatted string
813
+ */
814
+ function toIMF(date) {
815
+ function dtPad(v, lPad = 2) {
816
+ return v.padStart(lPad, "0");
817
+ }
818
+ const d = dtPad(date.getUTCDate().toString());
819
+ const h = dtPad(date.getUTCHours().toString());
820
+ const min = dtPad(date.getUTCMinutes().toString());
821
+ const s = dtPad(date.getUTCSeconds().toString());
822
+ const y = date.getUTCFullYear();
823
+ const days = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"];
824
+ const months = [
825
+ "Jan",
826
+ "Feb",
827
+ "Mar",
828
+ "Apr",
829
+ "May",
830
+ "Jun",
831
+ "Jul",
832
+ "Aug",
833
+ "Sep",
834
+ "Oct",
835
+ "Nov",
836
+ "Dec",
837
+ ];
838
+ return `${days[date.getUTCDay()]}, ${d} ${months[date.getUTCMonth()]} ${y} ${h}:${min}:${s} GMT`;
839
+ }
840
+
841
+ // Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
842
+ const FIELD_CONTENT_REGEXP = /^(?=[\x20-\x7E]*$)[^()@<>,;:\\"\[\]?={}\s]+$/;
843
+ function toString(cookie) {
844
+ if (!cookie.name) {
845
+ return "";
846
+ }
847
+ const out = [];
848
+ validateName(cookie.name);
849
+ validateValue(cookie.name, cookie.value);
850
+ out.push(`${cookie.name}=${cookie.value}`);
851
+ // Fallback for invalid Set-Cookie
852
+ // ref: https://tools.ietf.org/html/draft-ietf-httpbis-cookie-prefixes-00#section-3.1
853
+ if (cookie.name.startsWith("__Secure")) {
854
+ cookie.secure = true;
855
+ }
856
+ if (cookie.name.startsWith("__Host")) {
857
+ cookie.path = "/";
858
+ cookie.secure = true;
859
+ delete cookie.domain;
860
+ }
861
+ if (cookie.secure) {
862
+ out.push("Secure");
863
+ }
864
+ if (cookie.httpOnly) {
865
+ out.push("HttpOnly");
866
+ }
867
+ if (typeof cookie.maxAge === "number" && Number.isInteger(cookie.maxAge)) {
868
+ assert(cookie.maxAge >= 0, "Max-Age must be an integer superior or equal to 0");
869
+ out.push(`Max-Age=${cookie.maxAge}`);
870
+ }
871
+ if (cookie.domain) {
872
+ validateDomain(cookie.domain);
873
+ out.push(`Domain=${cookie.domain}`);
874
+ }
875
+ if (cookie.sameSite) {
876
+ out.push(`SameSite=${cookie.sameSite}`);
877
+ }
878
+ if (cookie.path) {
879
+ validatePath(cookie.path);
880
+ out.push(`Path=${cookie.path}`);
881
+ }
882
+ if (cookie.expires) {
883
+ const { expires } = cookie;
884
+ const dateString = toIMF(typeof expires === "number" ? new Date(expires) : expires);
885
+ out.push(`Expires=${dateString}`);
886
+ }
887
+ if (cookie.unparsed) {
888
+ out.push(cookie.unparsed.join("; "));
889
+ }
890
+ return out.join("; ");
891
+ }
892
+ /**
893
+ * Validate Cookie Name.
894
+ * @param name Cookie name.
895
+ */
896
+ function validateName(name) {
897
+ if (name && !FIELD_CONTENT_REGEXP.test(name)) {
898
+ throw new TypeError(`Invalid cookie name: "${name}".`);
899
+ }
900
+ }
901
+ /**
902
+ * Validate Path Value.
903
+ * See {@link https://tools.ietf.org/html/rfc6265#section-4.1.2.4}.
904
+ * @param path Path value.
905
+ */
906
+ function validatePath(path) {
907
+ if (path == null) {
908
+ return;
909
+ }
910
+ for (let i = 0; i < path.length; i++) {
911
+ const c = path.charAt(i);
912
+ if (c < String.fromCharCode(0x20) || c > String.fromCharCode(0x7E) || c == ";") {
913
+ throw new Error(path + ": Invalid cookie path char '" + c + "'");
914
+ }
915
+ }
916
+ }
917
+ /**
918
+ * Validate Cookie Value.
919
+ * See {@link https://tools.ietf.org/html/rfc6265#section-4.1}.
920
+ * @param value Cookie value.
921
+ */
922
+ function validateValue(name, value) {
923
+ if (value == null || name == null)
924
+ return;
925
+ for (let i = 0; i < value.length; i++) {
926
+ const c = value.charAt(i);
927
+ if (c < String.fromCharCode(0x21) || c == String.fromCharCode(0x22) ||
928
+ c == String.fromCharCode(0x2c) || c == String.fromCharCode(0x3b) ||
929
+ c == String.fromCharCode(0x5c) || c == String.fromCharCode(0x7f)) {
930
+ throw new Error("RFC2616 cookie '" + name + "' cannot contain character '" + c + "'");
931
+ }
932
+ if (c > String.fromCharCode(0x80)) {
933
+ throw new Error("RFC2616 cookie '" + name + "' can only have US-ASCII chars as value" +
934
+ c.charCodeAt(0).toString(16));
935
+ }
936
+ }
937
+ }
938
+ /**
939
+ * Validate Cookie Domain.
940
+ * See {@link https://datatracker.ietf.org/doc/html/rfc6265#section-4.1.2.3}.
941
+ * @param domain Cookie domain.
942
+ */
943
+ function validateDomain(domain) {
944
+ if (domain == null) {
945
+ return;
946
+ }
947
+ const char1 = domain.charAt(0);
948
+ const charN = domain.charAt(domain.length - 1);
949
+ if (char1 == "-" || charN == "." || charN == "-") {
950
+ throw new Error("Invalid first/last char in cookie domain: " + domain);
951
+ }
952
+ }
953
+ /**
954
+ * Parse cookies of a header
955
+ *
956
+ * @example
957
+ * ```ts
958
+ * import { getCookies } from "https://deno.land/std@$STD_VERSION/http/cookie.ts";
959
+ *
960
+ * const headers = new Headers();
961
+ * headers.set("Cookie", "full=of; tasty=chocolate");
962
+ *
963
+ * const cookies = getCookies(headers);
964
+ * console.log(cookies); // { full: "of", tasty: "chocolate" }
965
+ * ```
966
+ *
967
+ * @param headers The headers instance to get cookies from
968
+ * @return Object with cookie names as keys
969
+ */
970
+ function getCookies(headers) {
971
+ const cookie = headers.get("Cookie");
972
+ if (cookie != null) {
973
+ const out = {};
974
+ const c = cookie.split(";");
975
+ for (const kv of c) {
976
+ const [cookieKey, ...cookieVal] = kv.split("=");
977
+ assert(cookieKey != null);
978
+ const key = cookieKey.trim();
979
+ out[key] = cookieVal.join("=");
980
+ }
981
+ return out;
982
+ }
983
+ return {};
984
+ }
985
+ /**
986
+ * Set the cookie header properly in the headers
987
+ *
988
+ * @example
989
+ * ```ts
990
+ * import {
991
+ * Cookie,
992
+ * setCookie,
993
+ * } from "https://deno.land/std@$STD_VERSION/http/cookie.ts";
994
+ *
995
+ * const headers = new Headers();
996
+ * const cookie: Cookie = { name: "Space", value: "Cat" };
997
+ * setCookie(headers, cookie);
998
+ *
999
+ * const cookieHeader = headers.get("set-cookie");
1000
+ * console.log(cookieHeader); // Space=Cat
1001
+ * ```
1002
+ *
1003
+ * @param headers The headers instance to set the cookie to
1004
+ * @param cookie Cookie to set
1005
+ */
1006
+ function setCookie(headers, cookie) {
1007
+ // Parsing cookie headers to make consistent set-cookie header
1008
+ // ref: https://tools.ietf.org/html/rfc6265#section-4.1.1
1009
+ const v = toString(cookie);
1010
+ if (v) {
1011
+ headers.append("Set-Cookie", v);
1012
+ }
1013
+ }
1014
+
772
1015
  const GlobalRequest = global.Request;
773
1016
  class RapidRequest {
774
1017
  #raw;
775
1018
  #bodyParsed;
776
1019
  #body;
1020
+ #headers;
1021
+ #parsedCookies;
777
1022
  method;
778
1023
  url;
779
- headers;
780
1024
  constructor(req) {
781
1025
  this.#raw = req;
782
1026
  this.method = req.method;
783
1027
  this.url = new URL(req.url);
784
- this.headers = req.headers;
1028
+ this.#headers = req.headers;
785
1029
  }
786
1030
  async parseBody() {
787
1031
  if (this.#bodyParsed) {
@@ -791,7 +1035,7 @@ class RapidRequest {
791
1035
  const requestMethod = this.method;
792
1036
  if (requestMethod === "POST" || requestMethod === "PUT" || requestMethod === "PATCH") {
793
1037
  const req = this.#raw;
794
- const contentType = this.headers.get("Content-Type");
1038
+ const contentType = this.#headers.get("Content-Type");
795
1039
  if (contentType.includes("json")) {
796
1040
  this.#body = {
797
1041
  type: "json",
@@ -820,6 +1064,15 @@ class RapidRequest {
820
1064
  get rawRequest() {
821
1065
  return this.#raw;
822
1066
  }
1067
+ get headers() {
1068
+ return this.#headers;
1069
+ }
1070
+ get cookies() {
1071
+ if (!this.#parsedCookies) {
1072
+ this.#parsedCookies = getCookies(this.#headers);
1073
+ }
1074
+ return this.#parsedCookies;
1075
+ }
823
1076
  get body() {
824
1077
  if (!this.#bodyParsed) {
825
1078
  throw new Error("Request body not parsed, you should call 'parseBody()' method before getting the body.");
@@ -1299,9 +1552,9 @@ var bootstrapApplicationConfig = {
1299
1552
  required: true,
1300
1553
  },
1301
1554
  {
1302
- name: "handlers",
1303
- code: "handlers",
1304
- columnName: "handlers",
1555
+ name: "actions",
1556
+ code: "actions",
1557
+ columnName: "actions",
1305
1558
  type: "json",
1306
1559
  required: true,
1307
1560
  },
@@ -1316,7 +1569,7 @@ var bootstrapApplicationConfig = {
1316
1569
  type: "RESTful",
1317
1570
  method: "GET",
1318
1571
  endpoint: "/_meta/models",
1319
- handlers: [
1572
+ actions: [
1320
1573
  {
1321
1574
  code: "listMetaModels",
1322
1575
  },
@@ -1329,7 +1582,7 @@ var bootstrapApplicationConfig = {
1329
1582
  type: "RESTful",
1330
1583
  method: "GET",
1331
1584
  endpoint: "/_meta/models/:namespace/:singularCode",
1332
- handlers: [
1585
+ actions: [
1333
1586
  {
1334
1587
  code: "getMetaModelDetail",
1335
1588
  },
@@ -1342,7 +1595,7 @@ var bootstrapApplicationConfig = {
1342
1595
  type: "RESTful",
1343
1596
  method: "GET",
1344
1597
  endpoint: "/_meta/routes",
1345
- handlers: [
1598
+ actions: [
1346
1599
  {
1347
1600
  code: "listMetaRoutes",
1348
1601
  },
@@ -1363,7 +1616,7 @@ class RapidServer {
1363
1616
  #middlewares;
1364
1617
  #bootstrapApplicationConfig;
1365
1618
  #applicationConfig;
1366
- #httpHandlersMapByCode;
1619
+ #actionHandlersMapByCode;
1367
1620
  #databaseAccessor;
1368
1621
  queryBuilder;
1369
1622
  config;
@@ -1375,7 +1628,7 @@ class RapidServer {
1375
1628
  this.#middlewares = [];
1376
1629
  this.#bootstrapApplicationConfig = options.applicationConfig || bootstrapApplicationConfig;
1377
1630
  this.#applicationConfig = {};
1378
- this.#httpHandlersMapByCode = new Map();
1631
+ this.#actionHandlersMapByCode = new Map();
1379
1632
  this.#databaseAccessor = options.databaseAccessor;
1380
1633
  this.queryBuilder = new QueryBuilder({
1381
1634
  dbDefaultSchema: options.databaseConfig.dbDefaultSchema,
@@ -1415,7 +1668,7 @@ class RapidServer {
1415
1668
  const originalRoute = ___namespace.find(this.#applicationConfig.routes, (item) => item.code == route.code);
1416
1669
  if (originalRoute) {
1417
1670
  originalRoute.name = route.name;
1418
- originalRoute.handlers = route.handlers;
1671
+ originalRoute.actions = route.actions;
1419
1672
  }
1420
1673
  else {
1421
1674
  this.#applicationConfig.routes.push(route);
@@ -1423,12 +1676,12 @@ class RapidServer {
1423
1676
  }
1424
1677
  }
1425
1678
  }
1426
- registerHttpHandler(plugin, options) {
1679
+ registerActionHandler(plugin, options) {
1427
1680
  const handler = ___namespace.bind(options.handler, null, plugin);
1428
- this.#httpHandlersMapByCode.set(options.code, handler);
1681
+ this.#actionHandlersMapByCode.set(options.code, handler);
1429
1682
  }
1430
- getHttpHandlerByCode(code) {
1431
- return this.#httpHandlersMapByCode.get(code);
1683
+ getActionHandlerByCode(code) {
1684
+ return this.#actionHandlersMapByCode.get(code);
1432
1685
  }
1433
1686
  registerMiddleware(middleware) {
1434
1687
  this.#middlewares.push(middleware);
@@ -1474,7 +1727,7 @@ class RapidServer {
1474
1727
  await pluginManager.loadPlugins(this.#plugins);
1475
1728
  await pluginManager.initPlugins();
1476
1729
  await pluginManager.registerMiddlewares();
1477
- await pluginManager.registerHttpHandlers();
1730
+ await pluginManager.registerActionHandlers();
1478
1731
  await pluginManager.registerEventHandlers();
1479
1732
  await pluginManager.registerMessageHandlers();
1480
1733
  await pluginManager.registerTaskProcessors();
@@ -2305,10 +2558,10 @@ class MetaManager {
2305
2558
  }
2306
2559
  async registerMiddlewares(server) {
2307
2560
  }
2308
- async registerHttpHandlers(server) {
2309
- server.registerHttpHandler(this, listMetaModels);
2310
- server.registerHttpHandler(this, listMetaRoutes);
2311
- server.registerHttpHandler(this, getMetaModelDetail);
2561
+ async registerActionHandlers(server) {
2562
+ server.registerActionHandler(this, listMetaModels);
2563
+ server.registerActionHandler(this, listMetaRoutes);
2564
+ server.registerActionHandler(this, getMetaModelDetail);
2312
2565
  }
2313
2566
  async registerEventHandlers(server) {
2314
2567
  server.registerEventHandler("entity.create", handleEntityCreateEvent.bind(this, server));
@@ -2596,7 +2849,7 @@ function doNotMergeArray(objValue, srcValue) {
2596
2849
  }
2597
2850
  }
2598
2851
 
2599
- async function runCollectionEntityHttpHandler(ctx, options, code, handleDataAccess) {
2852
+ async function runCollectionEntityActionHandler(ctx, options, code, handleDataAccess) {
2600
2853
  const { server, input } = ctx;
2601
2854
  const { defaultInput, fixedInput } = options;
2602
2855
  console.debug(`Running ${code} handler...`);
@@ -2654,7 +2907,7 @@ function transformFilterWithSubFilters(filter) {
2654
2907
 
2655
2908
  const code$g = "findCollectionEntities";
2656
2909
  async function handler$g(plugin, ctx, options) {
2657
- await runCollectionEntityHttpHandler(ctx, options, code$g, async (dataAccessor, input) => {
2910
+ await runCollectionEntityActionHandler(ctx, options, code$g, async (dataAccessor, input) => {
2658
2911
  input.filters = removeFiltersWithNullValue(input.filters);
2659
2912
  const entities = await findEntities(ctx.server, dataAccessor, input);
2660
2913
  const result = { list: entities };
@@ -2667,7 +2920,7 @@ async function handler$g(plugin, ctx, options) {
2667
2920
  });
2668
2921
  }
2669
2922
 
2670
- var findCollectionEntitiesHttpHandler = /*#__PURE__*/Object.freeze({
2923
+ var findCollectionEntities = /*#__PURE__*/Object.freeze({
2671
2924
  __proto__: null,
2672
2925
  code: code$g,
2673
2926
  handler: handler$g
@@ -2702,7 +2955,7 @@ var findCollectionEntityById = /*#__PURE__*/Object.freeze({
2702
2955
 
2703
2956
  const code$e = "countCollectionEntities";
2704
2957
  async function handler$e(plugin, ctx, options) {
2705
- await runCollectionEntityHttpHandler(ctx, options, code$e, (dataAccessor, input) => {
2958
+ await runCollectionEntityActionHandler(ctx, options, code$e, (dataAccessor, input) => {
2706
2959
  input.filters = removeFiltersWithNullValue(input.filters);
2707
2960
  return dataAccessor.count(input);
2708
2961
  });
@@ -3076,17 +3329,17 @@ class DataManager {
3076
3329
  }
3077
3330
  async registerMiddlewares(server) {
3078
3331
  }
3079
- async registerHttpHandlers(server) {
3080
- server.registerHttpHandler(this, findCollectionEntitiesHttpHandler);
3081
- server.registerHttpHandler(this, findCollectionEntityById);
3082
- server.registerHttpHandler(this, countCollectionEntities);
3083
- server.registerHttpHandler(this, createCollectionEntity);
3084
- server.registerHttpHandler(this, createCollectionEntitiesBatch);
3085
- server.registerHttpHandler(this, updateCollectionEntityById);
3086
- server.registerHttpHandler(this, addEntityRelations);
3087
- server.registerHttpHandler(this, removeEntityRelations);
3088
- server.registerHttpHandler(this, deleteCollectionEntityById);
3089
- server.registerHttpHandler(this, queryDatabase);
3332
+ async registerActionHandlers(server) {
3333
+ server.registerActionHandler(this, findCollectionEntities);
3334
+ server.registerActionHandler(this, findCollectionEntityById);
3335
+ server.registerActionHandler(this, countCollectionEntities);
3336
+ server.registerActionHandler(this, createCollectionEntity);
3337
+ server.registerActionHandler(this, createCollectionEntitiesBatch);
3338
+ server.registerActionHandler(this, updateCollectionEntityById);
3339
+ server.registerActionHandler(this, addEntityRelations);
3340
+ server.registerActionHandler(this, removeEntityRelations);
3341
+ server.registerActionHandler(this, deleteCollectionEntityById);
3342
+ server.registerActionHandler(this, queryDatabase);
3090
3343
  }
3091
3344
  async registerEventHandlers(server) {
3092
3345
  }
@@ -3113,7 +3366,7 @@ class DataManager {
3113
3366
  type: "RESTful",
3114
3367
  method: routeConfig.method,
3115
3368
  endpoint: `/${namespace}/${pluralCode}${routeConfig.endpoint}`,
3116
- handlers: [
3369
+ actions: [
3117
3370
  {
3118
3371
  code: routeConfig.handlerCode,
3119
3372
  config: {
@@ -3219,8 +3472,8 @@ class RouteManager {
3219
3472
  }
3220
3473
  async registerMiddlewares(server) {
3221
3474
  }
3222
- async registerHttpHandlers(server) {
3223
- server.registerHttpHandler(this, httpProxy);
3475
+ async registerActionHandlers(server) {
3476
+ server.registerActionHandler(this, httpProxy);
3224
3477
  }
3225
3478
  async registerEventHandlers(server) {
3226
3479
  // TODO: Should rebuild routes after route configurations changed.
@@ -3377,7 +3630,7 @@ class WebhooksPlugin {
3377
3630
  }
3378
3631
  async registerMiddlewares(server) {
3379
3632
  }
3380
- async registerHttpHandlers(server) {
3633
+ async registerActionHandlers(server) {
3381
3634
  }
3382
3635
  async registerEventHandlers(server) {
3383
3636
  const events = [
@@ -3450,209 +3703,6 @@ class WebhooksPlugin {
3450
3703
  }
3451
3704
  }
3452
3705
 
3453
- // Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
3454
- class AssertionError extends Error {
3455
- name = "AssertionError";
3456
- constructor(message) {
3457
- super(message);
3458
- }
3459
- }
3460
-
3461
- // Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
3462
- /** Make an assertion, error will be thrown if `expr` does not have truthy value. */
3463
- function assert(expr, msg = "") {
3464
- if (!expr) {
3465
- throw new AssertionError(msg);
3466
- }
3467
- }
3468
-
3469
- // Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
3470
- // This module is browser compatible.
3471
- /**
3472
- * Formats the given date to IMF date time format. (Reference:
3473
- * https://tools.ietf.org/html/rfc7231#section-7.1.1.1).
3474
- * IMF is the time format to use when generating times in HTTP
3475
- * headers. The time being formatted must be in UTC for Format to
3476
- * generate the correct format.
3477
- *
3478
- * @example
3479
- * ```ts
3480
- * import { toIMF } from "https://deno.land/std@$STD_VERSION/datetime/to_imf.ts";
3481
- *
3482
- * toIMF(new Date(0)); // => returns "Thu, 01 Jan 1970 00:00:00 GMT"
3483
- * ```
3484
- * @param date Date to parse
3485
- * @return IMF date formatted string
3486
- */
3487
- function toIMF(date) {
3488
- function dtPad(v, lPad = 2) {
3489
- return v.padStart(lPad, "0");
3490
- }
3491
- const d = dtPad(date.getUTCDate().toString());
3492
- const h = dtPad(date.getUTCHours().toString());
3493
- const min = dtPad(date.getUTCMinutes().toString());
3494
- const s = dtPad(date.getUTCSeconds().toString());
3495
- const y = date.getUTCFullYear();
3496
- const days = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"];
3497
- const months = [
3498
- "Jan",
3499
- "Feb",
3500
- "Mar",
3501
- "Apr",
3502
- "May",
3503
- "Jun",
3504
- "Jul",
3505
- "Aug",
3506
- "Sep",
3507
- "Oct",
3508
- "Nov",
3509
- "Dec",
3510
- ];
3511
- return `${days[date.getUTCDay()]}, ${d} ${months[date.getUTCMonth()]} ${y} ${h}:${min}:${s} GMT`;
3512
- }
3513
-
3514
- // Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
3515
- const FIELD_CONTENT_REGEXP = /^(?=[\x20-\x7E]*$)[^()@<>,;:\\"\[\]?={}\s]+$/;
3516
- function toString(cookie) {
3517
- if (!cookie.name) {
3518
- return "";
3519
- }
3520
- const out = [];
3521
- validateName(cookie.name);
3522
- validateValue(cookie.name, cookie.value);
3523
- out.push(`${cookie.name}=${cookie.value}`);
3524
- // Fallback for invalid Set-Cookie
3525
- // ref: https://tools.ietf.org/html/draft-ietf-httpbis-cookie-prefixes-00#section-3.1
3526
- if (cookie.name.startsWith("__Secure")) {
3527
- cookie.secure = true;
3528
- }
3529
- if (cookie.name.startsWith("__Host")) {
3530
- cookie.path = "/";
3531
- cookie.secure = true;
3532
- delete cookie.domain;
3533
- }
3534
- if (cookie.secure) {
3535
- out.push("Secure");
3536
- }
3537
- if (cookie.httpOnly) {
3538
- out.push("HttpOnly");
3539
- }
3540
- if (typeof cookie.maxAge === "number" && Number.isInteger(cookie.maxAge)) {
3541
- assert(cookie.maxAge >= 0, "Max-Age must be an integer superior or equal to 0");
3542
- out.push(`Max-Age=${cookie.maxAge}`);
3543
- }
3544
- if (cookie.domain) {
3545
- validateDomain(cookie.domain);
3546
- out.push(`Domain=${cookie.domain}`);
3547
- }
3548
- if (cookie.sameSite) {
3549
- out.push(`SameSite=${cookie.sameSite}`);
3550
- }
3551
- if (cookie.path) {
3552
- validatePath(cookie.path);
3553
- out.push(`Path=${cookie.path}`);
3554
- }
3555
- if (cookie.expires) {
3556
- const { expires } = cookie;
3557
- const dateString = toIMF(typeof expires === "number" ? new Date(expires) : expires);
3558
- out.push(`Expires=${dateString}`);
3559
- }
3560
- if (cookie.unparsed) {
3561
- out.push(cookie.unparsed.join("; "));
3562
- }
3563
- return out.join("; ");
3564
- }
3565
- /**
3566
- * Validate Cookie Name.
3567
- * @param name Cookie name.
3568
- */
3569
- function validateName(name) {
3570
- if (name && !FIELD_CONTENT_REGEXP.test(name)) {
3571
- throw new TypeError(`Invalid cookie name: "${name}".`);
3572
- }
3573
- }
3574
- /**
3575
- * Validate Path Value.
3576
- * See {@link https://tools.ietf.org/html/rfc6265#section-4.1.2.4}.
3577
- * @param path Path value.
3578
- */
3579
- function validatePath(path) {
3580
- if (path == null) {
3581
- return;
3582
- }
3583
- for (let i = 0; i < path.length; i++) {
3584
- const c = path.charAt(i);
3585
- if (c < String.fromCharCode(0x20) || c > String.fromCharCode(0x7E) || c == ";") {
3586
- throw new Error(path + ": Invalid cookie path char '" + c + "'");
3587
- }
3588
- }
3589
- }
3590
- /**
3591
- * Validate Cookie Value.
3592
- * See {@link https://tools.ietf.org/html/rfc6265#section-4.1}.
3593
- * @param value Cookie value.
3594
- */
3595
- function validateValue(name, value) {
3596
- if (value == null || name == null)
3597
- return;
3598
- for (let i = 0; i < value.length; i++) {
3599
- const c = value.charAt(i);
3600
- if (c < String.fromCharCode(0x21) || c == String.fromCharCode(0x22) ||
3601
- c == String.fromCharCode(0x2c) || c == String.fromCharCode(0x3b) ||
3602
- c == String.fromCharCode(0x5c) || c == String.fromCharCode(0x7f)) {
3603
- throw new Error("RFC2616 cookie '" + name + "' cannot contain character '" + c + "'");
3604
- }
3605
- if (c > String.fromCharCode(0x80)) {
3606
- throw new Error("RFC2616 cookie '" + name + "' can only have US-ASCII chars as value" +
3607
- c.charCodeAt(0).toString(16));
3608
- }
3609
- }
3610
- }
3611
- /**
3612
- * Validate Cookie Domain.
3613
- * See {@link https://datatracker.ietf.org/doc/html/rfc6265#section-4.1.2.3}.
3614
- * @param domain Cookie domain.
3615
- */
3616
- function validateDomain(domain) {
3617
- if (domain == null) {
3618
- return;
3619
- }
3620
- const char1 = domain.charAt(0);
3621
- const charN = domain.charAt(domain.length - 1);
3622
- if (char1 == "-" || charN == "." || charN == "-") {
3623
- throw new Error("Invalid first/last char in cookie domain: " + domain);
3624
- }
3625
- }
3626
- /**
3627
- * Set the cookie header properly in the headers
3628
- *
3629
- * @example
3630
- * ```ts
3631
- * import {
3632
- * Cookie,
3633
- * setCookie,
3634
- * } from "https://deno.land/std@$STD_VERSION/http/cookie.ts";
3635
- *
3636
- * const headers = new Headers();
3637
- * const cookie: Cookie = { name: "Space", value: "Cat" };
3638
- * setCookie(headers, cookie);
3639
- *
3640
- * const cookieHeader = headers.get("set-cookie");
3641
- * console.log(cookieHeader); // Space=Cat
3642
- * ```
3643
- *
3644
- * @param headers The headers instance to set the cookie to
3645
- * @param cookie Cookie to set
3646
- */
3647
- function setCookie(headers, cookie) {
3648
- // Parsing cookie headers to make consistent set-cookie header
3649
- // ref: https://tools.ietf.org/html/rfc6265#section-4.1.1
3650
- const v = toString(cookie);
3651
- if (v) {
3652
- headers.append("Set-Cookie", v);
3653
- }
3654
- }
3655
-
3656
3706
  const code$5 = "createSession";
3657
3707
  async function handler$5(plugin, ctx, options) {
3658
3708
  const { server, input, routerContext } = ctx;
@@ -3752,7 +3802,7 @@ var getMyProfile$1 = /*#__PURE__*/Object.freeze({
3752
3802
  handler: handler$3
3753
3803
  });
3754
3804
 
3755
- var pluginHttpHandlers = [
3805
+ var pluginActionHandlers = [
3756
3806
  createSession,
3757
3807
  deleteSession,
3758
3808
  getMyProfile$1,
@@ -3824,7 +3874,7 @@ var getMyProfile = {
3824
3874
  type: "RESTful",
3825
3875
  method: "GET",
3826
3876
  endpoint: "/me",
3827
- handlers: [
3877
+ actions: [
3828
3878
  {
3829
3879
  code: "getMyProfile",
3830
3880
  },
@@ -3838,7 +3888,7 @@ var signin = {
3838
3888
  type: "RESTful",
3839
3889
  method: "POST",
3840
3890
  endpoint: "/signin",
3841
- handlers: [
3891
+ actions: [
3842
3892
  {
3843
3893
  code: "createSession",
3844
3894
  },
@@ -3852,7 +3902,7 @@ var signout = {
3852
3902
  type: "RESTful",
3853
3903
  method: "GET",
3854
3904
  endpoint: "/signout",
3855
- handlers: [
3905
+ actions: [
3856
3906
  {
3857
3907
  code: "deleteSession",
3858
3908
  },
@@ -3868,7 +3918,7 @@ var pluginRoutes = [
3868
3918
  /**
3869
3919
  * Auth manager plugin
3870
3920
  */
3871
- class AuthManager {
3921
+ class AuthPlugin {
3872
3922
  get code() {
3873
3923
  return "authManager";
3874
3924
  }
@@ -3888,9 +3938,9 @@ class AuthManager {
3888
3938
  }
3889
3939
  async registerMiddlewares(server) {
3890
3940
  }
3891
- async registerHttpHandlers(server) {
3892
- for (const httpHandler of pluginHttpHandlers) {
3893
- server.registerHttpHandler(this, httpHandler);
3941
+ async registerActionHandlers(server) {
3942
+ for (const actionHandler of pluginActionHandlers) {
3943
+ server.registerActionHandler(this, actionHandler);
3894
3944
  }
3895
3945
  }
3896
3946
  async registerEventHandlers(server) {
@@ -3914,6 +3964,26 @@ class AuthManager {
3914
3964
  }
3915
3965
  async onApplicationReady(server, applicationConfig) {
3916
3966
  }
3967
+ async onPrepareRouteContext(server, routeContext) {
3968
+ const request = routeContext.request;
3969
+ let token;
3970
+ const headers = request.headers;
3971
+ // No Authorization header
3972
+ if (headers.has("Authorization")) {
3973
+ // Authorization header has no Bearer or no token
3974
+ const authHeader = headers.get("Authorization");
3975
+ if (!authHeader.startsWith("Bearer ") || authHeader.length <= 7) {
3976
+ throw new Error('AUTHORIZATION_HEADER_INVALID');
3977
+ }
3978
+ token = authHeader.slice(7);
3979
+ }
3980
+ else {
3981
+ token = request.cookies[server.config.sessionCookieName];
3982
+ }
3983
+ const tokenPayload = verifyJwt(token, server.config.jwtKey);
3984
+ routeContext.state.userId = tokenPayload.aud;
3985
+ routeContext.state.userLogin = tokenPayload.act;
3986
+ }
3917
3987
  }
3918
3988
 
3919
3989
  async function readFile(path) {
@@ -3968,7 +4038,7 @@ async function handler$2(plugin, ctx, options) {
3968
4038
  response.headers.set("Content-Disposition", `attachment; filename="${encodeURIComponent(attachmentFileName)}"`);
3969
4039
  }
3970
4040
 
3971
- var downloadDocumentHttpHandler = /*#__PURE__*/Object.freeze({
4041
+ var downloadDocumentActionHandler = /*#__PURE__*/Object.freeze({
3972
4042
  __proto__: null,
3973
4043
  code: code$2,
3974
4044
  handler: handler$2
@@ -3993,7 +4063,7 @@ async function handler$1(plugin, ctx, options) {
3993
4063
  response.headers.set("Content-Disposition", `attachment; filename="${encodeURIComponent(attachmentFileName)}"`);
3994
4064
  }
3995
4065
 
3996
- var downloadFileHttpHandler = /*#__PURE__*/Object.freeze({
4066
+ var downloadFileActionHandler = /*#__PURE__*/Object.freeze({
3997
4067
  __proto__: null,
3998
4068
  code: code$1,
3999
4069
  handler: handler$1
@@ -4019,7 +4089,7 @@ async function handler(plugin, ctx, options) {
4019
4089
  ctx.output = { ok: true, fileKey };
4020
4090
  }
4021
4091
 
4022
- var uploadFileHttpHandler = /*#__PURE__*/Object.freeze({
4092
+ var uploadFileActionHandler = /*#__PURE__*/Object.freeze({
4023
4093
  __proto__: null,
4024
4094
  code: code,
4025
4095
  handler: handler
@@ -4048,10 +4118,10 @@ class FileManager {
4048
4118
  }
4049
4119
  async registerMiddlewares(server) {
4050
4120
  }
4051
- async registerHttpHandlers(server) {
4052
- server.registerHttpHandler(this, downloadDocumentHttpHandler);
4053
- server.registerHttpHandler(this, downloadFileHttpHandler);
4054
- server.registerHttpHandler(this, uploadFileHttpHandler);
4121
+ async registerActionHandlers(server) {
4122
+ server.registerActionHandler(this, downloadDocumentActionHandler);
4123
+ server.registerActionHandler(this, downloadFileActionHandler);
4124
+ server.registerActionHandler(this, uploadFileActionHandler);
4055
4125
  }
4056
4126
  async registerEventHandlers(server) {
4057
4127
  }
@@ -4076,7 +4146,7 @@ class FileManager {
4076
4146
 
4077
4147
  fixBigIntJSONSerialize();
4078
4148
 
4079
- exports.AuthPlugin = AuthManager;
4149
+ exports.AuthPlugin = AuthPlugin;
4080
4150
  exports.DataManagePlugin = DataManager;
4081
4151
  exports.FileManagePlugin = FileManager;
4082
4152
  exports.GlobalRequest = GlobalRequest;