@elek-io/core 0.11.1 → 0.12.0

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.
@@ -8,7 +8,16 @@ var __export = (target, all) => {
8
8
  import Fs7 from "fs-extra";
9
9
 
10
10
  // package.json
11
- var version = "0.11.1";
11
+ var version = "0.12.0";
12
+
13
+ // src/api/index.ts
14
+ import { serve } from "@hono/node-server";
15
+ import { swaggerUI } from "@hono/swagger-ui";
16
+ import { OpenAPIHono as OpenAPIHono2 } from "@hono/zod-openapi";
17
+ import { cors } from "hono/cors";
18
+
19
+ // src/api/v1/projects.ts
20
+ import { createRoute, OpenAPIHono, z as z13 } from "@hono/zod-openapi";
12
21
 
13
22
  // src/schema/assetSchema.ts
14
23
  import z4 from "zod";
@@ -743,7 +752,7 @@ var constructorElekIoCoreSchema = elekIoCoreOptionsSchema.partial({
743
752
  }).optional();
744
753
 
745
754
  // src/schema/projectSchema.ts
746
- import { z as z10 } from "zod";
755
+ import { z as z10 } from "@hono/zod-openapi";
747
756
  var projectStatusSchema = z10.enum(["foo", "bar", "todo"]);
748
757
  var projectSettingsSchema = z10.object({
749
758
  language: z10.object({
@@ -771,17 +780,16 @@ var projectFileSchema = baseFileSchema.extend({
771
780
  settings: projectSettingsSchema
772
781
  });
773
782
  var projectSchema = projectFileSchema.extend({
774
- remoteOriginUrl: z10.string().nullable(),
775
- /**
776
- * Commit history of this Project
777
- */
778
- history: z10.array(gitCommitSchema),
779
- /**
780
- * Full commit history of this Project
781
- * including all Assets, Collections, Entries and other files
782
- */
783
- fullHistory: z10.array(gitCommitSchema)
784
- });
783
+ remoteOriginUrl: z10.string().nullable().openapi({
784
+ description: "URL of the remote Git repository"
785
+ }),
786
+ history: z10.array(gitCommitSchema).openapi({
787
+ description: "Commit history of this Project"
788
+ }),
789
+ fullHistory: z10.array(gitCommitSchema).openapi({
790
+ description: "Full commit history of this Project including all Assets, Collections, Entries and other files"
791
+ })
792
+ }).openapi("Project");
785
793
  var outdatedProjectSchema = projectFileSchema.pick({
786
794
  id: true,
787
795
  name: true,
@@ -881,10 +889,24 @@ var serviceTypeSchema = z11.enum([
881
889
  "Entry",
882
890
  "Value"
883
891
  ]);
892
+ function paginatedListOf(schema) {
893
+ return z11.object({
894
+ total: z11.number(),
895
+ limit: z11.number(),
896
+ offset: z11.number(),
897
+ list: z11.array(schema)
898
+ });
899
+ }
884
900
  var listSchema = z11.object({
885
901
  projectId: uuidSchema,
886
- limit: z11.number().optional(),
887
- offset: z11.number().optional()
902
+ limit: z11.number().optional().openapi({
903
+ default: 15,
904
+ description: "The maximum number of items to return"
905
+ }),
906
+ offset: z11.number().optional().openapi({
907
+ default: 0,
908
+ description: "The number of items to skip before starting to collect the result set"
909
+ })
888
910
  });
889
911
  var listCollectionsSchema = listSchema;
890
912
  var listEntriesSchema = listSchema.extend({
@@ -904,6 +926,16 @@ var UserTypeSchema = z12.enum(["local", "cloud"]);
904
926
  var baseUserSchema = gitSignatureSchema.extend({
905
927
  userType: UserTypeSchema,
906
928
  language: supportedLanguageSchema,
929
+ localApi: z12.object({
930
+ /**
931
+ * If set to true the local API is started whenever Core is initialized
932
+ */
933
+ isEnabled: z12.boolean(),
934
+ /**
935
+ * The port the local API uses
936
+ */
937
+ port: z12.number()
938
+ }),
907
939
  window: z12.object({
908
940
  width: z12.number(),
909
941
  height: z12.number(),
@@ -924,6 +956,204 @@ var userFileSchema = z12.union([localUserSchema, cloudUserSchema]);
924
956
  var userSchema = userFileSchema;
925
957
  var setUserSchema = userSchema;
926
958
 
959
+ // src/api/v1/projects.ts
960
+ var ProjectsApiV1 = class {
961
+ api;
962
+ projectService;
963
+ constructor(projectService) {
964
+ this.projectService = projectService;
965
+ this.api = new OpenAPIHono();
966
+ this.registerRoutes();
967
+ }
968
+ registerRoutes() {
969
+ this.api.openapi(countProjectsRoute, async (context) => {
970
+ const count = await this.projectService.count();
971
+ return context.json(count, 200);
972
+ });
973
+ this.api.openapi(listProjectsRoute, async (context) => {
974
+ const { limit, offset } = context.req.valid("query");
975
+ const projects = await this.projectService.list({ limit, offset });
976
+ return context.json(projects, 200);
977
+ });
978
+ this.api.openapi(readProjectRoute, async (context) => {
979
+ const { id } = context.req.valid("param");
980
+ const project = await this.projectService.read({ id });
981
+ return context.json(project, 200);
982
+ });
983
+ }
984
+ };
985
+ var countProjectsRoute = createRoute({
986
+ tags: ["Projects"],
987
+ description: "Counts all Projects you currently have access to",
988
+ method: "get",
989
+ path: "/count",
990
+ operationId: "countProjects",
991
+ responses: {
992
+ 200: {
993
+ content: {
994
+ "application/json": {
995
+ schema: z13.number()
996
+ }
997
+ },
998
+ description: "The number of Projects you have acces to"
999
+ }
1000
+ }
1001
+ });
1002
+ var listProjectsRoute = createRoute({
1003
+ tags: ["Projects"],
1004
+ description: "Lists all Projects you currently have access to",
1005
+ method: "get",
1006
+ path: "/",
1007
+ operationId: "listProjects",
1008
+ request: {
1009
+ query: listProjectsSchema
1010
+ },
1011
+ responses: {
1012
+ 200: {
1013
+ content: {
1014
+ "application/json": {
1015
+ schema: paginatedListOf(projectSchema)
1016
+ }
1017
+ },
1018
+ description: "A list of Projects you have access to"
1019
+ }
1020
+ }
1021
+ });
1022
+ var readProjectRoute = createRoute({
1023
+ tags: ["Projects"],
1024
+ description: "Retrieve a Project by ID",
1025
+ method: "get",
1026
+ path: "/{id}",
1027
+ operationId: "readProject",
1028
+ request: {
1029
+ params: z13.object({
1030
+ id: uuidSchema.openapi({
1031
+ param: {
1032
+ name: "id",
1033
+ in: "path"
1034
+ }
1035
+ })
1036
+ })
1037
+ },
1038
+ responses: {
1039
+ 200: {
1040
+ content: {
1041
+ "application/json": {
1042
+ schema: projectSchema
1043
+ }
1044
+ },
1045
+ description: "The requested Project"
1046
+ },
1047
+ 404: {
1048
+ description: "The requested Project does not exist or you have no right to access it"
1049
+ }
1050
+ }
1051
+ });
1052
+
1053
+ // src/api/index.ts
1054
+ var LocalApi = class {
1055
+ logService;
1056
+ projectService;
1057
+ api;
1058
+ server = null;
1059
+ constructor(logService, projectService) {
1060
+ this.logService = logService;
1061
+ this.projectService = projectService;
1062
+ this.api = new OpenAPIHono2();
1063
+ this.api.use(
1064
+ cors({
1065
+ origin: ["http://localhost"]
1066
+ })
1067
+ );
1068
+ this.registerRoutes();
1069
+ this.api.doc("/openapi.json", {
1070
+ openapi: "3.0.0",
1071
+ info: {
1072
+ version: "0.1.0",
1073
+ title: "elek.io Project API",
1074
+ description: "This API allows reading data from elek.io Projects"
1075
+ },
1076
+ servers: [
1077
+ {
1078
+ url: "http://localhost:{port}/v1/",
1079
+ description: "Local development API",
1080
+ variables: {
1081
+ port: {
1082
+ default: 31310,
1083
+ description: "The port specified in elek.io Clients user configuration"
1084
+ }
1085
+ }
1086
+ },
1087
+ {
1088
+ url: "https://api.elek.io/v1/",
1089
+ description: "Public production API",
1090
+ variables: {}
1091
+ }
1092
+ ],
1093
+ tags: [
1094
+ {
1095
+ name: "Projects",
1096
+ description: "Retrieve information about Projects",
1097
+ externalDocs: { url: "https://elek.io/docs/projects" }
1098
+ },
1099
+ {
1100
+ name: "Collections",
1101
+ description: "Retrieve information about Collections",
1102
+ externalDocs: { url: "https://elek.io/docs/collections" }
1103
+ },
1104
+ {
1105
+ name: "Entries",
1106
+ description: "Retrieve information about Entries",
1107
+ externalDocs: { url: "https://elek.io/docs/entries" }
1108
+ },
1109
+ {
1110
+ name: "Assets",
1111
+ description: "Retrieve information about Assets",
1112
+ externalDocs: { url: "https://elek.io/docs/assets" }
1113
+ }
1114
+ ]
1115
+ });
1116
+ this.api.get("/ui", swaggerUI({ url: "/openapi.json" }));
1117
+ }
1118
+ /**
1119
+ * Starts the local API on given port
1120
+ */
1121
+ start(port) {
1122
+ this.server = serve(
1123
+ {
1124
+ fetch: this.api.fetch,
1125
+ port
1126
+ },
1127
+ (info) => {
1128
+ this.logService.info(
1129
+ `Started local API on ${info.address}:${info.port} (${info.family})`
1130
+ );
1131
+ }
1132
+ );
1133
+ }
1134
+ /**
1135
+ * Stops the local API
1136
+ */
1137
+ stop() {
1138
+ this.server?.close(() => {
1139
+ this.logService.info("Stopped local API");
1140
+ });
1141
+ }
1142
+ /**
1143
+ * Returns true if the local API is running
1144
+ */
1145
+ isRunning() {
1146
+ if (this.server?.listening) {
1147
+ return true;
1148
+ }
1149
+ return false;
1150
+ }
1151
+ registerRoutes() {
1152
+ const projectsV1 = new ProjectsApiV1(this.projectService);
1153
+ this.api.route("/v1/projects", projectsV1.api);
1154
+ }
1155
+ };
1156
+
927
1157
  // src/error/GitError.ts
928
1158
  var GitError = class extends Error {
929
1159
  constructor(message) {
@@ -3443,7 +3673,7 @@ var UserService = class {
3443
3673
  return await this.jsonFileService.read(pathTo.userFile, userFileSchema);
3444
3674
  } catch (error) {
3445
3675
  this.logService.info("No User found");
3446
- return void 0;
3676
+ return null;
3447
3677
  }
3448
3678
  }
3449
3679
  /**
@@ -3477,6 +3707,7 @@ var ElekIoCore = class {
3477
3707
  projectService;
3478
3708
  collectionService;
3479
3709
  entryService;
3710
+ localApi;
3480
3711
  constructor(props) {
3481
3712
  this.coreVersion = version;
3482
3713
  const parsedProps = constructorElekIoCoreSchema.parse(props);
@@ -3527,6 +3758,7 @@ var ElekIoCore = class {
3527
3758
  this.collectionService,
3528
3759
  this.entryService
3529
3760
  );
3761
+ this.localApi = new LocalApi(this.logService, this.projectService);
3530
3762
  this.logService.info(`Initializing elek.io Core ${this.coreVersion}`, {
3531
3763
  options: this.options
3532
3764
  });
@@ -3582,6 +3814,13 @@ var ElekIoCore = class {
3582
3814
  get entries() {
3583
3815
  return this.entryService;
3584
3816
  }
3817
+ /**
3818
+ * Allows starting and stopping a REST API
3819
+ * to allow developers to read local Project data
3820
+ */
3821
+ get api() {
3822
+ return this.localApi;
3823
+ }
3585
3824
  };
3586
3825
  export {
3587
3826
  BooleanFieldDefinitionBaseSchema,
@@ -3661,6 +3900,7 @@ export {
3661
3900
  numberFieldDefinitionSchema,
3662
3901
  objectTypeSchema,
3663
3902
  outdatedProjectSchema,
3903
+ paginatedListOf,
3664
3904
  projectBranchSchema,
3665
3905
  projectExportSchema,
3666
3906
  projectFileSchema,