@ai0x0/utils 0.0.37 → 0.0.38

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 (82) hide show
  1. package/README.md +42 -1
  2. package/es/backend/sqlite/actions/create-delete-action.d.ts +8 -0
  3. package/es/backend/sqlite/actions/create-delete-action.js +54 -0
  4. package/es/backend/sqlite/actions/create-get-action.d.ts +8 -0
  5. package/es/backend/sqlite/actions/create-get-action.js +64 -0
  6. package/es/backend/sqlite/actions/create-get-list-action.d.ts +11 -0
  7. package/es/backend/sqlite/actions/create-get-list-action.js +56 -0
  8. package/es/backend/sqlite/actions/create-post-action.d.ts +6 -0
  9. package/es/backend/sqlite/actions/create-post-action.js +31 -0
  10. package/es/backend/sqlite/actions/create-put-action.d.ts +8 -0
  11. package/es/backend/sqlite/actions/create-put-action.js +74 -0
  12. package/es/backend/sqlite/actions/get-list-data.d.ts +9 -0
  13. package/es/backend/sqlite/actions/get-list-data.js +75 -0
  14. package/es/backend/sqlite/actions/get-list-query.d.ts +19 -0
  15. package/es/backend/sqlite/actions/get-list-query.js +215 -0
  16. package/es/backend/sqlite/actions/index.d.ts +6 -0
  17. package/es/backend/sqlite/actions/index.js +6 -0
  18. package/es/backend/sqlite/actions/transform-body.d.ts +1 -0
  19. package/es/backend/sqlite/actions/transform-body.js +9 -0
  20. package/es/backend/sqlite/errors.d.ts +4 -0
  21. package/es/backend/sqlite/errors.js +31 -0
  22. package/es/backend/sqlite/index.d.ts +5 -0
  23. package/es/backend/sqlite/index.js +5 -0
  24. package/es/backend/sqlite/route-operation/delete-operation.d.ts +10 -0
  25. package/es/backend/sqlite/route-operation/delete-operation.js +112 -0
  26. package/es/backend/sqlite/route-operation/get-list-operation.d.ts +22 -0
  27. package/es/backend/sqlite/route-operation/get-list-operation.js +122 -0
  28. package/es/backend/sqlite/route-operation/get-operation.d.ts +16 -0
  29. package/es/backend/sqlite/route-operation/get-operation.js +122 -0
  30. package/es/backend/sqlite/route-operation/get-table-name.d.ts +3 -0
  31. package/es/backend/sqlite/route-operation/get-table-name.js +11 -0
  32. package/es/backend/sqlite/route-operation/index.d.ts +5 -0
  33. package/es/backend/sqlite/route-operation/index.js +5 -0
  34. package/es/backend/sqlite/route-operation/post-operation.d.ts +13 -0
  35. package/es/backend/sqlite/route-operation/post-operation.js +137 -0
  36. package/es/backend/sqlite/route-operation/put-operation.d.ts +14 -0
  37. package/es/backend/sqlite/route-operation/put-operation.js +144 -0
  38. package/es/backend/sqlite/schemas/index.d.ts +68 -0
  39. package/es/backend/sqlite/schemas/index.js +133 -0
  40. package/es/backend/sqlite/types.d.ts +16 -0
  41. package/es/backend/sqlite/types.js +1 -0
  42. package/lib/backend/sqlite/actions/create-delete-action.d.ts +8 -0
  43. package/lib/backend/sqlite/actions/create-delete-action.js +53 -0
  44. package/lib/backend/sqlite/actions/create-get-action.d.ts +8 -0
  45. package/lib/backend/sqlite/actions/create-get-action.js +65 -0
  46. package/lib/backend/sqlite/actions/create-get-list-action.d.ts +11 -0
  47. package/lib/backend/sqlite/actions/create-get-list-action.js +63 -0
  48. package/lib/backend/sqlite/actions/create-post-action.d.ts +6 -0
  49. package/lib/backend/sqlite/actions/create-post-action.js +39 -0
  50. package/lib/backend/sqlite/actions/create-put-action.d.ts +8 -0
  51. package/lib/backend/sqlite/actions/create-put-action.js +60 -0
  52. package/lib/backend/sqlite/actions/get-list-data.d.ts +9 -0
  53. package/lib/backend/sqlite/actions/get-list-data.js +51 -0
  54. package/lib/backend/sqlite/actions/get-list-query.d.ts +19 -0
  55. package/lib/backend/sqlite/actions/get-list-query.js +165 -0
  56. package/lib/backend/sqlite/actions/index.d.ts +6 -0
  57. package/lib/backend/sqlite/actions/index.js +44 -0
  58. package/lib/backend/sqlite/actions/transform-body.d.ts +1 -0
  59. package/lib/backend/sqlite/actions/transform-body.js +49 -0
  60. package/lib/backend/sqlite/errors.d.ts +4 -0
  61. package/lib/backend/sqlite/errors.js +35 -0
  62. package/lib/backend/sqlite/index.d.ts +5 -0
  63. package/lib/backend/sqlite/index.js +31 -0
  64. package/lib/backend/sqlite/route-operation/delete-operation.d.ts +10 -0
  65. package/lib/backend/sqlite/route-operation/delete-operation.js +91 -0
  66. package/lib/backend/sqlite/route-operation/get-list-operation.d.ts +22 -0
  67. package/lib/backend/sqlite/route-operation/get-list-operation.js +108 -0
  68. package/lib/backend/sqlite/route-operation/get-operation.d.ts +16 -0
  69. package/lib/backend/sqlite/route-operation/get-operation.js +107 -0
  70. package/lib/backend/sqlite/route-operation/get-table-name.d.ts +3 -0
  71. package/lib/backend/sqlite/route-operation/get-table-name.js +35 -0
  72. package/lib/backend/sqlite/route-operation/index.d.ts +5 -0
  73. package/lib/backend/sqlite/route-operation/index.js +31 -0
  74. package/lib/backend/sqlite/route-operation/post-operation.d.ts +13 -0
  75. package/lib/backend/sqlite/route-operation/post-operation.js +94 -0
  76. package/lib/backend/sqlite/route-operation/put-operation.d.ts +14 -0
  77. package/lib/backend/sqlite/route-operation/put-operation.js +103 -0
  78. package/lib/backend/sqlite/schemas/index.d.ts +68 -0
  79. package/lib/backend/sqlite/schemas/index.js +113 -0
  80. package/lib/backend/sqlite/types.d.ts +16 -0
  81. package/lib/backend/sqlite/types.js +17 -0
  82. package/package.json +3 -1
