The minimalistic document archiving platform.
refactor(client, services): updated API responses to use AsDto and date coercion (#250)
Corentin Thomasset9cba84e38ba2
diff --git a/apps/papra-client/src/modules/documents/documents.services.ts b/apps/papra-client/src/modules/documents/documents.services.tsindex b73cdc1f..620871a2 100644--- a/apps/papra-client/src/modules/documents/documents.services.ts+++ b/apps/papra-client/src/modules/documents/documents.services.ts@@ -1,6 +1,7 @@+import type { AsDto } from '../shared/http/http-client.types';import type { Document } from './documents.types';import { apiClient } from '../shared/http/api-client';-import { getFormData } from '../shared/http/http-client.models';+import { coerceDates, getFormData } from '../shared/http/http-client.models';export async function uploadDocument({file,@@ -9,18 +10,14 @@ export async function uploadDocument({file: File;organizationId: string;}) {- const { document } = await apiClient<{ document: Document }>({+ const { document } = await apiClient<{ document: AsDto<Document> }>({method: 'POST',path: `/api/organizations/${organizationId}/documents`,body: getFormData({ file }),});return {- document: {- ...document,- createdAt: new Date(document.createdAt),- updatedAt: document.updatedAt ? new Date(document.updatedAt) : undefined,- },+ document: coerceDates(document),};}@@ -40,7 +37,7 @@ export async function fetchOrganizationDocuments({const {documents,documentsCount,- } = await apiClient<{ documents: Document[]; documentsCount: number }>({+ } = await apiClient<{ documents: AsDto<Document>[]; documentsCount: number }>({method: 'GET',path: `/api/organizations/${organizationId}/documents`,query: {@@ -52,11 +49,7 @@ export async function fetchOrganizationDocuments({return {documentsCount,- documents: documents.map(document => ({- ...document,- createdAt: new Date(document.createdAt),- updatedAt: document.updatedAt ? new Date(document.updatedAt) : undefined,- })),+ documents: documents.map(coerceDates),};}@@ -72,7 +65,7 @@ export async function fetchOrganizationDeletedDocuments({const {documents,documentsCount,- } = await apiClient<{ documents: Document[]; documentsCount: number }>({+ } = await apiClient<{ documents: AsDto<Document>[]; documentsCount: number }>({method: 'GET',path: `/api/organizations/${organizationId}/documents/deleted`,query: {@@ -83,11 +76,7 @@ export async function fetchOrganizationDeletedDocuments({return {documentsCount,- documents: documents.map(document => ({- ...document,- createdAt: new Date(document.createdAt),- updatedAt: document.updatedAt ? new Date(document.updatedAt) : undefined,- })),+ documents: documents.map(coerceDates),};}@@ -124,17 +113,13 @@ export async function fetchDocument({documentId: string;organizationId: string;}) {- const { document } = await apiClient<{ document: Document }>({+ const { document } = await apiClient<{ document: AsDto<Document> }>({method: 'GET',path: `/api/organizations/${organizationId}/documents/${documentId}`,});return {- document: {- ...document,- createdAt: new Date(document.createdAt),- updatedAt: document.updatedAt ? new Date(document.updatedAt) : undefined,- },+ document: coerceDates(document),};}@@ -167,7 +152,7 @@ export async function searchDocuments({}) {const {documents,- } = await apiClient<{ documents: Document[] }>({+ } = await apiClient<{ documents: AsDto<Document>[] }>({method: 'GET',path: `/api/organizations/${organizationId}/documents/search`,query: {@@ -178,11 +163,7 @@ export async function searchDocuments({});return {- documents: documents.map(document => ({- ...document,- createdAt: new Date(document.createdAt),- updatedAt: document.updatedAt ? new Date(document.updatedAt) : undefined,- })),+ documents: documents.map(coerceDates),};}@@ -218,17 +199,13 @@ export async function updateDocument({organizationId: string;content: string;}) {- const { document } = await apiClient<{ document: Document }>({+ const { document } = await apiClient<{ document: AsDto<Document> }>({method: 'PATCH',path: `/api/organizations/${organizationId}/documents/${documentId}`,body: { content },});return {- document: {- ...document,- createdAt: new Date(document.createdAt),- updatedAt: document.updatedAt ? new Date(document.updatedAt) : undefined,- },+ document: coerceDates(document),};}diff --git a/apps/papra-client/src/modules/intake-emails/intake-emails.services.ts b/apps/papra-client/src/modules/intake-emails/intake-emails.services.tsindex e8eed55e..3f1370cc 100644--- a/apps/papra-client/src/modules/intake-emails/intake-emails.services.ts+++ b/apps/papra-client/src/modules/intake-emails/intake-emails.services.ts@@ -1,9 +1,10 @@+import type { AsDto } from '../shared/http/http-client.types';import type { IntakeEmail } from './intake-emails.types';import { apiClient } from '../shared/http/api-client';import { coerceDates } from '../shared/http/http-client.models';export async function fetchIntakeEmails({ organizationId }: { organizationId: string }) {- const { intakeEmails } = await apiClient<{ intakeEmails: IntakeEmail[] }>({+ const { intakeEmails } = await apiClient<{ intakeEmails: AsDto<IntakeEmail>[] }>({path: `/api/organizations/${organizationId}/intake-emails`,method: 'GET',});@@ -14,7 +15,7 @@ export async function fetchIntakeEmails({ organizationId }: { organizationId: st}export async function createIntakeEmail({ organizationId }: { organizationId: string }) {- const { intakeEmail } = await apiClient<{ intakeEmail: IntakeEmail }>({+ const { intakeEmail } = await apiClient<{ intakeEmail: AsDto<IntakeEmail> }>({path: `/api/organizations/${organizationId}/intake-emails`,method: 'POST',});@@ -42,7 +43,7 @@ export async function updateIntakeEmail({isEnabled?: boolean;allowedOrigins?: string[];}) {- const { intakeEmail } = await apiClient<{ intakeEmail: IntakeEmail }>({+ const { intakeEmail } = await apiClient<{ intakeEmail: AsDto<IntakeEmail> }>({path: `/api/organizations/${organizationId}/intake-emails/${intakeEmailId}`,method: 'PUT',body: {diff --git a/apps/papra-client/src/modules/intake-emails/intake-emails.types.ts b/apps/papra-client/src/modules/intake-emails/intake-emails.types.tsindex 8fc9fc55..23086eb6 100644--- a/apps/papra-client/src/modules/intake-emails/intake-emails.types.ts+++ b/apps/papra-client/src/modules/intake-emails/intake-emails.types.ts@@ -6,5 +6,5 @@ export type IntakeEmail = {allowedOrigins: string[];createdAt: Date;- updatedAt: Date | undefined;+ updatedAt: Date;};diff --git a/apps/papra-client/src/modules/organizations/organizations.services.ts b/apps/papra-client/src/modules/organizations/organizations.services.tsindex b3620098..45b720dd 100644--- a/apps/papra-client/src/modules/organizations/organizations.services.ts+++ b/apps/papra-client/src/modules/organizations/organizations.services.ts@@ -1,65 +1,51 @@+import type { AsDto } from '../shared/http/http-client.types';import type { Organization } from './organizations.types';import { apiClient } from '../shared/http/api-client';+import { coerceDates } from '../shared/http/http-client.models';export async function fetchOrganizations() {- const { organizations } = await apiClient<{ organizations: Organization[] }>({+ const { organizations } = await apiClient<{ organizations: AsDto<Organization>[] }>({path: '/api/organizations',method: 'GET',});return {- organizations: organizations.map(organization => ({- ...organization,- createdAt: new Date(organization.createdAt),- updatedAt: organization.updatedAt ? new Date(organization.updatedAt) : undefined,- })),+ organizations: organizations.map(coerceDates),};}export async function createOrganization({ name }: { name: string }) {- const { organization } = await apiClient<{ organization: Organization }>({+ const { organization } = await apiClient<{ organization: AsDto<Organization> }>({path: '/api/organizations',method: 'POST',body: { name },});return {- organization: {- ...organization,- createdAt: new Date(organization.createdAt),- updatedAt: organization.updatedAt ? new Date(organization.updatedAt) : undefined,- },+ organization: coerceDates(organization),};}export async function updateOrganization({ organizationId, name }: { organizationId: string; name: string }) {- const { organization } = await apiClient<{ organization: Organization }>({+ const { organization } = await apiClient<{ organization: AsDto<Organization> }>({path: `/api/organizations/${organizationId}`,method: 'PUT',body: { name },});return {- organization: {- ...organization,- createdAt: new Date(organization.createdAt),- updatedAt: organization.updatedAt ? new Date(organization.updatedAt) : undefined,- },+ organization: coerceDates(organization),};}export async function fetchOrganization({ organizationId }: { organizationId: string }) {- const { organization } = await apiClient<{ organization: Organization }>({+ const { organization } = await apiClient<{ organization: AsDto<Organization> }>({path: `/api/organizations/${organizationId}`,method: 'GET',});return {- organization: {- ...organization,- createdAt: new Date(organization.createdAt),- updatedAt: organization.updatedAt ? new Date(organization.updatedAt) : undefined,- },+ organization: coerceDates(organization),};}diff --git a/apps/papra-client/src/modules/organizations/organizations.types.ts b/apps/papra-client/src/modules/organizations/organizations.types.tsindex f33e97f4..743d401f 100644--- a/apps/papra-client/src/modules/organizations/organizations.types.ts+++ b/apps/papra-client/src/modules/organizations/organizations.types.ts@@ -2,5 +2,5 @@ export type Organization = {id: string;name: string;createdAt: Date;- updatedAt?: Date;+ updatedAt: Date;};diff --git a/apps/papra-client/src/modules/shared/http/http-client.types.ts b/apps/papra-client/src/modules/shared/http/http-client.types.tsnew file mode 100644index 00000000..33050c97--- /dev/null+++ b/apps/papra-client/src/modules/shared/http/http-client.types.ts@@ -0,0 +1,5 @@+import type { Expand } from '@corentinth/chisels';++export type AsDto<T> = Expand<{+ [K in keyof T]: T[K] extends Date ? string : T[K];+}>;diff --git a/apps/papra-client/src/modules/tagging-rules/tagging-rules.services.ts b/apps/papra-client/src/modules/tagging-rules/tagging-rules.services.tsindex 9b56b06c..8f959b94 100644--- a/apps/papra-client/src/modules/tagging-rules/tagging-rules.services.ts+++ b/apps/papra-client/src/modules/tagging-rules/tagging-rules.services.ts@@ -1,13 +1,15 @@+import type { AsDto } from '../shared/http/http-client.types';import type { TaggingRule, TaggingRuleForCreation } from './tagging-rules.types';import { apiClient } from '../shared/http/api-client';+import { coerceDates } from '../shared/http/http-client.models';export async function fetchTaggingRules({ organizationId }: { organizationId: string }) {- const { taggingRules } = await apiClient<{ taggingRules: TaggingRule[] }>({+ const { taggingRules } = await apiClient<{ taggingRules: AsDto<TaggingRule>[] }>({path: `/api/organizations/${organizationId}/tagging-rules`,method: 'GET',});- return { taggingRules };+ return { taggingRules: taggingRules.map(coerceDates) };}export async function createTaggingRule({ taggingRule, organizationId }: { taggingRule: TaggingRuleForCreation; organizationId: string }) {@@ -26,12 +28,12 @@ export async function deleteTaggingRule({ organizationId, taggingRuleId }: { org}export async function getTaggingRule({ organizationId, taggingRuleId }: { organizationId: string; taggingRuleId: string }) {- const { taggingRule } = await apiClient<{ taggingRule: TaggingRule }>({+ const { taggingRule } = await apiClient<{ taggingRule: AsDto<TaggingRule> }>({path: `/api/organizations/${organizationId}/tagging-rules/${taggingRuleId}`,method: 'GET',});- return { taggingRule };+ return { taggingRule: coerceDates(taggingRule) };}export async function updateTaggingRule({ organizationId, taggingRuleId, taggingRule }: { organizationId: string; taggingRuleId: string; taggingRule: TaggingRuleForCreation }) {diff --git a/apps/papra-client/src/modules/tags/tags.services.ts b/apps/papra-client/src/modules/tags/tags.services.tsindex ebecaffd..e7a8a8cd 100644--- a/apps/papra-client/src/modules/tags/tags.services.ts+++ b/apps/papra-client/src/modules/tags/tags.services.ts@@ -1,9 +1,10 @@+import type { AsDto } from '../shared/http/http-client.types';import type { Tag } from './tags.types';import { apiClient } from '../shared/http/api-client';import { coerceDates } from '../shared/http/http-client.models';export async function fetchTags({ organizationId }: { organizationId: string }) {- const { tags } = await apiClient<{ tags: Tag[] }>({+ const { tags } = await apiClient<{ tags: AsDto<Tag>[] }>({path: `/api/organizations/${organizationId}/tags`,method: 'GET',});@@ -14,7 +15,7 @@ export async function fetchTags({ organizationId }: { organizationId: string })}export async function createTag({ organizationId, name, color, description }: { organizationId: string; name: string; color: string; description: string }) {- const { tag } = await apiClient<{ tag: Tag }>({+ const { tag } = await apiClient<{ tag: AsDto<Tag> }>({path: `/api/organizations/${organizationId}/tags`,method: 'POST',body: { name, color, description },@@ -26,7 +27,7 @@ export async function createTag({ organizationId, name, color, description }: {}export async function updateTag({ organizationId, tagId, name, color, description }: { organizationId: string; tagId: string; name: string; color: string; description: string }) {- const { tag } = await apiClient<{ tag: Tag }>({+ const { tag } = await apiClient<{ tag: AsDto<Tag> }>({path: `/api/organizations/${organizationId}/tags/${tagId}`,method: 'PUT',body: { name, color, description },