Skip to content

armgit / papra

Public Imported from GitHub
0

The minimalistic document archiving platform.

feat(api-keys): sync permissions from server constants (#1146)

Corentin Thomasset69c9ce3d2890
diff --git a/apps/papra-client/src/modules/api-keys/api-keys.constants.test.ts b/apps/papra-client/src/modules/api-keys/api-keys.constants.test.ts
new file mode 100644
index 00000000..7fe3103a
--- /dev/null
+++ b/apps/papra-client/src/modules/api-keys/api-keys.constants.test.ts
@@ -0,0 +1,16 @@
+import { API_KEY_PERMISSIONS as SERVER_PERMISSIONS } from '@papra/app-server/apiKeys/constants';
+import { describe, expect, test } from 'vitest';
+import { API_KEY_PERMISSIONS } from './api-keys.constants';
+
+describe('api-keys.constants', () => {
+ describe('api keys permissions', () => {
+ test('all server side permissions should be defined in client', () => {
+ expect(
+ API_KEY_PERMISSIONS.flatMap(section => section.permissions).toSorted(),
+ ).to.eql(
+ Object.values(SERVER_PERMISSIONS).flatMap(permissions => Object.values(permissions)).toSorted(),
+ 'Some server side permissions are missing in client side API_KEY_PERMISSIONS',
+ );
+ });
+ });
+});
diff --git a/apps/papra-client/src/modules/api-keys/api-keys.constants.ts b/apps/papra-client/src/modules/api-keys/api-keys.constants.ts
index 7243e879..1c822427 100644
--- a/apps/papra-client/src/modules/api-keys/api-keys.constants.ts
+++ b/apps/papra-client/src/modules/api-keys/api-keys.constants.ts
@@ -1,46 +1,10 @@
-// export const API_KEY_PERMISSIONS = {
-// documents: {
-// create: 'documents:create',
-// },
-// } as const;
+import { API_KEY_PERMISSIONS_VALUES, API_KEY_PERMISSIONS as PERMISSIONS } from '@papra/app-server/apiKeys/constants';
export const API_KEY_PERMISSIONS = [
- {
- section: 'organizations',
- permissions: [
- 'organizations:create',
- 'organizations:read',
- 'organizations:update',
- 'organizations:delete',
- ],
- },
- {
- section: 'documents',
- permissions: [
- 'documents:create',
- 'documents:read',
- 'documents:update',
- 'documents:delete',
- ],
- },
- {
- section: 'tags',
- permissions: [
- 'tags:create',
- 'tags:read',
- 'tags:update',
- 'tags:delete',
- ],
- },
- {
- section: 'custom-properties',
- permissions: [
- 'custom-properties:create',
- 'custom-properties:read',
- 'custom-properties:update',
- 'custom-properties:delete',
- ],
- },
+ { section: 'organizations', permissions: Object.values(PERMISSIONS.ORGANIZATIONS) },
+ { section: 'documents', permissions: Object.values(PERMISSIONS.DOCUMENTS) },
+ { section: 'tags', permissions: Object.values(PERMISSIONS.TAGS) },
+ { section: 'custom-properties', permissions: Object.values(PERMISSIONS.CUSTOM_PROPERTIES) },
] as const;
-export const API_KEY_PERMISSIONS_LIST = API_KEY_PERMISSIONS.flatMap(permission => permission.permissions);
+export { API_KEY_PERMISSIONS_VALUES };
diff --git a/apps/papra-client/src/modules/api-keys/pages/create-api-key.page.tsx b/apps/papra-client/src/modules/api-keys/pages/create-api-key.page.tsx
index edde8cc7..c233836f 100644
--- a/apps/papra-client/src/modules/api-keys/pages/create-api-key.page.tsx
+++ b/apps/papra-client/src/modules/api-keys/pages/create-api-key.page.tsx
@@ -10,7 +10,7 @@ import { CopyButton } from '@/modules/shared/utils/copy';
import { Button } from '@/modules/ui/components/button';
import { createToast } from '@/modules/ui/components/sonner';
import { TextField, TextFieldLabel, TextFieldRoot } from '@/modules/ui/components/textfield';
-import { API_KEY_PERMISSIONS_LIST } from '../api-keys.constants';
+import { API_KEY_PERMISSIONS_VALUES } from '../api-keys.constants';
import { createApiKey } from '../api-keys.services';
import { ApiKeyPermissionsPicker } from '../components/api-key-permissions-picker.component';
@@ -42,13 +42,13 @@ export const CreateApiKeyPage: Component = () => {
v.nonEmpty(t('api-keys.create.form.name.required')),
),
permissions: v.pipe(
- v.array(v.picklist(API_KEY_PERMISSIONS_LIST as string[])),
+ v.array(v.picklist(API_KEY_PERMISSIONS_VALUES as string[])),
v.nonEmpty(t('api-keys.create.form.permissions.required')),
),
}),
initialValues: {
name: '',
- permissions: API_KEY_PERMISSIONS_LIST,
+ permissions: API_KEY_PERMISSIONS_VALUES,
},
});
diff --git a/apps/papra-server/package.json b/apps/papra-server/package.json
index cfa36578..da9cb91b 100644
--- a/apps/papra-server/package.json
+++ b/apps/papra-server/package.json
@@ -9,7 +9,8 @@
"keywords": [],
"exports": {
"./organizations/constants": "./src/modules/organizations/organizations.constants.ts",
- "./customProperties/constants": "./src/modules/custom-properties/custom-properties.constants.ts"
+ "./customProperties/constants": "./src/modules/custom-properties/custom-properties.constants.ts",
+ "./apiKeys/constants": "./src/modules/api-keys/api-keys.constants.ts"
},
"scripts": {
"dev": "tsx watch --env-file-if-exists=.env src/index.ts | crowlog-pretty",