@ai0x0/utils 0.5.0 → 0.6.1

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 (59) hide show
  1. package/es/backend/actions/create-delete-action.d.ts +2 -3
  2. package/es/backend/actions/create-delete-action.js +8 -44
  3. package/es/backend/actions/create-put-action.d.ts +1 -1
  4. package/es/backend/actions/create-put-action.js +15 -39
  5. package/es/backend/actions/get-list-query-core.d.ts +46 -0
  6. package/es/backend/actions/get-list-query-core.js +236 -0
  7. package/es/backend/actions/get-list-query.d.ts +5 -18
  8. package/es/backend/actions/get-list-query.js +20 -220
  9. package/es/backend/actions/write-action-core.d.ts +18 -0
  10. package/es/backend/actions/write-action-core.js +119 -0
  11. package/es/backend/route-operation/operation-common.d.ts +13 -1
  12. package/es/backend/route-operation/operation-common.js +25 -0
  13. package/es/backend/route-operation/read-operation-core.js +46 -33
  14. package/es/backend/route-operation/write-operation-core.d.ts +3 -1
  15. package/es/backend/route-operation/write-operation-core.js +69 -26
  16. package/es/backend/schemas/common.d.ts +112 -0
  17. package/es/backend/schemas/common.js +158 -0
  18. package/es/backend/schemas/index.d.ts +735 -364
  19. package/es/backend/schemas/index.js +8 -139
  20. package/es/backend/sqlite/actions/create-delete-action.d.ts +1 -2
  21. package/es/backend/sqlite/actions/create-delete-action.js +7 -43
  22. package/es/backend/sqlite/actions/create-put-action.d.ts +1 -1
  23. package/es/backend/sqlite/actions/create-put-action.js +13 -36
  24. package/es/backend/sqlite/actions/get-list-query.d.ts +4 -9
  25. package/es/backend/sqlite/actions/get-list-query.js +20 -217
  26. package/es/backend/sqlite/errors.d.ts +1 -4
  27. package/es/backend/sqlite/errors.js +6 -31
  28. package/es/backend/sqlite/schemas/index.d.ts +1 -111
  29. package/es/backend/sqlite/schemas/index.js +8 -139
  30. package/lib/backend/actions/create-delete-action.d.ts +2 -3
  31. package/lib/backend/actions/create-delete-action.js +2 -14
  32. package/lib/backend/actions/create-put-action.d.ts +1 -1
  33. package/lib/backend/actions/create-put-action.js +3 -16
  34. package/lib/backend/actions/get-list-query-core.d.ts +46 -0
  35. package/lib/backend/actions/get-list-query-core.js +151 -0
  36. package/lib/backend/actions/get-list-query.d.ts +5 -18
  37. package/lib/backend/actions/get-list-query.js +16 -141
  38. package/lib/backend/actions/write-action-core.d.ts +18 -0
  39. package/lib/backend/actions/write-action-core.js +70 -0
  40. package/lib/backend/route-operation/operation-common.d.ts +13 -1
  41. package/lib/backend/route-operation/operation-common.js +8 -0
  42. package/lib/backend/route-operation/read-operation-core.js +14 -4
  43. package/lib/backend/route-operation/write-operation-core.d.ts +3 -1
  44. package/lib/backend/route-operation/write-operation-core.js +24 -4
  45. package/lib/backend/schemas/common.d.ts +112 -0
  46. package/lib/backend/schemas/common.js +112 -0
  47. package/lib/backend/schemas/index.d.ts +735 -364
  48. package/lib/backend/schemas/index.js +12 -83
  49. package/lib/backend/sqlite/actions/create-delete-action.d.ts +1 -2
  50. package/lib/backend/sqlite/actions/create-delete-action.js +2 -14
  51. package/lib/backend/sqlite/actions/create-put-action.d.ts +1 -1
  52. package/lib/backend/sqlite/actions/create-put-action.js +3 -16
  53. package/lib/backend/sqlite/actions/get-list-query.d.ts +4 -9
  54. package/lib/backend/sqlite/actions/get-list-query.js +16 -141
  55. package/lib/backend/sqlite/errors.d.ts +1 -4
  56. package/lib/backend/sqlite/errors.js +2 -8
  57. package/lib/backend/sqlite/schemas/index.d.ts +1 -111
  58. package/lib/backend/sqlite/schemas/index.js +12 -83
  59. package/package.json +1 -1
