@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.
Files changed (86) hide show
  1. package/.agents/skills/ai0x0-utils/backend/SKILL.md +35 -11
  2. package/README.md +3 -3
  3. package/SKILL.md +35 -11
  4. package/es/backend/actions/create-delete-action.d.ts +4 -2
  5. package/es/backend/actions/create-delete-action.js +22 -28
  6. package/es/backend/actions/create-get-action.d.ts +4 -1
  7. package/es/backend/actions/create-get-action.js +10 -10
  8. package/es/backend/actions/create-get-list-action.d.ts +4 -1
  9. package/es/backend/actions/create-get-list-action.js +9 -9
  10. package/es/backend/actions/create-put-action.d.ts +3 -2
  11. package/es/backend/actions/create-put-action.js +23 -38
  12. package/es/backend/actions/get-list-query.d.ts +8 -1
  13. package/es/backend/actions/get-list-query.js +19 -8
  14. package/es/backend/index.d.ts +1 -0
  15. package/es/backend/index.js +2 -1
  16. package/es/backend/route-operation/delete-operation.d.ts +3 -1
  17. package/es/backend/route-operation/get-list-operation.d.ts +3 -1
  18. package/es/backend/route-operation/get-operation.d.ts +3 -1
  19. package/es/backend/route-operation/operation-common.d.ts +80 -8
  20. package/es/backend/route-operation/operation-common.js +216 -67
  21. package/es/backend/route-operation/post-operation.d.ts +3 -1
  22. package/es/backend/route-operation/put-operation.d.ts +3 -1
  23. package/es/backend/route-operation/read-operation-core.d.ts +17 -14
  24. package/es/backend/route-operation/read-operation-core.js +42 -34
  25. package/es/backend/route-operation/write-operation-core.d.ts +25 -22
  26. package/es/backend/route-operation/write-operation-core.js +195 -165
  27. package/es/backend/schemas/index.d.ts +240 -240
  28. package/es/backend/scope.d.ts +27 -0
  29. package/es/backend/scope.js +60 -0
  30. package/es/backend/sqlite/actions/create-delete-action.d.ts +4 -2
  31. package/es/backend/sqlite/actions/create-delete-action.js +21 -28
  32. package/es/backend/sqlite/actions/create-get-action.d.ts +4 -1
  33. package/es/backend/sqlite/actions/create-get-action.js +9 -7
  34. package/es/backend/sqlite/actions/create-get-list-action.d.ts +4 -1
  35. package/es/backend/sqlite/actions/create-get-list-action.js +8 -6
  36. package/es/backend/sqlite/actions/create-put-action.d.ts +3 -2
  37. package/es/backend/sqlite/actions/create-put-action.js +20 -38
  38. package/es/backend/sqlite/actions/get-list-query.d.ts +4 -1
  39. package/es/backend/sqlite/actions/get-list-query.js +8 -3
  40. package/es/backend/sqlite/route-operation/delete-operation.d.ts +3 -1
  41. package/es/backend/sqlite/route-operation/get-list-operation.d.ts +3 -1
  42. package/es/backend/sqlite/route-operation/get-operation.d.ts +3 -1
  43. package/es/backend/sqlite/route-operation/post-operation.d.ts +3 -1
  44. package/es/backend/sqlite/route-operation/put-operation.d.ts +3 -1
  45. package/lib/backend/actions/create-delete-action.d.ts +4 -2
  46. package/lib/backend/actions/create-delete-action.js +10 -16
  47. package/lib/backend/actions/create-get-action.d.ts +4 -1
  48. package/lib/backend/actions/create-get-action.js +2 -1
  49. package/lib/backend/actions/create-get-list-action.d.ts +4 -1
  50. package/lib/backend/actions/create-get-list-action.js +2 -1
  51. package/lib/backend/actions/create-put-action.d.ts +3 -2
  52. package/lib/backend/actions/create-put-action.js +10 -21
  53. package/lib/backend/actions/get-list-query.d.ts +8 -1
  54. package/lib/backend/actions/get-list-query.js +11 -4
  55. package/lib/backend/index.d.ts +1 -0
  56. package/lib/backend/index.js +3 -1
  57. package/lib/backend/route-operation/delete-operation.d.ts +3 -1
  58. package/lib/backend/route-operation/get-list-operation.d.ts +3 -1
  59. package/lib/backend/route-operation/get-operation.d.ts +3 -1
  60. package/lib/backend/route-operation/operation-common.d.ts +80 -8
  61. package/lib/backend/route-operation/operation-common.js +57 -7
  62. package/lib/backend/route-operation/post-operation.d.ts +3 -1
  63. package/lib/backend/route-operation/put-operation.d.ts +3 -1
  64. package/lib/backend/route-operation/read-operation-core.d.ts +17 -14
  65. package/lib/backend/route-operation/read-operation-core.js +12 -6
  66. package/lib/backend/route-operation/write-operation-core.d.ts +25 -22
  67. package/lib/backend/route-operation/write-operation-core.js +52 -16
  68. package/lib/backend/schemas/index.d.ts +240 -240
  69. package/lib/backend/scope.d.ts +27 -0
  70. package/lib/backend/scope.js +57 -0
  71. package/lib/backend/sqlite/actions/create-delete-action.d.ts +4 -2
  72. package/lib/backend/sqlite/actions/create-delete-action.js +10 -16
  73. package/lib/backend/sqlite/actions/create-get-action.d.ts +4 -1
  74. package/lib/backend/sqlite/actions/create-get-action.js +2 -1
  75. package/lib/backend/sqlite/actions/create-get-list-action.d.ts +4 -1
  76. package/lib/backend/sqlite/actions/create-get-list-action.js +2 -1
  77. package/lib/backend/sqlite/actions/create-put-action.d.ts +3 -2
  78. package/lib/backend/sqlite/actions/create-put-action.js +10 -21
  79. package/lib/backend/sqlite/actions/get-list-query.d.ts +4 -1
  80. package/lib/backend/sqlite/actions/get-list-query.js +5 -1
  81. package/lib/backend/sqlite/route-operation/delete-operation.d.ts +3 -1
  82. package/lib/backend/sqlite/route-operation/get-list-operation.d.ts +3 -1
  83. package/lib/backend/sqlite/route-operation/get-operation.d.ts +3 -1
  84. package/lib/backend/sqlite/route-operation/post-operation.d.ts +3 -1
  85. package/lib/backend/sqlite/route-operation/put-operation.d.ts +3 -1
  86. package/package.json +38 -1
