diff --git a/jest.config.js b/jest.config.js index 607114cc6..81ee95b66 100644 --- a/jest.config.js +++ b/jest.config.js @@ -1,4 +1,6 @@ const path = require('path'); +const { pathsToModuleNameMapper } = require('ts-jest/utils'); +const { compilerOptions } = require('./tsconfig.jest.json'); module.exports = { preset: 'ts-jest', @@ -10,6 +12,9 @@ module.exports = { // '**/__tests__/**/*.[jt]s?(x)', '**/?(*.)+(spec|test).[jt]s?(x)' ], + moduleNameMapper: pathsToModuleNameMapper(compilerOptions.paths, { + prefix: '/', + }), modulePathIgnorePatterns: [ "/.dumi/", "/packages/father-build/", diff --git a/packages/actions/src/__tests__/create.test.ts b/packages/actions/src/__tests__/create.test.ts index edf982eea..9f82cd5bb 100644 --- a/packages/actions/src/__tests__/create.test.ts +++ b/packages/actions/src/__tests__/create.test.ts @@ -23,7 +23,7 @@ describe('create', () => { ], }); await api.db.sync(); - const response = await api.resource('tests').create({ + const response = await api.agent().resource('tests').create({ values: { name: 'n1' }, }); expect(response.body.name).toBe('n1'); @@ -45,7 +45,7 @@ describe('create', () => { }); await api.db.sync(); const [Post, Comment] = api.db.getModels(['posts', 'comments']); - const response = await api.resource('posts').create({ + const response = await api.agent().resource('posts').create({ values: { title: 't1', comments: [ @@ -56,8 +56,8 @@ describe('create', () => { }); expect(await Post.count()).toBe(1); expect(await Comment.count()).toBe(2); - await api.resource('posts.comments').create({ - associatedKey: response.body.id, + await api.agent().resource('posts.comments').create({ + associatedIndex: response.body.id, values: { content: 'c1' }, }); expect(await Comment.count()).toBe(3); diff --git a/packages/actions/src/__tests__/destroy.test.ts b/packages/actions/src/__tests__/destroy.test.ts index bad44aa08..091f3b183 100644 --- a/packages/actions/src/__tests__/destroy.test.ts +++ b/packages/actions/src/__tests__/destroy.test.ts @@ -9,14 +9,14 @@ describe('destroy', () => { dataWrapping: false, }); registerActions(api); - api.db.table({ + api.collection({ name: 'posts', fields: [ { type: 'string', name: 'title' }, { type: 'hasMany', name: 'comments' }, ], }); - api.db.table({ + api.collection({ name: 'comments', fields: [{ type: 'string', name: 'content' }], }); @@ -35,8 +35,8 @@ describe('destroy', () => { where: { id: post.id }, }), ).toBe(1); - await api.resource('posts').destroy({ - resourceKey: post.id, + await api.agent().resource('posts').destroy({ + resourceIndex: post.id, }); expect( await Post.count({ @@ -52,9 +52,9 @@ describe('destroy', () => { await post.updateAssociations({ comments: [comment], }); - await api.resource('posts.comments').destroy({ - resourceKey: comment.id, - associatedKey: post.id, + await api.agent().resource('posts.comments').destroy({ + resourceIndex: comment.id, + associatedIndex: post.id, }); const comment2 = await Comment.findByPk(comment.id); expect(comment2).toBeNull(); diff --git a/packages/actions/src/__tests__/get.test.ts b/packages/actions/src/__tests__/get.test.ts index e8efffe31..8784f60d6 100644 --- a/packages/actions/src/__tests__/get.test.ts +++ b/packages/actions/src/__tests__/get.test.ts @@ -32,8 +32,8 @@ describe('get', () => { it('get', async () => { const Post = api.db.getModel('posts'); const post = await Post.create({ title: 't1' }); - const response = await api.resource('posts').get({ - resourceKey: post.id, + const response = await api.agent().resource('posts').get({ + resourceIndex: post.id, fields: ['id', 'title'] }); expect(post.toJSON()).toMatchObject(response.body); @@ -46,9 +46,9 @@ describe('get', () => { await post.updateAssociations({ comments: [comment] }); - const response = await api.resource('posts.comments').get({ - resourceKey: comment.id, - associatedKey: post.id, + const response = await api.agent().resource('posts.comments').get({ + resourceIndex: comment.id, + associatedIndex: post.id, fields: ['id', 'post_id', 'content'] }); const comment2 = await Comment.findByPk(comment.id); diff --git a/packages/actions/src/__tests__/list.test.ts b/packages/actions/src/__tests__/list.test.ts index 85b2aee00..4c7917ac1 100644 --- a/packages/actions/src/__tests__/list.test.ts +++ b/packages/actions/src/__tests__/list.test.ts @@ -40,7 +40,7 @@ describe('list', () => { describe('fields', () => { it('fields', async () => { - const response = await api.resource('posts').list({ + const response = await api.agent().resource('posts').list({ fields: ['title'], filter: { title: 't1', @@ -55,7 +55,7 @@ describe('list', () => { }); it('fields#appends', async () => { - const response = await api.resource('posts').list({ + const response = await api.agent().resource('posts').list({ fields: { appends: ['comments'], }, @@ -89,7 +89,7 @@ describe('list', () => { describe('filter', () => { it('and', async () => { - const response = await api.resource('posts').list({ + const response = await api.agent().resource('posts').list({ filter: { and: [ { title: 't1' }, @@ -109,7 +109,7 @@ describe('list', () => { }); }); it('or', async () => { - const response = await api.resource('posts').list({ + const response = await api.agent().resource('posts').list({ filter: { or: [ { title: 't1' }, diff --git a/packages/actions/src/__tests__/sort.test.ts b/packages/actions/src/__tests__/sort.test.ts index 0180e77be..87e6bb8e5 100644 --- a/packages/actions/src/__tests__/sort.test.ts +++ b/packages/actions/src/__tests__/sort.test.ts @@ -31,11 +31,11 @@ describe('sort', () => { }); it('targetId', async () => { - await api.resource('tests').sort({ + await api.agent().resource('tests').sort({ sourceId: 1, targetId: 3, }); - const response = await api.resource('tests').list({ + const response = await api.agent().resource('tests').list({ sort: ['sort'], }); expect(response.body).toMatchObject({ @@ -61,11 +61,11 @@ describe('sort', () => { }); it('targetId', async () => { - await api.resource('tests').sort({ + await api.agent().resource('tests').sort({ sourceId: 3, targetId: 1, }); - const response = await api.resource('tests').list({ + const response = await api.agent().resource('tests').list({ sort: ['sort'], }); expect(response.body).toMatchObject({ @@ -91,12 +91,12 @@ describe('sort', () => { }); it('sortField', async () => { - await api.resource('tests').sort({ + await api.agent().resource('tests').sort({ sortField: 'sort2', sourceId: 1, targetId: 3, }); - const response = await api.resource('tests').list({ + const response = await api.agent().resource('tests').list({ sort: ['sort2'], }); expect(response.body).toMatchObject({ @@ -122,11 +122,11 @@ describe('sort', () => { }); it('sticky', async () => { - await api.resource('tests').sort({ + await api.agent().resource('tests').sort({ sourceId: 3, sticky: true, }); - const response = await api.resource('tests').list({ + const response = await api.agent().resource('tests').list({ sort: ['sort'], }); expect(response.body).toMatchObject({ @@ -183,11 +183,11 @@ describe('sort', () => { }); it('targetId/1->6', async () => { - await api.resource('tests').sort({ + await api.agent().resource('tests').sort({ sourceId: 1, targetId: 6, }); - let response = await api.resource('tests').list({ + let response = await api.agent().resource('tests').list({ sort: ['sort'], filter: { state: 1 }, }); @@ -207,7 +207,7 @@ describe('sort', () => { }, ], }); - response = await api.resource('tests').list({ + response = await api.agent().resource('tests').list({ sort: ['sort'], filter: { state: 2 }, }); @@ -238,12 +238,12 @@ describe('sort', () => { }); it('targetId/1->6 - method=insertAfter', async () => { - await api.resource('tests').sort({ + await api.agent().resource('tests').sort({ sourceId: 1, targetId: 6, method: 'insertAfter', }); - let response = await api.resource('tests').list({ + let response = await api.agent().resource('tests').list({ sort: ['sort'], filter: { state: 1 }, }); @@ -263,7 +263,7 @@ describe('sort', () => { }, ], }); - response = await api.resource('tests').list({ + response = await api.agent().resource('tests').list({ sort: ['sort'], filter: { state: 2 }, }); @@ -294,11 +294,11 @@ describe('sort', () => { }); it('targetId/6->2', async () => { - await api.resource('tests').sort({ + await api.agent().resource('tests').sort({ sourceId: 6, targetId: 2, }); - let response = await api.resource('tests').list({ + let response = await api.agent().resource('tests').list({ sort: ['sort'], filter: { state: 1 }, }); @@ -326,7 +326,7 @@ describe('sort', () => { }, ], }); - response = await api.resource('tests').list({ + response = await api.agent().resource('tests').list({ sort: ['sort'], filter: { state: 2 }, }); @@ -349,12 +349,12 @@ describe('sort', () => { }); it('targetId/6->2 - method=insertAfter', async () => { - await api.resource('tests').sort({ + await api.agent().resource('tests').sort({ sourceId: 6, targetId: 2, method: 'insertAfter', }); - let response = await api.resource('tests').list({ + let response = await api.agent().resource('tests').list({ sort: ['sort'], filter: { state: 1 }, }); @@ -382,7 +382,7 @@ describe('sort', () => { }, ], }); - response = await api.resource('tests').list({ + response = await api.agent().resource('tests').list({ sort: ['sort'], filter: { state: 2 }, }); @@ -405,13 +405,13 @@ describe('sort', () => { }); it('targetScope', async () => { - await api.resource('tests').sort({ + await api.agent().resource('tests').sort({ sourceId: 1, targetScope: { state: 2, }, }); - let response = await api.resource('tests').list({ + let response = await api.agent().resource('tests').list({ sort: ['sort'], filter: { state: 1 }, }); @@ -431,7 +431,7 @@ describe('sort', () => { }, ], }); - response = await api.resource('tests').list({ + response = await api.agent().resource('tests').list({ sort: ['sort'], filter: { state: 2 }, }); @@ -462,14 +462,14 @@ describe('sort', () => { }); it('targetScope - method=prepend', async () => { - await api.resource('tests').sort({ + await api.agent().resource('tests').sort({ sourceId: 1, targetScope: { state: 2, }, method: 'prepend', }); - let response = await api.resource('tests').list({ + let response = await api.agent().resource('tests').list({ sort: ['sort'], filter: { state: 1 }, }); @@ -486,7 +486,7 @@ describe('sort', () => { }, ], }); - response = await api.resource('tests').list({ + response = await api.agent().resource('tests').list({ sort: ['sort'], filter: { state: 2 }, }); diff --git a/packages/actions/src/__tests__/update.test.ts b/packages/actions/src/__tests__/update.test.ts index e1723915c..1cda8fbf8 100644 --- a/packages/actions/src/__tests__/update.test.ts +++ b/packages/actions/src/__tests__/update.test.ts @@ -32,8 +32,8 @@ describe('update', () => { it('update', async () => { const Post = api.db.getModel('posts'); const post = await Post.create(); - await api.resource('posts').update({ - resourceKey: post.id, + await api.agent().resource('posts').update({ + resourceIndex: post.id, values: { title: 't1', }, @@ -51,9 +51,9 @@ describe('update', () => { await post.updateAssociations({ comments: [comment] }); - await api.resource('posts.comments').update({ - resourceKey: comment.id, - associatedKey: post.id, + await api.agent().resource('posts.comments').update({ + resourceIndex: comment.id, + associatedIndex: post.id, values: { content: 'c2', }, diff --git a/packages/actions/src/actions/add.ts b/packages/actions/src/actions/add.ts index 4debccece..fd77fcfca 100644 --- a/packages/actions/src/actions/add.ts +++ b/packages/actions/src/actions/add.ts @@ -28,7 +28,7 @@ export async function add(ctx: Context, next: Next) { throw new Error(`${associatedName} associated model invalid`); } const { add: addAccessor } = resourceField.getAccessors(); - const { resourceKey, resourceKeyAttribute, fields = [] } = ctx.action.params; + const { resourceIndex, resourceIndexAttribute, fields = [] } = ctx.action.params; const TargetModel = ctx.db.getModel(resourceField.getTarget()); // const options = TargetModel.parseApiJson({ // fields, @@ -36,7 +36,7 @@ export async function add(ctx: Context, next: Next) { const model = await TargetModel.findOne({ // ...options, where: { - [resourceKeyAttribute || resourceField.options.targetKey || TargetModel.primaryKeyAttribute]: resourceKey, + [resourceIndexAttribute || resourceField.options.targetKey || TargetModel.primaryKeyAttribute]: resourceIndex, }, // @ts-ignore context: ctx, diff --git a/packages/actions/src/actions/destroy.ts b/packages/actions/src/actions/destroy.ts index 8e05ef772..92afb8783 100644 --- a/packages/actions/src/actions/destroy.ts +++ b/packages/actions/src/actions/destroy.ts @@ -27,8 +27,8 @@ export async function destroy(ctx: Context, next: Next) { resourceField, associatedName, resourceName, - resourceKey, - resourceKeyAttribute, + resourceIndex, + resourceIndexAttribute, filter, } = ctx.action.params; const transaction = await ctx.db.sequelize.transaction(); @@ -58,11 +58,11 @@ export async function destroy(ctx: Context, next: Next) { resourceField instanceof BELONGSTOMANY ) { const primaryKey = - resourceKeyAttribute || + resourceIndexAttribute || resourceField.options.targetKey || TargetModel.primaryKeyAttribute; const models: Model[] = await associated[getAccessor]({ - where: resourceKey ? { [primaryKey]: resourceKey } : where, + where: resourceIndex ? { [primaryKey]: resourceIndex } : where, ...commonOptions, }); // TODO:不能程序上解除关系,直接通过 onDelete 触发,或者通过 afterDestroy 处理 @@ -79,9 +79,9 @@ export async function destroy(ctx: Context, next: Next) { } else { const Model = ctx.db.getModel(resourceName); const { where } = Model.parseApiJson({ filter, context: ctx }); - const primaryKey = resourceKeyAttribute || Model.primaryKeyAttribute; + const primaryKey = resourceIndexAttribute || Model.primaryKeyAttribute; count = await Model.destroy({ - where: resourceKey ? { [primaryKey]: resourceKey } : where, + where: resourceIndex ? { [primaryKey]: resourceIndex } : where, // @ts-ignore hooks 里添加 context ...commonOptions, individualHooks: true, diff --git a/packages/actions/src/actions/get.ts b/packages/actions/src/actions/get.ts index d6fe2efbf..a668f8324 100644 --- a/packages/actions/src/actions/get.ts +++ b/packages/actions/src/actions/get.ts @@ -20,8 +20,8 @@ export async function get(ctx: Context, next: Next) { resourceField, associatedName, resourceName, - resourceKey, - resourceKeyAttribute, + resourceIndex, + resourceIndexAttribute, fields = [] } = ctx.action.params; if (associated && resourceField) { @@ -50,7 +50,7 @@ export async function get(ctx: Context, next: Next) { const [model]: Model[] = await associated[getAccessor]({ ...options, where: { - [resourceKeyAttribute || resourceField.options.targetKey || TargetModel.primaryKeyAttribute]: resourceKey, + [resourceIndexAttribute || resourceField.options.targetKey || TargetModel.primaryKeyAttribute]: resourceIndex, }, context: ctx, }); @@ -64,7 +64,7 @@ export async function get(ctx: Context, next: Next) { const data = await Model.findOne({ ...options, where: { - [resourceKeyAttribute || Model.primaryKeyAttribute]: resourceKey, + [resourceIndexAttribute || Model.primaryKeyAttribute]: resourceIndex, }, // @ts-ignore hooks 里添加 context context: ctx, diff --git a/packages/actions/src/actions/remove.ts b/packages/actions/src/actions/remove.ts index 2003dd3ce..7fba10d88 100644 --- a/packages/actions/src/actions/remove.ts +++ b/packages/actions/src/actions/remove.ts @@ -33,7 +33,7 @@ export async function remove(ctx: Context, next: Next) { throw new Error(`${associatedName} associated model invalid`); } const { get: getAccessor, remove: removeAccessor, set: setAccessor } = resourceField.getAccessors(); - const { resourceKey, resourceKeyAttribute, fields = [] } = ctx.action.params; + const { resourceIndex, resourceIndexAttribute, fields = [] } = ctx.action.params; const TargetModel = ctx.db.getModel(resourceField.getTarget()); const options = TargetModel.parseApiJson({ fields, @@ -44,7 +44,7 @@ export async function remove(ctx: Context, next: Next) { const [model]: Model[] = await associated[getAccessor]({ ...options, where: { - [resourceKeyAttribute || resourceField.options.targetKey || TargetModel.primaryKeyAttribute]: resourceKey, + [resourceIndexAttribute || resourceField.options.targetKey || TargetModel.primaryKeyAttribute]: resourceIndex, }, context: ctx, }); diff --git a/packages/actions/src/actions/set.ts b/packages/actions/src/actions/set.ts index c30667006..8fe4dd3b3 100644 --- a/packages/actions/src/actions/set.ts +++ b/packages/actions/src/actions/set.ts @@ -33,14 +33,14 @@ export async function set(ctx: Context, next: Next) { throw new Error(`${associatedName} associated model invalid`); } const { set: setAccessor } = resourceField.getAccessors(); - const { resourceKey, resourceKeyAttribute, fields = [] } = ctx.action.params; + const { resourceIndex, resourceIndexAttribute, fields = [] } = ctx.action.params; const TargetModel = ctx.db.getModel(resourceField.getTarget()); // const options = TargetModel.parseApiJson({ // fields, // }); const model = await TargetModel.findOne({ where: { - [resourceKeyAttribute || resourceField.options.targetKey || TargetModel.primaryKeyAttribute]: resourceKey, + [resourceIndexAttribute || resourceField.options.targetKey || TargetModel.primaryKeyAttribute]: resourceIndex, }, // @ts-ignore context: ctx, diff --git a/packages/actions/src/actions/sort.ts b/packages/actions/src/actions/sort.ts index f61559a82..98000f098 100644 --- a/packages/actions/src/actions/sort.ts +++ b/packages/actions/src/actions/sort.ts @@ -18,10 +18,10 @@ import { export async function sort(ctx: Context, next: Next) { const { resourceName, - resourceKey, + resourceIndex, resourceField, associatedName, - associatedKey, + associatedIndex, associated, values = {}, ...others @@ -41,7 +41,7 @@ export async function sort(ctx: Context, next: Next) { const table = ctx.db.getTable(resourceName); const { primaryKeyAttribute } = Model; - const sourceId = others.sourceId || resourceKey; + const sourceId = others.sourceId || resourceIndex; const field = others.sortField || values?.sortField || values?.field || 'sort'; const targetId = others.targetId || values?.targetId || values?.target?.[primaryKeyAttribute]; const method = others.method || values?.method; @@ -70,7 +70,7 @@ export async function sort(ctx: Context, next: Next) { const where = {}; if (associated && resourceField instanceof HASMANY) { - where[resourceField.options.foreignKey] = associatedKey; + where[resourceField.options.foreignKey] = associatedIndex; } // 找到操作对象 diff --git a/packages/actions/src/actions/toggle.ts b/packages/actions/src/actions/toggle.ts index 5ad7ee3d6..5de3a8d09 100644 --- a/packages/actions/src/actions/toggle.ts +++ b/packages/actions/src/actions/toggle.ts @@ -24,20 +24,20 @@ export async function toggle(ctx: Context, next: Next) { throw new Error(`${associatedName} associated model invalid`); } const { get: getAccessor, remove: removeAccessor, set: setAccessor, add: addAccessor } = resourceField.getAccessors(); - const { resourceKey, resourceKeyAttribute, fields = [] } = ctx.action.params; + const { resourceIndex, resourceIndexAttribute, fields = [] } = ctx.action.params; const TargetModel = ctx.db.getModel(resourceField.getTarget()); const options = TargetModel.parseApiJson({ fields, }); if (resourceField instanceof HASONE || resourceField instanceof BELONGSTO) { const m1 = await associated[getAccessor](); - if (m1 && m1[resourceKeyAttribute || resourceField.options.targetKey || TargetModel.primaryKeyAttribute] == resourceKey) { + if (m1 && m1[resourceIndexAttribute || resourceField.options.targetKey || TargetModel.primaryKeyAttribute] == resourceIndex) { ctx.body = await associated[setAccessor](null); } else { const m2 = await TargetModel.findOne({ // ...options, where: { - [resourceKeyAttribute || resourceField.options.targetKey || TargetModel.primaryKeyAttribute]: resourceKey, + [resourceIndexAttribute || resourceField.options.targetKey || TargetModel.primaryKeyAttribute]: resourceIndex, }, // @ts-ignore context: ctx, @@ -48,7 +48,7 @@ export async function toggle(ctx: Context, next: Next) { const [model]: Model[] = await associated[getAccessor]({ ...options, where: { - [resourceKeyAttribute || resourceField.options.targetKey || TargetModel.primaryKeyAttribute]: resourceKey, + [resourceIndexAttribute || resourceField.options.targetKey || TargetModel.primaryKeyAttribute]: resourceIndex, }, context: ctx, }); @@ -58,7 +58,7 @@ export async function toggle(ctx: Context, next: Next) { const m2 = await TargetModel.findOne({ // ...options, where: { - [resourceKeyAttribute || resourceField.options.targetKey || TargetModel.primaryKeyAttribute]: resourceKey, + [resourceIndexAttribute || resourceField.options.targetKey || TargetModel.primaryKeyAttribute]: resourceIndex, }, // @ts-ignore context: ctx, diff --git a/packages/actions/src/actions/update.ts b/packages/actions/src/actions/update.ts index 3ab4cc5de..81cf0548a 100644 --- a/packages/actions/src/actions/update.ts +++ b/packages/actions/src/actions/update.ts @@ -24,9 +24,9 @@ export async function update(ctx: Context, next: Next) { associatedName, resourceField, resourceName, - resourceKey, + resourceIndex, // TODO(question): 这个属性从哪设置的? - resourceKeyAttribute, + resourceIndexAttribute, fields, values: data } = ctx.action.params; @@ -53,7 +53,7 @@ export async function update(ctx: Context, next: Next) { const [model]: Model[] = await associated[getAccessor]({ ...options, where: { - [resourceKeyAttribute || resourceField.options.targetKey || TargetModel.primaryKeyAttribute]: resourceKey, + [resourceIndexAttribute || resourceField.options.targetKey || TargetModel.primaryKeyAttribute]: resourceIndex, } }); @@ -66,7 +66,7 @@ export async function update(ctx: Context, next: Next) { const through = await ThroughModel.findOne({ where: { [foreignKey]: associated[sourceKey], - [otherKey]: resourceKey, + [otherKey]: resourceIndex, }, transaction }); @@ -88,7 +88,7 @@ export async function update(ctx: Context, next: Next) { const model = await Model.findOne({ ...options, where: { - [resourceKeyAttribute || Model.primaryKeyAttribute]: resourceKey, + [resourceIndexAttribute || Model.primaryKeyAttribute]: resourceIndex, } }); // @ts-ignore diff --git a/packages/actions/src/middlewares/associated.ts b/packages/actions/src/middlewares/associated.ts index 9715df1ae..3b97f7c9a 100644 --- a/packages/actions/src/middlewares/associated.ts +++ b/packages/actions/src/middlewares/associated.ts @@ -7,9 +7,9 @@ export async function associated(ctx: Context, next: Next) { return next(); } - const { associated, associatedName, associatedKey, resourceName } = ctx.action.params; + const { associated, associatedName, associatedIndex, resourceName } = ctx.action.params; - if (!associatedName || !associatedKey) { + if (!associatedName || !associatedIndex) { return next(); } @@ -37,7 +37,7 @@ export async function associated(ctx: Context, next: Next) { if (key) { const model = await Model.findOne({ where: { - [key]: associatedKey, + [key]: associatedIndex, } }); if (model) { diff --git a/packages/client/src/components/admin-layout/Collections/index.tsx b/packages/client/src/components/admin-layout/Collections/index.tsx index eec41a7de..8a434dc3f 100644 --- a/packages/client/src/components/admin-layout/Collections/index.tsx +++ b/packages/client/src/components/admin-layout/Collections/index.tsx @@ -66,7 +66,7 @@ const useActionPermissionSubmit = () => { const role = useContext(RoleContext); const resource = useResourceRequest({ resourceName: 'roles', - resourceKey: role.name, + resourceIndex: role.name, }); return { async run() { @@ -134,7 +134,7 @@ const useDetailsResource = ({ onSuccess }) => { const ctx = useContext(TableRowContext); const resource = useResourceRequest({ resourceName: 'collections', - resourceKey: ctx.record[props.rowKey], + resourceIndex: ctx.record[props.rowKey], }); const service = useRequest( (params?: any) => { @@ -762,7 +762,7 @@ function EditFieldButton() { const fieldInterface = interfaces.get(form.values?.interface); fieldInterface?.initialize && fieldInterface?.initialize(form.values); await resource.save(form.values, { - resourceKey: ctx.record.key, + resourceIndex: ctx.record.key, }); setVisible(false); await service.refresh(); diff --git a/packages/client/src/components/admin-layout/Permissions/ActionPermissionField.tsx b/packages/client/src/components/admin-layout/Permissions/ActionPermissionField.tsx index 08463c6fc..624f95fbd 100644 --- a/packages/client/src/components/admin-layout/Permissions/ActionPermissionField.tsx +++ b/packages/client/src/components/admin-layout/Permissions/ActionPermissionField.tsx @@ -66,7 +66,7 @@ const useFieldPermissions = () => { }; const resource = useResourceRequest({ associatedName: 'collections', - associatedKey: ctx.record.name, + associatedIndex: ctx.record.name, resourceName: 'fields', }); const service = useRequest( diff --git a/packages/client/src/components/admin-layout/Permissions/MenuPermissionTable.tsx b/packages/client/src/components/admin-layout/Permissions/MenuPermissionTable.tsx index 0025004c0..f04547b6d 100644 --- a/packages/client/src/components/admin-layout/Permissions/MenuPermissionTable.tsx +++ b/packages/client/src/components/admin-layout/Permissions/MenuPermissionTable.tsx @@ -33,7 +33,7 @@ export const MenuPermissionTable = observer((props) => { console.log('allUiSchemaKyes', allUiSchemaKyes); const resource = useResourceRequest({ associatedName: 'roles', - associatedKey: role.name, + associatedIndex: role.name, resourceName: 'ui_schemas', }); useRequest(() => resource.list(), { @@ -44,7 +44,7 @@ export const MenuPermissionTable = observer((props) => { }); const resource2 = useResourceRequest({ resourceName: 'roles', - resourceKey: role.name, + resourceIndex: role.name, }); if (loading) { return ; @@ -106,7 +106,7 @@ export const MenuPermissionTable = observer((props) => { return [...prevUiSchemaKeys]; }); await resource.toggle({ - resourceKey: value, + resourceIndex: value, }); }} /> diff --git a/packages/client/src/components/admin-layout/Permissions/index.tsx b/packages/client/src/components/admin-layout/Permissions/index.tsx index 002fbd658..70c1ab4e1 100644 --- a/packages/client/src/components/admin-layout/Permissions/index.tsx +++ b/packages/client/src/components/admin-layout/Permissions/index.tsx @@ -59,7 +59,7 @@ const useActionPermissionSubmit = () => { const role = useContext(RoleContext); const resource = useResourceRequest({ resourceName: 'roles', - resourceKey: role.name, + resourceIndex: role.name, }); return { async run() { @@ -127,7 +127,7 @@ const useDetailsResource = ({ onSuccess }) => { const ctx = useContext(TableRowContext); const resource = useResourceRequest({ resourceName: 'roles', - resourceKey: ctx.record[props.rowKey], + resourceIndex: ctx.record[props.rowKey], }); const service = useRequest( (params?: any) => { diff --git a/packages/client/src/components/admin-layout/SiteTitle.tsx b/packages/client/src/components/admin-layout/SiteTitle.tsx index 24c5dcfcb..316d53ab8 100644 --- a/packages/client/src/components/admin-layout/SiteTitle.tsx +++ b/packages/client/src/components/admin-layout/SiteTitle.tsx @@ -10,12 +10,12 @@ export const SystemSettingsContext = createContext(null); export function SystemSettingsProvider(props) { const resource = useResourceRequest({ resourceName: 'system_settings', - resourceKey: 1, + resourceIndex: 1, }); const service = useRequest( () => resource.get({ - resourceKey: 1, + resourceIndex: 1, appends: ['logo'], }), { diff --git a/packages/client/src/components/schema-renderer/index.tsx b/packages/client/src/components/schema-renderer/index.tsx index c42b47382..a8c13a180 100644 --- a/packages/client/src/components/schema-renderer/index.tsx +++ b/packages/client/src/components/schema-renderer/index.tsx @@ -152,13 +152,13 @@ const useAssociationResource = (options) => { const collectionField = useContext(CollectionFieldContext); const { collection } = useCollectionContext(); const ctx = useContext(TableRowContext); - const associatedKey = ctx?.record?.id; + const associatedIndex = ctx?.record?.id; console.log('useAssociationResource', collection, collectionField, schema['x-component-props']); const { associatedName, resourceName } = schema['x-component-props'] || {}; const resource = useResourceRequest({ associatedName, resourceName, - associatedKey, + associatedIndex, }); const service = useRequest((params) => resource.list(params), { manual: schema['x-component'] === 'Form', diff --git a/packages/client/src/resource.ts b/packages/client/src/resource.ts index 8acd5b981..4066af817 100644 --- a/packages/client/src/resource.ts +++ b/packages/client/src/resource.ts @@ -3,19 +3,19 @@ import { request as req } from './schemas'; export interface ResourceOptions { resourceName: string; - associatedKey?: any; + associatedIndex?: any; associatedName?: string; - resourceKey?: any; + resourceIndex?: any; } export interface GetOptions { - resourceKey?: any; + resourceIndex?: any; defaultAppends?: any[]; appends?: string[]; } export interface SaveOptions { - resourceKey?: any; + resourceIndex?: any; } export interface ListOptions { @@ -42,8 +42,8 @@ export class Resource { sort(options) { const { resourceName } = this.options; - const { resourceKey, target, field = 'sort' } = options; - return this.request(`${resourceName}:sort/${resourceKey}`, { + const { resourceIndex, target, field = 'sort' } = options; + return this.request(`${resourceName}:sort/${resourceIndex}`, { method: 'post', data: { target, @@ -61,10 +61,10 @@ export class Resource { pageSize, ...others } = options; - const { associatedKey, associatedName, resourceName } = this.options; + const { associatedIndex, associatedName, resourceName } = this.options; let url = `${resourceName}:list`; - if (associatedName && associatedKey) { - url = `${associatedName}/${associatedKey}/${resourceName}:list`; + if (associatedName && associatedIndex) { + url = `${associatedName}/${associatedIndex}/${resourceName}:list`; } return this.request(url, { method: 'get', @@ -80,13 +80,13 @@ export class Resource { } get(options: GetOptions = {}) { - const resourceKey = options.resourceKey || this.options.resourceKey; + const resourceIndex = options.resourceIndex || this.options.resourceIndex; const { resourceName } = this.options; - if (!resourceKey) { + if (!resourceIndex) { return Promise.resolve({ data: {} }); } const { defaultAppends = [], appends = [], ...others } = options; - return this.request(`${resourceName}:get/${resourceKey}`, { + return this.request(`${resourceName}:get/${resourceIndex}`, { params: { ...others, 'fields[appends]': defaultAppends.concat(appends).join(','), @@ -95,10 +95,10 @@ export class Resource { } create(values: any) { - const { associatedKey, associatedName, resourceName } = this.options; + const { associatedIndex, associatedName, resourceName } = this.options; let url = `${resourceName}:create`; - if (associatedKey && associatedName) { - url = `${associatedName}/${associatedKey}/${url}`; + if (associatedIndex && associatedName) { + url = `${associatedName}/${associatedIndex}/${url}`; } return this.request(url, { method: 'post', @@ -107,13 +107,13 @@ export class Resource { } save(values: any, options: SaveOptions = {}) { - const resourceKey = options.resourceKey || this.options.resourceKey; - const { associatedKey, associatedName, resourceName } = this.options; + const resourceIndex = options.resourceIndex || this.options.resourceIndex; + const { associatedIndex, associatedName, resourceName } = this.options; let url = `${resourceName}:${ - resourceKey ? `update/${resourceKey}` : 'create' + resourceIndex ? `update/${resourceIndex}` : 'create' }`; - if (associatedKey && associatedName) { - url = `${associatedName}/${associatedKey}/${url}`; + if (associatedIndex && associatedName) { + url = `${associatedName}/${associatedIndex}/${url}`; } return this.request(url, { method: 'post', @@ -171,9 +171,9 @@ export class Resource { } toggle(options?: any) { - const { associatedKey, associatedName, resourceName } = this.options; - const { resourceKey } = options; - let url = `${associatedName}/${associatedKey}/${resourceName}:toggle/${resourceKey}`; + const { associatedIndex, associatedName, resourceName } = this.options; + const { resourceIndex } = options; + let url = `${associatedName}/${associatedIndex}/${resourceName}:toggle/${resourceIndex}`; return this.request(url, { method: 'post', }); diff --git a/packages/client/src/schemas/calendar/index.tsx b/packages/client/src/schemas/calendar/index.tsx index 17561c4fe..84ccbc9b3 100644 --- a/packages/client/src/schemas/calendar/index.tsx +++ b/packages/client/src/schemas/calendar/index.tsx @@ -279,7 +279,7 @@ Calendar.useResource = ({ onSuccess }) => { const record = useContext(RecordContext); const resource = useResourceRequest({ resourceName: collection?.name || props.collectionName, - resourceKey: record['id'], + resourceIndex: record['id'], }); console.log( 'collection?.name || props.collectionName', diff --git a/packages/client/src/schemas/kanban/index.tsx b/packages/client/src/schemas/kanban/index.tsx index d0ab89863..b8ab60955 100644 --- a/packages/client/src/schemas/kanban/index.tsx +++ b/packages/client/src/schemas/kanban/index.tsx @@ -294,11 +294,11 @@ const InternalKanban = observer((props: any) => { [groupField.name]: overColumnId, }, { - resourceKey: activeId, + resourceIndex: activeId, }, ); await resource.sort({ - resourceKey: activeId, + resourceIndex: activeId, field: 'sort', target: lastId ? { @@ -331,11 +331,11 @@ const InternalKanban = observer((props: any) => { [groupField.name]: overColumnId, }, { - resourceKey: activeId, + resourceIndex: activeId, }, ); await resource.sort({ - resourceKey: activeId, + resourceIndex: activeId, field: 'sort', target: { id: overId, @@ -347,11 +347,11 @@ const InternalKanban = observer((props: any) => { [groupField.name]: overId, }, { - resourceKey: activeId, + resourceIndex: activeId, }, ); await resource.sort({ - resourceKey: activeId, + resourceIndex: activeId, field: 'sort', target: lastId ? { @@ -468,7 +468,7 @@ Kanban.useUpdateAction = () => { return { async run() { await resource.save(omit(form.values, ['sort']), { - resourceKey: ctx.record.id, + resourceIndex: ctx.record.id, }); setVisible(false); await service.refresh(); @@ -482,7 +482,7 @@ Kanban.useRowResource = ({ onSuccess }) => { const ctx = useContext(KanbanCardContext); const resource = useResourceRequest({ resourceName: collection?.name || props.collectionName, - resourceKey: ctx?.record?.id, + resourceIndex: ctx?.record?.id, }); const service = useRequest( (params?: any) => { @@ -524,7 +524,7 @@ Kanban.useSingleResource = ({ onSuccess }) => { const resource = useResourceRequest({ resourceName: collection?.name || props.collectionName, - resourceKey: ctx?.record?.id, + resourceIndex: ctx?.record?.id, }); const service = useRequest( (params?: any) => { diff --git a/packages/client/src/schemas/menu/index.tsx b/packages/client/src/schemas/menu/index.tsx index 5fe6a03dc..9948bf438 100644 --- a/packages/client/src/schemas/menu/index.tsx +++ b/packages/client/src/schemas/menu/index.tsx @@ -1006,7 +1006,7 @@ Menu.DesignableBar = (props) => { }; const resource = Resource.make({ associatedName: 'ui_schemas', - associatedKey: schema['key'], + associatedIndex: schema['key'], resourceName: 'roles', }); const uiSchemasRoles = await resource.list(); @@ -1043,7 +1043,7 @@ Menu.DesignableBar = (props) => { }); await Resource.make({ resourceName: 'ui_schemas', - resourceKey: schema['key'], + resourceIndex: schema['key'], }).save(values); }} > diff --git a/packages/client/src/schemas/select/index.tsx b/packages/client/src/schemas/select/index.tsx index 710598aeb..4f512763f 100644 --- a/packages/client/src/schemas/select/index.tsx +++ b/packages/client/src/schemas/select/index.tsx @@ -489,7 +489,7 @@ Select.Drawer.useResource = ({ onSuccess }) => { const ctx = useContext(OptionTagContext); const resource = useResourceRequest({ resourceName: collection?.name, - resourceKey: ctx.data.id, + resourceIndex: ctx.data.id, }); console.log('OptionTagContext', ctx.data.id); const { schema } = useDesignable(); diff --git a/packages/client/src/schemas/table/TableMain.tsx b/packages/client/src/schemas/table/TableMain.tsx index 4a2948225..eae9d7d5e 100644 --- a/packages/client/src/schemas/table/TableMain.tsx +++ b/packages/client/src/schemas/table/TableMain.tsx @@ -43,7 +43,7 @@ export const TableMain = () => { field.move(fromIndex, toIndex); refresh(); await resource.sort({ - resourceKey: fromId, + resourceIndex: fromId, target: { [rowKey]: toId, }, diff --git a/packages/client/src/schemas/table/hooks/useActionLogDetailsResource.ts b/packages/client/src/schemas/table/hooks/useActionLogDetailsResource.ts index d0ecefd2c..f1681d0bf 100644 --- a/packages/client/src/schemas/table/hooks/useActionLogDetailsResource.ts +++ b/packages/client/src/schemas/table/hooks/useActionLogDetailsResource.ts @@ -11,7 +11,7 @@ export const useActionLogDetailsResource = ({ onSuccess }) => { const ctx = useContext(TableRowContext); const resource = useResourceRequest({ resourceName: 'action_logs', - resourceKey: ctx.record[props.rowKey], + resourceIndex: ctx.record[props.rowKey], }); const service = useRequest( (params?: any) => { diff --git a/packages/client/src/schemas/table/hooks/useResource.ts b/packages/client/src/schemas/table/hooks/useResource.ts index 24b405302..bfa5b1006 100644 --- a/packages/client/src/schemas/table/hooks/useResource.ts +++ b/packages/client/src/schemas/table/hooks/useResource.ts @@ -12,7 +12,7 @@ export const useResource = ({ onSuccess, manual = true }) => { const ctx = useContext(TableRowContext); const resource = useResourceRequest({ resourceName: collection?.name || props.collectionName, - resourceKey: ctx.record[props.rowKey], + resourceIndex: ctx.record[props.rowKey], }); const { schema } = useDesignable(); const fieldFields = (schema: Schema) => { diff --git a/packages/client/src/schemas/table/hooks/useTableUpdateAction.ts b/packages/client/src/schemas/table/hooks/useTableUpdateAction.ts index 3ec497662..9034b0962 100644 --- a/packages/client/src/schemas/table/hooks/useTableUpdateAction.ts +++ b/packages/client/src/schemas/table/hooks/useTableUpdateAction.ts @@ -20,7 +20,7 @@ export const useTableUpdateAction = () => { async run() { if (refreshRequestOnChange) { await resource.save(form.values, { - resourceKey: ctx.record[rowKey], + resourceIndex: ctx.record[rowKey], }); if (formService) { await formService.refresh(); diff --git a/packages/plugin-action-logs/src/__tests__/hook.test.ts b/packages/plugin-action-logs/src/__tests__/hook.test.ts index e77d3063c..c24f867c4 100644 --- a/packages/plugin-action-logs/src/__tests__/hook.test.ts +++ b/packages/plugin-action-logs/src/__tests__/hook.test.ts @@ -1,5 +1,4 @@ import Database from '@nocobase/database'; -import { registerActions } from '@nocobase/actions'; import { mockServer, MockServer } from '@nocobase/test'; import logPlugin from '../server'; @@ -9,13 +8,9 @@ describe('hook', () => { beforeEach(async () => { api = mockServer(); - api.registerPlugin({ - collections: require('@nocobase/plugin-collections/src/server').default, - users: require('@nocobase/plugin-users/src/server').default, - logs: logPlugin, - }); - registerActions(api); - await api.loadPlugins(); + api.plugin(require('@nocobase/plugin-users').default); + api.plugin(logPlugin); + await api.load(); db = api.db; db.table({ name: 'posts', @@ -34,8 +29,7 @@ describe('hook', () => { }); await db.sync(); const User = db.getModel('users'); - const user = await User.create({ nickname: 'a', token: 'token1' }); - api.agent().set('Authorization', `Bearer ${user.token}`); + await User.create({ nickname: 'a', token: 'token1' }); }); afterEach(async () => { @@ -53,15 +47,17 @@ describe('hook', () => { }); it('resource', async () => { - const response = await api.resource('posts').create({ + const agent = api.agent() + agent.set('Authorization', `Bearer token1`); + const response = await agent.resource('posts').create({ values: { title: 't1' }, }); - await api.resource('posts').update({ - resourceKey: response.body.data.id, + await agent.resource('posts').update({ + resourceIndex: response.body.data.id, values: { title: 't2' }, }); - await api.resource('posts').destroy({ - resourceKey: response.body.data.id, + await agent.resource('posts').destroy({ + resourceIndex: response.body.data.id, }); const ActionLog = db.getModel('action_logs'); const count = await ActionLog.count(); diff --git a/packages/plugin-action-logs/src/index.ts b/packages/plugin-action-logs/src/index.ts new file mode 100644 index 000000000..7ddad5814 --- /dev/null +++ b/packages/plugin-action-logs/src/index.ts @@ -0,0 +1 @@ +export { default } from './server'; diff --git a/packages/plugin-automations/src/__tests__/index.ts b/packages/plugin-automations/src/__tests__/index.ts index ff38b9cb1..d9cd1889c 100644 --- a/packages/plugin-automations/src/__tests__/index.ts +++ b/packages/plugin-automations/src/__tests__/index.ts @@ -85,10 +85,10 @@ export async function getApp() { } interface ActionParams { - resourceKey?: string | number; + resourceIndex?: string | number; // resourceName?: string; // associatedName?: string; - associatedKey?: string | number; + associatedIndex?: string | number; fields?: any; filter?: any; values?: any; @@ -118,14 +118,14 @@ export function getAPI(agent) { return new Proxy({}, { get(target, method: string, receiver) { return (params: ActionParams = {}) => { - const { associatedKey, resourceKey, values = {}, filePath, ...restParams } = params; + const { associatedIndex, resourceIndex, values = {}, filePath, ...restParams } = params; let url = `/api/${name}`; - if (associatedKey) { - url = `/api/${name.split('.').join(`/${associatedKey}/`)}`; + if (associatedIndex) { + url = `/api/${name.split('.').join(`/${associatedIndex}/`)}`; } url += `:${method as string}`; - if (resourceKey) { - url += `/${resourceKey}`; + if (resourceIndex) { + url += `/${resourceIndex}`; } switch (method) { diff --git a/packages/plugin-automations/src/collections/automations.ts b/packages/plugin-automations/src/collections/automations.ts index b90fa0be8..5835ce8af 100644 --- a/packages/plugin-automations/src/collections/automations.ts +++ b/packages/plugin-automations/src/collections/automations.ts @@ -243,7 +243,7 @@ export default { // condition: "{{ $self.value }}", schema: { "x-component-props": { - "associatedKey": "{{ typeof $self.value === 'string' ? $self.value : $form.values.collection_name }}" + "associatedIndex": "{{ typeof $self.value === 'string' ? $self.value : $form.values.collection_name }}" }, }, }, @@ -253,7 +253,7 @@ export default { // condition: "{{ $self.value }}", schema: { "x-component-props": { - "associatedKey": "{{ typeof $self.value === 'string' ? $self.value : $form.values.collection_name }}" + "associatedIndex": "{{ typeof $self.value === 'string' ? $self.value : $form.values.collection_name }}" }, }, }, @@ -263,7 +263,7 @@ export default { // condition: "{{ $self.value }}", schema: { "x-component-props": { - "associatedKey": "{{ typeof $self.value === 'string' ? $self.value : $form.values.collection_name }}" + "associatedIndex": "{{ typeof $self.value === 'string' ? $self.value : $form.values.collection_name }}" }, }, }, @@ -273,7 +273,7 @@ export default { // condition: "{{ $self.value }}", schema: { "x-component-props": { - "associatedKey": "{{ typeof $self.value === 'string' ? $self.value : $form.values.collection_name }}" + "associatedIndex": "{{ typeof $self.value === 'string' ? $self.value : $form.values.collection_name }}" }, }, }, @@ -406,7 +406,7 @@ export default { condition: "{{ ($form.values.collection_name || $form.values.collection) && $self.value === 'customField' }}", schema: { "x-component-props": { - "associatedKey": "{{ $form.values.collection_name || $form.values.collection }}" + "associatedIndex": "{{ $form.values.collection_name || $form.values.collection }}" }, }, }, diff --git a/packages/plugin-automations/src/collections/automations_jobs.ts b/packages/plugin-automations/src/collections/automations_jobs.ts index 886eb6110..31c711323 100644 --- a/packages/plugin-automations/src/collections/automations_jobs.ts +++ b/packages/plugin-automations/src/collections/automations_jobs.ts @@ -84,7 +84,7 @@ export default { // condition: "{{ $self.value }}", schema: { "x-component-props": { - "associatedKey": "{{ typeof $self.value === 'string' ? $self.value : $form.values.collection_name }}", + "associatedIndex": "{{ typeof $self.value === 'string' ? $self.value : $form.values.collection_name }}", "sourceName": "{{ $form.values.automation ? $form.values.automation.collection_name : null }}" }, }, @@ -95,7 +95,7 @@ export default { // condition: "{{ $self.value }}", schema: { "x-component-props": { - "associatedKey": "{{ typeof $self.value === 'string' ? $self.value : $form.values.collection_name }}", + "associatedIndex": "{{ typeof $self.value === 'string' ? $self.value : $form.values.collection_name }}", "sourceName": "{{ $form.values.automation ? $form.values.automation.collection_name : null }}" }, }, diff --git a/packages/plugin-collections/src/__tests__/index.ts b/packages/plugin-collections/src/__tests__/index.ts index d4439acdc..d106f5372 100644 --- a/packages/plugin-collections/src/__tests__/index.ts +++ b/packages/plugin-collections/src/__tests__/index.ts @@ -80,10 +80,10 @@ export async function getApp() { } interface ActionParams { - resourceKey?: string | number; + resourceIndex?: string | number; // resourceName?: string; // associatedName?: string; - associatedKey?: string | number; + associatedIndex?: string | number; fields?: any; filter?: any; values?: any; @@ -110,14 +110,14 @@ export function getAgent(app: Application): Agent { return new Proxy({}, { get(target, method, receiver) { return (params: ActionParams = {}) => { - const { associatedKey, resourceKey, values = {}, ...restParams } = params; + const { associatedIndex, resourceIndex, values = {}, ...restParams } = params; let url = `/api/${name}`; - if (associatedKey) { - url = `/api/${name.split('.').join(`/${associatedKey}/`)}`; + if (associatedIndex) { + url = `/api/${name.split('.').join(`/${associatedIndex}/`)}`; } url += `:${method as string}`; - if (resourceKey) { - url += `/${resourceKey}`; + if (resourceIndex) { + url += `/${resourceIndex}`; } console.log(url); if (['list', 'get'].indexOf(method as string) !== -1) { diff --git a/packages/plugin-file-manager/src/__tests__/action.test.ts b/packages/plugin-file-manager/src/__tests__/action.test.ts index f280af173..2e603db9f 100644 --- a/packages/plugin-file-manager/src/__tests__/action.test.ts +++ b/packages/plugin-file-manager/src/__tests__/action.test.ts @@ -94,21 +94,21 @@ describe('action', () => { }); describe('belongsTo attachment', () => { - it('upload with associatedKey, fail as 400 because file mimetype does not match', async () => { + it('upload with associatedIndex, fail as 400 because file mimetype does not match', async () => { const User = db.getModel('users'); const user = await User.create(); const response = await agent.resource('users.avatar').upload({ - associatedKey: user.id, + associatedIndex: user.id, file: path.resolve(__dirname, './files/text.txt') }); expect(response.status).toBe(400); }); - it('upload with associatedKey', async () => { + it('upload with associatedIndex', async () => { const User = db.getModel('users'); const user = await User.create(); const { body } = await agent.resource('users.avatar').upload({ - associatedKey: user.id, + associatedIndex: user.id, file: path.resolve(__dirname, './files/image.png'), values: { width: 100, height: 100 } }); @@ -155,7 +155,7 @@ describe('action', () => { const User = db.getModel('users'); const user = await User.create(); const { body } = await agent.resource('users.pubkeys').upload({ - associatedKey: user.id, + associatedIndex: user.id, file: path.resolve(__dirname, './files/text.txt'), values: {} }); @@ -167,8 +167,8 @@ describe('action', () => { expect(content.text).toBe('Hello world!\n'); }); - // TODO(bug): 没有 associatedKey 时路径解析资源名称不对,无法进入 action - it.skip('upload without associatedKey', async () => { + // TODO(bug): 没有 associatedIndex 时路径解析资源名称不对,无法进入 action + it.skip('upload without associatedIndex', async () => { const { body } = await agent.resource('users.avatar').upload({ file: path.resolve(__dirname, './files/image.png'), values: { width: 100, height: 100 } diff --git a/packages/plugin-file-manager/src/__tests__/index.ts b/packages/plugin-file-manager/src/__tests__/index.ts index 069ce4c10..0d5326cff 100644 --- a/packages/plugin-file-manager/src/__tests__/index.ts +++ b/packages/plugin-file-manager/src/__tests__/index.ts @@ -2,7 +2,7 @@ import path from 'path'; import supertest from 'supertest'; import { MockServer, mockServer } from '@nocobase/test'; -import plugin from '../server'; +import plugin from '../'; export async function getApp(options = {}): Promise { const app = mockServer({ diff --git a/packages/plugin-file-manager/src/actions/upload.ts b/packages/plugin-file-manager/src/actions/upload.ts index e3cf92ef3..db92992c3 100644 --- a/packages/plugin-file-manager/src/actions/upload.ts +++ b/packages/plugin-file-manager/src/actions/upload.ts @@ -113,11 +113,11 @@ export async function action(ctx: Context, next: Next) { // TODO(optimize): 应使用关联 accessors 获取 const result = await storage.createAttachment(data, { transaction }); - const { associatedName, associatedKey, resourceField } = ctx.action.params; - if (associatedKey && resourceField) { + const { associatedName, associatedIndex, resourceField } = ctx.action.params; + if (associatedIndex && resourceField) { const Attachment = ctx.db.getModel('attachments'); const SourceModel = ctx.db.getModel(associatedName); - const source = await SourceModel.findByPk(associatedKey, { transaction }); + const source = await SourceModel.findByPk(associatedIndex, { transaction }); await source[resourceField.getAccessors().set](result[Attachment.primaryKeyAttribute], { transaction }); } diff --git a/packages/plugin-file-manager/src/index.ts b/packages/plugin-file-manager/src/index.ts index c94f80f84..0d3c193d0 100644 --- a/packages/plugin-file-manager/src/index.ts +++ b/packages/plugin-file-manager/src/index.ts @@ -1 +1,2 @@ export * from './constants'; +export { default } from './server'; diff --git a/packages/plugin-system-settings/src/server.ts b/packages/plugin-system-settings/src/server.ts index 41acbba25..21769d1eb 100644 --- a/packages/plugin-system-settings/src/server.ts +++ b/packages/plugin-system-settings/src/server.ts @@ -14,14 +14,14 @@ export default { const SystemSetting = database.getModel('system_settings'); resourcer.use(async (ctx, next) => { - const { actionName, resourceName, resourceKey } = ctx.action.params; + const { actionName, resourceName, resourceIndex } = ctx.action.params; if (resourceName === 'system_settings' && actionName === 'get') { let model = await SystemSetting.findOne(); if (!model) { model = await SystemSetting.create(); } ctx.action.mergeParams({ - resourceKey: model.id, + resourceIndex: model.id, }); } await next(); diff --git a/packages/plugin-ui-router/src/__tests__/index.ts b/packages/plugin-ui-router/src/__tests__/index.ts index d4439acdc..d106f5372 100644 --- a/packages/plugin-ui-router/src/__tests__/index.ts +++ b/packages/plugin-ui-router/src/__tests__/index.ts @@ -80,10 +80,10 @@ export async function getApp() { } interface ActionParams { - resourceKey?: string | number; + resourceIndex?: string | number; // resourceName?: string; // associatedName?: string; - associatedKey?: string | number; + associatedIndex?: string | number; fields?: any; filter?: any; values?: any; @@ -110,14 +110,14 @@ export function getAgent(app: Application): Agent { return new Proxy({}, { get(target, method, receiver) { return (params: ActionParams = {}) => { - const { associatedKey, resourceKey, values = {}, ...restParams } = params; + const { associatedIndex, resourceIndex, values = {}, ...restParams } = params; let url = `/api/${name}`; - if (associatedKey) { - url = `/api/${name.split('.').join(`/${associatedKey}/`)}`; + if (associatedIndex) { + url = `/api/${name.split('.').join(`/${associatedIndex}/`)}`; } url += `:${method as string}`; - if (resourceKey) { - url += `/${resourceKey}`; + if (resourceIndex) { + url += `/${resourceIndex}`; } console.log(url); if (['list', 'get'].indexOf(method as string) !== -1) { diff --git a/packages/plugin-ui-schema/src/actions/index.ts b/packages/plugin-ui-schema/src/actions/index.ts index 552849941..a93ce0226 100644 --- a/packages/plugin-ui-schema/src/actions/index.ts +++ b/packages/plugin-ui-schema/src/actions/index.ts @@ -25,8 +25,8 @@ export const create = async (ctx: Context, next: Next) => { const body = ctx.body; ctx.action.mergeParams( { - associatedKey: values.parentKey, - resourceKey: body.key, + associatedIndex: values.parentKey, + resourceIndex: body.key, ...(sticky ? { sticky: true, @@ -70,8 +70,8 @@ export const update = async (ctx: Context, next: Next) => { const body = ctx.body; ctx.action.mergeParams( { - associatedKey: values.parentKey, - resourceKey: body.key, + associatedIndex: values.parentKey, + resourceIndex: body.key, ...(sticky ? { sticky: true, @@ -95,10 +95,10 @@ export const update = async (ctx: Context, next: Next) => { }; export const getTree = async (ctx: Context, next: Next) => { - const { resourceKey, filter } = ctx.action.params; + const { resourceIndex, filter } = ctx.action.params; const UISchema = ctx.db.getModel('ui_schemas'); - if (resourceKey) { - const schema = await UISchema.findByPk(resourceKey); + if (resourceIndex) { + const schema = await UISchema.findByPk(resourceIndex); const property = schema.toProperty(); const properties = await schema.getProperties(); if (Object.keys(properties).length) { diff --git a/packages/plugin-users/src/index.ts b/packages/plugin-users/src/index.ts index e69de29bb..7ddad5814 100644 --- a/packages/plugin-users/src/index.ts +++ b/packages/plugin-users/src/index.ts @@ -0,0 +1 @@ +export { default } from './server'; diff --git a/packages/resourcer/src/__tests__/koa.test.ts b/packages/resourcer/src/__tests__/koa.test.ts index 9f9d7a75c..ac92c47d1 100644 --- a/packages/resourcer/src/__tests__/koa.test.ts +++ b/packages/resourcer/src/__tests__/koa.test.ts @@ -271,11 +271,11 @@ describe('koa middleware', () => { const response = await agent .post('/resourcer/tests:update') .send({ - resourceKey: 1, + resourceIndex: 1, values: { 'aa': 'aa' } }); expect(response.body).toEqual({ - resourceKey: 1, + resourceIndex: 1, actionName: 'update', resourceName: 'tests', values: { col1: 'val1', aa: 'aa' } @@ -293,7 +293,7 @@ describe('koa middleware', () => { .get('/users/1/settings'); expect(response.body).toEqual({ associatedName: 'users', - associatedKey: '1', + associatedIndex: '1', resourceName: 'settings', actionName: 'get' }); @@ -303,7 +303,7 @@ describe('koa middleware', () => { .post('/users/1/settings'); expect(response.body).toEqual({ associatedName: 'users', - associatedKey: '1', + associatedIndex: '1', resourceName: 'settings', actionName: 'update' }); @@ -313,7 +313,7 @@ describe('koa middleware', () => { .delete('/users/1/settings'); expect(response.body).toEqual({ associatedName: 'users', - associatedKey: '1', + associatedIndex: '1', resourceName: 'settings', actionName: 'destroy' }); @@ -331,7 +331,7 @@ describe('koa middleware', () => { .get('/users/1/posts'); expect(response.body).toEqual({ associatedName: 'users', - associatedKey: '1', + associatedIndex: '1', resourceName: 'posts', actionName: 'list' }); @@ -341,9 +341,9 @@ describe('koa middleware', () => { .get('/users/1/posts/1'); expect(response.body).toEqual({ associatedName: 'users', - associatedKey: '1', + associatedIndex: '1', resourceName: 'posts', - resourceKey: '1', + resourceIndex: '1', actionName: 'get' }); }); @@ -352,7 +352,7 @@ describe('koa middleware', () => { .post('/users/1/posts'); expect(response.body).toEqual({ associatedName: 'users', - associatedKey: '1', + associatedIndex: '1', resourceName: 'posts', actionName: 'create' }); @@ -362,9 +362,9 @@ describe('koa middleware', () => { .put('/users/1/posts/1'); expect(response.body).toEqual({ associatedName: 'users', - associatedKey: '1', + associatedIndex: '1', resourceName: 'posts', - resourceKey: '1', + resourceIndex: '1', actionName: 'update' }); }); @@ -373,9 +373,9 @@ describe('koa middleware', () => { .delete('/users/1/posts/1'); expect(response.body).toEqual({ associatedName: 'users', - associatedKey: '1', + associatedIndex: '1', resourceName: 'posts', - resourceKey: '1', + resourceIndex: '1', actionName: 'destroy' }); }); @@ -392,7 +392,7 @@ describe('koa middleware', () => { .get('/posts/1/user'); expect(response.body).toEqual({ associatedName: 'posts', - associatedKey: '1', + associatedIndex: '1', resourceName: 'user', actionName: 'get' }); @@ -402,9 +402,9 @@ describe('koa middleware', () => { .post('/posts/1/user/1'); expect(response.body).toEqual({ associatedName: 'posts', - associatedKey: '1', + associatedIndex: '1', resourceName: 'user', - resourceKey: '1', + resourceIndex: '1', actionName: 'set' }); }); @@ -413,7 +413,7 @@ describe('koa middleware', () => { .delete('/posts/1/user'); expect(response.body).toEqual({ associatedName: 'posts', - associatedKey: '1', + associatedIndex: '1', resourceName: 'user', actionName: 'remove' }); @@ -431,7 +431,7 @@ describe('koa middleware', () => { .get('/posts/1/tags'); expect(response.body).toEqual({ associatedName: 'posts', - associatedKey: '1', + associatedIndex: '1', resourceName: 'tags', actionName: 'list' }); @@ -441,9 +441,9 @@ describe('koa middleware', () => { .get('/posts/1/tags/1'); expect(response.body).toEqual({ associatedName: 'posts', - associatedKey: '1', + associatedIndex: '1', resourceName: 'tags', - resourceKey: '1', + resourceIndex: '1', actionName: 'get' }); }); @@ -452,7 +452,7 @@ describe('koa middleware', () => { .post('/posts/1/tags'); expect(response.body).toEqual({ associatedName: 'posts', - associatedKey: '1', + associatedIndex: '1', resourceName: 'tags', actionName: 'set' }); @@ -462,9 +462,9 @@ describe('koa middleware', () => { .post('/posts/1/tags/1'); expect(response.body).toEqual({ associatedName: 'posts', - associatedKey: '1', + associatedIndex: '1', resourceName: 'tags', - resourceKey: '1', + resourceIndex: '1', actionName: 'add' }); }); @@ -473,9 +473,9 @@ describe('koa middleware', () => { .put('/posts/1/tags/1'); expect(response.body).toEqual({ associatedName: 'posts', - associatedKey: '1', + associatedIndex: '1', resourceName: 'tags', - resourceKey: '1', + resourceIndex: '1', actionName: 'update' }); }); @@ -484,9 +484,9 @@ describe('koa middleware', () => { .delete('/posts/1/tags/1'); expect(response.body).toEqual({ associatedName: 'posts', - associatedKey: '1', + associatedIndex: '1', resourceName: 'tags', - resourceKey: '1', + resourceIndex: '1', actionName: 'remove' }); }); @@ -628,7 +628,7 @@ describe('koa middleware', () => { }); expect(response.body).toEqual({ associatedName: 'users', - associatedKey: 'name', + associatedIndex: 'name', resourceName: 'posts', actionName: 'list', fields: { appends: ['rel1', 'rel2'] } @@ -640,7 +640,7 @@ describe('koa middleware', () => { actions: { list: { async middleware(ctx, next) { - ctx.action.mergeParams({ filter: { user_name: ctx.action.params.associatedKey } }); + ctx.action.mergeParams({ filter: { user_name: ctx.action.params.associatedIndex } }); await next(); }, }, @@ -650,7 +650,7 @@ describe('koa middleware', () => { .get('/users/name/posts'); expect(response.body).toEqual({ associatedName: 'users', - associatedKey: 'name', + associatedIndex: 'name', resourceName: 'posts', actionName: 'list', filter: { user_name: 'name' }, @@ -662,7 +662,7 @@ describe('koa middleware', () => { actions: { list: { async middleware(ctx, next) { - ctx.action.mergeParams({ fields: { only: [ctx.action.params.associatedKey] } }, { fields: 'append' }); + ctx.action.mergeParams({ fields: { only: [ctx.action.params.associatedIndex] } }, { fields: 'append' }); await next(); }, }, @@ -672,7 +672,7 @@ describe('koa middleware', () => { .get('/users/name/posts'); expect(response.body).toEqual({ associatedName: 'users', - associatedKey: 'name', + associatedIndex: 'name', resourceName: 'posts', actionName: 'list', fields: { diff --git a/packages/resourcer/src/__tests__/utils.test.ts b/packages/resourcer/src/__tests__/utils.test.ts index ce5592602..0e1c0c0ad 100644 --- a/packages/resourcer/src/__tests__/utils.test.ts +++ b/packages/resourcer/src/__tests__/utils.test.ts @@ -111,7 +111,7 @@ describe('utils', () => { path: '/posts/1', method: 'GET', }); - expect(params).toEqual({ resourceName: 'posts', resourceKey: '1', actionName: 'get' }); + expect(params).toEqual({ resourceName: 'posts', resourceIndex: '1', actionName: 'get' }); }); it('update action', () => { @@ -119,7 +119,7 @@ describe('utils', () => { path: '/posts/1', method: 'PUT', }); - expect(params).toEqual({ resourceName: 'posts', resourceKey: '1', actionName: 'update' }); + expect(params).toEqual({ resourceName: 'posts', resourceIndex: '1', actionName: 'update' }); }); it('update action', () => { @@ -127,7 +127,7 @@ describe('utils', () => { path: '/posts/1', method: 'PATCH', }); - expect(params).toEqual({ resourceName: 'posts', resourceKey: '1', actionName: 'update' }); + expect(params).toEqual({ resourceName: 'posts', resourceIndex: '1', actionName: 'update' }); }); it('delete action', () => { @@ -135,7 +135,7 @@ describe('utils', () => { path: '/posts/1', method: 'delete', }); - expect(params).toEqual({ resourceName: 'posts', resourceKey: '1', actionName: 'destroy' }); + expect(params).toEqual({ resourceName: 'posts', resourceIndex: '1', actionName: 'destroy' }); }); it('delete action', () => { @@ -143,7 +143,7 @@ describe('utils', () => { path: '/posts/1,2,3,4,5,6', method: 'delete', }); - expect(params).toEqual({ resourceName: 'posts', resourceKey: '1,2,3,4,5,6', actionName: 'destroy' }); + expect(params).toEqual({ resourceName: 'posts', resourceIndex: '1,2,3,4,5,6', actionName: 'destroy' }); }); it('index action', () => { @@ -151,7 +151,7 @@ describe('utils', () => { path: '/posts/1/comments', method: 'GET', }); - expect(params).toEqual({ resourceName: 'comments', associatedName: 'posts', associatedKey: '1', actionName: 'list' }); + expect(params).toEqual({ resourceName: 'comments', associatedName: 'posts', associatedIndex: '1', actionName: 'list' }); }); it('store action', () => { @@ -159,7 +159,7 @@ describe('utils', () => { path: '/posts/1/comments', method: 'POST', }); - expect(params).toEqual({ resourceName: 'comments', associatedName: 'posts', associatedKey: '1', actionName: 'create' }); + expect(params).toEqual({ resourceName: 'comments', associatedName: 'posts', associatedIndex: '1', actionName: 'create' }); }); it('get action', () => { @@ -167,7 +167,7 @@ describe('utils', () => { path: '/posts/1/comments/1', method: 'GET', }); - expect(params).toEqual({ resourceName: 'comments', resourceKey: '1', associatedName: 'posts', associatedKey: '1', actionName: 'get' }); + expect(params).toEqual({ resourceName: 'comments', resourceIndex: '1', associatedName: 'posts', associatedIndex: '1', actionName: 'get' }); }); it('update action', () => { @@ -175,7 +175,7 @@ describe('utils', () => { path: '/posts/1/comments/1', method: 'PUT', }); - expect(params).toEqual({ resourceName: 'comments', resourceKey: '1', associatedName: 'posts', associatedKey: '1', actionName: 'update' }); + expect(params).toEqual({ resourceName: 'comments', resourceIndex: '1', associatedName: 'posts', associatedIndex: '1', actionName: 'update' }); }); it('update action', () => { @@ -183,7 +183,7 @@ describe('utils', () => { path: '/posts/1/comments/1', method: 'PATCH', }); - expect(params).toEqual({ resourceName: 'comments', resourceKey: '1', associatedName: 'posts', associatedKey: '1', actionName: 'update' }); + expect(params).toEqual({ resourceName: 'comments', resourceIndex: '1', associatedName: 'posts', associatedIndex: '1', actionName: 'update' }); }); it('get action', () => { @@ -191,7 +191,7 @@ describe('utils', () => { path: '/posts/1/comments/1', method: 'delete', }); - expect(params).toEqual({ resourceName: 'comments', resourceKey: '1', associatedName: 'posts', associatedKey: '1', actionName: 'destroy' }); + expect(params).toEqual({ resourceName: 'comments', resourceIndex: '1', associatedName: 'posts', associatedIndex: '1', actionName: 'destroy' }); }); it('export action', () => { @@ -215,7 +215,7 @@ describe('utils', () => { path: '/posts:export/1', method: 'POST', }); - expect(params).toEqual({ resourceName: 'posts', resourceKey: '1', actionName: 'export' }); + expect(params).toEqual({ resourceName: 'posts', resourceIndex: '1', actionName: 'export' }); }); it('attach action', () => { @@ -225,8 +225,8 @@ describe('utils', () => { }); expect(params).toEqual({ resourceName: 'tags', - resourceKey: '2', - associatedKey: '1', + resourceIndex: '2', + associatedIndex: '1', associatedName: 'posts', actionName: 'attach', }); diff --git a/packages/resourcer/src/action.ts b/packages/resourcer/src/action.ts index 033a201ab..77920c842 100644 --- a/packages/resourcer/src/action.ts +++ b/packages/resourcer/src/action.ts @@ -149,7 +149,7 @@ export interface ActionParams { /** * 资源标识符 */ - resourceKey?: string; + resourceIndex?: string; /** * 资源的从属关系 */ @@ -157,7 +157,7 @@ export interface ActionParams { /** * 从属关系的标识符 */ - associatedKey?: string; + associatedIndex?: string; /** * 从属关系的当前实例 */ diff --git a/packages/resourcer/src/utils.ts b/packages/resourcer/src/utils.ts index 47d15a346..1924d9abf 100644 --- a/packages/resourcer/src/utils.ts +++ b/packages/resourcer/src/utils.ts @@ -26,9 +26,9 @@ export interface ParseOptions { export interface ParsedParams { actionName?: string; resourceName?: string; - resourceKey?: string; + resourceIndex?: string; associatedName?: string; - associatedKey?: string; + associatedIndex?: string; // table: string; // tableKey?: string | number; // relatedTable?: string; @@ -76,18 +76,18 @@ export function parseRequest(request: ParseRequest, options: ParseOptions = {}): post: accessors.create, delete: accessors.delete }, - '/:resourceName/:resourceKey': { + '/:resourceName/:resourceIndex': { get: accessors.get, put: accessors.update, patch: accessors.update, delete: accessors.delete, }, - '/:associatedName/:associatedKey/:resourceName': { + '/:associatedName/:associatedIndex/:resourceName': { get: accessors.list, post: accessors.create, delete: accessors.delete, }, - '/:associatedName/:associatedKey/:resourceName/:resourceKey': { + '/:associatedName/:associatedIndex/:resourceName/:resourceIndex': { get: accessors.get, post: accessors.create, put: accessors.update, @@ -96,7 +96,7 @@ export function parseRequest(request: ParseRequest, options: ParseOptions = {}): }, }, hasOne: { - '/:associatedName/:associatedKey/:resourceName': { + '/:associatedName/:associatedIndex/:resourceName': { get: accessors.get, post: accessors.update, put: accessors.update, @@ -105,12 +105,12 @@ export function parseRequest(request: ParseRequest, options: ParseOptions = {}): }, }, hasMany: { - '/:associatedName/:associatedKey/:resourceName': { + '/:associatedName/:associatedIndex/:resourceName': { get: accessors.list, post: accessors.create, delete: accessors.delete, }, - '/:associatedName/:associatedKey/:resourceName/:resourceKey': { + '/:associatedName/:associatedIndex/:resourceName/:resourceIndex': { get: accessors.get, post: accessors.create, put: accessors.update, @@ -119,20 +119,20 @@ export function parseRequest(request: ParseRequest, options: ParseOptions = {}): }, }, belongsTo: { - '/:associatedName/:associatedKey/:resourceName': { + '/:associatedName/:associatedIndex/:resourceName': { get: accessors.get, delete: accessors.remove, }, - '/:associatedName/:associatedKey/:resourceName/:resourceKey': { + '/:associatedName/:associatedIndex/:resourceName/:resourceIndex': { post: accessors.set, }, }, belongsToMany: { - '/:associatedName/:associatedKey/:resourceName': { + '/:associatedName/:associatedIndex/:resourceName': { get: accessors.list, post: accessors.set, }, - '/:associatedName/:associatedKey/:resourceName/:resourceKey': { + '/:associatedName/:associatedIndex/:resourceName/:resourceIndex': { get: accessors.get, post: accessors.add, put: accessors.update, // Many to Many 的 update 是针对 through diff --git a/packages/test/src/mockServer.ts b/packages/test/src/mockServer.ts index 206e7d88b..6e73bce16 100644 --- a/packages/test/src/mockServer.ts +++ b/packages/test/src/mockServer.ts @@ -15,17 +15,17 @@ interface ActionParams { perPage?: number; values?: any; resourceName?: string; - resourceKey?: string; + resourceIndex?: string; associatedName?: string; - associatedKey?: string; + associatedIndex?: string; [key: string]: any; } interface SortActionParams { resourceName?: string; - resourceKey?: any; + resourceIndex?: any; associatedName?: string; - associatedKey?: any; + associatedIndex?: any; sourceId?: any; targetId?: any; sortField?: string; @@ -58,21 +58,21 @@ export class MockServer extends Application { get(target, method: string, receiver) { return (params: ActionParams = {}) => { const { - associatedKey, - resourceKey, + associatedIndex, + resourceIndex, values = {}, file, ...restParams } = params; let url = prefix; if (keys.length > 1) { - url = `/${keys[0]}/${associatedKey}/${keys[1]}` + url = `/${keys[0]}/${associatedIndex}/${keys[1]}` } else { url = `/${name}`; } url += `:${method as string}`; - if (resourceKey) { - url += `/${resourceKey}`; + if (resourceIndex) { + url += `/${resourceIndex}`; } switch (method) {