@ai0x0/utils 0.0.46 → 0.0.47

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 (28) hide show
  1. package/es/backend/route-operation/delete-operation.d.ts +12 -6
  2. package/es/backend/route-operation/delete-operation.js +52 -46
  3. package/es/backend/route-operation/get-list-operation.d.ts +5 -1
  4. package/es/backend/route-operation/get-list-operation.js +30 -23
  5. package/es/backend/route-operation/get-operation.d.ts +5 -1
  6. package/es/backend/route-operation/get-operation.js +30 -23
  7. package/es/backend/sqlite/route-operation/delete-operation.d.ts +12 -6
  8. package/es/backend/sqlite/route-operation/delete-operation.js +52 -46
  9. package/es/backend/sqlite/route-operation/get-list-operation.d.ts +5 -1
  10. package/es/backend/sqlite/route-operation/get-list-operation.js +30 -23
  11. package/es/backend/sqlite/route-operation/get-operation.d.ts +5 -1
  12. package/es/backend/sqlite/route-operation/get-operation.js +30 -23
  13. package/es/backend/sqlite/schemas/index.d.ts +69 -5
  14. package/es/backend/sqlite/schemas/index.js +0 -1
  15. package/lib/backend/route-operation/delete-operation.d.ts +12 -6
  16. package/lib/backend/route-operation/delete-operation.js +10 -5
  17. package/lib/backend/route-operation/get-list-operation.d.ts +5 -1
  18. package/lib/backend/route-operation/get-list-operation.js +10 -6
  19. package/lib/backend/route-operation/get-operation.d.ts +5 -1
  20. package/lib/backend/route-operation/get-operation.js +10 -6
  21. package/lib/backend/sqlite/route-operation/delete-operation.d.ts +12 -6
  22. package/lib/backend/sqlite/route-operation/delete-operation.js +10 -5
  23. package/lib/backend/sqlite/route-operation/get-list-operation.d.ts +5 -1
  24. package/lib/backend/sqlite/route-operation/get-list-operation.js +10 -6
  25. package/lib/backend/sqlite/route-operation/get-operation.d.ts +5 -1
  26. package/lib/backend/sqlite/route-operation/get-operation.js +10 -6
  27. package/lib/backend/sqlite/schemas/index.d.ts +69 -5
  28. package/package.json +1 -1
@@ -1,17 +1,20 @@
1
1
  import { TypedNextResponse } from "next-rest-framework";
2
+ import { z } from "zod";
2
3
  import { BaseTable } from "../types.js";
3
4
  import { NodePgDatabase } from "drizzle-orm/node-postgres";
4
5
  import { NextRequest } from "next/server";
5
- export interface DeleteOperationOptions<TTable extends BaseTable> {
6
+ type DeleteOperationParams<B extends z.ZodSchema> = z.infer<B> & Record<string, unknown> & {
7
+ creatorId?: string;
8
+ id: string;
9
+ };
10
+ export interface DeleteOperationOptions<TTable extends BaseTable, B extends z.ZodSchema> {
6
11
  table?: TTable;
12
+ bodySchema?: B;
7
13
  description?: string;
8
14
  summary?: string;
9
15
  tags?: string[];
10
16
  onSuccess?: (payload: {
11
- params: {
12
- id: string;
13
- creatorId?: string;
14
- };
17
+ params: DeleteOperationParams<B>;
15
18
  data: unknown;
16
19
  }) => Promise<void>;
17
20
  onError?(error: Error): Promise<ReturnType<(typeof TypedNextResponse)["json"]> | undefined>;
@@ -22,4 +25,7 @@ export declare const createDeleteOperation: ({ db, getSession, }: {
22
25
  getSession(req: NextRequest): Promise<{
23
26
  userId?: string;
24
27
  } | undefined>;
25
- }) => <TTable extends BaseTable>({ table, description, summary, tags, onSuccess, onError, byCreator, }: DeleteOperationOptions<TTable>) => import("next-rest-framework").RouteOperationDefinition<"DELETE">;
28
+ }) => <TTable extends BaseTable, B extends z.ZodType<unknown, unknown, z.core.$ZodTypeInternals<unknown, unknown>> = z.ZodObject<{
29
+ id: z.ZodString;
30
+ }, z.core.$strip>>({ table, bodySchema, description, summary, tags, onSuccess, onError, byCreator, }: DeleteOperationOptions<TTable, B>) => import("next-rest-framework").RouteOperationDefinition<"DELETE">;
31
+ export {};
@@ -7,11 +7,16 @@ import { z } from "zod";
7
7
  import getTableName from "./get-table-name.js";