@@ -0,0 +1,19 @@
1
+ import { BaseTable, GetListRelations } from "../types.js";
2
+ import { SelectedFields } from "drizzle-orm/sqlite-core/query-builders/select.types";
3
+ export declare function getListQuery<TTable extends BaseTable, TSelection extends SelectedFields>({ db, fields, jsonArrayFields, params, relations, table, }: {
4
+ db: any;
5
+ fields: TSelection;
6
+ jsonArrayFields?: string[];
7
+ params: {
8
+ [field: string]: unknown;
9
+ orderBy?: keyof BaseTable;
10
+ orderDir?: "asc" | "desc";
11
+ page?: number;
12
+ pageSize?: number;
13
+ };
14
+ relations?: GetListRelations;
15
+ table: TTable;
16
+ }): {
17
+ countQuery: any;
18
+ query: any;
19
+ };
@@ -0,0 +1,215 @@
1
+ function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
2
+ var _templateObject, _templateObject2, _templateObject3;
3
+ var _excluded = ["page", "pageSize", "orderBy", "orderDir"];
4
+ function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
5
+ function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
6
+ function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t.return && (u = t.return(), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } }
7
+ function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
8
+ function _taggedTemplateLiteral(strings, raw) { if (!raw) { raw = strings.slice(0); } return Object.freeze(Object.defineProperties(strings, { raw: { value: Object.freeze(raw) } })); }
9
+ function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); }
10
+ function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
11
+ function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); }
12
+ function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); }
13
+ function _createForOfIteratorHelper(o, allowArrayLike) { var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"]; if (!it) { if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function e(_e) { throw _e; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var normalCompletion = true, didErr = false, err; return { s: function s() { it = it.call(o); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e2) { didErr = true; err = _e2; }, f: function f() { try { if (!normalCompletion && it.return != null) it.return(); } finally { if (didErr) throw err; } } }; }
14
+ function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
15
+ function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
16
+ function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
17
+ function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
18
+ function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
19
+ function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : String(i); }
20
+ function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
21
+ function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }
22
+ function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }
23
+ import { and, asc, desc, eq, gte, like, inArray, lte, or, sql } from "drizzle-orm";
24
+ export function getListQuery(_ref) {
25
+ var db = _ref.db,
26
+ fields = _ref.fields,
27
+ jsonArrayFields = _ref.jsonArrayFields,
28
+ params = _ref.params,
29
+ relations = _ref.relations,
30
+ table = _ref.table;
31
+ var _params$page = params.page,
32
+ page = _params$page === void 0 ? 1 : _params$page,
33
+ _params$pageSize = params.pageSize,
34
+ pageSize = _params$pageSize === void 0 ? 10 : _params$pageSize,
35
+ _params$orderBy = params.orderBy,
36
+ orderBy = _params$orderBy === void 0 ? "createdAt" : _params$orderBy,
37
+ _params$orderDir = params.orderDir,
38
+ orderDir = _params$orderDir === void 0 ? "desc" : _params$orderDir,
39
+ filters = _objectWithoutProperties(params, _excluded);
40
+
41
+ // 构建基础查询
42
+ function buildBaseQuery(table, fields, relations) {
43
+ var queryFields = _objectSpread({}, fields);
44
+
45
+ // 添加关联字段
46
+ if (relations !== null && relations !== void 0 && relations.length) {
47
+ var _iterator = _createForOfIteratorHelper(relations),
48
+ _step;
49
+ try {
50
+ for (_iterator.s(); !(_step = _iterator.n()).done;) {
51
+ var select = _step.value.select;
52
+ queryFields = _objectSpread(_objectSpread({}, queryFields), select);
53
+ }
54
+ } catch (err) {
55
+ _iterator.e(err);
56
+ } finally {
57
+ _iterator.f();
58
+ }
59
+ }
60
+ var query = db.select(queryFields).from(table);
61
+
62
+ // 添加分组
63
+ if (relations !== null && relations !== void 0 && relations.length) {
64
+ query.groupBy.apply(query, [table.id].concat(_toConsumableArray(relations.filter(function (_ref2) {
65
+ var groupBy = _ref2.groupBy;
66
+ return groupBy;
67
+ }).filter(function (_ref3) {
68
+ var table = _ref3.table;
69
+ return table;
70
+ }).map(function (_ref4) {
71
+ var table = _ref4.table;
72
+ return table.id;
73
+ }))));
74
+ }
75
+ return query;
76
+ }
77
+
78
+ // 查找目标列
79
+ function findTargetColumn(key, table, relations) {
80
+ var _concat$find;
81
+ return (_concat$find = [table].concat((relations === null || relations === void 0 ? void 0 : relations.filter(function (_ref5) {
82
+ var table = _ref5.table;
83
+ return table;
84
+ }).map(function (_ref6) {
85
+ var table = _ref6.table;
86
+ return table;
87
+ })) || []).find(function (table) {
88
+ return table[key];
89
+ })) === null || _concat$find === void 0 ? void 0 : _concat$find[key];
90
+ }
91
+
92
+ // 添加条件
93
+ function addCondition(conditions, key, value, targetColumn) {
94
+ var isIdField = /id/i.test(key);
95
+ var isJsonArray = jsonArrayFields === null || jsonArrayFields === void 0 ? void 0 : jsonArrayFields.includes(key);
96
+ if (typeof value === "string") {
97
+ if (value.includes(",")) {
98
+ // 处理多值
99
+ var values = value.split(",").map(function (val) {
100
+ return val.trim();
101
+ }).filter(Boolean);
102
+ if (values.length > 0) {
103
+ if (isJsonArray) {
104
+ // 处理 JSON 数组字段
105
+ conditions.push(sql(_templateObject || (_templateObject = _taggedTemplateLiteral(["\n EXISTS (\n SELECT 1 FROM json_each(", ") tag\n WHERE tag.value LIKE ", "\n )\n "])), targetColumn, "%".concat(values[0], "%")));
106
+ } else if (isIdField) {
107
+ conditions.push(inArray(targetColumn, values));
108
+ } else {
109
+ conditions.push(or.apply(void 0, _toConsumableArray(values.map(function (val) {
110
+ return like(targetColumn, "%".concat(val, "%"));
111
+ }))));
112
+ }
113
+ }
114
+ } else {
115
+ // 处理单值
116
+ if (isJsonArray) {
117
+ // 处理 JSON 数组字段
118
+ conditions.push(sql(_templateObject2 || (_templateObject2 = _taggedTemplateLiteral(["\n EXISTS (\n SELECT 1 FROM json_each(", ") tag\n WHERE tag.value LIKE ", "\n )\n "])), targetColumn, "%".concat(value, "%")));
119
+ } else if (isIdField) {
120
+ conditions.push(eq(targetColumn, value));
121
+ } else {
122
+ conditions.push(like(targetColumn, "%".concat(value, "%")));
123
+ }
124
+ }
125
+ } else {
126
+ conditions.push(eq(targetColumn, value));
127
+ }
128
+ }
129
+
130
+ // 构建查询条件
131
+ function buildConditions(filters, table, relations) {
132
+ var conditions = [];
133
+
134
+ // 处理过滤条件
135
+ for (var _i = 0, _Object$entries = Object.entries(filters); _i < _Object$entries.length; _i++) {
136
+ var _Object$entries$_i = _slicedToArray(_Object$entries[_i], 2),
137
+ key = _Object$entries$_i[0],
138
+ value = _Object$entries$_i[1];
139
+ if (!value) {
140
+ continue;
141
+ }
142
+
143
+ // 处理日期范围字段
144
+ if (key.endsWith("AtFrom") || key.endsWith("AtTo")) {
145
+ var baseFieldName = key.replace(/(AtFrom|AtTo)$/, "");
146
+ var _targetColumn = findTargetColumn("".concat(baseFieldName, "At"), table, relations);
147
+ if (!_targetColumn) {
148
+ continue;
149
+ }
150
+ if (key.endsWith("AtFrom")) {
151
+ conditions.push(gte(_targetColumn, new Date(value)));
152
+ } else if (key.endsWith("AtTo")) {
153
+ conditions.push(lte(_targetColumn, new Date(value)));
154
+ }
155
+ continue;
156
+ }
157
+
158
+ // 处理其他普通过滤条件
159
+ var targetColumn = findTargetColumn(key, table, relations);
160
+ if (!targetColumn) {
161
+ continue;
162
+ }
163
+ addCondition(conditions, key, value, targetColumn);
164
+ }
165
+ return conditions;
166
+ }
167
+
168
+ // 构建排序
169
+ function buildOrderBy(table, orderBy, orderDir) {
170
+ var orderColumn = table[orderBy];
171
+ return orderDir === "asc" ? asc(orderColumn) : desc(orderColumn);
172
+ }
173
+
174
+ // 构建基础查询
175
+ var baseQuery = buildBaseQuery(table, fields, relations);
176
+
177
+ // 构建条件
178
+ var conditions = buildConditions(filters, table, relations);
179
+
180
+ // 构建主查询
181
+ var query = baseQuery.where(conditions.length > 0 ? and.apply(void 0, _toConsumableArray(conditions)) : undefined).orderBy(buildOrderBy(table, orderBy, orderDir)).limit(pageSize).offset((page - 1) * pageSize);
182
+
183
+ // 构建计数查询(优化后)
184
+ var countQuery = db.select({
185
+ count: sql(_templateObject3 || (_templateObject3 = _taggedTemplateLiteral(["COUNT(DISTINCT ", ")"])), table.id)
186
+ }).from(table).where(conditions.length > 0 ? and.apply(void 0, _toConsumableArray(conditions)) : undefined);
187
+
188
+ // 添加关联
189
+ if (relations !== null && relations !== void 0 && relations.length) {
190
+ var _iterator2 = _createForOfIteratorHelper(relations),
191
+ _step2;
192
+ try {
193
+ for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) {
194
+ var _step2$value = _step2.value,
195
+ relationTable = _step2$value.table,
196
+ joinSql = _step2$value.sql;
197
+ if (relationTable) {
198
+ query.leftJoin(relationTable, joinSql);
199
+ countQuery.leftJoin(relationTable, joinSql);
200
+ }
201
+ }
202
+ } catch (err) {
203
+ _iterator2.e(err);
204
+ } finally {
205
+ _iterator2.f();
206
+ }
207
+ }
208
+
209
+ // console.log(query.prepare("a").getQuery());
210
+
211
+ return {
212
+ countQuery: countQuery,
213
+ query: query
214
+ };
215
+ }
@@ -0,0 +1,6 @@
1
+ export { createDeleteAction } from "./create-delete-action.js";
2
+ export { createGetAction } from "./create-get-action.js";
3
+ export { createGetListAction } from "./create-get-list-action.js";
4
+ export { createPostAction } from "./create-post-action.js";
5
+ export { createPutAction } from "./create-put-action.js";
6
+ export { getListQuery } from "./get-list-query.js";
@@ -0,0 +1,6 @@
1
+ export { createDeleteAction } from "./create-delete-action.js";
2
+ export { createGetAction } from "./create-get-action.js";
3
+ export { createGetListAction } from "./create-get-list-action.js";
4
+ export { createPostAction } from "./create-post-action.js";
5
+ export { createPutAction } from "./create-put-action.js";
6
+ export { getListQuery } from "./get-list-query.js";
@@ -0,0 +1 @@
1
+ export declare const transformBody: <T extends Record<string, unknown>>(body: T) => T;
@@ -0,0 +1,9 @@
1
+ import dayjs from "dayjs";
2
+ export var transformBody = function transformBody(body) {
3
+ for (var key in body) {
4
+ if (key.endsWith("At")) {
5
+ body[key] = dayjs(body[key]).toDate();
6
+ }
7
+ }
8
+ return body;
9
+ };
@@ -0,0 +1,4 @@
1
+ export declare class HttpError extends Error {
2
+ status: number;
3
+ constructor(status: number, message: string);
4
+ }
@@ -0,0 +1,31 @@
1
+ function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
2
+ function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, _toPropertyKey(descriptor.key), descriptor); } }
3
+ function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }
4
+ function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
5
+ function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); Object.defineProperty(subClass, "prototype", { writable: false }); if (superClass) _setPrototypeOf(subClass, superClass); }
6
+ function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; }
7
+ function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } else if (call !== void 0) { throw new TypeError("Derived constructors may only return object or undefined"); } return _assertThisInitialized(self); }
8
+ function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
9
+ function _wrapNativeSuper(Class) { var _cache = typeof Map === "function" ? new Map() : undefined; _wrapNativeSuper = function _wrapNativeSuper(Class) { if (Class === null || !_isNativeFunction(Class)) return Class; if (typeof Class !== "function") { throw new TypeError("Super expression must either be null or a function"); } if (typeof _cache !== "undefined") { if (_cache.has(Class)) return _cache.get(Class); _cache.set(Class, Wrapper); } function Wrapper() { return _construct(Class, arguments, _getPrototypeOf(this).constructor); } Wrapper.prototype = Object.create(Class.prototype, { constructor: { value: Wrapper, enumerable: false, writable: true, configurable: true } }); return _setPrototypeOf(Wrapper, Class); }; return _wrapNativeSuper(Class); }
10
+ function _construct(Parent, args, Class) { if (_isNativeReflectConstruct()) { _construct = Reflect.construct.bind(); } else { _construct = function _construct(Parent, args, Class) { var a = [null]; a.push.apply(a, args); var Constructor = Function.bind.apply(Parent, a); var instance = new Constructor(); if (Class) _setPrototypeOf(instance, Class.prototype); return instance; }; } return _construct.apply(null, arguments); }
11
+ function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } }
12
+ function _isNativeFunction(fn) { try { return Function.toString.call(fn).indexOf("[native code]") !== -1; } catch (e) { return typeof fn === "function"; } }
13
+ function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
14
+ function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf.bind() : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }
15
+ function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
16
+ function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : String(i); }
17
+ function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
18
+ export var HttpError = /*#__PURE__*/function (_Error) {
19
+ _inherits(HttpError, _Error);
20
+ var _super = _createSuper(HttpError);
21
+ function HttpError(status, message) {
22
+ var _this;
23
+ _classCallCheck(this, HttpError);
24
+ _this = _super.call(this, message);
25
+ _defineProperty(_assertThisInitialized(_this), "status", void 0);
26
+ _this.name = "HttpError";
27
+ _this.status = status;
28
+ return _this;
29
+ }
30
+ return _createClass(HttpError);
31
+ }( /*#__PURE__*/_wrapNativeSuper(Error));
@@ -0,0 +1,5 @@
1
+ export * from "./actions/index.js";
2
+ export * from "./route-operation/index.js";
3
+ export * from "./schemas/index.js";
4
+ export * from "./errors.js";
5
+ export * from "./types.js";
@@ -0,0 +1,5 @@
1
+ export * from "./actions/index.js";
2
+ export * from "./route-operation/index.js";
3
+ export * from "./schemas/index.js";
4
+ export * from "./errors.js";
5
+ export * from "./types.js";
@@ -0,0 +1,10 @@
1
+ import { TypedNextResponse } from "next-rest-framework";
2
+ import { BaseTable } from "../types.js";
3
+ export interface DeleteOperationOptions<TTable extends BaseTable> {
4
+ table: TTable;
5
+ summary?: string;
6
+ onSuccess?: () => Promise<void>;
7
+ onError?: (_error: Error) => Promise<ReturnType<(typeof TypedNextResponse)["json"]> | undefined>;
8
+ byCreator?: boolean;
9
+ }
10
+ export declare const createDeleteOperation: any;
@@ -0,0 +1,112 @@
1
+ function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
2
+ function _regeneratorRuntime() { "use strict"; /*! regenerator-runtime -- Copyright (c) 2014-present, Facebook, Inc. -- license (MIT): https://github.com/facebook/regenerator/blob/main/LICENSE */ _regeneratorRuntime = function _regeneratorRuntime() { return e; }; var t, e = {}, r = Object.prototype, n = r.hasOwnProperty, o = Object.defineProperty || function (t, e, r) { t[e] = r.value; }, i = "function" == typeof Symbol ? Symbol : {}, a = i.iterator || "@@iterator", c = i.asyncIterator || "@@asyncIterator", u = i.toStringTag || "@@toStringTag"; function define(t, e, r) { return Object.defineProperty(t, e, { value: r, enumerable: !0, configurable: !0, writable: !0 }), t[e]; } try { define({}, ""); } catch (t) { define = function define(t, e, r) { return t[e] = r; }; } function wrap(t, e, r, n) { var i = e && e.prototype instanceof Generator ? e : Generator, a = Object.create(i.prototype), c = new Context(n || []); return o(a, "_invoke", { value: makeInvokeMethod(t, r, c) }), a; } function tryCatch(t, e, r) { try { return { type: "normal", arg: t.call(e, r) }; } catch (t) { return { type: "throw", arg: t }; } } e.wrap = wrap; var h = "suspendedStart", l = "suspendedYield", f = "executing", s = "completed", y = {}; function Generator() {} function GeneratorFunction() {} function GeneratorFunctionPrototype() {} var p = {}; define(p, a, function () { return this; }); var d = Object.getPrototypeOf, v = d && d(d(values([]))); v && v !== r && n.call(v, a) && (p = v); var g = GeneratorFunctionPrototype.prototype = Generator.prototype = Object.create(p); function defineIteratorMethods(t) { ["next", "throw", "return"].forEach(function (e) { define(t, e, function (t) { return this._invoke(e, t); }); }); } function AsyncIterator(t, e) { function invoke(r, o, i, a) { var c = tryCatch(t[r], t, o); if ("throw" !== c.type) { var u = c.arg, h = u.value; return h && "object" == _typeof(h) && n.call(h, "__await") ? e.resolve(h.__await).then(function (t) { invoke("next", t, i, a); }, function (t) { invoke("throw", t, i, a); }) : e.resolve(h).then(function (t) { u.value = t, i(u); }, function (t) { return invoke("throw", t, i, a); }); } a(c.arg); } var r; o(this, "_invoke", { value: function value(t, n) { function callInvokeWithMethodAndArg() { return new e(function (e, r) { invoke(t, n, e, r); }); } return r = r ? r.then(callInvokeWithMethodAndArg, callInvokeWithMethodAndArg) : callInvokeWithMethodAndArg(); } }); } function makeInvokeMethod(e, r, n) { var o = h; return function (i, a) { if (o === f) throw new Error("Generator is already running"); if (o === s) { if ("throw" === i) throw a; return { value: t, done: !0 }; } for (n.method = i, n.arg = a;;) { var c = n.delegate; if (c) { var u = maybeInvokeDelegate(c, n); if (u) { if (u === y) continue; return u; } } if ("next" === n.method) n.sent = n._sent = n.arg;else if ("throw" === n.method) { if (o === h) throw o = s, n.arg; n.dispatchException(n.arg); } else "return" === n.method && n.abrupt("return", n.arg); o = f; var p = tryCatch(e, r, n); if ("normal" === p.type) { if (o = n.done ? s : l, p.arg === y) continue; return { value: p.arg, done: n.done }; } "throw" === p.type && (o = s, n.method = "throw", n.arg = p.arg); } }; } function maybeInvokeDelegate(e, r) { var n = r.method, o = e.iterator[n]; if (o === t) return r.delegate = null, "throw" === n && e.iterator.return && (r.method = "return", r.arg = t, maybeInvokeDelegate(e, r), "throw" === r.method) || "return" !== n && (r.method = "throw", r.arg = new TypeError("The iterator does not provide a '" + n + "' method")), y; var i = tryCatch(o, e.iterator, r.arg); if ("throw" === i.type) return r.method = "throw", r.arg = i.arg, r.delegate = null, y; var a = i.arg; return a ? a.done ? (r[e.resultName] = a.value, r.next = e.nextLoc, "return" !== r.method && (r.method = "next", r.arg = t), r.delegate = null, y) : a : (r.method = "throw", r.arg = new TypeError("iterator result is not an object"), r.delegate = null, y); } function pushTryEntry(t) { var e = { tryLoc: t[0] }; 1 in t && (e.catchLoc = t[1]), 2 in t && (e.finallyLoc = t[2], e.afterLoc = t[3]), this.tryEntries.push(e); } function resetTryEntry(t) { var e = t.completion || {}; e.type = "normal", delete e.arg, t.completion = e; } function Context(t) { this.tryEntries = [{ tryLoc: "root" }], t.forEach(pushTryEntry, this), this.reset(!0); } function values(e) { if (e || "" === e) { var r = e[a]; if (r) return r.call(e); if ("function" == typeof e.next) return e; if (!isNaN(e.length)) { var o = -1, i = function next() { for (; ++o < e.length;) if (n.call(e, o)) return next.value = e[o], next.done = !1, next; return next.value = t, next.done = !0, next; }; return i.next = i; } } throw new TypeError(_typeof(e) + " is not iterable"); } return GeneratorFunction.prototype = GeneratorFunctionPrototype, o(g, "constructor", { value: GeneratorFunctionPrototype, configurable: !0 }), o(GeneratorFunctionPrototype, "constructor", { value: GeneratorFunction, configurable: !0 }), GeneratorFunction.displayName = define(GeneratorFunctionPrototype, u, "GeneratorFunction"), e.isGeneratorFunction = function (t) { var e = "function" == typeof t && t.constructor; return !!e && (e === GeneratorFunction || "GeneratorFunction" === (e.displayName || e.name)); }, e.mark = function (t) { return Object.setPrototypeOf ? Object.setPrototypeOf(t, GeneratorFunctionPrototype) : (t.__proto__ = GeneratorFunctionPrototype, define(t, u, "GeneratorFunction")), t.prototype = Object.create(g), t; }, e.awrap = function (t) { return { __await: t }; }, defineIteratorMethods(AsyncIterator.prototype), define(AsyncIterator.prototype, c, function () { return this; }), e.AsyncIterator = AsyncIterator, e.async = function (t, r, n, o, i) { void 0 === i && (i = Promise); var a = new AsyncIterator(wrap(t, r, n, o), i); return e.isGeneratorFunction(r) ? a : a.next().then(function (t) { return t.done ? t.value : a.next(); }); }, defineIteratorMethods(g), define(g, u, "Generator"), define(g, a, function () { return this; }), define(g, "toString", function () { return "[object Generator]"; }), e.keys = function (t) { var e = Object(t), r = []; for (var n in e) r.push(n); return r.reverse(), function next() { for (; r.length;) { var t = r.pop(); if (t in e) return next.value = t, next.done = !1, next; } return next.done = !0, next; }; }, e.values = values, Context.prototype = { constructor: Context, reset: function reset(e) { if (this.prev = 0, this.next = 0, this.sent = this._sent = t, this.done = !1, this.delegate = null, this.method = "next", this.arg = t, this.tryEntries.forEach(resetTryEntry), !e) for (var r in this) "t" === r.charAt(0) && n.call(this, r) && !isNaN(+r.slice(1)) && (this[r] = t); }, stop: function stop() { this.done = !0; var t = this.tryEntries[0].completion; if ("throw" === t.type) throw t.arg; return this.rval; }, dispatchException: function dispatchException(e) { if (this.done) throw e; var r = this; function handle(n, o) { return a.type = "throw", a.arg = e, r.next = n, o && (r.method = "next", r.arg = t), !!o; } for (var o = this.tryEntries.length - 1; o >= 0; --o) { var i = this.tryEntries[o], a = i.completion; if ("root" === i.tryLoc) return handle("end"); if (i.tryLoc <= this.prev) { var c = n.call(i, "catchLoc"), u = n.call(i, "finallyLoc"); if (c && u) { if (this.prev < i.catchLoc) return handle(i.catchLoc, !0); if (this.prev < i.finallyLoc) return handle(i.finallyLoc); } else if (c) { if (this.prev < i.catchLoc) return handle(i.catchLoc, !0); } else { if (!u) throw new Error("try statement without catch or finally"); if (this.prev < i.finallyLoc) return handle(i.finallyLoc); } } } }, abrupt: function abrupt(t, e) { for (var r = this.tryEntries.length - 1; r >= 0; --r) { var o = this.tryEntries[r]; if (o.tryLoc <= this.prev && n.call(o, "finallyLoc") && this.prev < o.finallyLoc) { var i = o; break; } } i && ("break" === t || "continue" === t) && i.tryLoc <= e && e <= i.finallyLoc && (i = null); var a = i ? i.completion : {}; return a.type = t, a.arg = e, i ? (this.method = "next", this.next = i.finallyLoc, y) : this.complete(a); }, complete: function complete(t, e) { if ("throw" === t.type) throw t.arg; return "break" === t.type || "continue" === t.type ? this.next = t.arg : "return" === t.type ? (this.rval = this.arg = t.arg, this.method = "return", this.next = "end") : "normal" === t.type && e && (this.next = e), y; }, finish: function finish(t) { for (var e = this.tryEntries.length - 1; e >= 0; --e) { var r = this.tryEntries[e]; if (r.finallyLoc === t) return this.complete(r.completion, r.afterLoc), resetTryEntry(r), y; } }, catch: function _catch(t) { for (var e = this.tryEntries.length - 1; e >= 0; --e) { var r = this.tryEntries[e]; if (r.tryLoc === t) { var n = r.completion; if ("throw" === n.type) { var o = n.arg; resetTryEntry(r); } return o; } } throw new Error("illegal catch attempt"); }, delegateYield: function delegateYield(e, r, n) { return this.delegate = { iterator: values(e), resultName: r, nextLoc: n }, "next" === this.method && (this.arg = t), y; } }, e; }
3
+ function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } }
4
+ function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; }
5
+ /* eslint-disable no-unused-vars */
6
+ import { routeOperation, TypedNextResponse } from "next-rest-framework";
7
+ import { z } from "zod";
8
+ import getTableName from "./get-table-name.js";
9
+ import { createDeleteAction } from "../actions/index.js";
10
+ import { HttpError } from "../errors.js";
11
+ export var createDeleteOperation = function createDeleteOperation(_ref) {
12
+ var db = _ref.db,
13
+ getSession = _ref.getSession;
14
+ return function (_ref2) {
15
+ var table = _ref2.table,
16
+ summary = _ref2.summary,
17
+ onSuccess = _ref2.onSuccess,
18
+ onError = _ref2.onError,
19
+ _ref2$byCreator = _ref2.byCreator,
20
+ byCreator = _ref2$byCreator === void 0 ? true : _ref2$byCreator;
21
+ return routeOperation({
22
+ method: "DELETE",
23
+ openApiOperation: {
24
+ summary: summary,
25
+ tags: [getTableName(table)]
26
+ }
27
+ }).input({
28
+ body: z.object({
29
+ id: z.string()
30
+ }),
31
+ contentType: "application/json"
32
+ }).handler( /*#__PURE__*/function () {
33
+ var _ref3 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(req) {
34
+ var body, _ref4, userId, data, response;
35
+ return _regeneratorRuntime().wrap(function _callee$(_context) {
36
+ while (1) switch (_context.prev = _context.next) {
37
+ case 0:
38
+ _context.prev = 0;
39
+ _context.next = 3;
40
+ return req.json();
41
+ case 3:
42
+ body = _context.sent;
43
+ if (!byCreator) {
44
+ _context.next = 13;
45
+ break;
46
+ }
47
+ _context.next = 7;
48
+ return getSession(req);
49
+ case 7:
50
+ _context.t0 = _context.sent;
51
+ if (_context.t0) {
52
+ _context.next = 10;
53
+ break;
54
+ }
55
+ _context.t0 = {};
56
+ case 10:
57
+ _ref4 = _context.t0;
58
+ userId = _ref4.userId;
59
+ body.creatorId = userId;
60
+ case 13:
61
+ _context.next = 15;
62
+ return createDeleteAction({
63
+ table: table,
64
+ db: db
65
+ })(body);
66
+ case 15:
67
+ data = _context.sent;
68
+ _context.next = 18;
69
+ return onSuccess === null || onSuccess === void 0 ? void 0 : onSuccess();
70
+ case 18:
71
+ return _context.abrupt("return", TypedNextResponse.json(data, {
72
+ status: 200
73
+ }));
74
+ case 21:
75
+ _context.prev = 21;
76
+ _context.t1 = _context["catch"](0);
77
+ if (!(_context.t1 instanceof HttpError)) {
78
+ console.error(_context.t1);
79
+ }
80
+ _context.next = 26;
81
+ return onError === null || onError === void 0 ? void 0 : onError(_context.t1);
82
+ case 26:
83
+ response = _context.sent;
84
+ if (!response) {
85
+ _context.next = 29;
86
+ break;
87
+ }
88
+ return _context.abrupt("return", response);
89
+ case 29:
90
+ if (!(_context.t1 instanceof HttpError)) {
91
+ _context.next = 31;
92
+ break;
93
+ }
94
+ return _context.abrupt("return", TypedNextResponse.json({
95
+ message: _context.t1.message
96
+ }, {
97
+ status: _context.t1.status
98
+ }));
99
+ case 31:
100
+ throw _context.t1;
101
+ case 32:
102
+ case "end":
103
+ return _context.stop();
104
+ }
105
+ }, _callee, null, [[0, 21]]);
106
+ }));
107
+ return function (_x) {
108
+ return _ref3.apply(this, arguments);
109
+ };
110
+ }());
111
+ };
112
+ };
@@ -0,0 +1,22 @@
1
+ import { z, ZodSchema } from "zod";
2
+ import { TypedNextRequest, TypedNextResponse } from "next-rest-framework";
3
+ import { BaseTable, GetListRelations } from "../types.js";
4
+ export interface GetListOperationOptions<T extends ZodSchema, Q extends ZodSchema, TTable extends BaseTable> {
5
+ bodySchema: T;
6
+ jsonArrayFields?: string[];
7
+ querySchema: Q;
8
+ relations?: GetListRelations;
9
+ setParams?: (_req: TypedNextRequest<"GET", "application/json", unknown, z.infer<Q>>) => Promise<Record<string, unknown>>;
10
+ byCreator?: boolean;
11
+ summary?: string;
12
+ table: TTable;
13
+ onSuccess?: <D extends T>(_data: {
14
+ data: z.infer<D>[];
15
+ total: number;
16
+ }) => Promise<{
17
+ data: z.infer<T>[];
18
+ total: number;
19
+ }>;
20
+ onError?: (_error: Error) => Promise<ReturnType<(typeof TypedNextResponse)["json"]> | undefined>;
21
+ }
22
+ export declare const createGetListOperation: any;
@@ -0,0 +1,122 @@
1
+ function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
2
+ function _regeneratorRuntime() { "use strict"; /*! regenerator-runtime -- Copyright (c) 2014-present, Facebook, Inc. -- license (MIT): https://github.com/facebook/regenerator/blob/main/LICENSE */ _regeneratorRuntime = function _regeneratorRuntime() { return e; }; var t, e = {}, r = Object.prototype, n = r.hasOwnProperty, o = Object.defineProperty || function (t, e, r) { t[e] = r.value; }, i = "function" == typeof Symbol ? Symbol : {}, a = i.iterator || "@@iterator", c = i.asyncIterator || "@@asyncIterator", u = i.toStringTag || "@@toStringTag"; function define(t, e, r) { return Object.defineProperty(t, e, { value: r, enumerable: !0, configurable: !0, writable: !0 }), t[e]; } try { define({}, ""); } catch (t) { define = function define(t, e, r) { return t[e] = r; }; } function wrap(t, e, r, n) { var i = e && e.prototype instanceof Generator ? e : Generator, a = Object.create(i.prototype), c = new Context(n || []); return o(a, "_invoke", { value: makeInvokeMethod(t, r, c) }), a; } function tryCatch(t, e, r) { try { return { type: "normal", arg: t.call(e, r) }; } catch (t) { return { type: "throw", arg: t }; } } e.wrap = wrap; var h = "suspendedStart", l = "suspendedYield", f = "executing", s = "completed", y = {}; function Generator() {} function GeneratorFunction() {} function GeneratorFunctionPrototype() {} var p = {}; define(p, a, function () { return this; }); var d = Object.getPrototypeOf, v = d && d(d(values([]))); v && v !== r && n.call(v, a) && (p = v); var g = GeneratorFunctionPrototype.prototype = Generator.prototype = Object.create(p); function defineIteratorMethods(t) { ["next", "throw", "return"].forEach(function (e) { define(t, e, function (t) { return this._invoke(e, t); }); }); } function AsyncIterator(t, e) { function invoke(r, o, i, a) { var c = tryCatch(t[r], t, o); if ("throw" !== c.type) { var u = c.arg, h = u.value; return h && "object" == _typeof(h) && n.call(h, "__await") ? e.resolve(h.__await).then(function (t) { invoke("next", t, i, a); }, function (t) { invoke("throw", t, i, a); }) : e.resolve(h).then(function (t) { u.value = t, i(u); }, function (t) { return invoke("throw", t, i, a); }); } a(c.arg); } var r; o(this, "_invoke", { value: function value(t, n) { function callInvokeWithMethodAndArg() { return new e(function (e, r) { invoke(t, n, e, r); }); } return r = r ? r.then(callInvokeWithMethodAndArg, callInvokeWithMethodAndArg) : callInvokeWithMethodAndArg(); } }); } function makeInvokeMethod(e, r, n) { var o = h; return function (i, a) { if (o === f) throw new Error("Generator is already running"); if (o === s) { if ("throw" === i) throw a; return { value: t, done: !0 }; } for (n.method = i, n.arg = a;;) { var c = n.delegate; if (c) { var u = maybeInvokeDelegate(c, n); if (u) { if (u === y) continue; return u; } } if ("next" === n.method) n.sent = n._sent = n.arg;else if ("throw" === n.method) { if (o === h) throw o = s, n.arg; n.dispatchException(n.arg); } else "return" === n.method && n.abrupt("return", n.arg); o = f; var p = tryCatch(e, r, n); if ("normal" === p.type) { if (o = n.done ? s : l, p.arg === y) continue; return { value: p.arg, done: n.done }; } "throw" === p.type && (o = s, n.method = "throw", n.arg = p.arg); } }; } function maybeInvokeDelegate(e, r) { var n = r.method, o = e.iterator[n]; if (o === t) return r.delegate = null, "throw" === n && e.iterator.return && (r.method = "return", r.arg = t, maybeInvokeDelegate(e, r), "throw" === r.method) || "return" !== n && (r.method = "throw", r.arg = new TypeError("The iterator does not provide a '" + n + "' method")), y; var i = tryCatch(o, e.iterator, r.arg); if ("throw" === i.type) return r.method = "throw", r.arg = i.arg, r.delegate = null, y; var a = i.arg; return a ? a.done ? (r[e.resultName] = a.value, r.next = e.nextLoc, "return" !== r.method && (r.method = "next", r.arg = t), r.delegate = null, y) : a : (r.method = "throw", r.arg = new TypeError("iterator result is not an object"), r.delegate = null, y); } function pushTryEntry(t) { var e = { tryLoc: t[0] }; 1 in t && (e.catchLoc = t[1]), 2 in t && (e.finallyLoc = t[2], e.afterLoc = t[3]), this.tryEntries.push(e); } function resetTryEntry(t) { var e = t.completion || {}; e.type = "normal", delete e.arg, t.completion = e; } function Context(t) { this.tryEntries = [{ tryLoc: "root" }], t.forEach(pushTryEntry, this), this.reset(!0); } function values(e) { if (e || "" === e) { var r = e[a]; if (r) return r.call(e); if ("function" == typeof e.next) return e; if (!isNaN(e.length)) { var o = -1, i = function next() { for (; ++o < e.length;) if (n.call(e, o)) return next.value = e[o], next.done = !1, next; return next.value = t, next.done = !0, next; }; return i.next = i; } } throw new TypeError(_typeof(e) + " is not iterable"); } return GeneratorFunction.prototype = GeneratorFunctionPrototype, o(g, "constructor", { value: GeneratorFunctionPrototype, configurable: !0 }), o(GeneratorFunctionPrototype, "constructor", { value: GeneratorFunction, configurable: !0 }), GeneratorFunction.displayName = define(GeneratorFunctionPrototype, u, "GeneratorFunction"), e.isGeneratorFunction = function (t) { var e = "function" == typeof t && t.constructor; return !!e && (e === GeneratorFunction || "GeneratorFunction" === (e.displayName || e.name)); }, e.mark = function (t) { return Object.setPrototypeOf ? Object.setPrototypeOf(t, GeneratorFunctionPrototype) : (t.__proto__ = GeneratorFunctionPrototype, define(t, u, "GeneratorFunction")), t.prototype = Object.create(g), t; }, e.awrap = function (t) { return { __await: t }; }, defineIteratorMethods(AsyncIterator.prototype), define(AsyncIterator.prototype, c, function () { return this; }), e.AsyncIterator = AsyncIterator, e.async = function (t, r, n, o, i) { void 0 === i && (i = Promise); var a = new AsyncIterator(wrap(t, r, n, o), i); return e.isGeneratorFunction(r) ? a : a.next().then(function (t) { return t.done ? t.value : a.next(); }); }, defineIteratorMethods(g), define(g, u, "Generator"), define(g, a, function () { return this; }), define(g, "toString", function () { return "[object Generator]"; }), e.keys = function (t) { var e = Object(t), r = []; for (var n in e) r.push(n); return r.reverse(), function next() { for (; r.length;) { var t = r.pop(); if (t in e) return next.value = t, next.done = !1, next; } return next.done = !0, next; }; }, e.values = values, Context.prototype = { constructor: Context, reset: function reset(e) { if (this.prev = 0, this.next = 0, this.sent = this._sent = t, this.done = !1, this.delegate = null, this.method = "next", this.arg = t, this.tryEntries.forEach(resetTryEntry), !e) for (var r in this) "t" === r.charAt(0) && n.call(this, r) && !isNaN(+r.slice(1)) && (this[r] = t); }, stop: function stop() { this.done = !0; var t = this.tryEntries[0].completion; if ("throw" === t.type) throw t.arg; return this.rval; }, dispatchException: function dispatchException(e) { if (this.done) throw e; var r = this; function handle(n, o) { return a.type = "throw", a.arg = e, r.next = n, o && (r.method = "next", r.arg = t), !!o; } for (var o = this.tryEntries.length - 1; o >= 0; --o) { var i = this.tryEntries[o], a = i.completion; if ("root" === i.tryLoc) return handle("end"); if (i.tryLoc <= this.prev) { var c = n.call(i, "catchLoc"), u = n.call(i, "finallyLoc"); if (c && u) { if (this.prev < i.catchLoc) return handle(i.catchLoc, !0); if (this.prev < i.finallyLoc) return handle(i.finallyLoc); } else if (c) { if (this.prev < i.catchLoc) return handle(i.catchLoc, !0); } else { if (!u) throw new Error("try statement without catch or finally"); if (this.prev < i.finallyLoc) return handle(i.finallyLoc); } } } }, abrupt: function abrupt(t, e) { for (var r = this.tryEntries.length - 1; r >= 0; --r) { var o = this.tryEntries[r]; if (o.tryLoc <= this.prev && n.call(o, "finallyLoc") && this.prev < o.finallyLoc) { var i = o; break; } } i && ("break" === t || "continue" === t) && i.tryLoc <= e && e <= i.finallyLoc && (i = null); var a = i ? i.completion : {}; return a.type = t, a.arg = e, i ? (this.method = "next", this.next = i.finallyLoc, y) : this.complete(a); }, complete: function complete(t, e) { if ("throw" === t.type) throw t.arg; return "break" === t.type || "continue" === t.type ? this.next = t.arg : "return" === t.type ? (this.rval = this.arg = t.arg, this.method = "return", this.next = "end") : "normal" === t.type && e && (this.next = e), y; }, finish: function finish(t) { for (var e = this.tryEntries.length - 1; e >= 0; --e) { var r = this.tryEntries[e]; if (r.finallyLoc === t) return this.complete(r.completion, r.afterLoc), resetTryEntry(r), y; } }, catch: function _catch(t) { for (var e = this.tryEntries.length - 1; e >= 0; --e) { var r = this.tryEntries[e]; if (r.tryLoc === t) { var n = r.completion; if ("throw" === n.type) { var o = n.arg; resetTryEntry(r); } return o; } } throw new Error("illegal catch attempt"); }, delegateYield: function delegateYield(e, r, n) { return this.delegate = { iterator: values(e), resultName: r, nextLoc: n }, "next" === this.method && (this.arg = t), y; } }, e; }
3
+ function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } }
4
+ function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; }
5
+ /* eslint-disable no-unused-vars */
6
+
7
+ import { routeOperation, TypedNextResponse } from "next-rest-framework";
8
+ import getTableName from "./get-table-name.js";
9
+ import { listBodySchema } from "../schemas/index.js";
10
+ import { createGetListAction } from "../actions/index.js";
11
+ export var createGetListOperation = function createGetListOperation(_ref) {
12
+ var db = _ref.db,
13
+ getSession = _ref.getSession;
14
+ return function (_ref2) {
15
+ var querySchema = _ref2.querySchema,
16
+ bodySchema = _ref2.bodySchema,
17
+ table = _ref2.table,
18
+ summary = _ref2.summary,
19
+ relations = _ref2.relations,
20
+ jsonArrayFields = _ref2.jsonArrayFields,
21
+ setParams = _ref2.setParams,
22
+ onSuccess = _ref2.onSuccess,
23
+ onError = _ref2.onError,
24
+ _ref2$byCreator = _ref2.byCreator,
25
+ byCreator = _ref2$byCreator === void 0 ? true : _ref2$byCreator;
26
+ return routeOperation({
27
+ method: "GET",
28
+ openApiOperation: {
29
+ summary: summary,
30
+ tags: [getTableName(table)]
31
+ }
32
+ }).input({
33
+ query: querySchema
34
+ }).outputs([{
35
+ body: listBodySchema(bodySchema),
36
+ contentType: "application/json",
37
+ status: 200
38
+ }]).handler( /*#__PURE__*/function () {
39
+ var _ref3 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(req) {
40
+ var params, _ref4, userId, result, response;
41
+ return _regeneratorRuntime().wrap(function _callee$(_context) {
42
+ while (1) switch (_context.prev = _context.next) {
43
+ case 0:
44
+ _context.prev = 0;
45
+ _context.next = 3;
46
+ return setParams === null || setParams === void 0 ? void 0 : setParams(req);
47
+ case 3:
48
+ _context.t0 = _context.sent;
49
+ if (_context.t0) {
50
+ _context.next = 6;
51
+ break;
52
+ }
53
+ _context.t0 = {};
54
+ case 6:
55
+ params = _context.t0;
56
+ if (!byCreator) {
57
+ _context.next = 16;
58
+ break;
59
+ }
60
+ _context.next = 10;
61
+ return getSession(req);
62
+ case 10:
63
+ _context.t1 = _context.sent;
64
+ if (_context.t1) {
65
+ _context.next = 13;
66
+ break;
67
+ }
68
+ _context.t1 = {};
69
+ case 13:
70
+ _ref4 = _context.t1;
71
+ userId = _ref4.userId;
72
+ params.creatorId = userId;
73
+ case 16:
74
+ _context.next = 18;
75
+ return createGetListAction({
76
+ bodySchema: bodySchema,
77
+ db: db,
78
+ jsonArrayFields: jsonArrayFields,
79
+ relations: relations,
80
+ table: table
81
+ })(Object.assign(Object.fromEntries(new URL(req.url).searchParams), params));
82
+ case 18:
83
+ result = _context.sent;
84
+ if (!onSuccess) {
85
+ _context.next = 23;
86
+ break;
87
+ }
88
+ _context.next = 22;
89
+ return onSuccess(result);
90
+ case 22:
91
+ result = _context.sent;
92
+ case 23:
93
+ return _context.abrupt("return", TypedNextResponse.json(result, {
94
+ status: 200
95
+ }));
96
+ case 26:
97
+ _context.prev = 26;
98
+ _context.t2 = _context["catch"](0);
99
+ console.error(_context.t2);
100
+ _context.next = 31;
101
+ return onError === null || onError === void 0 ? void 0 : onError(_context.t2);
102
+ case 31:
103
+ response = _context.sent;
104
+ if (!response) {
105
+ _context.next = 36;
106
+ break;
107
+ }
108
+ return _context.abrupt("return", response);
109
+ case 36:
110
+ throw _context.t2;
111
+ case 37:
112
+ case "end":
113
+ return _context.stop();
114
+ }
115
+ }, _callee, null, [[0, 26]]);
116
+ }));
117
+ return function (_x) {
118
+ return _ref3.apply(this, arguments);
119
+ };
120
+ }());
121
+ };
122
+ };