@@ -1,11 +1,16 @@
1
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
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 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; }
4
+ 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; }
5
+ 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; }
6
+ function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : String(i); }
7
+ 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); }
3
8
  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
9
  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
10
  import { routeOperation, TypedNextResponse } from "next-rest-framework";
6
11
  import { listBodySchema } from "../schemas/index.js";
7
12
  import { createOpenApiOperation } from "./open-api-operation.js";
8
- import { getDefaultTags, getReadParams, handleOperationError } from "./operation-common.js";
13
+ import { callerIdOf, getDefaultTags, getReadParams, handleOperationError } from "./operation-common.js";
9
14
 
10
15
  // ==============================================================================
11
16
  // GET single operation
@@ -44,7 +49,7 @@ export function createGetOperationFactory(_ref) {
44
49
  status: 200
45
50
  }]).handler( /*#__PURE__*/function () {
46
51
  var _ref4 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(req) {
47
- var _yield$getReadParams, mergedParams, scope, rawResult, result;
52
+ var _yield$getReadParams, mergedParams, scope, session, rawResult, result;
48
53
  return _regeneratorRuntime().wrap(function _callee$(_context) {
49
54
  while (1) switch (_context.prev = _context.next) {
50
55
  case 0:
@@ -61,11 +66,12 @@ export function createGetOperationFactory(_ref) {
61
66
  _yield$getReadParams = _context.sent;
62
67
  mergedParams = _yield$getReadParams.params;
63
68
  scope = _yield$getReadParams.scope;
69
+ session = _yield$getReadParams.session;
64
70
  if (!(table && db)) {
65
- _context.next = 12;
71
+ _context.next = 13;
66
72
  break;
67
73
  }
68
- _context.next = 9;
74
+ _context.next = 10;
69
75
  return createAction({
70
76
  bodySchema: schemas.response,
71
77
  db: db,
@@ -75,40 +81,43 @@ export function createGetOperationFactory(_ref) {
75
81
  })(mergedParams, {
76
82
  scope: scope
77
83
  });
78
- case 9:
84
+ case 10:
79
85
  _context.t0 = _context.sent;
80
- _context.next = 13;
86
+ _context.next = 14;
81
87
  break;
82
- case 12:
83
- _context.t0 = mergedParams;
84
88
  case 13:
89
+ _context.t0 = mergedParams;
90
+ case 14:
85
91
  rawResult = _context.t0;
86
92
  result = rawResult !== null && rawResult !== void 0 ? rawResult : {};
87
93
  if (!handler) {
88
- _context.next = 19;
94
+ _context.next = 20;
89
95
  break;
90
96
  }
91
- _context.next = 18;
97
+ _context.next = 19;
92
98
  return handler({
93
99
  data: result,
94
- params: mergedParams,
100
+ // callerId 只给 handler,不进上面那份筛选/写入用的 params(见 getReadParams)。
101
+ params: _objectSpread(_objectSpread({}, mergedParams), {}, {
102
+ callerId: callerIdOf(session)
103
+ }),
95
104
  req: req
96
105
  });
97
- case 18:
98
- result = _context.sent;
99
106
  case 19:
107
+ result = _context.sent;
108
+ case 20:
100
109
  return _context.abrupt("return", TypedNextResponse.json(result, {
101
110
  status: 200
102
111
  }));
103
- case 22:
104
- _context.prev = 22;
112
+ case 23:
113
+ _context.prev = 23;
105
114
  _context.t1 = _context["catch"](0);
106
115
  return _context.abrupt("return", handleOperationError(_context.t1, catchHandler));
107
- case 25:
116
+ case 26:
108
117
  case "end":
109
118
  return _context.stop();
110
119
  }
111
- }, _callee, null, [[0, 22]]);
120
+ }, _callee, null, [[0, 23]]);
112
121
  }));
