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

Add mapping module #567

Merged
merged 13 commits into from
Nov 2, 2023
15,160 changes: 3,767 additions & 11,393 deletions package-lock.json

Large diffs are not rendered by default.

10 changes: 6 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,16 @@
},
"homepage": "https://github.com/magieno/pristine-ts#readme",
"devDependencies": {
"@types/jest": "^27.4.1",
"@types/jest": "^29.5.7",
"@types/lodash": "^4.14.168",
"@types/node": "^14.14.31",
"@typescript-eslint/eslint-plugin": "^5.16.0",
"@typescript-eslint/parser": "^5.16.0",
"eslint": "^8.11.0",
"jest": "^26.6.3",
"eslint": "^8.52.0",
"jest": "^29.7.0",
"jest-extended": "^4.0.2",
"lerna": "^4.0.0",
"ts-jest": "^26.4.4",
"ts-jest": "^29.1.1",
"typescript": "^4.6.2"
},
"dependencies": {
Expand All @@ -51,6 +52,7 @@
"@pristine-ts/common": "file:packages/common",
"@pristine-ts/configuration": "file:packages/configuration",
"@pristine-ts/core": "file:packages/core",
"@pristine-ts/data-transformer": "file:packages/data-transformer",
"@pristine-ts/e2e": "file:tests/e2e",
"@pristine-ts/express": "file:packages/express",
"@pristine-ts/file": "file:packages/file",
Expand Down
1 change: 1 addition & 0 deletions packages/common/src/enums/service-definition-tag.enum.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
export enum ServiceDefinitionTagEnum {
Command = "COMMAND",
CurrentChildContainer = "CURRENT_CHILD_CONTAINER",
DataNormalizer = "DATA_NORMALIZER",
EventHandler = "EventHandlerInterface",
EventInterceptor = "EventInterceptorInterface",
EventListener = "EventListenerInterface",
Expand Down
72 changes: 72 additions & 0 deletions packages/data-transformer/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

63 changes: 63 additions & 0 deletions packages/data-transformer/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
{
"name": "@pristine-ts/data-transformer",
"version": "0.0.239",
"description": "",
"module": "dist/lib/esm/data-transformer.module.js",
"main": "dist/lib/cjs/data-transformer.module.js",
"types": "dist/types/data-transformer.module.d.ts",
"scripts": {
"build": "tsc -p tsconfig.json && tsc -p tsconfig.cjs.json",
"prepublish": "npm run build",
"test": "jest",
"test:cov": "jest --coverage"
},
"files": [
"dist"
],
"author": "",
"license": "ISC",
"publishConfig": {
"access": "public"
},
"dependencies": {
"@camaro/redis": "^2.2.5",
"@pristine-ts/common": "file:../common",
"@pristine-ts/logging": "file:../logging"
},
"jest": {
"transform": {
".(ts|tsx)": "ts-jest"
},
"globals": {
"ts-jest": {
"tsconfig": {
"strictNullChecks": false
}
}
},
"testEnvironment": "node",
"testRegex": "(/__tests__/.*|\\.(test|spec))\\.(ts|tsx|js)$",
"moduleFileExtensions": [
"ts",
"tsx",
"js"
],
"coveragePathIgnorePatterns": [
"/node_modules/",
"/test/"
],
"coverageThreshold": {
"global": {
"branches": 90,
"functions": 95,
"lines": 95,
"statements": 95
}
},
"collectCoverageFrom": [
"src/*.{js,ts}"
],
"setupFilesAfterEnv": ["jest-extended/all"]
},
"gitHead": "112f715bf4ac6467192d416aa6e419f772b77c58"
}
1 change: 1 addition & 0 deletions packages/data-transformer/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Data Transformer module.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const DataTransformerModuleKeyname: string = "pristine.data-transformer";
25 changes: 25 additions & 0 deletions packages/data-transformer/src/data-transformer.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import {ModuleInterface} from "@pristine-ts/common";
import {LoggingModule} from "@pristine-ts/logging";
import {DataTransformerModuleKeyname} from "./data-transformer.module.keyname";
import { EnvironmentVariableResolver, NumberResolver} from "@pristine-ts/configuration";
import {DataTransformerBuilder} from "./transformers/data-transformer.builder";

export * from "./errors/errors";
export * from "./interceptors/interceptors";
export * from "./interfaces/interfaces";
export * from "./normalizer-options/normalizer-options";
export * from "./normalizers/normalizers";
export * from "./transformers/transformers";
export * from "./types/types";

export const DataTransformerModule: ModuleInterface = {
keyname: DataTransformerModuleKeyname,
importModules: [
LoggingModule,
],
providerRegistrations: [
],
configurationDefinitions: [
]

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import {LoggableError} from "@pristine-ts/common";
import {Request} from "@pristine-ts/common";
import {DataTransformerInterceptorUniqueKeyType} from "../types/data-transformer-interceptor-unique-key.type";

/**
* This Error is thrown when the before row interceptor is added more than once to the builder.
*/
export class DataAfterRowTransformerInterceptorAlreadyAddedError extends LoggableError {

public constructor(message: string, uniqueKey: DataTransformerInterceptorUniqueKeyType, options?: any) {
super(message, {
uniqueKey,
options,
});

// Set the prototype explicitly.
// As specified in the documentation in TypeScript
// https://github.com/Microsoft/TypeScript/wiki/Breaking-Changes#extending-built-ins-like-error-array-and-map-may-no-longer-work
Object.setPrototypeOf(this, DataAfterRowTransformerInterceptorAlreadyAddedError.prototype);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import {LoggableError} from "@pristine-ts/common";
import {Request} from "@pristine-ts/common";
import {DataTransformerInterceptorUniqueKeyType} from "../types/data-transformer-interceptor-unique-key.type";

/**
* This Error is thrown when the after row interceptor is added more than once to the builder.
*/
export class DataBeforeRowTransformerInterceptorAlreadyAddedError extends LoggableError {

public constructor(message: string, uniqueKey: DataTransformerInterceptorUniqueKeyType, options?: any) {
super(message, {
uniqueKey,
options,
});

// Set the prototype explicitly.
// As specified in the documentation in TypeScript
// https://github.com/Microsoft/TypeScript/wiki/Breaking-Changes#extending-built-ins-like-error-array-and-map-may-no-longer-work
Object.setPrototypeOf(this, DataBeforeRowTransformerInterceptorAlreadyAddedError.prototype);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import {LoggableError} from "@pristine-ts/common";
import {Request} from "@pristine-ts/common";

/**
* This Error is thrown when a normalizer is added more than once.
etiennenoel marked this conversation as resolved.
Show resolved Hide resolved
*/
export class DataNormalizerAlreadyAdded extends LoggableError {

public constructor(message: string, normalizerUniqueKey: string, options?: any) {
super(message, {
normalizerUniqueKey,
options,
});


// Set the prototype explicitly.
// As specified in the documentation in TypeScript
// https://github.com/Microsoft/TypeScript/wiki/Breaking-Changes#extending-built-ins-like-error-array-and-map-may-no-longer-work
Object.setPrototypeOf(this, DataNormalizerAlreadyAdded.prototype);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import {LoggableError} from "@pristine-ts/common";
import {Request} from "@pristine-ts/common";
import {DataTransformerInterceptorUniqueKeyType} from "../types/data-transformer-interceptor-unique-key.type";

/**
* This Error is thrown if the Data Transformer Class is not found in the list of available interceptors. It might be missing a tag.
*/
export class DataTransformerInterceptorNotFoundError extends LoggableError {

public constructor(message: string, uniqueKey: DataTransformerInterceptorUniqueKeyType, options?: any) {
super(message, {
uniqueKey,
options,
});

// Set the prototype explicitly.
// As specified in the documentation in TypeScript
// https://github.com/Microsoft/TypeScript/wiki/Breaking-Changes#extending-built-ins-like-error-array-and-map-may-no-longer-work
Object.setPrototypeOf(this, DataTransformerInterceptorNotFoundError.prototype);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import {LoggableError} from "@pristine-ts/common";
import {Request} from "@pristine-ts/common";

/**
* This Error is thrown when a property isn't optional and should be found in the source object.
*/
export class DataTransformerSourcePropertyNotFoundError extends LoggableError {

public constructor(message: string, sourceProperty: string) {
super(message, {
sourceProperty,
});


// Set the prototype explicitly.
// As specified in the documentation in TypeScript
// https://github.com/Microsoft/TypeScript/wiki/Breaking-Changes#extending-built-ins-like-error-array-and-map-may-no-longer-work
Object.setPrototypeOf(this, DataTransformerSourcePropertyNotFoundError.prototype);
}
}
5 changes: 5 additions & 0 deletions packages/data-transformer/src/errors/errors.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export * from "./data-after-row-transformer-interceptor-already-added.error";
export * from "./data-before-row-transformer-interceptor-already-added.error";
export * from "./data-normalizer-already-added.error";
export * from "./data-transformer-source-property-not-found.error";
export * from "./normalizer-invalid-source-type.error";
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import {LoggableError} from "@pristine-ts/common";
import {Request} from "@pristine-ts/common";

/**
* This Error is thrown when an invalid source type is passed to the source type while the normalizer expects another type.
*/
export class NormalizerInvalidSourceTypeError extends LoggableError {

public constructor(message: string, normalizerUniqueKey: string, options: any, source: any, sourceType: any) {
super(message, {
normalizerUniqueKey,
options,
source,
sourceType,
});

// Set the prototype explicitly.
// As specified in the documentation in TypeScript
// https://github.com/Microsoft/TypeScript/wiki/Breaking-Changes#extending-built-ins-like-error-array-and-map-may-no-longer-work
Object.setPrototypeOf(this, NormalizerInvalidSourceTypeError.prototype);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import {DataTransformerInterceptorInterface} from "../interfaces/data-transformer-interceptor.interface";
import {moduleScoped, tag} from "@pristine-ts/common";
import {DataTransformerModuleKeyname} from "../data-transformer.module.keyname";
import {injectable} from "tsyringe";
import {DataTransformerRow} from "../types/data-transformer.row";
import {DataTransformerInterceptorUniqueKeyType} from "../types/data-transformer-interceptor-unique-key.type";

@tag("DataTransformerInterceptor")
@moduleScoped(DataTransformerModuleKeyname)
@injectable()
export class DefaultDataTransformerInterceptor implements DataTransformerInterceptorInterface {
async afterRowTransform(row: DataTransformerRow): Promise<DataTransformerRow> {
return row;
}

async beforeRowTransform(row: DataTransformerRow): Promise<DataTransformerRow> {
return row;
}

getUniqueKey(): DataTransformerInterceptorUniqueKeyType {
return DefaultDataTransformerInterceptor.name;
}
}
1 change: 1 addition & 0 deletions packages/data-transformer/src/interceptors/interceptors.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from "./default-data-transformer.interceptor";
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import {DataNormalizerUniqueKey} from "../types/data-normalizer-unique-key.type";

export interface DataNormalizerInterface<T, R> {
/**
* Every data normalizer must define a unique key. Then, during the transformation, the schema can specify which
* normalizer it must use. Using the unique key, we can quickly (in O(1)) retrieve the normalizer.
*/
getUniqueKey(): DataNormalizerUniqueKey;

/**
* This method takes the source property value, can receive options to control the behaviour (example, you might
* want to specify a number of significant digits or a data format) and returns the normalized value.
* @param source
* @param options
*/
normalize(source: any, options?: R): T;
}
Loading
Loading