8
8
  import { createDeleteAction } from "../actions/index.js";
9
9
  import { HttpError } from "../errors.js";
10
+ var defaultDeleteBodySchema = z.object({
11
+ id: z.string()
12
+ });
10
13
  export var createDeleteOperation = function createDeleteOperation(_ref) {
11
14
  var db = _ref.db,
12
15
  getSession = _ref.getSession;
13
16
  return function (_ref2) {
14
17
  var table = _ref2.table,
18
+ _ref2$bodySchema = _ref2.bodySchema,
19
+ bodySchema = _ref2$bodySchema === void 0 ? defaultDeleteBodySchema : _ref2$bodySchema,
15
20
  description = _ref2.description,
16
21
  summary = _ref2.summary,
17
22
  tags = _ref2.tags,
@@ -27,97 +32,98 @@ export var createDeleteOperation = function createDeleteOperation(_ref) {
27
32
  tags: tags !== null && tags !== void 0 ? tags : table ? [getTableName(table)] : []
28
33
  }
29
34
  }).input({
30
- body: z.object({
31
- id: z.string()
32
- }),
35
+ body: bodySchema,
33
36
  contentType: "application/json"
34
37
  }).handler( /*#__PURE__*/function () {
35
38
  var _ref3 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(req) {
36
- var body, _ref4, userId, data, response;
39
+ var body, _ref4, userId, tableParams, data, response;
37
40
  return _regeneratorRuntime().wrap(function _callee$(_context) {
38
41
  while (1) switch (_context.prev = _context.next) {
39
42
  case 0:
40
43
  _context.prev = 0;
41
- _context.next = 3;
44
+ _context.t0 = bodySchema;
45
+ _context.next = 4;
42
46
  return req.json();
43
- case 3:
44
- body = _context.sent;
47
+ case 4:
48
+ _context.t1 = _context.sent;
49
+ body = _context.t0.parse.call(_context.t0, _context.t1);
45
50
  if (!byCreator) {
46
- _context.next = 13;
51
+ _context.next = 15;
47
52
  break;
48
53
  }
49
- _context.next = 7;
54
+ _context.next = 9;
50
55
  return getSession(req);
51
- case 7:
52
- _context.t0 = _context.sent;
53
- if (_context.t0) {
54
- _context.next = 10;
56
+ case 9:
57
+ _context.t2 = _context.sent;
58
+ if (_context.t2) {
59
+ _context.next = 12;
55
60
  break;
56
61
  }
57
- _context.t0 = {};
58
- case 10:
59
- _ref4 = _context.t0;
62
+ _context.t2 = {};
63
+ case 12:
64
+ _ref4 = _context.t2;
60
65
  userId = _ref4.userId;
61
66
  body.creatorId = userId;
62
- case 13:
67
+ case 15:
68
+ tableParams = body;
63
69
  if (!(table && db)) {
64
- _context.next = 19;
70
+ _context.next = 22;
65
71
  break;
66
72
  }
67
- _context.next = 16;
73
+ _context.next = 19;
68
74
  return createDeleteAction({
69
75
  table: table,
70
76
  db: db
71
- })(body);
72
- case 16:
73
- _context.t1 = _context.sent;
74
- _context.next = 20;
75
- break;
77
+ })(tableParams);
76
78
  case 19:
77
- _context.t1 = body;
78
- case 20:
79
- data = _context.t1;
79
+ _context.t3 = _context.sent;
80
80
  _context.next = 23;
81
+ break;
82
+ case 22:
83
+ _context.t3 = body;
84
+ case 23:
85
+ data = _context.t3;
86
+ _context.next = 26;
81
87
  return onSuccess === null || onSuccess === void 0 ? void 0 : onSuccess({
82
88
  data: data,
83
89
  params: body
84
90
  });
85
- case 23:
91
+ case 26:
86
92
  return _context.abrupt("return", TypedNextResponse.json(data, {
87
93
  status: 200
88
94
  }));
89
- case 26:
90
- _context.prev = 26;
91
- _context.t2 = _context["catch"](0);
92
- if (!(_context.t2 instanceof HttpError)) {
93
- console.error(_context.t2);
95
+ case 29:
96
+ _context.prev = 29;
97
+ _context.t4 = _context["catch"](0);
98
+ if (!(_context.t4 instanceof HttpError)) {
99
+ console.error(_context.t4);
94
100
  }
95
- _context.next = 31;
96
- return onError === null || onError === void 0 ? void 0 : onError(_context.t2);
97
- case 31:
101
+ _context.next = 34;
102
+ return onError === null || onError === void 0 ? void 0 : onError(_context.t4);
103
+ case 34:
98
104
  response = _context.sent;
99
105
  if (!response) {
100
- _context.next = 34;
106
+ _context.next = 37;
101
107
  break;
102
108
  }
103
109
  return _context.abrupt("return", response);
104
- case 34:
105
- if (!(_context.t2 instanceof HttpError)) {
106
- _context.next = 36;
110
+ case 37:
111
+ if (!(_context.t4 instanceof HttpError)) {
112
+ _context.next = 39;
107
113
  break;
108
114
  }
109
115
  return _context.abrupt("return", TypedNextResponse.json({
110
- message: _context.t2.message
116
+ message: _context.t4.message
111
117
  }, {
112
- status: _context.t2.status
118
+ status: _context.t4.status
113
119
  }));
114
- case 36:
115
- throw _context.t2;
116
- case 37:
120
+ case 39:
121
+ throw _context.t4;
122
+ case 40:
117
123
  case "end":
118
124
  return _context.stop();
119
125
  }
120
- }, _callee, null, [[0, 26]]);
126
+ }, _callee, null, [[0, 29]]);
121
127
  }));
