@ai0x0/utils 0.0.50 → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.agents/skills/ai0x0-utils/backend/SKILL.md +35 -11
- package/README.md +3 -3
- package/SKILL.md +35 -11
- package/es/backend/actions/create-delete-action.d.ts +4 -2
- package/es/backend/actions/create-delete-action.js +22 -28
- package/es/backend/actions/create-get-action.d.ts +4 -1
- package/es/backend/actions/create-get-action.js +10 -10
- package/es/backend/actions/create-get-list-action.d.ts +4 -1
- package/es/backend/actions/create-get-list-action.js +9 -9
- package/es/backend/actions/create-put-action.d.ts +3 -2
- package/es/backend/actions/create-put-action.js +23 -38
- package/es/backend/actions/get-list-query.d.ts +8 -1
- package/es/backend/actions/get-list-query.js +19 -8
- package/es/backend/index.d.ts +1 -0
- package/es/backend/index.js +2 -1
- package/es/backend/route-operation/delete-operation.d.ts +3 -1
- package/es/backend/route-operation/get-list-operation.d.ts +3 -1
- package/es/backend/route-operation/get-operation.d.ts +3 -1
- package/es/backend/route-operation/operation-common.d.ts +80 -8
- package/es/backend/route-operation/operation-common.js +216 -67
- package/es/backend/route-operation/post-operation.d.ts +3 -1
- package/es/backend/route-operation/put-operation.d.ts +3 -1
- package/es/backend/route-operation/read-operation-core.d.ts +17 -14
- package/es/backend/route-operation/read-operation-core.js +42 -34
- package/es/backend/route-operation/write-operation-core.d.ts +25 -22
- package/es/backend/route-operation/write-operation-core.js +195 -165
- package/es/backend/schemas/index.d.ts +240 -240
- package/es/backend/scope.d.ts +27 -0
- package/es/backend/scope.js +60 -0
- package/es/backend/sqlite/actions/create-delete-action.d.ts +4 -2
- package/es/backend/sqlite/actions/create-delete-action.js +21 -28
- package/es/backend/sqlite/actions/create-get-action.d.ts +4 -1
- package/es/backend/sqlite/actions/create-get-action.js +9 -7
- package/es/backend/sqlite/actions/create-get-list-action.d.ts +4 -1
- package/es/backend/sqlite/actions/create-get-list-action.js +8 -6
- package/es/backend/sqlite/actions/create-put-action.d.ts +3 -2
- package/es/backend/sqlite/actions/create-put-action.js +20 -38
- package/es/backend/sqlite/actions/get-list-query.d.ts +4 -1
- package/es/backend/sqlite/actions/get-list-query.js +8 -3
- package/es/backend/sqlite/route-operation/delete-operation.d.ts +3 -1
- package/es/backend/sqlite/route-operation/get-list-operation.d.ts +3 -1
- package/es/backend/sqlite/route-operation/get-operation.d.ts +3 -1
- package/es/backend/sqlite/route-operation/post-operation.d.ts +3 -1
- package/es/backend/sqlite/route-operation/put-operation.d.ts +3 -1
- package/lib/backend/actions/create-delete-action.d.ts +4 -2
- package/lib/backend/actions/create-delete-action.js +10 -16
- package/lib/backend/actions/create-get-action.d.ts +4 -1
- package/lib/backend/actions/create-get-action.js +2 -1
- package/lib/backend/actions/create-get-list-action.d.ts +4 -1
- package/lib/backend/actions/create-get-list-action.js +2 -1
- package/lib/backend/actions/create-put-action.d.ts +3 -2
- package/lib/backend/actions/create-put-action.js +10 -21
- package/lib/backend/actions/get-list-query.d.ts +8 -1
- package/lib/backend/actions/get-list-query.js +11 -4
- package/lib/backend/index.d.ts +1 -0
- package/lib/backend/index.js +3 -1
- package/lib/backend/route-operation/delete-operation.d.ts +3 -1
- package/lib/backend/route-operation/get-list-operation.d.ts +3 -1
- package/lib/backend/route-operation/get-operation.d.ts +3 -1
- package/lib/backend/route-operation/operation-common.d.ts +80 -8
- package/lib/backend/route-operation/operation-common.js +57 -7
- package/lib/backend/route-operation/post-operation.d.ts +3 -1
- package/lib/backend/route-operation/put-operation.d.ts +3 -1
- package/lib/backend/route-operation/read-operation-core.d.ts +17 -14
- package/lib/backend/route-operation/read-operation-core.js +12 -6
- package/lib/backend/route-operation/write-operation-core.d.ts +25 -22
- package/lib/backend/route-operation/write-operation-core.js +52 -16
- package/lib/backend/schemas/index.d.ts +240 -240
- package/lib/backend/scope.d.ts +27 -0
- package/lib/backend/scope.js +57 -0
- package/lib/backend/sqlite/actions/create-delete-action.d.ts +4 -2
- package/lib/backend/sqlite/actions/create-delete-action.js +10 -16
- package/lib/backend/sqlite/actions/create-get-action.d.ts +4 -1
- package/lib/backend/sqlite/actions/create-get-action.js +2 -1
- package/lib/backend/sqlite/actions/create-get-list-action.d.ts +4 -1
- package/lib/backend/sqlite/actions/create-get-list-action.js +2 -1
- package/lib/backend/sqlite/actions/create-put-action.d.ts +3 -2
- package/lib/backend/sqlite/actions/create-put-action.js +10 -21
- package/lib/backend/sqlite/actions/get-list-query.d.ts +4 -1
- package/lib/backend/sqlite/actions/get-list-query.js +5 -1
- package/lib/backend/sqlite/route-operation/delete-operation.d.ts +3 -1
- package/lib/backend/sqlite/route-operation/get-list-operation.d.ts +3 -1
- package/lib/backend/sqlite/route-operation/get-operation.d.ts +3 -1
- package/lib/backend/sqlite/route-operation/post-operation.d.ts +3 -1
- package/lib/backend/sqlite/route-operation/put-operation.d.ts +3 -1
- package/package.json +38 -1
|
@@ -21,12 +21,14 @@ function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e
|
|
|
21
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
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
23
|
import { and, asc, desc, eq, gte, ilike, inArray, lte, or, sql } from "drizzle-orm";
|
|
24
|
+
import { scopeCondition } from "../scope.js";
|
|
24
25
|
export function getListQuery(_ref) {
|
|
25
26
|
var db = _ref.db,
|
|
26
27
|
fields = _ref.fields,
|
|
27
28
|
jsonArrayFields = _ref.jsonArrayFields,
|
|
28
29
|
params = _ref.params,
|
|
29
30
|
relations = _ref.relations,
|
|
31
|
+
scope = _ref.scope,
|
|
30
32
|
table = _ref.table;
|
|
31
33
|
var _params$page = params.page,
|
|
32
34
|
page = _params$page === void 0 ? 1 : _params$page,
|
|
@@ -136,13 +138,17 @@ export function getListQuery(_ref) {
|
|
|
136
138
|
var _Object$entries$_i = _slicedToArray(_Object$entries[_i], 2),
|
|
137
139
|
key = _Object$entries$_i[0],
|
|
138
140
|
value = _Object$entries$_i[1];
|
|
139
|
-
if (!value)
|
|
141
|
+
if (!value) {
|
|
142
|
+
continue;
|
|
143
|
+
}
|
|
140
144
|
|
|
141
145
|
// 处理日期范围字段
|
|
142
146
|
if (key.endsWith("AtFrom") || key.endsWith("AtTo")) {
|
|
143
147
|
var baseFieldName = key.replace(/(AtFrom|AtTo)$/, "");
|
|
144
148
|
var _targetColumn = findTargetColumn("".concat(baseFieldName, "At"), table, relations);
|
|
145
|
-
if (!_targetColumn)
|
|
149
|
+
if (!_targetColumn) {
|
|
150
|
+
continue;
|
|
151
|
+
}
|
|
146
152
|
if (key.endsWith("AtFrom")) {
|
|
147
153
|
conditions.push(gte(_targetColumn, new Date(value)));
|
|
148
154
|
} else if (key.endsWith("AtTo")) {
|
|
@@ -153,7 +159,9 @@ export function getListQuery(_ref) {
|
|
|
153
159
|
|
|
154
160
|
// 处理其他普通过滤条件
|
|
155
161
|
var targetColumn = findTargetColumn(key, table, relations);
|
|
156
|
-
if (!targetColumn)
|
|
162
|
+
if (!targetColumn) {
|
|
163
|
+
continue;
|
|
164
|
+
}
|
|
157
165
|
addCondition(conditions, key, value, targetColumn);
|
|
158
166
|
}
|
|
159
167
|
return conditions;
|
|
@@ -168,19 +176,22 @@ export function getListQuery(_ref) {
|
|
|
168
176
|
// 构建基础查询
|
|
169
177
|
var baseQuery = buildBaseQuery(table, fields, relations);
|
|
170
178
|
|
|
171
|
-
//
|
|
172
|
-
|
|
179
|
+
// 构建条件。**作用域先于筛选加进去**,且它不受 buildConditions 里「空值跳过」那条规矩管:
|
|
180
|
+
// 那条规矩对 `?name=` 这种空参数是对的,对归属则是灾难性的 —— 条件消失即全表可见。
|
|
181
|
+
var scopeSql = scopeCondition(table, scope);
|
|
182
|
+
var conditions = scopeSql ? [scopeSql] : [];
|
|
183
|
+
conditions.push.apply(conditions, _toConsumableArray(buildConditions(filters, table, relations)));
|
|
173
184
|
|
|
174
185
|
// 构建主查询
|
|
175
|
-
var query = baseQuery.where(conditions.length > 0 ? and.apply(void 0,
|
|
176
|
-
|
|
186
|
+
var query = baseQuery.where(conditions.length > 0 ? and.apply(void 0, conditions) : undefined).orderBy(buildOrderBy(table, orderBy, orderDir))
|
|
187
|
+
|
|
177
188
|
// @ts-ignore
|
|
178
189
|
.limit(pageSize).offset((page - 1) * pageSize);
|
|
179
190
|
|
|
180
191
|
// 构建计数查询(优化后)
|
|
181
192
|
var countQuery = db.select({
|
|
182
193
|
count: sql(_templateObject3 || (_templateObject3 = _taggedTemplateLiteral(["COUNT(DISTINCT ", ")"])), table.id)
|
|
183
|
-
}).from(table).where(conditions.length > 0 ? and.apply(void 0,
|
|
194
|
+
}).from(table).where(conditions.length > 0 ? and.apply(void 0, conditions) : undefined);
|
|
184
195
|
|
|
185
196
|
// 添加关联
|
|
186
197
|
if (relations !== null && relations !== void 0 && relations.length) {
|
package/es/backend/index.d.ts
CHANGED
package/es/backend/index.js
CHANGED
|
@@ -11,4 +11,6 @@ export declare const createDeleteOperation: ({ db, getSession, }: {
|
|
|
11
11
|
} | undefined>;
|
|
12
12
|
}) => <B extends ZodSchema<unknown, unknown, import("zod/v4/core").$ZodTypeInternals<unknown, unknown>> = import("zod").ZodObject<{
|
|
13
13
|
id: import("zod").ZodString;
|
|
14
|
-
}, import("zod/v4/core").$strip>>({ table, schemas, access, openApiOperation, handler, catch: catchHandler, }: CoreDeleteOperationOptions<BaseTable, B
|
|
14
|
+
}, import("zod/v4/core").$strip>>({ table, schemas, access, openApiOperation, handler, catch: catchHandler, }: CoreDeleteOperationOptions<BaseTable, B, {
|
|
15
|
+
userId?: string | undefined;
|
|
16
|
+
}>) => import("next-rest-framework").RouteOperationDefinition<"DELETE">;
|
|
@@ -9,4 +9,6 @@ export declare const createGetListOperation: ({ db, getSession, }: {
|
|
|
9
9
|
getSession(req: NextRequest): Promise<{
|
|
10
10
|
userId?: string;
|
|
11
11
|
} | undefined>;
|
|
12
|
-
}) => <T extends ZodSchema<unknown, unknown, import("zod/v4/core").$ZodTypeInternals<unknown, unknown>>, Q extends ZodSchema<unknown, unknown, import("zod/v4/core").$ZodTypeInternals<unknown, unknown>>>({ schemas, access, table, openApiOperation, relations, jsonArrayFields, setParams, handler, catch: catchHandler, }: CoreGetListOperationOptions<T, Q, BaseTable, GetListRelations
|
|
12
|
+
}) => <T extends ZodSchema<unknown, unknown, import("zod/v4/core").$ZodTypeInternals<unknown, unknown>>, Q extends ZodSchema<unknown, unknown, import("zod/v4/core").$ZodTypeInternals<unknown, unknown>>>({ schemas, access, table, openApiOperation, relations, jsonArrayFields, setParams, handler, catch: catchHandler, }: CoreGetListOperationOptions<T, Q, BaseTable, GetListRelations, {
|
|
13
|
+
userId?: string | undefined;
|
|
14
|
+
}>) => import("next-rest-framework").RouteOperationDefinition<"GET">;
|
|
@@ -9,4 +9,6 @@ export declare const createGetOperation: ({ getSession, db, }: {
|
|
|
9
9
|
getSession(req: NextRequest): Promise<{
|
|
10
10
|
userId?: string;
|
|
11
11
|
} | undefined>;
|
|
12
|
-
}) => <T extends ZodSchema<unknown, unknown, import("zod/v4/core").$ZodTypeInternals<unknown, unknown>>, Q extends ZodSchema<unknown, unknown, import("zod/v4/core").$ZodTypeInternals<unknown, unknown>>>({ schemas, access, table, openApiOperation, relations, setParams, jsonArrayFields, handler, catch: catchHandler, }: CoreGetOperationOptions<T, Q, BaseTable, GetListRelations
|
|
12
|
+
}) => <T extends ZodSchema<unknown, unknown, import("zod/v4/core").$ZodTypeInternals<unknown, unknown>>, Q extends ZodSchema<unknown, unknown, import("zod/v4/core").$ZodTypeInternals<unknown, unknown>>>({ schemas, access, table, openApiOperation, relations, setParams, jsonArrayFields, handler, catch: catchHandler, }: CoreGetOperationOptions<T, Q, BaseTable, GetListRelations, {
|
|
13
|
+
userId?: string | undefined;
|
|
14
|
+
}>) => import("next-rest-framework").RouteOperationDefinition<"GET">;
|
|
@@ -1,11 +1,75 @@
|
|
|
1
1
|
import { NextRequest } from "next/server";
|
|
2
2
|
import { TypedNextRequest, TypedNextResponse } from "next-rest-framework";
|
|
3
3
|
import { z, ZodSchema } from "zod";
|
|
4
|
+
import { ScopeArg, ScopeValue } from "../scope.js";
|
|
4
5
|
export type JsonResponse = ReturnType<(typeof TypedNextResponse)["json"]>;
|
|
5
6
|
export type RouteCatch = (error: Error) => Promise<JsonResponse | undefined>;
|
|
6
|
-
|
|
7
|
+
/** 默认的会话形状。够用于「一条记录属于一个人」的场景。 */
|
|
8
|
+
export interface DefaultSession {
|
|
7
9
|
userId?: string;
|
|
8
|
-
}
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* 会话读取器。泛型化是为了让调用方能在这里一次性解析出「你是谁 + 你现在站在哪个空间 +
|
|
13
|
+
* 你在那个空间里是什么角色」,后面 access.scope / access.can 都读同一份,不必各自再查一遍库。
|
|
14
|
+
*/
|
|
15
|
+
export type SessionGetter<TSession = DefaultSession> = (req: NextRequest) => Promise<TSession | undefined>;
|
|
16
|
+
export type HttpMethod = "GET" | "POST" | "PUT" | "DELETE";
|
|
17
|
+
/** can 返回它表示拒绝,可自定状态码与文案;返回 false 等价于 { status: 403 }。 */
|
|
18
|
+
export interface AccessDenial {
|
|
19
|
+
status?: number;
|
|
20
|
+
message?: string;
|
|
21
|
+
}
|
|
22
|
+
export interface AccessOptions<TSession = DefaultSession> {
|
|
23
|
+
/**
|
|
24
|
+
* 老写法,等价于 `scope: { column: "creatorId", value: (s) => s.userId }`。
|
|
25
|
+
* 与 scope 同时给时以 scope 为准。
|
|
26
|
+
*/
|
|
27
|
+
byCreator?: boolean;
|
|
28
|
+
/**
|
|
29
|
+
* 行级作用域:这次请求只能看到 / 只能动 `column` 等于(或属于)`value` 的那些行。
|
|
30
|
+
* column 不填默认 "creatorId"。
|
|
31
|
+
*
|
|
32
|
+
* value 拿不到值(会话没有、或返回空)时请求会被拒绝,**不会**退化成「不筛」——
|
|
33
|
+
* 那正是这套东西存在的意义,见 scope.ts 的说明。
|
|
34
|
+
*/
|
|
35
|
+
scope?: {
|
|
36
|
+
column?: string;
|
|
37
|
+
value: (session: TSession) => ScopeValue | undefined;
|
|
38
|
+
};
|
|
39
|
+
/**
|
|
40
|
+
* 准入判断。作用域管「能看到哪些行」,这里管「能不能做这个动作」——比如只读角色不能写。
|
|
41
|
+
* 收在一处而不是散在各路由里:多一档角色时,散着写必然漏。
|
|
42
|
+
*/
|
|
43
|
+
can?: (_ctx: {
|
|
44
|
+
session: TSession;
|
|
45
|
+
method: HttpMethod;
|
|
46
|
+
}) => boolean | AccessDenial | Promise<boolean | AccessDenial>;
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* 把会话读取包成「本次请求最多取一次」的取值器。
|
|
50
|
+
*
|
|
51
|
+
* 会话在一次请求里可能被好几处要:作用域要它算归属、can 要它判角色、PUT 还要它填 editorId。
|
|
52
|
+
* 各自去调 getSession 就是重复往返;预先无条件取一次,则公开端点(关掉隔离的那些)要白付一次
|
|
53
|
+
* 读取。包一层惰性缓存之后两个问题一起没了 —— 谁要谁调,取不取由用不用决定,不必谁去传一个
|
|
54
|
+
* 「要不要取」的开关。
|
|
55
|
+
*/
|
|
56
|
+
export declare function sessionLoader<TSession>(getSession: SessionGetter<TSession>, req: NextRequest): () => Promise<TSession | undefined>;
|
|
57
|
+
/**
|
|
58
|
+
* 解析这次请求的会话、作用域与准入。
|
|
59
|
+
*
|
|
60
|
+
* **失败一律抛,不降级。** 会话拿不到就是 401、作用域取不到值就是 403 —— 绝不把一个空值
|
|
61
|
+
* 往下传。这条是本次改动的核心:在此之前空值会一路流到查询构造器,被「空值跳过筛选」那条
|
|
62
|
+
* 规则吃掉,结果是列表返回全表、删除按 id 删任意行。
|
|
63
|
+
*/
|
|
64
|
+
export declare function resolveAccess<TSession>({ access, loadSession, method, }: {
|
|
65
|
+
access?: AccessOptions<TSession>;
|
|
66
|
+
/** 见 sessionLoader —— 传取值器而不是 (getSession, req),取不取由这里自己决定。 */
|
|
67
|
+
loadSession: () => Promise<TSession | undefined>;
|
|
68
|
+
method: HttpMethod;
|
|
69
|
+
}): Promise<{
|
|
70
|
+
scope: ScopeArg;
|
|
71
|
+
session: TSession | undefined;
|
|
72
|
+
}>;
|
|
9
73
|
export type ActionFactory<TTable, TAction> = (_options: {
|
|
10
74
|
bodySchema?: ZodSchema;
|
|
11
75
|
db: unknown;
|
|
@@ -25,14 +89,22 @@ export declare function getDefaultTags<TTable>({ getTableName, table, }: {
|
|
|
25
89
|
export type GetOperationParams<Q extends ZodSchema> = z.infer<Q> & Record<string, unknown> & {
|
|
26
90
|
creatorId?: string;
|
|
27
91
|
};
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
92
|
+
/**
|
|
93
|
+
* 读操作的参数与作用域。
|
|
94
|
+
*
|
|
95
|
+
* 注意作用域**不进 params**:params 会被查询构造器当筛选条件处理,而那里的规矩是「空值跳过」。
|
|
96
|
+
* 作用域走单独一条通道交给 action,见 scope.ts 顶部第 1 条。
|
|
97
|
+
*/
|
|
98
|
+
export declare function getReadParams<Q extends ZodSchema, TSession>({ access, getSession, req, schemas, setParams, }: {
|
|
99
|
+
access?: AccessOptions<TSession>;
|
|
100
|
+
getSession: SessionGetter<TSession>;
|
|
33
101
|
req: NextRequest;
|
|
34
102
|
schemas: {
|
|
35
103
|
query: Q;
|
|
36
104
|
};
|
|
37
105
|
setParams?: (_req: TypedNextRequest<"GET", "application/json", unknown, z.infer<Q>>) => Promise<Record<string, unknown>>;
|
|
38
|
-
}): Promise<
|
|
106
|
+
}): Promise<{
|
|
107
|
+
params: GetOperationParams<Q>;
|
|
108
|
+
scope: ScopeArg;
|
|
109
|
+
session: TSession | undefined;
|
|
110
|
+
}>;
|
|
@@ -1,43 +1,186 @@
|
|
|
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
1
|
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; }
|
|
3
2
|
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; }
|
|
4
3
|
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; }
|
|
5
4
|
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : String(i); }
|
|
6
5
|
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); }
|
|
7
6
|
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; }
|
|
7
|
+
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); }
|
|
8
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); } }
|
|
9
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); }); }; }
|
|
10
10
|
import { TypedNextResponse } from "next-rest-framework";
|
|
11
|
+
import { HttpError } from "../errors.js";
|
|
12
|
+
import { isEmptyScopeValue, NO_SCOPE } from "../scope.js";
|
|
13
|
+
|
|
14
|
+
// ==============================================================================
|
|
15
|
+
// 会话与准入的类型
|
|
16
|
+
// ==============================================================================
|
|
17
|
+
|
|
18
|
+
/** 默认的会话形状。够用于「一条记录属于一个人」的场景。 */
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* 会话读取器。泛型化是为了让调用方能在这里一次性解析出「你是谁 + 你现在站在哪个空间 +
|
|
22
|
+
* 你在那个空间里是什么角色」,后面 access.scope / access.can 都读同一份,不必各自再查一遍库。
|
|
23
|
+
*/
|
|
24
|
+
|
|
25
|
+
/** can 返回它表示拒绝,可自定状态码与文案;返回 false 等价于 { status: 403 }。 */
|
|
26
|
+
|
|
27
|
+
var DEFAULT_SCOPE_COLUMN = "creatorId";
|
|
28
|
+
|
|
29
|
+
// ==============================================================================
|
|
30
|
+
// 作用域解析
|
|
31
|
+
// ==============================================================================
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* 把会话读取包成「本次请求最多取一次」的取值器。
|
|
35
|
+
*
|
|
36
|
+
* 会话在一次请求里可能被好几处要:作用域要它算归属、can 要它判角色、PUT 还要它填 editorId。
|
|
37
|
+
* 各自去调 getSession 就是重复往返;预先无条件取一次,则公开端点(关掉隔离的那些)要白付一次
|
|
38
|
+
* 读取。包一层惰性缓存之后两个问题一起没了 —— 谁要谁调,取不取由用不用决定,不必谁去传一个
|
|
39
|
+
* 「要不要取」的开关。
|
|
40
|
+
*/
|
|
41
|
+
export function sessionLoader(getSession, req) {
|
|
42
|
+
var pending;
|
|
43
|
+
return function () {
|
|
44
|
+
var _pending;
|
|
45
|
+
return (_pending = pending) !== null && _pending !== void 0 ? _pending : pending = getSession(req);
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* 解析这次请求的会话、作用域与准入。
|
|
51
|
+
*
|
|
52
|
+
* **失败一律抛,不降级。** 会话拿不到就是 401、作用域取不到值就是 403 —— 绝不把一个空值
|
|
53
|
+
* 往下传。这条是本次改动的核心:在此之前空值会一路流到查询构造器,被「空值跳过筛选」那条
|
|
54
|
+
* 规则吃掉,结果是列表返回全表、删除按 id 删任意行。
|
|
55
|
+
*/
|
|
56
|
+
export function resolveAccess(_x) {
|
|
57
|
+
return _resolveAccess.apply(this, arguments);
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
// ==============================================================================
|
|
61
|
+
// 动作工厂
|
|
62
|
+
// ==============================================================================
|
|
63
|
+
function _resolveAccess() {
|
|
64
|
+
_resolveAccess = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(_ref) {
|
|
65
|
+
var _access$byCreator, _access$scope$column, _access$scope;
|
|
66
|
+
var access, loadSession, method, wantsScope, session, verdict, _denial$status, _denial$message, denial, column, value;
|
|
67
|
+
return _regeneratorRuntime().wrap(function _callee$(_context) {
|
|
68
|
+
while (1) switch (_context.prev = _context.next) {
|
|
69
|
+
case 0:
|
|
70
|
+
access = _ref.access, loadSession = _ref.loadSession, method = _ref.method;
|
|
71
|
+
wantsScope = Boolean(access === null || access === void 0 ? void 0 : access.scope) || ((_access$byCreator = access === null || access === void 0 ? void 0 : access.byCreator) !== null && _access$byCreator !== void 0 ? _access$byCreator : true); // 不需要就不取:关掉隔离的路由(多半是公开端点)不该为此白付一次会话读取。
|
|
72
|
+
if (!(wantsScope || access !== null && access !== void 0 && access.can)) {
|
|
73
|
+
_context.next = 8;
|
|
74
|
+
break;
|
|
75
|
+
}
|
|
76
|
+
_context.next = 5;
|
|
77
|
+
return loadSession();
|
|
78
|
+
case 5:
|
|
79
|
+
_context.t0 = _context.sent;
|
|
80
|
+
_context.next = 9;
|
|
81
|
+
break;
|
|
82
|
+
case 8:
|
|
83
|
+
_context.t0 = undefined;
|
|
84
|
+
case 9:
|
|
85
|
+
session = _context.t0;
|
|
86
|
+
if (!(access !== null && access !== void 0 && access.can)) {
|
|
87
|
+
_context.next = 19;
|
|
88
|
+
break;
|
|
89
|
+
}
|
|
90
|
+
if (session) {
|
|
91
|
+
_context.next = 13;
|
|
92
|
+
break;
|
|
93
|
+
}
|
|
94
|
+
throw new HttpError(401, "未登录");
|
|
95
|
+
case 13:
|
|
96
|
+
_context.next = 15;
|
|
97
|
+
return access.can({
|
|
98
|
+
method: method,
|
|
99
|
+
session: session
|
|
100
|
+
});
|
|
101
|
+
case 15:
|
|
102
|
+
verdict = _context.sent;
|
|
103
|
+
if (!(verdict !== true)) {
|
|
104
|
+
_context.next = 19;
|
|
105
|
+
break;
|
|
106
|
+
}
|
|
107
|
+
denial = _typeof(verdict) === "object" ? verdict : {};
|
|
108
|
+
throw new HttpError((_denial$status = denial.status) !== null && _denial$status !== void 0 ? _denial$status : 403, (_denial$message = denial.message) !== null && _denial$message !== void 0 ? _denial$message : "没有权限");
|
|
109
|
+
case 19:
|
|
110
|
+
if (wantsScope) {
|
|
111
|
+
_context.next = 21;
|
|
112
|
+
break;
|
|
113
|
+
}
|
|
114
|
+
return _context.abrupt("return", {
|
|
115
|
+
scope: NO_SCOPE,
|
|
116
|
+
session: session
|
|
117
|
+
});
|
|
118
|
+
case 21:
|
|
119
|
+
if (session) {
|
|
120
|
+
_context.next = 23;
|
|
121
|
+
break;
|
|
122
|
+
}
|
|
123
|
+
throw new HttpError(401, "未登录");
|
|
124
|
+
case 23:
|
|
125
|
+
column = (_access$scope$column = access === null || access === void 0 || (_access$scope = access.scope) === null || _access$scope === void 0 ? void 0 : _access$scope.column) !== null && _access$scope$column !== void 0 ? _access$scope$column : DEFAULT_SCOPE_COLUMN;
|
|
126
|
+
value = access !== null && access !== void 0 && access.scope ? access.scope.value(session) : session.userId;
|
|
127
|
+
if (!isEmptyScopeValue(value)) {
|
|
128
|
+
_context.next = 27;
|
|
129
|
+
break;
|
|
130
|
+
}
|
|
131
|
+
throw new HttpError(403, "\u65E0\u6CD5\u786E\u5B9A\u8FD9\u6B21\u8BF7\u6C42\u7684\u5F52\u5C5E\uFF08".concat(column, "\uFF09\u3002\u8FD9\u4E0D\u662F\u300C\u4E0D\u505A\u9694\u79BB\u300D\u2014\u2014\u8BF7\u6C42\u88AB\u62D2\u7EDD\u3002"));
|
|
132
|
+
case 27:
|
|
133
|
+
return _context.abrupt("return", {
|
|
134
|
+
scope: {
|
|
135
|
+
column: column,
|
|
136
|
+
value: value
|
|
137
|
+
},
|
|
138
|
+
session: session
|
|
139
|
+
});
|
|
140
|
+
case 28:
|
|
141
|
+
case "end":
|
|
142
|
+
return _context.stop();
|
|
143
|
+
}
|
|
144
|
+
}, _callee);
|
|
145
|
+
}));
|
|
146
|
+
return _resolveAccess.apply(this, arguments);
|
|
147
|
+
} // ==============================================================================
|
|
148
|
+
// 错误处理
|
|
149
|
+
// ==============================================================================
|
|
11
150
|
function isHttpLikeError(error) {
|
|
12
151
|
return error instanceof Error && error.name === "HttpError" && typeof error.status === "number";
|
|
13
152
|
}
|
|
14
|
-
export function handleOperationError(
|
|
153
|
+
export function handleOperationError(_x2, _x3) {
|
|
15
154
|
return _handleOperationError.apply(this, arguments);
|
|
16
155
|
}
|
|
156
|
+
|
|
157
|
+
// ==============================================================================
|
|
158
|
+
// 请求体与标签
|
|
159
|
+
// ==============================================================================
|
|
17
160
|
function _handleOperationError() {
|
|
18
|
-
_handleOperationError = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function
|
|
161
|
+
_handleOperationError = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee2(error, catchHandler) {
|
|
19
162
|
var response;
|
|
20
|
-
return _regeneratorRuntime().wrap(function
|
|
21
|
-
while (1) switch (
|
|
163
|
+
return _regeneratorRuntime().wrap(function _callee2$(_context2) {
|
|
164
|
+
while (1) switch (_context2.prev = _context2.next) {
|
|
22
165
|
case 0:
|
|
23
166
|
if (!isHttpLikeError(error)) {
|
|
24
167
|
console.error(error);
|
|
25
168
|
}
|
|
26
|
-
|
|
169
|
+
_context2.next = 3;
|
|
27
170
|
return catchHandler === null || catchHandler === void 0 ? void 0 : catchHandler(error);
|
|
28
171
|
case 3:
|
|
29
|
-
response =
|
|
172
|
+
response = _context2.sent;
|
|
30
173
|
if (!response) {
|
|
31
|
-
|
|
174
|
+
_context2.next = 6;
|
|
32
175
|
break;
|
|
33
176
|
}
|
|
34
|
-
return
|
|
177
|
+
return _context2.abrupt("return", response);
|
|
35
178
|
case 6:
|
|
36
179
|
if (!isHttpLikeError(error)) {
|
|
37
|
-
|
|
180
|
+
_context2.next = 8;
|
|
38
181
|
break;
|
|
39
182
|
}
|
|
40
|
-
return
|
|
183
|
+
return _context2.abrupt("return", TypedNextResponse.json({
|
|
41
184
|
message: error.message
|
|
42
185
|
}, {
|
|
43
186
|
status: error.status
|
|
@@ -46,103 +189,109 @@ function _handleOperationError() {
|
|
|
46
189
|
throw error;
|
|
47
190
|
case 9:
|
|
48
191
|
case "end":
|
|
49
|
-
return
|
|
192
|
+
return _context2.stop();
|
|
50
193
|
}
|
|
51
|
-
},
|
|
194
|
+
}, _callee2);
|
|
52
195
|
}));
|
|
53
196
|
return _handleOperationError.apply(this, arguments);
|
|
54
197
|
}
|
|
55
|
-
export function readPostBody(
|
|
198
|
+
export function readPostBody(_x4, _x5) {
|
|
56
199
|
return _readPostBody.apply(this, arguments);
|
|
57
200
|
}
|
|
58
201
|
function _readPostBody() {
|
|
59
|
-
_readPostBody = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function
|
|
202
|
+
_readPostBody = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee3(req, _ref2) {
|
|
60
203
|
var contentType, parseBody;
|
|
61
|
-
return _regeneratorRuntime().wrap(function
|
|
62
|
-
while (1) switch (
|
|
204
|
+
return _regeneratorRuntime().wrap(function _callee3$(_context3) {
|
|
205
|
+
while (1) switch (_context3.prev = _context3.next) {
|
|
63
206
|
case 0:
|
|
64
|
-
contentType =
|
|
207
|
+
contentType = _ref2.contentType, parseBody = _ref2.parseBody;
|
|
65
208
|
if (!parseBody) {
|
|
66
|
-
|
|
209
|
+
_context3.next = 3;
|
|
67
210
|
break;
|
|
68
211
|
}
|
|
69
|
-
return
|
|
212
|
+
return _context3.abrupt("return", parseBody(req));
|
|
70
213
|
case 3:
|
|
71
214
|
if (!(contentType === "multipart/form-data")) {
|
|
72
|
-
|
|
215
|
+
_context3.next = 9;
|
|
73
216
|
break;
|
|
74
217
|
}
|
|
75
|
-
|
|
76
|
-
|
|
218
|
+
_context3.t0 = Object;
|
|
219
|
+
_context3.next = 7;
|
|
77
220
|
return req.formData();
|
|
78
221
|
case 7:
|
|
79
|
-
|
|
80
|
-
return
|
|
222
|
+
_context3.t1 = _context3.sent;
|
|
223
|
+
return _context3.abrupt("return", _context3.t0.fromEntries.call(_context3.t0, _context3.t1));
|
|
81
224
|
case 9:
|
|
82
|
-
|
|
225
|
+
_context3.next = 11;
|
|
83
226
|
return req.json();
|
|
84
227
|
case 11:
|
|
85
|
-
return
|
|
228
|
+
return _context3.abrupt("return", _context3.sent);
|
|
86
229
|
case 12:
|
|
87
230
|
case "end":
|
|
88
|
-
return
|
|
231
|
+
return _context3.stop();
|
|
89
232
|
}
|
|
90
|
-
},
|
|
233
|
+
}, _callee3);
|
|
91
234
|
}));
|
|
92
235
|
return _readPostBody.apply(this, arguments);
|
|
93
236
|
}
|
|
94
|
-
export function getDefaultTags(
|
|
95
|
-
var getTableName =
|
|
96
|
-
table =
|
|
237
|
+
export function getDefaultTags(_ref3) {
|
|
238
|
+
var getTableName = _ref3.getTableName,
|
|
239
|
+
table = _ref3.table;
|
|
97
240
|
return table ? [getTableName(table)] : [];
|
|
98
241
|
}
|
|
99
|
-
|
|
242
|
+
|
|
243
|
+
// ==============================================================================
|
|
244
|
+
// 读操作的参数
|
|
245
|
+
// ==============================================================================
|
|
246
|
+
|
|
247
|
+
/**
|
|
248
|
+
* 读操作的参数与作用域。
|
|
249
|
+
*
|
|
250
|
+
* 注意作用域**不进 params**:params 会被查询构造器当筛选条件处理,而那里的规矩是「空值跳过」。
|
|
251
|
+
* 作用域走单独一条通道交给 action,见 scope.ts 顶部第 1 条。
|
|
252
|
+
*/
|
|
253
|
+
export function getReadParams(_x6) {
|
|
100
254
|
return _getReadParams.apply(this, arguments);
|
|
101
255
|
}
|
|
102
256
|
function _getReadParams() {
|
|
103
|
-
_getReadParams = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function
|
|
104
|
-
var
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
while (1) switch (_context3.prev = _context3.next) {
|
|
257
|
+
_getReadParams = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee4(_ref4) {
|
|
258
|
+
var access, getSession, req, schemas, setParams, extraParams, _yield$resolveAccess, scope, session, queryParams;
|
|
259
|
+
return _regeneratorRuntime().wrap(function _callee4$(_context4) {
|
|
260
|
+
while (1) switch (_context4.prev = _context4.next) {
|
|
108
261
|
case 0:
|
|
109
|
-
access =
|
|
110
|
-
|
|
262
|
+
access = _ref4.access, getSession = _ref4.getSession, req = _ref4.req, schemas = _ref4.schemas, setParams = _ref4.setParams;
|
|
263
|
+
_context4.next = 3;
|
|
111
264
|
return setParams === null || setParams === void 0 ? void 0 : setParams(req);
|
|
112
265
|
case 3:
|
|
113
|
-
|
|
114
|
-
if (
|
|
115
|
-
|
|
266
|
+
_context4.t0 = _context4.sent;
|
|
267
|
+
if (_context4.t0) {
|
|
268
|
+
_context4.next = 6;
|
|
116
269
|
break;
|
|
117
270
|
}
|
|
118
|
-
|
|
271
|
+
_context4.t0 = {};
|
|
119
272
|
case 6:
|
|
120
|
-
extraParams =
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
case
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
break;
|
|
132
|
-
}
|
|
133
|
-
_context3.t1 = {};
|
|
134
|
-
case 13:
|
|
135
|
-
_ref4 = _context3.t1;
|
|
136
|
-
userId = _ref4.userId;
|
|
137
|
-
extraParams.creatorId = userId;
|
|
138
|
-
case 16:
|
|
273
|
+
extraParams = _context4.t0;
|
|
274
|
+
_context4.next = 9;
|
|
275
|
+
return resolveAccess({
|
|
276
|
+
access: access,
|
|
277
|
+
loadSession: sessionLoader(getSession, req),
|
|
278
|
+
method: "GET"
|
|
279
|
+
});
|
|
280
|
+
case 9:
|
|
281
|
+
_yield$resolveAccess = _context4.sent;
|
|
282
|
+
scope = _yield$resolveAccess.scope;
|
|
283
|
+
session = _yield$resolveAccess.session;
|
|
139
284
|
queryParams = schemas.query.parse(Object.fromEntries(new URL(req.url).searchParams));
|
|
140
|
-
return
|
|
141
|
-
|
|
285
|
+
return _context4.abrupt("return", {
|
|
286
|
+
params: _objectSpread(_objectSpread({}, queryParams), extraParams),
|
|
287
|
+
scope: scope,
|
|
288
|
+
session: session
|
|
289
|
+
});
|
|
290
|
+
case 14:
|
|
142
291
|
case "end":
|
|
143
|
-
return
|
|
292
|
+
return _context4.stop();
|
|
144
293
|
}
|
|
145
|
-
},
|
|
294
|
+
}, _callee4);
|
|
146
295
|
}));
|
|
147
296
|
return _getReadParams.apply(this, arguments);
|
|
148
297
|
}
|
|
@@ -9,4 +9,6 @@ export declare const createPostOperation: ({ getSession, db, }: {
|
|
|
9
9
|
getSession(req: NextRequest): Promise<{
|
|
10
10
|
userId?: string;
|
|
11
11
|
} | undefined>;
|
|
12
|
-
}) => <IB extends ZodSchema<unknown, unknown, import("zod/v4/core").$ZodTypeInternals<unknown, unknown>>, OB extends ZodSchema<unknown, unknown, import("zod/v4/core").$ZodTypeInternals<unknown, unknown>>>({ schemas, contentType, openApiOperation, parseBody, setBody, table, handler, catch: catchHandler, }: CorePostOperationOptions<IB, OB, PgTable<import("drizzle-orm/pg-core").TableConfig
|
|
12
|
+
}) => <IB extends ZodSchema<unknown, unknown, import("zod/v4/core").$ZodTypeInternals<unknown, unknown>>, OB extends ZodSchema<unknown, unknown, import("zod/v4/core").$ZodTypeInternals<unknown, unknown>>>({ schemas, access, contentType, openApiOperation, parseBody, setBody, table, handler, catch: catchHandler, }: CorePostOperationOptions<IB, OB, PgTable<import("drizzle-orm/pg-core").TableConfig>, {
|
|
13
|
+
userId?: string | undefined;
|
|
14
|
+
}>) => import("next-rest-framework").RouteOperationDefinition<"POST">;
|
|
@@ -9,4 +9,6 @@ export declare const createPutOperation: ({ getSession, db, }: {
|
|
|
9
9
|
userId?: string;
|
|
10
10
|
} | undefined>;
|
|
11
11
|
db?: NodePgDatabase<Record<string, unknown>> | undefined;
|
|
12
|
-
}) => <IB extends ZodSchema<unknown, unknown, import("zod/v4/core").$ZodTypeInternals<unknown, unknown>>, OB extends ZodSchema<unknown, unknown, import("zod/v4/core").$ZodTypeInternals<unknown, unknown>>>({ schemas, access, openApiOperation, table, setBody, handler, catch: catchHandler, }: CorePutOperationOptions<IB, OB, PgTable<import("drizzle-orm/pg-core").TableConfig
|
|
12
|
+
}) => <IB extends ZodSchema<unknown, unknown, import("zod/v4/core").$ZodTypeInternals<unknown, unknown>>, OB extends ZodSchema<unknown, unknown, import("zod/v4/core").$ZodTypeInternals<unknown, unknown>>>({ schemas, access, openApiOperation, table, setBody, handler, catch: catchHandler, }: CorePutOperationOptions<IB, OB, PgTable<import("drizzle-orm/pg-core").TableConfig>, {
|
|
13
|
+
userId?: string | undefined;
|
|
14
|
+
}>) => import("next-rest-framework").RouteOperationDefinition<"PUT">;
|