@@ -2,15 +2,21 @@ import { NextRequest } from "next/server";
2
2
  import { TypedNextRequest } from "next-rest-framework";
3
3
  import { z, ZodSchema } from "zod";
4
4
  import { type RouteOpenApiOperation } from "./open-api-operation.js";
5
- import { ActionFactory, RouteCatch, SessionGetter } from "./operation-common.js";
5
+ import { AccessOptions, ActionFactory, DefaultSession, RouteCatch, SessionGetter } from "./operation-common.js";
6
+ import { ScopeArg } from "../scope.js";
6
7
  export type PostOperationParams<IB extends ZodSchema> = z.infer<IB> & Record<string, unknown> & {
7
8
  creatorId?: string;
8
9
  };
9
- export interface PostOperationOptions<IB extends ZodSchema, OB extends ZodSchema, TTable> {
10
+ export interface PostOperationOptions<IB extends ZodSchema, OB extends ZodSchema, TTable, TSession = DefaultSession> {
10
11
  schemas: {
11
12
  body: IB;
12
13
  response?: OB;
13
14
  };
15
+ /**
16
+ * 新建时把归属列盖成这次请求的作用域值(默认就是 creatorId = 当前用户)。
17
+ * can 同样生效 —— 只读角色不能新建。
18
+ */
19
+ access?: AccessOptions<TSession>;
14
20
  contentType?: string;
15
21
  openApiOperation?: RouteOpenApiOperation;
16
22
  parseBody?: (_req: NextRequest) => Promise<Record<string, unknown>>;
@@ -26,21 +32,19 @@ export interface PostOperationOptions<IB extends ZodSchema, OB extends ZodSchema
26
32
  export declare function createPostOperationFactory<TTable>({ createAction, getTableName, }: {
27
33
  createAction: ActionFactory<TTable, (_body: Record<string, unknown>) => Promise<unknown[]>>;
28
34
  getTableName(table: TTable): string;
29
- }): ({ db, getSession }: {
35
+ }): <TSession = DefaultSession>({ db, getSession, }: {
30
36
  db?: unknown;
31
- getSession: SessionGetter;
32
- }) => <IB extends z.ZodType<unknown, unknown, z.core.$ZodTypeInternals<unknown, unknown>>, OB extends z.ZodType<unknown, unknown, z.core.$ZodTypeInternals<unknown, unknown>>>({ schemas, contentType, openApiOperation, parseBody, setBody, table, handler, catch: catchHandler, }: PostOperationOptions<IB, OB, TTable>) => import("next-rest-framework").RouteOperationDefinition<"POST">;
37
+ getSession: SessionGetter<TSession>;
38
+ }) => <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">;
33
39
  export type PutOperationParams<IB extends ZodSchema> = z.infer<IB> & Record<string, unknown> & {
34
40
  editorId?: string;
35
41
  };
