Skip to content

armgit / papra

Public Imported from GitHub
0

The minimalistic document archiving platform.

feat(std): create standard library package (#1277)

Corentin Thomassetd8dfa95ce939
diff --git a/apps/papra-client/package.json b/apps/papra-client/package.json
index 2fc62f98..b32b29a6 100644
--- a/apps/papra-client/package.json
+++ b/apps/papra-client/package.json
@@ -54,6 +54,7 @@
"@iconify-json/tabler": "catalog:",
"@papra/app-server": "workspace:*",
"@papra/lecture": "workspace:*",
+ "@papra/std": "workspace:*",
"@types/node": "catalog:",
"tsx": "catalog:",
"typescript": "catalog:",
diff --git a/apps/papra-client/src/modules/demo/demo-api-mock.ts b/apps/papra-client/src/modules/demo/demo-api-mock.ts
index d5d20be4..bb217ae0 100644
--- a/apps/papra-client/src/modules/demo/demo-api-mock.ts
+++ b/apps/papra-client/src/modules/demo/demo-api-mock.ts
@@ -24,6 +24,7 @@ import {
import { findMany, getValues } from './demo.storage.models';
import { generatePropertyKey, searchDemoDocuments } from './search/demo.search.services';
import { demoUser } from './seed/users.fixtures';
+import { stringify } from '@papra/std';
function assert(
condition: unknown,
@@ -82,7 +83,7 @@ function hydratePropertyValue({
}
if (definition.type === 'select') {
- const optionId = String(value);
+ const optionId = stringify(value);
const option = definition.options.find((o) => o.id === optionId || o.key === optionId);
if (!option) {
@@ -93,7 +94,7 @@ function hydratePropertyValue({
}
if (definition.type === 'multi_select') {
- const ids = Array.isArray(value) ? value.map(String) : [String(value)];
+ const ids = Array.isArray(value) ? value.map(String) : [stringify(value)];
return ids
.map((id) => {
diff --git a/apps/papra-client/src/modules/demo/search/demo.search.services.ts b/apps/papra-client/src/modules/demo/search/demo.search.services.ts
index dc938ab4..f1d0c141 100644
--- a/apps/papra-client/src/modules/demo/search/demo.search.services.ts
+++ b/apps/papra-client/src/modules/demo/search/demo.search.services.ts
@@ -8,6 +8,7 @@ import type {
} from '@papra/search-parser';
import type { Document } from '../../documents/documents.types';
import { parseSearchQuery } from '@papra/search-parser';
+import { stringify } from '@papra/std';
export function generatePropertyKey({ name }: { name: string }): string {
return name.replace(/[^\p{L}\p{N}]/gu, '').toLowerCase();
@@ -296,7 +297,7 @@ function buildCustomPropertyFilterCondition({
return false;
}
- return someCorpusTokenStartsWith({ corpus: String(prop.value), prefix: value });
+ return someCorpusTokenStartsWith({ corpus: stringify(prop.value), prefix: value });
}
case 'select': {
if (operator !== '=') {
@@ -326,7 +327,7 @@ function buildCustomPropertyFilterCondition({
return false;
}
- return String(prop.value).toLowerCase() === value.toLowerCase();
+ return stringify(prop.value).toLowerCase() === value.toLowerCase();
}
}
};
diff --git a/apps/papra-server/package.json b/apps/papra-server/package.json
index e3943df2..75b1a81b 100644
--- a/apps/papra-server/package.json
+++ b/apps/papra-server/package.json
@@ -59,6 +59,7 @@
"@owlrelay/webhook": "^0.0.3",
"@papra/lecture": "workspace:*",
"@papra/search-parser": "workspace:*",
+ "@papra/std": "workspace:*",
"@papra/webhooks": "workspace:*",
"@paralleldrive/cuid2": "catalog:",
"@sindresorhus/slugify": "^3.0.0",
diff --git a/apps/papra-server/src/modules/app/database/database.test-utils.ts b/apps/papra-server/src/modules/app/database/database.test-utils.ts
index ae16c61a..4e790274 100644
--- a/apps/papra-server/src/modules/app/database/database.test-utils.ts
+++ b/apps/papra-server/src/modules/app/database/database.test-utils.ts
@@ -33,6 +33,7 @@ import {
import { accountsTable, sessionsTable, twoFactorTable } from '../auth/auth.tables';
import { setupDatabase } from './database';
import { planEntitlementsTable } from '../../plan-entitlements/plan-entitlements.tables';
+import { stringify } from '@papra/std';
export { createInMemoryDatabase, seedDatabase };
@@ -124,7 +125,7 @@ export async function serializeSchema({ db }: { db: Database }) {
]);
return Array.from(result.values())
- .flatMap(({ rows }) => rows.map(({ sql }) => minifyQuery(String(sql))))
+ .flatMap(({ rows }) => rows.map(({ sql }) => minifyQuery(stringify(sql))))
.join('\n');
}
diff --git a/apps/papra-server/src/modules/app/database/database.usecases.ts b/apps/papra-server/src/modules/app/database/database.usecases.ts
index 101b6345..a463dbe3 100644
--- a/apps/papra-server/src/modules/app/database/database.usecases.ts
+++ b/apps/papra-server/src/modules/app/database/database.usecases.ts
@@ -2,6 +2,7 @@ import type { SQLiteSelect } from 'drizzle-orm/sqlite-core';
import type { ArrayElement } from '../../shared/types';
import type { Database } from './database.types';
import { sql } from 'drizzle-orm';
+import { ensureString } from '@papra/std';
export function createIterator<T extends SQLiteSelect>({
query,
@@ -53,5 +54,5 @@ export async function getRuntimeTableColumns({
}): Promise<string[]> {
const { rows } = await db.run(sql`SELECT name FROM pragma_table_info(${tableName})`);
- return rows.map((row) => String(row.name));
+ return rows.map((row) => ensureString(row.name));
}
diff --git a/apps/papra-server/src/modules/tagging-rules/tagging-rules.models.ts b/apps/papra-server/src/modules/tagging-rules/tagging-rules.models.ts
index f0df8a8f..2e412689 100644
--- a/apps/papra-server/src/modules/tagging-rules/tagging-rules.models.ts
+++ b/apps/papra-server/src/modules/tagging-rules/tagging-rules.models.ts
@@ -1,3 +1,4 @@
+import { stringify } from '@papra/std';
import type { Document } from '../documents/documents.types';
export function getDocumentFieldValue({ document, field }: { document: Document; field: string }) {
@@ -5,5 +6,5 @@ export function getDocumentFieldValue({ document, field }: { document: Document;
? document[field as keyof Document]
: undefined;
- return { fieldValue: String(fieldValue ?? '') };
+ return { fieldValue: stringify(fieldValue ?? '') };
}
diff --git a/packages/lecture/package.json b/packages/lecture/package.json
index 4e9e72d8..290ce5ed 100644
--- a/packages/lecture/package.json
+++ b/packages/lecture/package.json
@@ -52,6 +52,7 @@
"unpdf": "^1.4.0"
},
"devDependencies": {
+ "@papra/std": "workspace:*",
"@types/node": "catalog:",
"@vitest/coverage-v8": "catalog:",
"mime": "4.0.7",
diff --git a/packages/lecture/src/extractors/doc.extractor.ts b/packages/lecture/src/extractors/doc.extractor.ts
index 8d4e3775..baa7f094 100644
--- a/packages/lecture/src/extractors/doc.extractor.ts
+++ b/packages/lecture/src/extractors/doc.extractor.ts
@@ -1,7 +1,7 @@
import { XMLParser } from 'fast-xml-parser';
import { defineTextExtractor } from '../extractors.models';
import { getFileContentFromArchive } from '../utils/archive';
-import { stringify } from '../utils/string';
+import { stringify } from '@papra/std';
export const docExtractorDefinition = defineTextExtractor({
name: 'doc',
diff --git a/packages/lecture/src/extractors/odp.extractor.ts b/packages/lecture/src/extractors/odp.extractor.ts
index f29cd25c..b4bc96df 100644
--- a/packages/lecture/src/extractors/odp.extractor.ts
+++ b/packages/lecture/src/extractors/odp.extractor.ts
@@ -1,7 +1,7 @@
import { XMLParser } from 'fast-xml-parser';
import { defineTextExtractor } from '../extractors.models';
import { getFileContentFromArchive } from '../utils/archive';
-import { stringify } from '../utils/string';
+import { stringify } from '@papra/std';
export const odpExtractorDefinition = defineTextExtractor({
name: 'odp',
diff --git a/packages/lecture/src/extractors/ods.extractor.ts b/packages/lecture/src/extractors/ods.extractor.ts
index 3b895b58..8e7f89f2 100644
--- a/packages/lecture/src/extractors/ods.extractor.ts
+++ b/packages/lecture/src/extractors/ods.extractor.ts
@@ -1,7 +1,7 @@
import { XMLParser } from 'fast-xml-parser';
import { defineTextExtractor } from '../extractors.models';
import { getFileContentFromArchive } from '../utils/archive';
-import { stringify } from '../utils/string';
+import { stringify } from '@papra/std';
type XmlNode = Record<string, unknown>;
diff --git a/packages/lecture/src/extractors/odt.extractor.ts b/packages/lecture/src/extractors/odt.extractor.ts
index e7ebed2d..8c5f43c9 100644
--- a/packages/lecture/src/extractors/odt.extractor.ts
+++ b/packages/lecture/src/extractors/odt.extractor.ts
@@ -1,7 +1,7 @@
import { XMLParser } from 'fast-xml-parser';
import { defineTextExtractor } from '../extractors.models';
import { getFileContentFromArchive } from '../utils/archive';
-import { stringify } from '../utils/string';
+import { stringify } from '@papra/std';
export const odtExtractorDefinition = defineTextExtractor({
name: 'odt',
diff --git a/packages/lecture/src/extractors/pptx.extractor.ts b/packages/lecture/src/extractors/pptx.extractor.ts
index 6504c3ba..53490660 100644
--- a/packages/lecture/src/extractors/pptx.extractor.ts
+++ b/packages/lecture/src/extractors/pptx.extractor.ts
@@ -1,7 +1,7 @@
import { XMLParser } from 'fast-xml-parser';
import JSZip from 'jszip';
import { defineTextExtractor } from '../extractors.models';
-import { stringify } from '../utils/string';
+import { stringify } from '@papra/std';
export const pptxExtractorDefinition = defineTextExtractor({
name: 'pptx',
diff --git a/packages/lecture/src/extractors/xlsx.extractor.ts b/packages/lecture/src/extractors/xlsx.extractor.ts
index 2a00435e..b7d42d81 100644
--- a/packages/lecture/src/extractors/xlsx.extractor.ts
+++ b/packages/lecture/src/extractors/xlsx.extractor.ts
@@ -1,7 +1,7 @@
import { XMLParser } from 'fast-xml-parser';
import JSZip from 'jszip';
import { defineTextExtractor } from '../extractors.models';
-import { stringify } from '../utils/string';
+import { stringify } from '@papra/std';
type XmlNode = Record<string, unknown>;
diff --git a/packages/lecture/tsdown.config.ts b/packages/lecture/tsdown.config.ts
index 1feec394..10a58dad 100644
--- a/packages/lecture/tsdown.config.ts
+++ b/packages/lecture/tsdown.config.ts
@@ -4,4 +4,7 @@ export default defineConfig({
entry: ['./src/index.ts'],
dts: true,
exports: true,
+ deps: {
+ alwaysBundle: ['@papra/std'],
+ },
});
diff --git a/packages/std/package.json b/packages/std/package.json
new file mode 100644
index 00000000..0b631283
--- /dev/null
+++ b/packages/std/package.json
@@ -0,0 +1,30 @@
+{
+ "name": "@papra/std",
+ "version": "0.1.1",
+ "description": "A collection of utility functions.",
+ "bugs": {
+ "url": "https://github.com/papra-hq/papra/issues"
+ },
+ "license": "MIT",
+ "author": "Corentin Thomasset <[email protected]> (https://corentin.tech)",
+ "repository": {
+ "type": "git",
+ "url": "https://github.com/papra-hq/papra",
+ "directory": "packages/std"
+ },
+ "type": "module",
+ "main": "./src/index.ts",
+ "exports": {
+ ".": "./src/index.ts",
+ "./package.json": "./package.json"
+ },
+ "scripts": {
+ "test": "vitest run",
+ "test:watch": "vitest watch"
+ },
+ "dependencies": {},
+ "devDependencies": {
+ "typescript": "catalog:",
+ "vitest": "catalog:"
+ }
+}
diff --git a/packages/std/src/index.ts b/packages/std/src/index.ts
new file mode 100644
index 00000000..7d02200d
--- /dev/null
+++ b/packages/std/src/index.ts
@@ -0,0 +1 @@
+export * from './string/string';
diff --git a/packages/lecture/src/utils/string.test.ts b/packages/std/src/string/string.test.ts
similarity index 72%
rename from packages/lecture/src/utils/string.test.ts
rename to packages/std/src/string/string.test.ts
index 18db9e3d..52d31bb1 100644
--- a/packages/lecture/src/utils/string.test.ts
+++ b/packages/std/src/string/string.test.ts
@@ -1,5 +1,5 @@
import { describe, expect, test } from 'vitest';
-import { stringify } from './string';
+import { stringify, ensureString } from './string';
describe('string', () => {
describe('stringify', () => {
@@ -47,4 +47,18 @@ describe('string', () => {
expect(stringify(circular)).toBe('[object Object]');
});
});
+
+ describe('ensureString', () => {
+ test('returns strings unchanged and throws for non-string values', () => {
+ expect(ensureString('hello')).toBe('hello');
+ expect(ensureString('')).toBe('');
+
+ expect(() => ensureString(42)).toThrow(TypeError);
+ expect(() => ensureString(true)).toThrow(TypeError);
+ expect(() => ensureString(null)).toThrow(TypeError);
+ expect(() => ensureString(undefined)).toThrow(TypeError);
+ expect(() => ensureString({})).toThrow(TypeError);
+ expect(() => ensureString([])).toThrow(TypeError);
+ });
+ });
});
diff --git a/packages/lecture/src/utils/string.ts b/packages/std/src/string/string.ts
similarity index 72%
rename from packages/lecture/src/utils/string.ts
rename to packages/std/src/string/string.ts
index 8e67baaf..df5b4cda 100644
--- a/packages/lecture/src/utils/string.ts
+++ b/packages/std/src/string/string.ts
@@ -15,3 +15,11 @@ export function stringify(value: unknown): string {
// number, boolean, bigint, symbol, function, null and undefined are all safe to coerce with String.
return String(value);
}
+
+export function ensureString(value: unknown): string {
+ if (typeof value === 'string') {
+ return value;
+ }
+
+ throw new TypeError(`Expected a string, but received: ${typeof value}`);
+}
diff --git a/packages/std/vitest.config.ts b/packages/std/vitest.config.ts
new file mode 100644
index 00000000..0c241c1b
--- /dev/null
+++ b/packages/std/vitest.config.ts
@@ -0,0 +1,10 @@
+import { defineConfig } from 'vitest/config';
+
+export default defineConfig({
+ test: {
+ isolate: false,
+ env: {
+ TZ: 'UTC',
+ },
+ },
+});
diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml
index f6973bbd..b0b6442f 100644
--- a/pnpm-lock.yaml
+++ b/pnpm-lock.yaml
@@ -389,6 +389,9 @@ importers:
'@papra/lecture':
specifier: workspace:*
version: link:../../packages/lecture
+ '@papra/std':
+ specifier: workspace:*
+ version: link:../../packages/std
'@types/node':
specifier: 'catalog:'
version: 26.0.0
@@ -464,6 +467,9 @@ importers:
'@papra/search-parser':
specifier: workspace:*
version: link:../../packages/search-parser
+ '@papra/std':
+ specifier: workspace:*
+ version: link:../../packages/std
'@papra/webhooks':
specifier: workspace:*
version: link:../../packages/webhooks
@@ -780,6 +786,9 @@ importers:
specifier: ^1.4.0
version: 1.4.0(@napi-rs/[email protected])
devDependencies:
+ '@papra/std':
+ specifier: workspace:*
+ version: link:../std
'@types/node':
specifier: 'catalog:'
version: 26.0.0
@@ -839,6 +848,15 @@ importers:
specifier: 'catalog:'
+ packages/std:
+ devDependencies:
+ typescript:
+ specifier: 'catalog:'
+ version: 6.0.3
+ vitest:
+ specifier: 'catalog:'
+
packages/webhooks:
dependencies:
'@corentinth/chisels':