122
128
  return function (_x) {
123
129
  return _ref3.apply(this, arguments);
@@ -3,6 +3,9 @@ import { NextRequest } from "next/server";
3
3
  import { TypedNextRequest, TypedNextResponse } from "next-rest-framework";
4
4
  import { NodePgDatabase } from "drizzle-orm/node-postgres";
5
5
  import { BaseTable, GetListRelations } from "../types.js";
6
+ type GetListOperationParams<Q extends ZodSchema> = z.infer<Q> & Record<string, unknown> & {
7
+ creatorId?: string;
8
+ };
6
9
  export interface GetListOperationOptions<T extends ZodSchema, Q extends ZodSchema, TTable extends BaseTable> {
7
10
  bodySchema: T;
8
11
  jsonArrayFields?: string[];
@@ -15,7 +18,7 @@ export interface GetListOperationOptions<T extends ZodSchema, Q extends ZodSchem
15
18
  tags?: string[];
16
19
  table?: TTable;
17
20
  onSuccess?<D extends T>(payload: {
18
- params: Partial<z.infer<T>> & Record<string, unknown>;
21
+ params: GetListOperationParams<Q>;
19
22
  data: {
20
23
  data: z.infer<D>[];
21
24
  total: number;
@@ -32,3 +35,4 @@ export declare const createGetListOperation: ({ db, getSession, }: {
32
35
  userId?: string;
33
36
  } | undefined>;
34
37
  }) => <T extends z.ZodType<unknown, unknown, z.core.$ZodTypeInternals<unknown, unknown>>, Q extends z.ZodType<unknown, unknown, z.core.$ZodTypeInternals<unknown, unknown>>, TTable extends BaseTable>({ querySchema, bodySchema, table, description, summary, tags, relations, jsonArrayFields, setParams, onSuccess, onError, byCreator, }: GetListOperationOptions<T, Q, TTable>) => import("next-rest-framework").RouteOperationDefinition<"GET">;
38
+ export {};
@@ -1,5 +1,10 @@
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";
@@ -38,7 +43,7 @@ export var createGetListOperation = function createGetListOperation(_ref) {
38
43
  status: 200
39
44
  }]).handler( /*#__PURE__*/function () {
40
45
  var _ref3 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(req) {
41
- var params, _ref4, userId, mergedParams, result, response;
46
+ var extraParams, _ref4, userId, queryParams, mergedParams, tableParams, result, response;
42
47
  return _regeneratorRuntime().wrap(function _callee$(_context) {
43
48
  while (1) switch (_context.prev = _context.next) {
44
49
  case 0:
@@ -53,7 +58,7 @@ export var createGetListOperation = function createGetListOperation(_ref) {
53
58
  }
54
59
  _context.t0 = {};
55
60
  case 6:
56
- params = _context.t0;
61
+ extraParams = _context.t0;
57
62
  if (!byCreator) {
58
63
  _context.next = 16;
59
64
  break;
@@ -70,67 +75,69 @@ export var createGetListOperation = function createGetListOperation(_ref) {
70
75
  case 13:
71
76
  _ref4 = _context.t1;
72
77
  userId = _ref4.userId;
73
- params.creatorId = userId;
78
+ extraParams.creatorId = userId;
74
79
  case 16:
75
- mergedParams = Object.assign(Object.fromEntries(new URL(req.url).searchParams), params);
80
+ queryParams = querySchema.parse(Object.fromEntries(new URL(req.url).searchParams));
81
+ mergedParams = _objectSpread(_objectSpread({}, queryParams), extraParams);
82
+ tableParams = mergedParams;
76
83
  if (!(table && db)) {
77
- _context.next = 23;
84
+ _context.next = 25;
78
85
  break;
79
86
  }
80
- _context.next = 20;
87
+ _context.next = 22;
81
88
  return createGetListAction({
82
89
  bodySchema: bodySchema,
83
90
  db: db,
84
91
  jsonArrayFields: jsonArrayFields,
85
92
  relations: relations,
86
93
  table: table
87
- })(mergedParams);
88
- case 20:
94
+ })(tableParams);
95
+ case 22:
89
96
  _context.t2 = _context.sent;
90
- _context.next = 24;
97
+ _context.next = 26;
91
98
  break;
92
- case 23:
99
+ case 25:
93
100
  _context.t2 = {
94
101
  data: [],
95
102
  total: 0
96
103
  };
97
- case 24:
104
+ case 26:
98
105
  result = _context.t2;
99
106
  if (!onSuccess) {
100
- _context.next = 29;
107
+ _context.next = 31;
101
108
  break;
102
109
  }
103
- _context.next = 28;
110
+ _context.next = 30;
104
111
  return onSuccess({
105
112
  data: result,
106
113
  params: mergedParams
107
114
  });
108
- case 28:
115
+ case 30:
109
116
  result = _context.sent;
110
- case 29:
117
+ case 31:
111
118
  return _context.abrupt("return", TypedNextResponse.json(result, {
112
119
  status: 200
113
120
  }));
114
- case 32:
115
- _context.prev = 32;
121
+ case 34:
122
+ _context.prev = 34;
116
123
  _context.t3 = _context["catch"](0);
117
124
  console.error(_context.t3);
118
- _context.next = 37;
125
+ _context.next = 39;
119
126
  return onError === null || onError === void 0 ? void 0 : onError(_context.t3);
120
- case 37:
127
+ case 39:
121
128
  response = _context.sent;
122
129
  if (!response) {
123
- _context.next = 42;
130
+ _context.next = 44;
124
131
  break;
125
132
  }
126
133
  return _context.abrupt("return", response);
127
- case 42:
134
+ case 44:
128
135
  throw _context.t3;
129
- case 43:
136
+ case 45:
130
137
  case "end":
131
138
  return _context.stop();
132
139
  }
133
- }, _callee, null, [[0, 32]]);
140
+ }, _callee, null, [[0, 34]]);
134
141
  }));