113
122
  return function (_x) {
114
123
  return _ref4.apply(this, arguments);
@@ -155,7 +164,7 @@ export function createGetListOperationFactory(_ref5) {
155
164
  status: 200
156
165
  }]).handler( /*#__PURE__*/function () {
157
166
  var _ref8 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee2(req) {
158
- var _yield$getReadParams2, mergedParams, scope, result;
167
+ var _yield$getReadParams2, mergedParams, scope, session, result;
159
168
  return _regeneratorRuntime().wrap(function _callee2$(_context2) {
160
169
  while (1) switch (_context2.prev = _context2.next) {
161
170
  case 0:
@@ -172,11 +181,12 @@ export function createGetListOperationFactory(_ref5) {
172
181
  _yield$getReadParams2 = _context2.sent;
173
182
  mergedParams = _yield$getReadParams2.params;
174
183
  scope = _yield$getReadParams2.scope;
184
+ session = _yield$getReadParams2.session;
175
185
  if (!(table && db)) {
176
- _context2.next = 12;
186
+ _context2.next = 13;
177
187
  break;
178
188
  }
179
- _context2.next = 9;
189
+ _context2.next = 10;
180
190
  return createAction({
181
191
  bodySchema: schemas.response,
182
192
  db: db,
@@ -186,42 +196,45 @@ export function createGetListOperationFactory(_ref5) {
186
196
  })(mergedParams, {
187
197
  scope: scope
188
198
  });
189
- case 9:
199
+ case 10:
190
200
  _context2.t0 = _context2.sent;
191
- _context2.next = 13;
201
+ _context2.next = 14;
192
202
  break;
193
- case 12:
203
+ case 13:
194
204
  _context2.t0 = {
195
205
  data: [],
196
206
  total: 0
197
207
  };
198
- case 13:
208
+ case 14:
199
209
  result = _context2.t0;
200
210
  if (!handler) {
201
- _context2.next = 18;
211
+ _context2.next = 19;
202
212
  break;
203
213
  }
204
- _context2.next = 17;
214
+ _context2.next = 18;
205
215
  return handler({
206
216
  data: result,
207
- params: mergedParams,
217
+ // callerId 只给 handler,不进上面那份筛选/写入用的 params(见 getReadParams)。
218
+ params: _objectSpread(_objectSpread({}, mergedParams), {}, {
219
+ callerId: callerIdOf(session)
220
+ }),
208
221
  req: req
209
222
  });
210
- case 17:
211
- result = _context2.sent;
212
223
  case 18:
224
+ result = _context2.sent;
225
+ case 19:
213
226
  return _context2.abrupt("return", TypedNextResponse.json(result, {
214
227
  status: 200
215
228
  }));
216
- case 21:
217
- _context2.prev = 21;
229
+ case 22:
230
+ _context2.prev = 22;
218
231
  _context2.t1 = _context2["catch"](0);
219
232
  return _context2.abrupt("return", handleOperationError(_context2.t1, catchHandler));
220
- case 24:
233
+ case 25:
221
234
  case "end":
222
235
  return _context2.stop();
223
236
  }
224
- }, _callee2, null, [[0, 21]]);
237
+ }, _callee2, null, [[0, 22]]);
225
238
  }));
