feat: manually mirror opencoze's code from bytedance
Change-Id: I09a73aadda978ad9511264a756b2ce51f5761adf
This commit is contained in:
69
frontend/packages/common/auth-adapter/README.md
Normal file
69
frontend/packages/common/auth-adapter/README.md
Normal file
@@ -0,0 +1,69 @@
|
||||
# @coze-common/auth-adapter
|
||||
|
||||
统一的权限控制逻辑
|
||||
|
||||
## Overview
|
||||
|
||||
This package is part of the Coze Studio monorepo and provides utilities functionality. It serves as a core component in the Coze ecosystem.
|
||||
|
||||
## Getting Started
|
||||
|
||||
### Installation
|
||||
|
||||
Add this package to your `package.json`:
|
||||
|
||||
```json
|
||||
{
|
||||
"dependencies": {
|
||||
"@coze-common/auth-adapter": "workspace:*"
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
Then run:
|
||||
|
||||
```bash
|
||||
rush update
|
||||
```
|
||||
|
||||
### Usage
|
||||
|
||||
```typescript
|
||||
import { /* exported functions/components */ } from '@coze-common/auth-adapter';
|
||||
|
||||
// Example usage
|
||||
// TODO: Add specific usage examples
|
||||
```
|
||||
|
||||
## Features
|
||||
|
||||
- Core functionality for Coze Studio
|
||||
- TypeScript support
|
||||
- Modern ES modules
|
||||
|
||||
## API Reference
|
||||
|
||||
### Exports
|
||||
|
||||
- `useInitSpaceRole`
|
||||
- `useInitProjectRole`
|
||||
|
||||
|
||||
For detailed API documentation, please refer to the TypeScript definitions.
|
||||
|
||||
## Development
|
||||
|
||||
This package is built with:
|
||||
|
||||
- TypeScript
|
||||
- React
|
||||
- Vitest for testing
|
||||
- ESLint for code quality
|
||||
|
||||
## Contributing
|
||||
|
||||
This package is part of the Coze Studio monorepo. Please follow the monorepo contribution guidelines.
|
||||
|
||||
## License
|
||||
|
||||
Apache-2.0
|
||||
@@ -0,0 +1,93 @@
|
||||
/*
|
||||
* Copyright 2025 coze-dev Authors
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
import { describe, it, expect, vi, beforeEach } from 'vitest';
|
||||
import { renderHook } from '@testing-library/react-hooks';
|
||||
import { ProjectRoleType, useProjectAuthStore } from '@coze-common/auth';
|
||||
|
||||
import { useInitProjectRole } from '../../src/project/use-init-project-role';
|
||||
|
||||
// Mock the auth store
|
||||
vi.mock('@coze-common/auth', () => ({
|
||||
useProjectAuthStore: vi.fn(),
|
||||
ProjectRoleType: {
|
||||
Owner: 'owner',
|
||||
},
|
||||
}));
|
||||
|
||||
describe('useInitProjectRole', () => {
|
||||
const mockIsReady = {
|
||||
'project-1': true,
|
||||
'project-2': true,
|
||||
};
|
||||
const mockSetIsReady = vi.fn();
|
||||
const mockSetRoles = vi.fn();
|
||||
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
(useProjectAuthStore as any).mockImplementation((selector: any) =>
|
||||
selector({
|
||||
setIsReady: mockSetIsReady,
|
||||
setRoles: mockSetRoles,
|
||||
isReady: mockIsReady,
|
||||
}),
|
||||
);
|
||||
});
|
||||
|
||||
it('should initialize project role and set ready state', () => {
|
||||
const spaceId = 'space-1';
|
||||
const projectId = 'project-1';
|
||||
const { result } = renderHook(() => useInitProjectRole(spaceId, projectId));
|
||||
|
||||
console.log('result', result.current);
|
||||
console.log('mockIsReady', mockIsReady);
|
||||
|
||||
// 验证是否调用了 setRoles 和 setIsReady
|
||||
expect(mockSetRoles).toHaveBeenCalledWith(projectId, [
|
||||
ProjectRoleType.Owner,
|
||||
]);
|
||||
expect(mockSetIsReady).toHaveBeenCalledWith(projectId, true);
|
||||
|
||||
// 验证返回值
|
||||
expect(result.current).toBe(true);
|
||||
});
|
||||
|
||||
it('should handle multiple project IDs correctly', () => {
|
||||
const testSpaceId = 'space-1';
|
||||
const projectId1 = 'project-1';
|
||||
const projectId2 = 'project-2';
|
||||
|
||||
const { rerender } = renderHook(
|
||||
({ spaceId, projectId }) => useInitProjectRole(spaceId, projectId),
|
||||
{
|
||||
initialProps: { spaceId: testSpaceId, projectId: projectId1 },
|
||||
},
|
||||
);
|
||||
|
||||
expect(mockSetRoles).toHaveBeenCalledWith(projectId1, [
|
||||
ProjectRoleType.Owner,
|
||||
]);
|
||||
expect(mockSetIsReady).toHaveBeenCalledWith(projectId1, true);
|
||||
|
||||
// 重新渲染,使用新的 projectId
|
||||
rerender({ spaceId: testSpaceId, projectId: projectId2 });
|
||||
|
||||
expect(mockSetRoles).toHaveBeenCalledWith(projectId2, [
|
||||
ProjectRoleType.Owner,
|
||||
]);
|
||||
expect(mockSetIsReady).toHaveBeenCalledWith(projectId2, true);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,77 @@
|
||||
/*
|
||||
* Copyright 2025 coze-dev Authors
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
import { describe, it, expect, vi, beforeEach } from 'vitest';
|
||||
import { renderHook } from '@testing-library/react-hooks';
|
||||
import { useSpaceAuthStore } from '@coze-common/auth';
|
||||
import { SpaceRoleType } from '@coze-arch/idl/developer_api';
|
||||
|
||||
import { useInitSpaceRole } from '../../src/space/use-init-space-role';
|
||||
|
||||
// Mock the auth store
|
||||
vi.mock('@coze-common/auth', () => ({
|
||||
useSpaceAuthStore: vi.fn(),
|
||||
}));
|
||||
|
||||
describe('useInitSpaceRole', () => {
|
||||
const mockSetIsReady = vi.fn();
|
||||
const mockSetRoles = vi.fn();
|
||||
const mockIsReady = {
|
||||
'space-1': true,
|
||||
'space-2': true,
|
||||
};
|
||||
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
(useSpaceAuthStore as any).mockImplementation((selector: any) =>
|
||||
selector({
|
||||
setIsReady: mockSetIsReady,
|
||||
setRoles: mockSetRoles,
|
||||
isReady: mockIsReady,
|
||||
}),
|
||||
);
|
||||
});
|
||||
|
||||
it('should initialize space role and set ready state', () => {
|
||||
const spaceId = 'space-1';
|
||||
const { result } = renderHook(() => useInitSpaceRole(spaceId));
|
||||
|
||||
// 验证是否调用了 setRoles 和 setIsReady
|
||||
expect(mockSetRoles).toHaveBeenCalledWith(spaceId, [SpaceRoleType.Owner]);
|
||||
expect(mockSetIsReady).toHaveBeenCalledWith(spaceId, true);
|
||||
|
||||
// 验证返回值
|
||||
expect(result.current).toBe(true);
|
||||
});
|
||||
|
||||
it('should handle multiple space IDs correctly', () => {
|
||||
const spaceId1 = 'space-1';
|
||||
const spaceId2 = 'space-2';
|
||||
|
||||
const { rerender } = renderHook(({ id }) => useInitSpaceRole(id), {
|
||||
initialProps: { id: spaceId1 },
|
||||
});
|
||||
|
||||
expect(mockSetRoles).toHaveBeenCalledWith(spaceId1, [SpaceRoleType.Owner]);
|
||||
expect(mockSetIsReady).toHaveBeenCalledWith(spaceId1, true);
|
||||
|
||||
// 重新渲染,使用新的 spaceId
|
||||
rerender({ id: spaceId2 });
|
||||
|
||||
expect(mockSetRoles).toHaveBeenCalledWith(spaceId2, [SpaceRoleType.Owner]);
|
||||
expect(mockSetIsReady).toHaveBeenCalledWith(spaceId2, true);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,12 @@
|
||||
{
|
||||
"operationSettings": [
|
||||
{
|
||||
"operationName": "test:cov",
|
||||
"outputFolderNames": ["coverage"]
|
||||
},
|
||||
{
|
||||
"operationName": "ts-check",
|
||||
"outputFolderNames": ["./dist"]
|
||||
}
|
||||
]
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
{
|
||||
"codecov": {
|
||||
"coverage": 0,
|
||||
"incrementCoverage": 0
|
||||
}
|
||||
}
|
||||
7
frontend/packages/common/auth-adapter/eslint.config.js
Normal file
7
frontend/packages/common/auth-adapter/eslint.config.js
Normal file
@@ -0,0 +1,7 @@
|
||||
const { defineConfig } = require('@coze-arch/eslint-config');
|
||||
|
||||
module.exports = defineConfig({
|
||||
packageRoot: __dirname,
|
||||
preset: 'node',
|
||||
rules: {},
|
||||
});
|
||||
37
frontend/packages/common/auth-adapter/package.json
Normal file
37
frontend/packages/common/auth-adapter/package.json
Normal file
@@ -0,0 +1,37 @@
|
||||
{
|
||||
"name": "@coze-common/auth-adapter",
|
||||
"version": "0.0.1",
|
||||
"description": "统一的权限控制逻辑",
|
||||
"license": "Apache-2.0",
|
||||
"author": "sunzhiyuan.evan@bytedance.com",
|
||||
"maintainers": [],
|
||||
"main": "src/index.ts",
|
||||
"scripts": {
|
||||
"build": "exit 0",
|
||||
"lint": "eslint ./ --cache",
|
||||
"test": "vitest --run --passWithNoTests",
|
||||
"test:cov": "npm run test -- --coverage"
|
||||
},
|
||||
"dependencies": {
|
||||
"@coze-arch/idl": "workspace:*",
|
||||
"@coze-common/auth": "workspace:*",
|
||||
"react": "~18.2.0",
|
||||
"zustand": "^4.4.7"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@coze-arch/bot-typings": "workspace:*",
|
||||
"@coze-arch/eslint-config": "workspace:*",
|
||||
"@coze-arch/ts-config": "workspace:*",
|
||||
"@coze-arch/vitest-config": "workspace:*",
|
||||
"@testing-library/jest-dom": "^6.1.5",
|
||||
"@testing-library/react": "^14.1.2",
|
||||
"@testing-library/react-hooks": "^8.0.1",
|
||||
"@types/node": "^18",
|
||||
"@types/react": "18.2.37",
|
||||
"@vitest/coverage-v8": "~3.0.5",
|
||||
"react-dom": "~18.2.0",
|
||||
"sucrase": "^3.32.0",
|
||||
"vitest": "~3.0.5"
|
||||
}
|
||||
}
|
||||
|
||||
19
frontend/packages/common/auth-adapter/src/index.ts
Normal file
19
frontend/packages/common/auth-adapter/src/index.ts
Normal file
@@ -0,0 +1,19 @@
|
||||
/*
|
||||
* Copyright 2025 coze-dev Authors
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
export { useInitSpaceRole } from './space/use-init-space-role';
|
||||
|
||||
export { useInitProjectRole } from './project/use-init-project-role';
|
||||
@@ -0,0 +1,37 @@
|
||||
/*
|
||||
* Copyright 2025 coze-dev Authors
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
import { useEffect } from 'react';
|
||||
|
||||
import { useShallow } from 'zustand/react/shallow';
|
||||
import { useProjectAuthStore, ProjectRoleType } from '@coze-common/auth';
|
||||
|
||||
export function useInitProjectRole(spaceId: string, projectId: string) {
|
||||
const { setIsReady, setRoles, isReady } = useProjectAuthStore(
|
||||
useShallow(store => ({
|
||||
isReady: store.isReady[projectId],
|
||||
setIsReady: store.setIsReady,
|
||||
setRoles: store.setRoles,
|
||||
})),
|
||||
);
|
||||
|
||||
useEffect(() => {
|
||||
setRoles(projectId, [ProjectRoleType.Owner]);
|
||||
setIsReady(projectId, true);
|
||||
}, [projectId]);
|
||||
|
||||
return isReady; // 是否初始化完成。
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
/*
|
||||
* Copyright 2025 coze-dev Authors
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
/**
|
||||
* @file 社区版暂时不提供权限控制功能,本文件中导出的方法用于未来拓展使用。
|
||||
*/
|
||||
|
||||
import { useEffect } from 'react';
|
||||
|
||||
import { useShallow } from 'zustand/react/shallow';
|
||||
import { SpaceRoleType } from '@coze-arch/idl/developer_api';
|
||||
import { useSpaceAuthStore } from '@coze-common/auth';
|
||||
|
||||
export function useInitSpaceRole(spaceId: string) {
|
||||
const { setIsReady, setRoles, isReady } = useSpaceAuthStore(
|
||||
useShallow(store => ({
|
||||
setIsReady: store.setIsReady,
|
||||
setRoles: store.setRoles,
|
||||
isReady: store.isReady[spaceId],
|
||||
})),
|
||||
);
|
||||
|
||||
useEffect(() => {
|
||||
setRoles(spaceId, [SpaceRoleType.Owner]);
|
||||
setIsReady(spaceId, true);
|
||||
}, [spaceId]);
|
||||
|
||||
return isReady;
|
||||
}
|
||||
19
frontend/packages/common/auth-adapter/src/typings.d.ts
vendored
Normal file
19
frontend/packages/common/auth-adapter/src/typings.d.ts
vendored
Normal file
@@ -0,0 +1,19 @@
|
||||
/*
|
||||
* Copyright 2025 coze-dev Authors
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
declare const IS_DEV_MODE: boolean;
|
||||
|
||||
/// <reference types='@coze-arch/bot-typings' />
|
||||
31
frontend/packages/common/auth-adapter/tsconfig.build.json
Normal file
31
frontend/packages/common/auth-adapter/tsconfig.build.json
Normal file
@@ -0,0 +1,31 @@
|
||||
{
|
||||
"$schema": "https://json.schemastore.org/tsconfig",
|
||||
"extends": "@coze-arch/ts-config/tsconfig.node.json",
|
||||
"compilerOptions": {
|
||||
"types": [],
|
||||
"rootDir": "./src",
|
||||
"outDir": "./dist",
|
||||
"tsBuildInfoFile": "./dist/tsconfig.build.tsbuildinfo"
|
||||
},
|
||||
"include": ["src"],
|
||||
"references": [
|
||||
{
|
||||
"path": "../../arch/bot-typings/tsconfig.build.json"
|
||||
},
|
||||
{
|
||||
"path": "../../arch/idl/tsconfig.build.json"
|
||||
},
|
||||
{
|
||||
"path": "../auth/tsconfig.build.json"
|
||||
},
|
||||
{
|
||||
"path": "../../../config/eslint-config/tsconfig.build.json"
|
||||
},
|
||||
{
|
||||
"path": "../../../config/ts-config/tsconfig.build.json"
|
||||
},
|
||||
{
|
||||
"path": "../../../config/vitest-config/tsconfig.build.json"
|
||||
}
|
||||
]
|
||||
}
|
||||
15
frontend/packages/common/auth-adapter/tsconfig.json
Normal file
15
frontend/packages/common/auth-adapter/tsconfig.json
Normal file
@@ -0,0 +1,15 @@
|
||||
{
|
||||
"$schema": "https://json.schemastore.org/tsconfig",
|
||||
"compilerOptions": {
|
||||
"composite": true
|
||||
},
|
||||
"references": [
|
||||
{
|
||||
"path": "./tsconfig.build.json"
|
||||
},
|
||||
{
|
||||
"path": "./tsconfig.misc.json"
|
||||
}
|
||||
],
|
||||
"exclude": ["**/*"]
|
||||
}
|
||||
16
frontend/packages/common/auth-adapter/tsconfig.misc.json
Normal file
16
frontend/packages/common/auth-adapter/tsconfig.misc.json
Normal file
@@ -0,0 +1,16 @@
|
||||
{
|
||||
"extends": "@coze-arch/ts-config/tsconfig.node.json",
|
||||
"$schema": "https://json.schemastore.org/tsconfig",
|
||||
"include": ["__tests__", "vitest.config.ts"],
|
||||
"exclude": ["./dist"],
|
||||
"references": [
|
||||
{
|
||||
"path": "./tsconfig.build.json"
|
||||
}
|
||||
],
|
||||
"compilerOptions": {
|
||||
"rootDir": "./",
|
||||
"outDir": "./dist",
|
||||
"types": ["vitest/globals"]
|
||||
}
|
||||
}
|
||||
22
frontend/packages/common/auth-adapter/vitest.config.ts
Normal file
22
frontend/packages/common/auth-adapter/vitest.config.ts
Normal file
@@ -0,0 +1,22 @@
|
||||
/*
|
||||
* Copyright 2025 coze-dev Authors
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
import { defineConfig } from '@coze-arch/vitest-config';
|
||||
|
||||
export default defineConfig({
|
||||
dirname: __dirname,
|
||||
preset: 'web',
|
||||
});
|
||||
Reference in New Issue
Block a user