135
142
  return function (_x) {
136
143
  return _ref3.apply(this, arguments);
@@ -3,6 +3,9 @@ import { NextRequest } from "next/server";
3
3
  import { TypedNextRequest, TypedNextResponse } from "next-rest-framework";
4
4
  import { NodePgDatabase } from "drizzle-orm/node-postgres";
5
5
  import { BaseTable, GetListRelations } from "../types.js";
6
+ type GetOperationParams<Q extends ZodSchema> = z.infer<Q> & Record<string, unknown> & {
7
+ creatorId?: string;
8
+ };
6
9
  export interface GetOperationOptions<T extends ZodSchema, Q extends ZodSchema, TTable extends BaseTable> {
7
10
  bodySchema: T;
8
11
  jsonArrayFields?: string[];
@@ -10,7 +13,7 @@ export interface GetOperationOptions<T extends ZodSchema, Q extends ZodSchema, T
10
13
  relations?: GetListRelations;
11
14
  setParams?(req: TypedNextRequest<"GET", "application/json", unknown, z.infer<Q>>): Promise<Record<string, unknown>>;
12
15
  onSuccess?(payload: {
13
- params: Partial<z.infer<T>> & Record<string, unknown>;
16
+ params: GetOperationParams<Q>;
14
17
  data: z.infer<T>;
15
18
  }): Promise<z.infer<T>>;
16
19
  onError?(error: Error): Promise<ReturnType<(typeof TypedNextResponse)["json"]> | undefined>;
@@ -26,3 +29,4 @@ export declare const createGetOperation: ({ getSession, db, }: {
26
29
  userId?: string;
27
30
  } | undefined>;
28
31
  }) => <T extends z.ZodType<unknown, unknown, z.core.$ZodTypeInternals<unknown, unknown>>, Q extends z.ZodType<unknown, unknown, z.core.$ZodTypeInternals<unknown, unknown>>, TTable extends BaseTable>({ querySchema, bodySchema, table, description, summary, tags, relations, setParams, jsonArrayFields, onSuccess, onError, byCreator, }: GetOperationOptions<T, Q, TTable>) => import("next-rest-framework").RouteOperationDefinition<"GET">;
32
+ export {};
@@ -1,5 +1,10 @@
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";
@@ -37,7 +42,7 @@ export var createGetOperation = function createGetOperation(_ref) {
37
42
  status: 200
38
43
  }]).handler( /*#__PURE__*/function () {
39
44
  var _ref3 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(req) {
40
- var params, _ref4, userId, mergedParams, rawResult, result, response;
45
+ var extraParams, _ref4, userId, queryParams, mergedParams, tableParams, rawResult, result, response;
41
46
  return _regeneratorRuntime().wrap(function _callee$(_context) {
42
47
  while (1) switch (_context.prev = _context.next) {
43
48
  case 0:
@@ -52,7 +57,7 @@ export var createGetOperation = function createGetOperation(_ref) {
52
57
  }
53
58
  _context.t0 = {};
54
59
  case 6:
55
- params = _context.t0;
60
+ extraParams = _context.t0;
56
61
  if (!byCreator) {
57
62
  _context.next = 16;
58
63
  break;
@@ -69,65 +74,67 @@ export var createGetOperation = function createGetOperation(_ref) {
69
74
  case 13:
70
75
  _ref4 = _context.t1;
71
76
  userId = _ref4.userId;
72
- params.creatorId = userId;
77
+ extraParams.creatorId = userId;
73
78
  case 16:
74
- mergedParams = Object.assign(Object.fromEntries(new URL(req.url).searchParams), params);
79
+ queryParams = querySchema.parse(Object.fromEntries(new URL(req.url).searchParams));
80
+ mergedParams = _objectSpread(_objectSpread({}, queryParams), extraParams);
81
+ tableParams = mergedParams;
75
82
  if (!(table && db)) {
76
- _context.next = 23;
83
+ _context.next = 25;
77
84
  break;
78
85
  }
79
- _context.next = 20;
86
+ _context.next = 22;
80
87
  return createGetAction({
81
88
  bodySchema: bodySchema,
82
89
  db: db,
83
90
  jsonArrayFields: jsonArrayFields,
84
91
  relations: relations,
85
92
  table: table
86
- })(mergedParams);
87
- case 20:
93
+ })(tableParams);
94
+ case 22:
88
95
  _context.t2 = _context.sent;
89
- _context.next = 24;
96
+ _context.next = 26;
90
97
  break;
91
- case 23:
98
+ case 25:
92
99
  _context.t2 = mergedParams;
93
- case 24:
100
+ case 26:
94
101
  rawResult = _context.t2;
95
102
  result = rawResult !== null && rawResult !== void 0 ? rawResult : {};
96
103
  if (!onSuccess) {
97
- _context.next = 30;
104
+ _context.next = 32;
98
105
  break;
99
106
  }
100
- _context.next = 29;
107
+ _context.next = 31;
101
108
  return onSuccess({
102
109
  data: result,
103
110
  params: mergedParams
104
111
  });
105
- case 29:
112
+ case 31:
106
113
  result = _context.sent;
107
- case 30:
114
+ case 32:
108
115
  return _context.abrupt("return", TypedNextResponse.json(result, {
109
116
  status: 200
110
117
  }));
111
- case 33:
112
- _context.prev = 33;
118
+ case 35:
119
+ _context.prev = 35;
113
120
  _context.t3 = _context["catch"](0);
114
121
  console.error(_context.t3);
115
- _context.next = 38;
122
+ _context.next = 40;
116
123
  return onError === null || onError === void 0 ? void 0 : onError(_context.t3);
117
- case 38:
124
+ case 40:
118
125
  response = _context.sent;
119
126
  if (!response) {
120
- _context.next = 43;
127
+ _context.next = 45;
121
128
  break;
122
129
  }
123
130
  return _context.abrupt("return", response);
124
- case 43:
131
+ case 45:
125
132
  throw _context.t3;
126
- case 44:
133
+ case 46:
127
134
  case "end":
128
135
  return _context.stop();
129
136
  }
130
- }, _callee, null, [[0, 33]]);
137
+ }, _callee, null, [[0, 35]]);
131
138
  }));