226
239
  return function (_x2) {
227
240
  return _ref8.apply(this, arguments);
@@ -5,6 +5,7 @@ import { type RouteOpenApiOperation } from "./open-api-operation.js";
5
5
  import { AccessOptions, ActionFactory, DefaultSession, RouteCatch, SessionGetter } from "./operation-common.js";
6
6
  import { ScopeArg } from "../scope.js";
7
7
  export type PostOperationParams<IB extends ZodSchema> = z.infer<IB> & Record<string, unknown> & {
8
+ callerId?: string;
8
9
  creatorId?: string;
9
10
  };
10
11
  export interface PostOperationOptions<IB extends ZodSchema, OB extends ZodSchema, TTable, TSession = DefaultSession> {
@@ -37,6 +38,7 @@ export declare function createPostOperationFactory<TTable>({ createAction, getTa
37
38
  getSession: SessionGetter<TSession>;
38
39
  }) => <IB extends z.ZodType<unknown, unknown, z.core.$ZodTypeInternals<unknown, unknown>>, OB extends z.ZodType<unknown, unknown, z.core.$ZodTypeInternals<unknown, unknown>>>({ schemas, access, contentType, openApiOperation, parseBody, setBody, table, handler, catch: catchHandler, }: PostOperationOptions<IB, OB, TTable, TSession>) => import("next-rest-framework").RouteOperationDefinition<"POST">;
39
40
  export type PutOperationParams<IB extends ZodSchema> = z.infer<IB> & Record<string, unknown> & {
41
+ callerId?: string;
40
42
  editorId?: string;
41
43
  };
42
44
  export interface PutOperationOptions<IB extends ZodSchema, OB extends ZodSchema, TTable, TSession = DefaultSession> {
@@ -65,7 +67,7 @@ export declare function createPutOperationFactory<TTable>({ createAction, getTab
65
67
  getSession: SessionGetter<TSession>;
66
68
  }) => <IB extends z.ZodType<unknown, unknown, z.core.$ZodTypeInternals<unknown, unknown>>, OB extends z.ZodType<unknown, unknown, z.core.$ZodTypeInternals<unknown, unknown>>>({ schemas, access, openApiOperation, table, setBody, handler, catch: catchHandler, }: PutOperationOptions<IB, OB, TTable, TSession>) => import("next-rest-framework").RouteOperationDefinition<"PUT">;
67
69
  export type DeleteOperationParams<B extends ZodSchema> = z.infer<B> & Record<string, unknown> & {
68
- creatorId?: string;
70
+ callerId?: string;
69
71
  id: string;
70
72
  };
71
73
  export interface DeleteOperationOptions<TTable, B extends ZodSchema, TSession = DefaultSession> {
@@ -10,13 +10,17 @@ function _asyncToGenerator(fn) { return function () { var self = this, args = ar
10
10
  import { routeOperation, TypedNextResponse } from "next-rest-framework";
11
11
  import { z } from "zod";
12
12
  import { createOpenApiOperation } from "./open-api-operation.js";
13
- import { getDefaultTags, handleOperationError, readPostBody, resolveAccess, sessionLoader } from "./operation-common.js";
13
+ import { callerIdOf, getDefaultTags, handleOperationError, readPostBody, resolveAccess, sessionLoader } from "./operation-common.js";
14
14
  import { NO_SCOPE } from "../scope.js";
15
15
 
16
16
  // ==============================================================================
17
17
  // POST create operation
18
18
  // ==============================================================================
19
19
 
20
+ // creatorId 是**归属戳**(access.scope 指的那一列)——默认作用域下它确实叫 creatorId,
21
+ // 但换成 `scope: { column: "ownerId" }` 之后这一行盖的就是 ownerId 了。要「谁发的请求」
22
+ // 一律读 callerId,那个四个方法都有、也不受作用域配置影响。
23
+
20
24
  export function createPostOperationFactory(_ref) {
21
25
  var createAction = _ref.createAction,
22
26
  getTableName = _ref.getTableName;
@@ -127,7 +131,11 @@ export function createPostOperationFactory(_ref) {
127
131
  _context.next = 28;
128
132
  return handler({
129
133
  data: raw,
130
- params: params,
134
+ // callerId 只加在**交给 handler 的那一份**上,不进上面 createAction 收到的
135
+ // params —— 那一份会被原样 insert,而 callerId 不是任何表的列。
136
+ params: _objectSpread(_objectSpread({}, params), {}, {
137
+ callerId: callerIdOf(session)
138
+ }),
131
139
  req: req
132
140
  });
133
141
  case 28:
@@ -163,6 +171,9 @@ export function createPostOperationFactory(_ref) {
163
171
  // PUT update operation
164
172
  // ==============================================================================
165
173
 
174
+ // editorId 是要写进行里的**审计列**(「谁改的」)。它恰好等于调用者,但语义是两件事 ——
175
+ // 判权请读 callerId,别读它。
176
+
166
177
  export function createPutOperationFactory(_ref5) {
167
178
  var createAction = _ref5.createAction,
168
179
  getTableName = _ref5.getTableName;
@@ -279,7 +290,11 @@ export function createPutOperationFactory(_ref5) {
279
290
  _context2.next = 39;
280
291
  return handler({
281
292
  data: raw,
282
- params: params,
293
+ // 同 POST:callerId 不进 createAction 那一份(它会被 .set() 原样写进 UPDATE,
294
+ // 而 callerId 不是列)。editorId 是列,所以它留在 params 里。
295
+ params: _objectSpread(_objectSpread({}, params), {}, {
296
+ callerId: editorId
297
+ }),
283
298
  req: req
284
299
  });
285
300
  case 39:
@@ -318,6 +333,11 @@ export function createPutOperationFactory(_ref5) {
318
333
  var defaultDeleteBodySchema = z.object({
319
334
  id: z.string().describe("Id of the row to delete.")
320
335
  });
336
+
337
+ // 原先这里声明的是 `creatorId?: string`,而删除**从来没有填过它** —— 类型说有、运行时没有,
338
+ // 于是 `requireAdmin(params.creatorId)` 这种判权写法编译绿灯、线上对所有人 401。换成 callerId
339
+ // 并且真的盖上。
340
+
321
341
  export function createDeleteOperationFactory(_ref9) {
322
342
  var createAction = _ref9.createAction,
323
343
  getTableName = _ref9.getTableName;
@@ -345,7 +365,7 @@ export function createDeleteOperationFactory(_ref9) {
345
365
  contentType: "application/json"
346
366
  }).handler( /*#__PURE__*/function () {
347
367
  var _ref12 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee3(req) {
348
- var bodySchema, body, _yield$resolveAccess3, scope, data;
368
+ var bodySchema, body, loadSession, _yield$resolveAccess3, scope, data;
349
369
  return _regeneratorRuntime().wrap(function _callee3$(_context3) {
350
370
  while (1) switch (_context3.prev = _context3.next) {
351
371
  case 0:
@@ -357,20 +377,22 @@ export function createDeleteOperationFactory(_ref9) {
357
377
  case 5:
358
378
  _context3.t1 = _context3.sent;
359
379
  body = _context3.t0.parse.call(_context3.t0, _context3.t1);
360
- _context3.next = 9;
380
+ // loadSession 带缓存,下面取 callerId 时不会再往返一次。
381
+ loadSession = sessionLoader(getSession, req);
382
+ _context3.next = 10;
361
383
  return resolveAccess({
362
384
  access: access,
363
- loadSession: sessionLoader(getSession, req),
385
+ loadSession: loadSession,
364
386
  method: "DELETE"
365
387
  });
366
- case 9:
388
+ case 10:
367
389
  _yield$resolveAccess3 = _context3.sent;
368
390
  scope = _yield$resolveAccess3.scope;
369
391
  if (!(table && db)) {
370
- _context3.next = 17;
392
+ _context3.next = 18;
371
393
  break;
372
394
  }
373
- _context3.next = 14;
395
+ _context3.next = 15;
374
396
  return createAction({
375
397
  db: db,
376
398
  table: table
@@ -379,33 +401,54 @@ export function createDeleteOperationFactory(_ref9) {
379
401
  }, {
380
402
  scope: scope
381
403
  });
382
- case 14:
404
+ case 15:
383
405
  _context3.t2 = _context3.sent;
384
- _context3.next = 18;
406
+ _context3.next = 19;
385
407
  break;
386
- case 17:
387
- _context3.t2 = body;
388
408
  case 18:
409
+ _context3.t2 = body;
410
+ case 19:
389
411
  data = _context3.t2;
390
- _context3.next = 21;
391
- return handler === null || handler === void 0 ? void 0 : handler({
392
- data: data,
393
- params: body,
394
- req: req
395
- });
396
- case 21:
412
+ if (!handler) {
413
+ _context3.next = 37;
414
+ break;
415
+ }
416
+ _context3.t3 = handler;
417
+ _context3.t4 = data;
418
+ _context3.t5 = _objectSpread;
419
+ _context3.t6 = _objectSpread({}, body);
420
+ _context3.t7 = {};
421
+ _context3.t8 = callerIdOf;
422
+ _context3.next = 29;
423
+ return loadSession();
424
+ case 29:
425
+ _context3.t9 = _context3.sent;
426
+ _context3.t10 = (0, _context3.t8)(_context3.t9);
427
+ _context3.t11 = {
428
+ callerId: _context3.t10
429
+ };
430
+ _context3.t12 = (0, _context3.t5)(_context3.t6, _context3.t7, _context3.t11);
431
+ _context3.t13 = req;
432
+ _context3.t14 = {
433
+ data: _context3.t4,
434
+ params: _context3.t12,
435
+ req: _context3.t13
436
+ };
437
+ _context3.next = 37;
438
+ return (0, _context3.t3)(_context3.t14);
439
+ case 37:
397
440
  return _context3.abrupt("return", TypedNextResponse.json(data, {
398
441
  status: 200
399
442
  }));
400
- case 24:
401
- _context3.prev = 24;
402
- _context3.t3 = _context3["catch"](0);
403
- return _context3.abrupt("return", handleOperationError(_context3.t3, catchHandler));
404
- case 27:
443
+ case 40:
444
+ _context3.prev = 40;
445
+ _context3.t15 = _context3["catch"](0);
446
+ return _context3.abrupt("return", handleOperationError(_context3.t15, catchHandler));
447
+ case 43:
405
448
  case "end":
406
449
  return _context3.stop();
407
450
  }
408
- }, _callee3, null, [[0, 24]]);
451
+ }, _callee3, null, [[0, 40]]);
409
452
  }));
410
453
  return function (_x3) {
411
454
  return _ref12.apply(this, arguments);
@@ -0,0 +1,112 @@
1
+ import { z } from "zod";
2
+ /**
3
+ * 业务侧 insert schema 需要从 drizzle-zod 生成的 schema 中排除的基础字段。
4
+ * 这些字段由后端在 createPostAction 中自动写入。
5
+ *
6
+ * 使用方式(在业务 schema 中):
7
+ * const insertFooSchema = createInsertSchema(foos)
8
+ * .omit(BASIC_INSERT_OMIT)
9
+ * .extend({ ... });
10
+ *
11
+ * 说明:上游导出为 const 而不是 helper function,是为了避免经过泛型
12
+ * wrapper 后 TS 把 drizzle-zod 推导出的深层条件 Shape 归约成 Omit,
13
+ * 从而在下游 `.extend(...)` 时丢失原有字段类型。
14
+ */
15
+ export declare const BASIC_INSERT_OMIT: {
16
+ readonly id: true;
17
+ readonly creatorId: true;
18
+ readonly editorId: true;
19
+ readonly accessedAt: true;
20
+ readonly createdAt: true;
21
+ readonly updatedAt: true;
22
+ };
23
+ /**
24
+ * 业务侧 update schema 需要从 drizzle-zod 生成的 schema 中排除的基础字段。
25
+ * update 保留 id(随后用 `.required({ id: true })` 变成必填),其余由
26
+ * 后端 createPutAction 自动维护。
27
+ */
28
+ export declare const BASIC_UPDATE_OMIT: {
29
+ readonly creatorId: true;
30
+ readonly editorId: true;
31
+ readonly accessedAt: true;
32
+ readonly createdAt: true;
33
+ readonly updatedAt: true;
34
+ };
35
+ export declare const queryListSchema: <Incoming extends z.ZodObject<z.core.$ZodLooseShape, z.core.$strip>>(schema: Incoming) => z.ZodObject<(("orderBy" | "orderDir" | "pageSize" | "creatorId" | "current" | "createdAtFrom" | "createdAtTo") & keyof Incoming["shape"] extends never ? {
36
+ current: z.ZodDefault<z.ZodOptional<z.ZodString>>;
37
+ pageSize: z.ZodDefault<z.ZodOptional<z.ZodString>>;
38
+ createdAtFrom: z.ZodOptional<z.ZodString>;
39
+ createdAtTo: z.ZodOptional<z.ZodString>;
40
+ orderBy: z.ZodOptional<z.ZodString>;
41
+ creatorId: z.ZodOptional<z.ZodString>;
42
+ orderDir: z.ZodOptional<z.ZodEnum<{
43
+ asc: "asc";
44
+ desc: "desc";
45
+ }>>;
46
+ } & Incoming["shape"] : ({
47
+ current: z.ZodDefault<z.ZodOptional<z.ZodString>>;
48
+ pageSize: z.ZodDefault<z.ZodOptional<z.ZodString>>;
49
+ createdAtFrom: z.ZodOptional<z.ZodString>;
50
+ createdAtTo: z.ZodOptional<z.ZodString>;
51
+ orderBy: z.ZodOptional<z.ZodString>;
52
+ creatorId: z.ZodOptional<z.ZodString>;
53
+ orderDir: z.ZodOptional<z.ZodEnum<{
54
+ asc: "asc";
55
+ desc: "desc";
56
+ }>>;
57
+ } extends infer T_1 extends z.core.util.SomeObject ? { [K in keyof T_1 as K extends keyof Incoming["shape"] ? never : K]: {
58
+ current: z.ZodDefault<z.ZodOptional<z.ZodString>>;
59
+ pageSize: z.ZodDefault<z.ZodOptional<z.ZodString>>;
60
+ createdAtFrom: z.ZodOptional<z.ZodString>;
61
+ createdAtTo: z.ZodOptional<z.ZodString>;
62
+ orderBy: z.ZodOptional<z.ZodString>;
63
+ creatorId: z.ZodOptional<z.ZodString>;
64
+ orderDir: z.ZodOptional<z.ZodEnum<{
65
+ asc: "asc";
66
+ desc: "desc";
67
+ }>>;
68
+ }[K]; } : never) & (Incoming["shape"] extends infer T_2 extends z.core.util.SomeObject ? { [K_1 in keyof T_2]: Incoming["shape"][K_1]; } : never)) extends infer T ? { [k in keyof T]: (("orderBy" | "orderDir" | "pageSize" | "creatorId" | "current" | "createdAtFrom" | "createdAtTo") & keyof Incoming["shape"] extends never ? {
69
+ current: z.ZodDefault<z.ZodOptional<z.ZodString>>;
70
+ pageSize: z.ZodDefault<z.ZodOptional<z.ZodString>>;
71
+ createdAtFrom: z.ZodOptional<z.ZodString>;
72
+ createdAtTo: z.ZodOptional<z.ZodString>;
73
+ orderBy: z.ZodOptional<z.ZodString>;
74
+ creatorId: z.ZodOptional<z.ZodString>;
75
+ orderDir: z.ZodOptional<z.ZodEnum<{
76
+ asc: "asc";
77
+ desc: "desc";
78
+ }>>;
79
+ } & Incoming["shape"] : ({
80
+ current: z.ZodDefault<z.ZodOptional<z.ZodString>>;
81
+ pageSize: z.ZodDefault<z.ZodOptional<z.ZodString>>;
82
+ createdAtFrom: z.ZodOptional<z.ZodString>;
83
+ createdAtTo: z.ZodOptional<z.ZodString>;
84
+ orderBy: z.ZodOptional<z.ZodString>;
85
+ creatorId: z.ZodOptional<z.ZodString>;
86
+ orderDir: z.ZodOptional<z.ZodEnum<{
87
+ asc: "asc";
88
+ desc: "desc";
89
+ }>>;
90
+ } extends infer T_1 extends z.core.util.SomeObject ? { [K in keyof T_1 as K extends keyof Incoming["shape"] ? never : K]: {
91
+ current: z.ZodDefault<z.ZodOptional<z.ZodString>>;
92
+ pageSize: z.ZodDefault<z.ZodOptional<z.ZodString>>;
93
+ createdAtFrom: z.ZodOptional<z.ZodString>;
94
+ createdAtTo: z.ZodOptional<z.ZodString>;
95
+ orderBy: z.ZodOptional<z.ZodString>;
96
+ creatorId: z.ZodOptional<z.ZodString>;
97
+ orderDir: z.ZodOptional<z.ZodEnum<{
98
+ asc: "asc";
99
+ desc: "desc";
100
+ }>>;
101
+ }[K]; } : never) & (Incoming["shape"] extends infer T_2 extends z.core.util.SomeObject ? { [K_1 in keyof T_2]: Incoming["shape"][K_1]; } : never))[k]; } : never, Incoming["_zod"]["config"]>;
102
+ export declare const listBodySchema: <T extends z.ZodType<unknown, unknown, z.core.$ZodTypeInternals<unknown, unknown>>>(schema: T) => z.ZodObject<{
103
+ total: z.ZodNumber;
104
+ data: z.ZodArray<T>;
105
+ }, z.core.$strip>;
106
+ /**
107
+ * 给 schema 上已有的字段挂说明。字段不在这个 schema 里就跳过 —— 同一张表的
108
+ * insert / update / select 三份含的列不一样(basicFields 只在 select 里),一份说明表要能同时
109
+ * 喂给三者,就不能对「字段一定存在」有要求。**不给不存在的字段凭空造一个。**
110
+ */
111
+ export declare const describeFields: <T extends z.ZodObject<z.core.$ZodLooseShape, z.core.$strip>>(schema: T, notes: Record<string, string>) => T;
112
+ export declare const describeBasicFields: <T extends z.ZodObject<z.core.$ZodLooseShape, z.core.$strip>>(schema: T) => T;