36
- export interface PutOperationOptions<IB extends ZodSchema, OB extends ZodSchema, TTable> {
42
+ export interface PutOperationOptions<IB extends ZodSchema, OB extends ZodSchema, TTable, TSession = DefaultSession> {
37
43
  schemas: {
38
44
  body: IB;
39
45
  response?: OB;
40
46
  };
41
- access?: {
42
- byCreator?: boolean;
43
- };
47
+ access?: AccessOptions<TSession>;
44
48
  openApiOperation?: RouteOpenApiOperation;
45
49
  table?: TTable;
46
50
  setBody?: (_req: TypedNextRequest<"PUT", "application/json", z.infer<IB>>) => Promise<Partial<z.infer<IB>>>;
@@ -52,26 +56,24 @@ export interface PutOperationOptions<IB extends ZodSchema, OB extends ZodSchema,
52
56
  catch?: RouteCatch;
53
57
  }
54
58
  export declare function createPutOperationFactory<TTable>({ createAction, getTableName, }: {
55
- createAction: ActionFactory<TTable, (_body: Record<string, unknown>, _options?: {
56
- byCreator?: boolean;
59
+ createAction: ActionFactory<TTable, (_body: Record<string, unknown>, _options: {
60
+ scope: ScopeArg;
57
61
  }) => Promise<unknown>>;
58
62
  getTableName(table: TTable): string;
59
- }): ({ db, getSession }: {
63
+ }): <TSession = DefaultSession>({ db, getSession, }: {
60
64
  db?: unknown;
61
- getSession: SessionGetter;
62
- }) => <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>) => import("next-rest-framework").RouteOperationDefinition<"PUT">;
65
+ getSession: SessionGetter<TSession>;
66
+ }) => <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">;
63
67
  export type DeleteOperationParams<B extends ZodSchema> = z.infer<B> & Record<string, unknown> & {
64
68
  creatorId?: string;
65
69
  id: string;
66
70
  };
