@restura/core 0.1.2 → 0.2.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.
package/dist/index.d.mts CHANGED
@@ -373,8 +373,10 @@ declare const responseDataSchema: z.ZodObject<{
373
373
  order: "ASC" | "DESC";
374
374
  } | undefined;
375
375
  }>>;
376
+ type: z.ZodOptional<z.ZodString>;
376
377
  }, "strict", z.ZodTypeAny, {
377
378
  name: string;
379
+ type?: string | undefined;
378
380
  selector?: string | undefined;
379
381
  subquery?: {
380
382
  table: string;
@@ -407,6 +409,7 @@ declare const responseDataSchema: z.ZodObject<{
407
409
  } | undefined;
408
410
  }, {
409
411
  name: string;
412
+ type?: string | undefined;
410
413
  selector?: string | undefined;
411
414
  subquery?: {
412
415
  table: string;
@@ -663,8 +666,10 @@ declare const standardRouteSchema: z.ZodObject<z.objectUtil.extendShape<{
663
666
  order: "ASC" | "DESC";
664
667
  } | undefined;
665
668
  }>>;
669
+ type: z.ZodOptional<z.ZodString>;
666
670
  }, "strict", z.ZodTypeAny, {
667
671
  name: string;
672
+ type?: string | undefined;
668
673
  selector?: string | undefined;
669
674
  subquery?: {
670
675
  table: string;
@@ -697,6 +702,7 @@ declare const standardRouteSchema: z.ZodObject<z.objectUtil.extendShape<{
697
702
  } | undefined;
698
703
  }, {
699
704
  name: string;
705
+ type?: string | undefined;
700
706
  selector?: string | undefined;
701
707
  subquery?: {
702
708
  table: string;
@@ -790,6 +796,7 @@ declare const standardRouteSchema: z.ZodObject<z.objectUtil.extendShape<{
790
796
  }[];
791
797
  response: {
792
798
  name: string;
799
+ type?: string | undefined;
793
800
  selector?: string | undefined;
794
801
  subquery?: {
795
802
  table: string;
@@ -869,6 +876,7 @@ declare const standardRouteSchema: z.ZodObject<z.objectUtil.extendShape<{
869
876
  }[];
870
877
  response: {
871
878
  name: string;
879
+ type?: string | undefined;
872
880
  selector?: string | undefined;
873
881
  subquery?: {
874
882
  table: string;
@@ -1563,8 +1571,10 @@ declare const resturaSchema: z.ZodObject<{
1563
1571
  order: "ASC" | "DESC";
1564
1572
  } | undefined;
1565
1573
  }>>;
1574
+ type: z.ZodOptional<z.ZodString>;
1566
1575
  }, "strict", z.ZodTypeAny, {
1567
1576
  name: string;
1577
+ type?: string | undefined;
1568
1578
  selector?: string | undefined;
1569
1579
  subquery?: {
1570
1580
  table: string;
@@ -1597,6 +1607,7 @@ declare const resturaSchema: z.ZodObject<{
1597
1607
  } | undefined;
1598
1608
  }, {
1599
1609
  name: string;
1610
+ type?: string | undefined;
1600
1611
  selector?: string | undefined;
1601
1612
  subquery?: {
1602
1613
  table: string;
@@ -1690,6 +1701,7 @@ declare const resturaSchema: z.ZodObject<{
1690
1701
  }[];
1691
1702
  response: {
1692
1703
  name: string;
1704
+ type?: string | undefined;
1693
1705
  selector?: string | undefined;
1694
1706
  subquery?: {
1695
1707
  table: string;
@@ -1769,6 +1781,7 @@ declare const resturaSchema: z.ZodObject<{
1769
1781
  }[];
1770
1782
  response: {
1771
1783
  name: string;
1784
+ type?: string | undefined;
1772
1785
  selector?: string | undefined;
1773
1786
  subquery?: {
1774
1787
  table: string;
@@ -1942,6 +1955,7 @@ declare const resturaSchema: z.ZodObject<{
1942
1955
  }[];
1943
1956
  response: {
1944
1957
  name: string;
1958
+ type?: string | undefined;
1945
1959
  selector?: string | undefined;
1946
1960
  subquery?: {
1947
1961
  table: string;
@@ -2048,6 +2062,7 @@ declare const resturaSchema: z.ZodObject<{
2048
2062
  }[];
2049
2063
  response: {
2050
2064
  name: string;
2065
+ type?: string | undefined;
2051
2066
  selector?: string | undefined;
2052
2067
  subquery?: {
2053
2068
  table: string;
@@ -2197,6 +2212,7 @@ declare const resturaSchema: z.ZodObject<{
2197
2212
  }[];
2198
2213
  response: {
2199
2214
  name: string;
2215
+ type?: string | undefined;
2200
2216
  selector?: string | undefined;
2201
2217
  subquery?: {
2202
2218
  table: string;
@@ -2345,6 +2361,7 @@ declare const resturaSchema: z.ZodObject<{
2345
2361
  }[];
2346
2362
  response: {
2347
2363
  name: string;
2364
+ type?: string | undefined;
2348
2365
  selector?: string | undefined;
2349
2366
  subquery?: {
2350
2367
  table: string;
package/dist/index.d.ts CHANGED
@@ -373,8 +373,10 @@ declare const responseDataSchema: z.ZodObject<{
373
373
  order: "ASC" | "DESC";
374
374
  } | undefined;
375
375
  }>>;
376
+ type: z.ZodOptional<z.ZodString>;
376
377
  }, "strict", z.ZodTypeAny, {
377
378
  name: string;
379
+ type?: string | undefined;
378
380
  selector?: string | undefined;
379
381
  subquery?: {
380
382
  table: string;
@@ -407,6 +409,7 @@ declare const responseDataSchema: z.ZodObject<{
407
409
  } | undefined;
408
410
  }, {
409
411
  name: string;
412
+ type?: string | undefined;
410
413
  selector?: string | undefined;
411
414
  subquery?: {
412
415
  table: string;
@@ -663,8 +666,10 @@ declare const standardRouteSchema: z.ZodObject<z.objectUtil.extendShape<{
663
666
  order: "ASC" | "DESC";
664
667
  } | undefined;
665
668
  }>>;
669
+ type: z.ZodOptional<z.ZodString>;
666
670
  }, "strict", z.ZodTypeAny, {
667
671
  name: string;
672
+ type?: string | undefined;
668
673
  selector?: string | undefined;
669
674
  subquery?: {
670
675
  table: string;
@@ -697,6 +702,7 @@ declare const standardRouteSchema: z.ZodObject<z.objectUtil.extendShape<{
697
702
  } | undefined;
698
703
  }, {
699
704
  name: string;
705
+ type?: string | undefined;
700
706
  selector?: string | undefined;
701
707
  subquery?: {
702
708
  table: string;
@@ -790,6 +796,7 @@ declare const standardRouteSchema: z.ZodObject<z.objectUtil.extendShape<{
790
796
  }[];
791
797
  response: {
792
798
  name: string;
799
+ type?: string | undefined;
793
800
  selector?: string | undefined;
794
801
  subquery?: {
795
802
  table: string;
@@ -869,6 +876,7 @@ declare const standardRouteSchema: z.ZodObject<z.objectUtil.extendShape<{
869
876
  }[];
870
877
  response: {
871
878
  name: string;
879
+ type?: string | undefined;
872
880
  selector?: string | undefined;
873
881
  subquery?: {
874
882
  table: string;
@@ -1563,8 +1571,10 @@ declare const resturaSchema: z.ZodObject<{
1563
1571
  order: "ASC" | "DESC";
1564
1572
  } | undefined;
1565
1573
  }>>;
1574
+ type: z.ZodOptional<z.ZodString>;
1566
1575
  }, "strict", z.ZodTypeAny, {
1567
1576
  name: string;
1577
+ type?: string | undefined;
1568
1578
  selector?: string | undefined;
1569
1579
  subquery?: {
1570
1580
  table: string;
@@ -1597,6 +1607,7 @@ declare const resturaSchema: z.ZodObject<{
1597
1607
  } | undefined;
1598
1608
  }, {
1599
1609
  name: string;
1610
+ type?: string | undefined;
1600
1611
  selector?: string | undefined;
1601
1612
  subquery?: {
1602
1613
  table: string;
@@ -1690,6 +1701,7 @@ declare const resturaSchema: z.ZodObject<{
1690
1701
  }[];
1691
1702
  response: {
1692
1703
  name: string;
1704
+ type?: string | undefined;
1693
1705
  selector?: string | undefined;
1694
1706
  subquery?: {
1695
1707
  table: string;
@@ -1769,6 +1781,7 @@ declare const resturaSchema: z.ZodObject<{
1769
1781
  }[];
1770
1782
  response: {
1771
1783
  name: string;
1784
+ type?: string | undefined;
1772
1785
  selector?: string | undefined;
1773
1786
  subquery?: {
1774
1787
  table: string;
@@ -1942,6 +1955,7 @@ declare const resturaSchema: z.ZodObject<{
1942
1955
  }[];
1943
1956
  response: {
1944
1957
  name: string;
1958
+ type?: string | undefined;
1945
1959
  selector?: string | undefined;
1946
1960
  subquery?: {
1947
1961
  table: string;
@@ -2048,6 +2062,7 @@ declare const resturaSchema: z.ZodObject<{
2048
2062
  }[];
2049
2063
  response: {
2050
2064
  name: string;
2065
+ type?: string | undefined;
2051
2066
  selector?: string | undefined;
2052
2067
  subquery?: {
2053
2068
  table: string;
@@ -2197,6 +2212,7 @@ declare const resturaSchema: z.ZodObject<{
2197
2212
  }[];
2198
2213
  response: {
2199
2214
  name: string;
2215
+ type?: string | undefined;
2200
2216
  selector?: string | undefined;
2201
2217
  subquery?: {
2202
2218
  table: string;
@@ -2345,6 +2361,7 @@ declare const resturaSchema: z.ZodObject<{
2345
2361
  }[];
2346
2362
  response: {
2347
2363
  name: string;
2364
+ type?: string | undefined;
2348
2365
  selector?: string | undefined;
2349
2366
  subquery?: {
2350
2367
  table: string;
package/dist/index.js CHANGED
@@ -619,7 +619,15 @@ var ResponseValidator = class _ResponseValidator {
619
619
  return { validator: map, isArray: route.type === "ARRAY" };
620
620
  }
621
621
  getFieldResponseType(field, tableName) {
622
- if (field.selector) {
622
+ if (field.type) {
623
+ if (_ResponseValidator.validatorIsValidString(field.type)) {
624
+ return { validator: field.type };
625
+ }
626
+ if (field.type.includes("|")) {
627
+ return { validator: this.parseValidationEnum(field.type) };
628
+ }
629
+ return { validator: "object" };
630
+ } else if (field.selector) {
623
631
  return this.getTypeFromTable(field.selector, tableName);
624
632
  } else if (field.subquery) {
625
633
  const table = this.database.find((t) => t.name == tableName);
@@ -845,7 +853,9 @@ var ApiTree = class _ApiTree {
845
853
  }
846
854
  getNameAndType(p, routeBaseTable, joins) {
847
855
  let responseType = "any", isNullable = false, array = false;
848
- if (p.selector) {
856
+ if (p.type) {
857
+ responseType = p.type;
858
+ } else if (p.selector) {
849
859
  ({ responseType, isNullable } = this.getTypeFromTable(p.selector, p.name));
850
860
  const selectorKey = p.selector.split(".")[0];
851
861
  if (selectorKey !== routeBaseTable) {
@@ -1129,7 +1139,8 @@ var responseDataSchema = import_zod3.z.object({
1129
1139
  // Explicit type for the lazy schema
1130
1140
  groupBy: groupBySchema.optional(),
1131
1141
  orderBy: orderBySchema.optional()
1132
- }).optional()
1142
+ }).optional(),
1143
+ type: import_zod3.z.string().optional()
1133
1144
  }).strict();
1134
1145
  var routeDataBaseSchema = import_zod3.z.object({
1135
1146
  method: import_zod3.z.enum(["GET", "POST", "PUT", "PATCH", "DELETE"]),
@@ -1761,7 +1772,7 @@ var import_core_utils4 = require("@redskytech/core-utils");
1761
1772
  var SqlEngine = class {
1762
1773
  async runQueryForRoute(req, routeData, schema) {
1763
1774
  if (!this.doesRoleHavePermissionToTable(req.requesterDetails.role, schema, routeData.table))
1764
- throw new RsError("UNAUTHORIZED", "You do not have permission to access this table");
1775
+ throw new RsError("FORBIDDEN", "You do not have permission to access this table");
1765
1776
  switch (routeData.method) {
1766
1777
  case "POST":
1767
1778
  return this.executeCreateRequest(req, routeData, schema);
@@ -1780,6 +1791,7 @@ var SqlEngine = class {
1780
1791
  return tableSchema;
1781
1792
  }
1782
1793
  doesRoleHavePermissionToColumn(role, schema, item, joins) {
1794
+ if (item.type) return true;
1783
1795
  if (item.selector) {
1784
1796
  let tableName = item.selector.split(".")[0];
1785
1797
  const columnName = item.selector.split(".")[1];
@@ -2254,7 +2266,7 @@ var PsqlEngine = class extends SqlEngine {
2254
2266
  if (item.subquery || this.doesRoleHavePermissionToColumn(userRole, schema, item, routeData.joins))
2255
2267
  selectColumns.push(item);
2256
2268
  });
2257
- if (!selectColumns.length) throw new RsError("UNAUTHORIZED", `You do not have permission to access this data.`);
2269
+ if (!selectColumns.length) throw new RsError("FORBIDDEN", `You do not have permission to access this data.`);
2258
2270
  let selectStatement = "SELECT \n";
2259
2271
  selectStatement += ` ${selectColumns.map((item) => {
2260
2272
  if (item.subquery) {
@@ -2262,7 +2274,7 @@ var PsqlEngine = class extends SqlEngine {
2262
2274
  item.name
2263
2275
  )}`;
2264
2276
  }
2265
- return `${escapeColumnName(item.selector)} AS ${escapeColumnName(item.name)}`;
2277
+ return `${item.type ? item.selector : escapeColumnName(item.selector)} AS ${escapeColumnName(item.name)}`;
2266
2278
  }).join(",\n ")}
2267
2279
  `;
2268
2280
  sqlStatement += `FROM "${routeData.table}"
@@ -2362,7 +2374,7 @@ DELETE FROM "${routeData.table}" ${joinStatement} ${whereClause}`;
2362
2374
  let joinStatements = "";
2363
2375
  joins.forEach((item) => {
2364
2376
  if (!this.doesRoleHavePermissionToTable(userRole, schema, item.table))
2365
- throw new RsError("UNAUTHORIZED", "You do not have permission to access this table");
2377
+ throw new RsError("FORBIDDEN", "You do not have permission to access this table");
2366
2378
  if (item.custom) {
2367
2379
  const customReplaced = this.replaceParamKeywords(item.custom, routeData, req, sqlParams);
2368
2380
  joinStatements += ` ${item.type} JOIN ${escapeColumnName(item.table)}${item.alias ? `AS "${item.alias}"` : ""} ON ${customReplaced}
@@ -3005,8 +3017,7 @@ var ResturaEngine = class {
3005
3017
  validateAuthorization(req, routeData) {
3006
3018
  const role = req.requesterDetails.role;
3007
3019
  if (routeData.roles.length === 0 || !role) return;
3008
- if (!routeData.roles.includes(role))
3009
- throw new RsError("UNAUTHORIZED", "Not authorized to access this endpoint");
3020
+ if (!routeData.roles.includes(role)) throw new RsError("FORBIDDEN", "Not authorized to access this endpoint");
3010
3021
  }
3011
3022
  getRouteData(method, baseUrl, path5) {
3012
3023
  const endpoint = this.schema.endpoints.find((item) => {