Skip to content

Commit

Permalink
make deserializer and serializer optional and replace by a default on…
Browse files Browse the repository at this point in the history
…e if not provided
  • Loading branch information
AmauryD committed Aug 14, 2024
1 parent f53142d commit 429b751
Showing 1 changed file with 24 additions and 2 deletions.
26 changes: 24 additions & 2 deletions packages/resource-jsonapi/src/registry/registry.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
/* 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';
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,
Expand Down Expand Up @@ -35,11 +38,30 @@ export class ResourcesRegistryImpl implements ResourcesRegistry {
return container.resolve(`deserializer:${type}`);
}

// eslint-disable-next-line max-statements
register<T extends Resource> (type: string, classes: {
serializer: Class<ResourceSerializer>,
deserializer: Class<ResourceDeserializer<T>>,
serializer?: Class<ResourceSerializer>,
deserializer?: Class<ResourceDeserializer<T>>,
schema: ResourceSchema<T>,
}): 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<T> {
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}`, {
Expand Down

0 comments on commit 429b751

Please sign in to comment.