67
- export interface DeleteOperationOptions<TTable, B extends ZodSchema> {
71
+ export interface DeleteOperationOptions<TTable, B extends ZodSchema, TSession = DefaultSession> {
68
72
  table?: TTable;
69
73
  schemas?: {
70
74
  body?: B;
71
75
  };
72
- access?: {
73
- byCreator?: boolean;
74
- };
76
+ access?: AccessOptions<TSession>;
75
77
  openApiOperation?: RouteOpenApiOperation;
76
78
  handler?: (_payload: {
77
79
  params: DeleteOperationParams<B>;
@@ -83,12 +85,13 @@ export interface DeleteOperationOptions<TTable, B extends ZodSchema> {
83
85
  export declare function createDeleteOperationFactory<TTable>({ createAction, getTableName, }: {
84
86
  createAction: ActionFactory<TTable, (_params: {
85
87
  id: string;
86
- creatorId?: string;
88
+ }, _options: {
89
+ scope: ScopeArg;
87
90
  }) => Promise<unknown>>;
88
91
  getTableName(table: TTable): string;
89
- }): ({ db, getSession }: {
92
+ }): <TSession = DefaultSession>({ db, getSession, }: {
90
93
  db?: unknown;
91
- getSession: SessionGetter;
94
+ getSession: SessionGetter<TSession>;
92
95
  }) => <B extends z.ZodType<unknown, unknown, z.core.$ZodTypeInternals<unknown, unknown>> = z.ZodObject<{
93
96
  id: z.ZodString;
94
- }, z.core.$strip>>({ table, schemas, access, openApiOperation, handler, catch: catchHandler, }: DeleteOperationOptions<TTable, B>) => import("next-rest-framework").RouteOperationDefinition<"DELETE">;
97
+ }, z.core.$strip>>({ table, schemas, access, openApiOperation, handler, catch: catchHandler, }: DeleteOperationOptions<TTable, B, TSession>) => import("next-rest-framework").RouteOperationDefinition<"DELETE">;
@@ -28,12 +28,17 @@ var import_next_rest_framework = require("next-rest-framework");
28
28
  var import_zod = require("zod");
29
29
  var import_open_api_operation = require("./open-api-operation");
30
30
  var import_operation_common = require("./operation-common");
31
+ var import_scope = require("../scope");
31
32
  function createPostOperationFactory({
32
33
  createAction,
33
34
  getTableName
34
35
  }) {
35
- return ({ db, getSession }) => ({
36
+ return ({
37
+ db,
38
+ getSession
39
+ }) => ({
36
40
  schemas,
41
+ access,
37
42
  contentType = "application/json",
38
43
  openApiOperation,
39
44
  parseBody,
@@ -58,10 +63,14 @@ function createPostOperationFactory({
58
63
  }
59
64
  ]).handler(async (req) => {
60
65
  try {
61
- const { userId } = await getSession(req) || {};
66
+ const { scope } = await (0, import_operation_common.resolveAccess)({
67
+ access,
68
+ loadSession: (0, import_operation_common.sessionLoader)(getSession, req),
69
+ method: "POST"
70
+ });
62
71
  const body = await (0, import_operation_common.readPostBody)(req, { contentType, parseBody });
63
72
  const extraBody = await (setBody == null ? void 0 : setBody(req)) || {};
64
- const params = { creatorId: userId, ...body, ...extraBody };
73
+ const params = { ...scopeStamp(scope), ...body, ...extraBody };
65
74
  const raw = table && db ? (await createAction({
66
75
  bodySchema: schemas.body,
67
76
  db,
@@ -82,7 +91,10 @@ function createPutOperationFactory({
82
91
  createAction,
83
92
  getTableName
84
93
  }) {
85
- return ({ db, getSession }) => ({
94
+ return ({
95
+ db,
96
+ getSession
97
+ }) => ({
86
98
  schemas,
87
99
  access,
88
100
  openApiOperation,
@@ -106,18 +118,30 @@ function createPutOperationFactory({
106
118
  status: 200
107
119
  }
108
120
  ]).handler(async (req) => {
121
+ var _a;
109
122
  try {
110
- const { userId } = await getSession(req) || {};
123
+ const loadSession = (0, import_operation_common.sessionLoader)(getSession, req);
124
+ const { scope } = await (0, import_operation_common.resolveAccess)({
125
+ access,
126
+ loadSession,
127
+ method: "PUT"
128
+ });
111
129
  const body = await req.json();
112
130
  const extraBody = await (setBody == null ? void 0 : setBody(
113
131
  req
114
132
  )) || {};
115
- const params = { editorId: userId, ...body, ...extraBody };
133
+ const params = {
134
+ // 「谁改的」与「能不能改」是两件事,所以这里自己取一次 —— 取值器带缓存,
135
+ // 上面 resolveAccess 若已取过就不会再往返一次。
136
+ editorId: (_a = await loadSession()) == null ? void 0 : _a.userId,
137
+ ...body,
138
+ ...extraBody
139
+ };
116
140
  const raw = table && db ? await createAction({
117
141
  bodySchema: schemas.body,
118
142
  db,
119
143
  table
120
- })(params, { byCreator: (access == null ? void 0 : access.byCreator) ?? true }) : params;
144
+ })(params, { scope }) : params;
121
145
  const data = handler ? await handler({
122
146
  data: raw,
123
147
  params,
@@ -136,7 +160,10 @@ function createDeleteOperationFactory({
136
160
  createAction,
137
161
  getTableName
138
162
  }) {
139
- return ({ db, getSession }) => ({
163
+ return ({
164
+ db,
165
+ getSession
166
+ }) => ({
140
167
  table,
141
168
  schemas,
142
169
  access,
@@ -158,14 +185,12 @@ function createDeleteOperationFactory({
158
185
  const body = bodySchema.parse(
159
186
  await req.json()
160
187
  );
161
- if ((access == null ? void 0 : access.byCreator) ?? true) {
162
- const { userId } = await getSession(req) || {};
163
- body.creatorId = userId;
164
- }
165
- const data = table && db ? await createAction({ db, table })({
166
- creatorId: body.creatorId,
167
- id: body.id
168
- }) : body;
188
+ const { scope } = await (0, import_operation_common.resolveAccess)({
189
+ access,
190
+ loadSession: (0, import_operation_common.sessionLoader)(getSession, req),
191
+ method: "DELETE"
192
+ });
193
+ const data = table && db ? await createAction({ db, table })({ id: body.id }, { scope }) : body;
169
194
  await (handler == null ? void 0 : handler({ data, params: body, req }));
170
195
  return import_next_rest_framework.TypedNextResponse.json(data, { status: 200 });
171
196
  } catch (error) {
@@ -173,6 +198,17 @@ function createDeleteOperationFactory({
173
198
  }
174
199
  });
175
200
  }
201
+ function scopeStamp(scope) {
202
+ if (scope === import_scope.NO_SCOPE) {
203
+ return {};
204
+ }
205
+ if (Array.isArray(scope.value)) {
206
+ throw new Error(
207
+ `新建操作的作用域不能是多值:一行只能属于一个 ${scope.column}。多值作用域只用于读。`
208
+ );
209
+ }
210
+ return { [scope.column]: scope.value };
211
+ }
176
212
  // Annotate the CommonJS export names for ESM import in node:
177
213
  0 && (module.exports = {
178
214
  createDeleteOperationFactory,