Skip to content

armgit / papra

Public Imported from GitHub
0

The minimalistic document archiving platform.

refactor(shared): extract id regex into separate module (#1143)

Corentin Thomasseta4bf48b4f3e9
diff --git a/apps/papra-server/src/modules/api-keys/api-keys.constants.ts b/apps/papra-server/src/modules/api-keys/api-keys.constants.ts
index 720943e1..25e55103 100644
--- a/apps/papra-server/src/modules/api-keys/api-keys.constants.ts
+++ b/apps/papra-server/src/modules/api-keys/api-keys.constants.ts
@@ -1,4 +1,4 @@
-import { createPrefixedIdRegex } from '../shared/random/ids';
+import { createPrefixedIdRegex } from '../shared/random/ids.constants.models';
export const API_KEY_ID_PREFIX = 'ak';
export const API_KEY_ID_REGEX = createPrefixedIdRegex({ prefix: API_KEY_ID_PREFIX });
diff --git a/apps/papra-server/src/modules/custom-properties/custom-properties.constants.ts b/apps/papra-server/src/modules/custom-properties/custom-properties.constants.ts
index 5a1bf18e..173e870b 100644
--- a/apps/papra-server/src/modules/custom-properties/custom-properties.constants.ts
+++ b/apps/papra-server/src/modules/custom-properties/custom-properties.constants.ts
@@ -1,4 +1,4 @@
-import { createPrefixedIdRegex } from '../shared/random/ids';
+import { createPrefixedIdRegex } from '../shared/random/ids.constants.models';
export const CUSTOM_PROPERTY_DEFINITION_ID_PREFIX = 'cpd';
export const CUSTOM_PROPERTY_DEFINITION_ID_REGEX = createPrefixedIdRegex({ prefix: CUSTOM_PROPERTY_DEFINITION_ID_PREFIX });
diff --git a/apps/papra-server/src/modules/custom-properties/options/custom-properties-options.constants.ts b/apps/papra-server/src/modules/custom-properties/options/custom-properties-options.constants.ts
index 48cc1bd0..6d05ee0e 100644
--- a/apps/papra-server/src/modules/custom-properties/options/custom-properties-options.constants.ts
+++ b/apps/papra-server/src/modules/custom-properties/options/custom-properties-options.constants.ts
@@ -1,4 +1,4 @@
-import { createPrefixedIdRegex } from '../../shared/random/ids';
+import { createPrefixedIdRegex } from '../../shared/random/ids.constants.models';
export const CUSTOM_PROPERTY_SELECT_OPTION_ID_PREFIX = 'cpso';
export const CUSTOM_PROPERTY_SELECT_OPTION_ID_REGEX = createPrefixedIdRegex({ prefix: CUSTOM_PROPERTY_SELECT_OPTION_ID_PREFIX });
diff --git a/apps/papra-server/src/modules/document-share-links/document-share-links.constants.ts b/apps/papra-server/src/modules/document-share-links/document-share-links.constants.ts
index 9777bb4c..24f8df5f 100644
--- a/apps/papra-server/src/modules/document-share-links/document-share-links.constants.ts
+++ b/apps/papra-server/src/modules/document-share-links/document-share-links.constants.ts
@@ -1,4 +1,4 @@
-import { createPrefixedIdRegex } from '../shared/random/ids';
+import { createPrefixedIdRegex } from '../shared/random/ids.constants.models';
export const SHARE_LINK_ID_PREFIX = 'dsl';
export const SHARE_LINK_ID_REGEX = createPrefixedIdRegex({ prefix: SHARE_LINK_ID_PREFIX });
diff --git a/apps/papra-server/src/modules/document-views/document-views.constants.ts b/apps/papra-server/src/modules/document-views/document-views.constants.ts
index 168bcecd..799a8d4f 100644
--- a/apps/papra-server/src/modules/document-views/document-views.constants.ts
+++ b/apps/papra-server/src/modules/document-views/document-views.constants.ts
@@ -1,4 +1,4 @@
-import { createPrefixedIdRegex } from '../shared/random/ids';
+import { createPrefixedIdRegex } from '../shared/random/ids.constants.models';
export const DOCUMENT_VIEW_ID_PREFIX = 'dv';
export const DOCUMENT_VIEW_ID_REGEX = createPrefixedIdRegex({ prefix: DOCUMENT_VIEW_ID_PREFIX });
diff --git a/apps/papra-server/src/modules/documents/documents.constants.ts b/apps/papra-server/src/modules/documents/documents.constants.ts
index 72b63cfc..10138973 100644
--- a/apps/papra-server/src/modules/documents/documents.constants.ts
+++ b/apps/papra-server/src/modules/documents/documents.constants.ts
@@ -1,4 +1,4 @@
-import { createPrefixedIdRegex } from '../shared/random/ids';
+import { createPrefixedIdRegex } from '../shared/random/ids.constants.models';
export const DOCUMENT_ID_PREFIX = 'doc';
export const DOCUMENT_ID_REGEX = createPrefixedIdRegex({ prefix: DOCUMENT_ID_PREFIX });
diff --git a/apps/papra-server/src/modules/intake-emails/intake-emails.constants.ts b/apps/papra-server/src/modules/intake-emails/intake-emails.constants.ts
index ace3d8ab..27ee291f 100644
--- a/apps/papra-server/src/modules/intake-emails/intake-emails.constants.ts
+++ b/apps/papra-server/src/modules/intake-emails/intake-emails.constants.ts
@@ -1,4 +1,4 @@
-import { createPrefixedIdRegex } from '../shared/random/ids';
+import { createPrefixedIdRegex } from '../shared/random/ids.constants.models';
export const INTAKE_EMAIL_ID_PREFIX = 'ie';
export const INTAKE_EMAIL_ID_REGEX = createPrefixedIdRegex({ prefix: INTAKE_EMAIL_ID_PREFIX });
diff --git a/apps/papra-server/src/modules/organizations/organizations.constants.ts b/apps/papra-server/src/modules/organizations/organizations.constants.ts
index eee59526..8a30c38a 100644
--- a/apps/papra-server/src/modules/organizations/organizations.constants.ts
+++ b/apps/papra-server/src/modules/organizations/organizations.constants.ts
@@ -1,4 +1,4 @@
-import { createPrefixedIdRegex } from '../shared/random/ids';
+import { createPrefixedIdRegex } from '../shared/random/ids.constants.models';
export const ORGANIZATION_ID_PREFIX = 'org';
export const ORGANIZATION_ID_REGEX = createPrefixedIdRegex({ prefix: ORGANIZATION_ID_PREFIX });
diff --git a/apps/papra-server/src/modules/shared/random/ids.constants.models.test.ts b/apps/papra-server/src/modules/shared/random/ids.constants.models.test.ts
new file mode 100644
index 00000000..9c4de23e
--- /dev/null
+++ b/apps/papra-server/src/modules/shared/random/ids.constants.models.test.ts
@@ -0,0 +1,12 @@
+import { describe, expect, test } from 'vitest';
+import { createPrefixedIdRegex } from './ids.constants.models';
+
+describe('database models', () => {
+ describe('createPrefixedIdRegex', () => {
+ test('build a regex for prefixed id validation', () => {
+ const regex = createPrefixedIdRegex({ prefix: 'tag' });
+
+ expect(regex.toString()).to.eql('/^tag_[a-z0-9]{24}$/');
+ });
+ });
+});
diff --git a/apps/papra-server/src/modules/shared/random/ids.constants.models.ts b/apps/papra-server/src/modules/shared/random/ids.constants.models.ts
new file mode 100644
index 00000000..d146a7b8
--- /dev/null
+++ b/apps/papra-server/src/modules/shared/random/ids.constants.models.ts
@@ -0,0 +1,6 @@
+// constant models: used to build constants, so the import chain must stay pure without side effects
+import { ID_RANDOM_PART_LENGTH } from '../../app/database/database.constants';
+
+export function createPrefixedIdRegex({ prefix }: { prefix: string }) {
+ return new RegExp(`^${prefix}_[a-z0-9]{${ID_RANDOM_PART_LENGTH}}$`);
+}
diff --git a/apps/papra-server/src/modules/shared/random/ids.test.ts b/apps/papra-server/src/modules/shared/random/ids.test.ts
index a1f8d92d..d625c2d7 100644
--- a/apps/papra-server/src/modules/shared/random/ids.test.ts
+++ b/apps/papra-server/src/modules/shared/random/ids.test.ts
@@ -1,16 +1,8 @@
import { describe, expect, test } from 'vitest';
import { ID_RANDOM_PART_LENGTH } from '../../app/database/database.constants';
-import { createDeterministicIdGenerator, createPrefixedIdRegex, generateId } from './ids';
+import { createDeterministicIdGenerator, generateId } from './ids';
describe('database models', () => {
- describe('createPrefixedIdRegex', () => {
- test('build a regex for prefixed id validation', () => {
- const regex = createPrefixedIdRegex({ prefix: 'tag' });
-
- expect(regex.toString()).to.eql('/^tag_[a-z0-9]{24}$/');
- });
- });
-
describe('createDeterministicIdGenerator', () => {
describe('provide a id generator that increments ids deterministically, mainly used for testing', () => {
test('the id generator produces sequential ids with the given prefix', () => {
diff --git a/apps/papra-server/src/modules/shared/random/ids.ts b/apps/papra-server/src/modules/shared/random/ids.ts
index 8621e551..f12352af 100644
--- a/apps/papra-server/src/modules/shared/random/ids.ts
+++ b/apps/papra-server/src/modules/shared/random/ids.ts
@@ -10,10 +10,6 @@ export function generateId({ prefix, getRandomPart = createId }: { prefix?: stri
return prefix !== undefined ? `${prefix}_${id}` : id;
}
-export function createPrefixedIdRegex({ prefix }: { prefix: string }) {
- return new RegExp(`^${prefix}_[a-z0-9]{${ID_RANDOM_PART_LENGTH}}$`);
-}
-
export function createDeterministicIdGenerator({ prefix }: { prefix?: string } = {}) {
let counter = 1;
diff --git a/apps/papra-server/src/modules/tagging-rules/tagging-rules.constants.ts b/apps/papra-server/src/modules/tagging-rules/tagging-rules.constants.ts
index b3516559..c800168c 100644
--- a/apps/papra-server/src/modules/tagging-rules/tagging-rules.constants.ts
+++ b/apps/papra-server/src/modules/tagging-rules/tagging-rules.constants.ts
@@ -1,5 +1,5 @@
import type { DbSelectableDocument } from '../documents/documents.types';
-import { createPrefixedIdRegex } from '../shared/random/ids';
+import { createPrefixedIdRegex } from '../shared/random/ids.constants.models';
export const TAGGING_RULE_ID_PREFIX = 'tr';
export const TAGGING_RULE_ID_REGEX = createPrefixedIdRegex({ prefix: TAGGING_RULE_ID_PREFIX });
diff --git a/apps/papra-server/src/modules/tags/tags.constants.ts b/apps/papra-server/src/modules/tags/tags.constants.ts
index 3651a121..094d9145 100644
--- a/apps/papra-server/src/modules/tags/tags.constants.ts
+++ b/apps/papra-server/src/modules/tags/tags.constants.ts
@@ -1,4 +1,4 @@
-import { createPrefixedIdRegex } from '../shared/random/ids';
+import { createPrefixedIdRegex } from '../shared/random/ids.constants.models';
export const TagColorRegex = /^#[0-9A-F]{6}$/;
diff --git a/apps/papra-server/src/modules/users/users.constants.ts b/apps/papra-server/src/modules/users/users.constants.ts
index 429af6e9..01254035 100644
--- a/apps/papra-server/src/modules/users/users.constants.ts
+++ b/apps/papra-server/src/modules/users/users.constants.ts
@@ -1,4 +1,4 @@
-import { createPrefixedIdRegex } from '../shared/random/ids';
+import { createPrefixedIdRegex } from '../shared/random/ids.constants.models';
export const USER_ID_PREFIX = 'usr';
export const USER_ID_REGEX = createPrefixedIdRegex({ prefix: USER_ID_PREFIX });
diff --git a/apps/papra-server/src/modules/webhooks/webhooks.constants.ts b/apps/papra-server/src/modules/webhooks/webhooks.constants.ts
index 2427482c..ec6f4019 100644
--- a/apps/papra-server/src/modules/webhooks/webhooks.constants.ts
+++ b/apps/papra-server/src/modules/webhooks/webhooks.constants.ts
@@ -1,4 +1,4 @@
-import { createPrefixedIdRegex } from '../shared/random/ids';
+import { createPrefixedIdRegex } from '../shared/random/ids.constants.models';
export const WEBHOOK_ID_PREFIX = 'wbh';
export const WEBHOOK_ID_REGEX = createPrefixedIdRegex({ prefix: WEBHOOK_ID_PREFIX });