132
139
  return function (_x) {
133
140
  return _ref3.apply(this, arguments);
@@ -1,16 +1,19 @@
1
1
  import { TypedNextResponse } from "next-rest-framework";
2
+ import { z } from "zod";
2
3
  import { BaseTable } from "../types.js";
3
4
  import { NextRequest } from "next/server";
4
- export interface DeleteOperationOptions<TTable extends BaseTable> {
5
+ type DeleteOperationParams<B extends z.ZodSchema> = z.infer<B> & Record<string, unknown> & {
6
+ creatorId?: string;
7
+ id: string;
8
+ };
9
+ export interface DeleteOperationOptions<TTable extends BaseTable, B extends z.ZodSchema> {
5
10
  table?: TTable;
11
+ bodySchema?: B;
6
12
  description?: string;
7
13
  summary?: string;
8
14
  tags?: string[];
9
15
  onSuccess?: (_payload: {
10
- params: {
11
- id: string;
12
- creatorId?: string;
13
- };
16
+ params: DeleteOperationParams<B>;
14
17
  data: unknown;
15
18
  }) => Promise<void>;
16
19
  onError?: (_error: Error) => Promise<ReturnType<(typeof TypedNextResponse)["json"]> | undefined>;
@@ -21,4 +24,7 @@ export declare const createDeleteOperation: ({ db, getSession, }: {
21
24
  getSession: (_req: NextRequest) => Promise<{
22
25
  userId?: string;
23
26
  } | undefined>;
24
- }) => <TTable extends BaseTable>({ table, description, summary, tags, onSuccess, onError, byCreator, }: DeleteOperationOptions<TTable>) => import("next-rest-framework").RouteOperationDefinition<"DELETE">;
27
+ }) => <TTable extends BaseTable, B extends z.ZodType<unknown, unknown, z.core.$ZodTypeInternals<unknown, unknown>> = z.ZodObject<{
28
+ id: z.ZodString;
29
+ }, z.core.$strip>>({ table, bodySchema, description, summary, tags, onSuccess, onError, byCreator, }: DeleteOperationOptions<TTable, B>) => import("next-rest-framework").RouteOperationDefinition<"DELETE">;
30
+ export {};