From 429b75101ab088bf0c0dc3fdf3dab8118b4ee6a4 Mon Sep 17 00:00:00 2001 From: AmauryD Date: Wed, 14 Aug 2024 10:00:36 +0200 Subject: [PATCH] make deserializer and serializer optional and replace by a default one if not provided --- .../resource-jsonapi/src/registry/registry.ts | 26 +++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) diff --git a/packages/resource-jsonapi/src/registry/registry.ts b/packages/resource-jsonapi/src/registry/registry.ts index 0d9c7ef3..1627975a 100644 --- a/packages/resource-jsonapi/src/registry/registry.ts +++ b/packages/resource-jsonapi/src/registry/registry.ts @@ -1,3 +1,4 @@ +/* eslint-disable max-classes-per-file */ /* eslint-disable class-methods-use-this */ import { container, singleton } from '@triptyk/nfw-core'; import type { Class } from 'type-fest'; @@ -5,6 +6,8 @@ import type { ResourceDeserializer } from '../interfaces/deserializer.js'; import type { Resource } from '../interfaces/resource.js'; import type { ResourceSchema } from '../interfaces/schema.js'; import type { ResourceSerializer } from '../interfaces/serializer.js'; +import { JsonApiResourceSerializer } from '../serialization/serializer.js'; +import { JsonApiResourceDeserializer } from '../serialization/deserializer.js'; export interface BaseConfig { host: string, @@ -35,11 +38,30 @@ export class ResourcesRegistryImpl implements ResourcesRegistry { return container.resolve(`deserializer:${type}`); } + // eslint-disable-next-line max-statements register (type: string, classes: { - serializer: Class, - deserializer: Class>, + serializer?: Class, + deserializer?: Class>, schema: ResourceSchema, }): void { + const registry = this; + + if (!classes.serializer) { + classes.serializer = class DefaultSerializer extends JsonApiResourceSerializer { + public constructor () { + super(registry); + } + }; + } + + if (!classes.deserializer) { + classes.deserializer = class DefaultDeserializer extends JsonApiResourceDeserializer { + public constructor () { + super(classes.schema.resourceType, registry); + } + }; + } + container.register(`serializer:${type}`, { useClass: classes.serializer }); container.register(`deserializer:${type}`, { useClass: classes.deserializer }); container.register(`schema:${type}`, {