Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

make deserializer and serializer optional and replace by a default on… #30

Merged
merged 2 commits into from
Aug 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
node-version: [18.x]
node-version: [18.x, 20.x]
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v3
uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node-version }}

- uses: pnpm/action-setup@v2.2.4
- uses: pnpm/action-setup@v2
name: Install pnpm
id: pnpm-install
with:
Expand All @@ -42,10 +42,10 @@ jobs:
runs-on: ubuntu-latest
needs: [test]
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
- name: Use Node.js
uses: actions/setup-node@v3
- uses: pnpm/action-setup@v2.2.4
uses: actions/setup-node@v4
- uses: pnpm/action-setup@v2
name: Install pnpm
id: pnpm-install
with:
Expand Down
1 change: 0 additions & 1 deletion packages/http/src/decorators/use-guard.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

import { container } from '@triptyk/nfw-core';
import type { Class } from 'type-fest';
import type { GuardInterface } from '../interfaces/guard.js';
Expand Down
1 change: 0 additions & 1 deletion packages/http/src/decorators/use-middleware.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

import { container } from '@triptyk/nfw-core';
import type { Class } from 'type-fest';
import { MetadataStorage } from '../storages/metadata-storage.js';
Expand Down
1 change: 0 additions & 1 deletion packages/http/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

export * from './builders/default.js';
export * from './decorators/verbs.js';
export * from './decorators/controller.js';
Expand Down
1 change: 0 additions & 1 deletion packages/http/src/storages/metadata/use-middleware.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

import type { Class } from 'type-fest';
import type { AnyMiddlewareType } from '../../types/any-middleware.js';

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

import type { Class } from 'type-fest';
import type { ResponseHandlerInterface } from '../../interfaces/response-handler.js';

Expand Down
1 change: 0 additions & 1 deletion packages/http/src/utils/numerical-sort.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

export function numericalSortOnKeyASC<O extends Record<string, any>> (array : O[], propertyName: keyof O) {
return [...array].sort((a, b) => a[propertyName] - b[propertyName]);
}
1 change: 0 additions & 1 deletion packages/http/src/utils/resolve-params.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

import { ParamResolver } from '../resolvers/param-resolver.js';
import type { UseParamsMetadataArgs } from '../storages/metadata/use-param.js';
import type { ControllerContextType } from '../types/controller-context.js';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const rejects = createCustomDecorator(() => Promise.reject(new Error('test')), '
})
class C {
@POST('/')
public test (@rejects reject: string) {
public test (@rejects _reject: string) {
return 'test';
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export class SortValidator {
}
}

// eslint-disable-next-line max-statements
// eslint-disable-next-line max-statements, complexity
private recursiveSortValidation (sort: SortQuery, field: string, schema: ResourceSchema<Resource>) {
const directionOrSortQuery = sort[field];

Expand Down
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
4 changes: 2 additions & 2 deletions packages/resource-jsonapi/src/serialization/document.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,13 @@ export class DocumentSerializer {
return doc;
}

private getSchemaFor(resource: Resource) {
private getSchemaFor (resource: Resource) {
if (!resource.resourceType) {
throw new Error(`Resource ${resource} has no resourceType assigned`);
}

try {
return this.registry.getSchemaFor(resource.resourceType);
return this.registry.getSchemaFor(resource.resourceType);
} catch (e) {
throw new Error(`No schema found for resource type ${resource.resourceType}`);
}
Expand Down
Loading