From d8811d1df194bb44baf4ff9c794c2c3f4d13aa81 Mon Sep 17 00:00:00 2001 From: Chau Tran Date: Sat, 28 Mar 2020 23:25:25 -0500 Subject: [PATCH] feat(automapper.module.ts): cut the next release This is the next release to match with `@nartc/automapper@next` BREAKING CHANGE: - Deprecating `forRoot` because `Mapper.intialize` is deprecated - Add `withMapper`to replace `forRoot` and also allow adding `AutoMapperGlobalSettings` to `withMapper` --- .travis.yml | 18 ++++----- package-lock.json | 6 +-- package.json | 11 +++++- src/automapper.explorer.ts | 11 ++++-- src/automapper.module.ts | 39 +++++++++++++++++-- src/automapper.provider.ts | 15 +++++++ .../automapper-configuration.interface.ts | 7 ++-- src/interfaces/index.ts | 14 ++++++- src/utils/getWithMapperArgs.ts | 24 ++++++++++++ test/automapper.test.ts | 10 ++--- 10 files changed, 126 insertions(+), 29 deletions(-) create mode 100644 src/utils/getWithMapperArgs.ts diff --git a/.travis.yml b/.travis.yml index 01ff2dc..25e4965 100644 --- a/.travis.yml +++ b/.travis.yml @@ -15,22 +15,22 @@ jobs: - stage: Production before_script: - npm run build:docs - node_js: - - '12' + node_js: '12' name: deploy_docs - script: if [ "$TRAVIS_BRANCH" = "master" -a "$TRAVIS_PULL_REQUEST" = "false" ]; then npm run deploy-docs; fi + if: branch = master AND env(TRAVIS_PULL_REQUEST) IS false + script: npm run deploy-docs - name: npm_release - node_js: - - '12' + node_js: '12' before_script: - npm run build - script: if [ "$TRAVIS_BRANCH" = "master" -a "$TRAVIS_PULL_REQUEST" = "false" ]; then npm run semantic-release; fi + if: (branch = master OR branch = next) AND env(TRAVIS_PULL_REQUEST) IS false + script: npm run semantic-release - name: produce_coverage - node_js: - - '12' + node_js: '12' before_script: - npm run test:cov - script: if [ "$TRAVIS_BRANCH" = "master" -a "$TRAVIS_PULL_REQUEST" = "false" ]; then npm run report-coverage; fi + if: branch = master AND env(TRAVIS_PULL_REQUEST) IS false + script: npm run report-coverage branches: except: - /^v\d+\.\d+\.\d+$/ diff --git a/package-lock.json b/package-lock.json index 2f00596..08f4d72 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1681,9 +1681,9 @@ } }, "@nartc/automapper": { - "version": "5.0.13", - "resolved": "https://registry.npmjs.org/@nartc/automapper/-/automapper-5.0.13.tgz", - "integrity": "sha512-vZj/UDZOI1ZXaDi2fjjJHZ/iZJWGvNmP3e6yojv5tHVOr3v00XW7Z8mxDmUAk47mvk/kvw6lnLqMKYfbktaJEw==", + "version": "6.0.0-next.11", + "resolved": "https://registry.npmjs.org/@nartc/automapper/-/automapper-6.0.0-next.11.tgz", + "integrity": "sha512-kjnTbNhh/hESDm5DOrX9V0473HUt/yNvHzMS9PN0Sme3bQ7J4Zd6XCYxNoOtN5LdhSX1BdlphP0msk5vZMmPRw==", "requires": { "lodash.set": "4.3.2" } diff --git a/package.json b/package.json index 6b9fee0..fa0231e 100644 --- a/package.json +++ b/package.json @@ -63,6 +63,15 @@ "publishConfig": { "access": "public" }, + "release": { + "branches": [ + "master", + { + "name": "next", + "prerelease": true + } + ] + }, "jest": { "transform": { ".(ts|tsx)": "ts-jest" @@ -104,7 +113,7 @@ "typescript": "3.8.3" }, "dependencies": { - "@nartc/automapper": "5.0.13" + "@nartc/automapper": "6.0.0-next.11" }, "peerDependencies": { "@nestjs/common": "^7.0.6", diff --git a/src/automapper.explorer.ts b/src/automapper.explorer.ts index 407dc4d..f1d2552 100644 --- a/src/automapper.explorer.ts +++ b/src/automapper.explorer.ts @@ -1,4 +1,4 @@ -import { AutoMapper, MappingProfile } from '@nartc/automapper'; +import { AutoMapper, Constructible, MappingProfile } from '@nartc/automapper'; import { Inject, Injectable, Logger } from '@nestjs/common'; import { Reflector } from '@nestjs/core'; import { MAPPER_MAP } from './utils/mapperMap'; @@ -12,8 +12,8 @@ export class AutomapperExplorer { @Inject(MAPPER_MAP) private readonly mapperMap: Map, @Inject(PROFILE_MAP) private readonly profileMap: Map< - string, - new (...args: any) => MappingProfile + Constructible, + Constructible > ) {} @@ -25,7 +25,7 @@ export class AutomapperExplorer { this.profileMap.forEach(this.exploreProfile.bind(this)); } - private exploreProfile(value: new (...args: any) => MappingProfile) { + private exploreProfile(value: Constructible) { const mapperKey = this.reflector.get('AUTO_MAPPER_PROFILE', value); const mapper = this.mapperMap.get(mapperKey); @@ -36,6 +36,9 @@ export class AutomapperExplorer { return; } + this.logger.log( + `${value.name} added to Mapper ${mapperKey.split('__').pop()}` + ); mapper.addProfile(value); } } diff --git a/src/automapper.module.ts b/src/automapper.module.ts index 6e5f158..3fc9668 100644 --- a/src/automapper.module.ts +++ b/src/automapper.module.ts @@ -1,9 +1,10 @@ -import { AutoMapper } from '@nartc/automapper'; +import { AutoMapper, AutoMapperGlobalSettings } from '@nartc/automapper'; import { DynamicModule, Global, Logger, Module } from '@nestjs/common'; import { OnModuleInit } from '@nestjs/common/interfaces'; import { AutomapperExplorer } from './automapper.explorer'; -import { forRootProviders } from './automapper.provider'; +import { forRootProviders, withMapperProviders } from './automapper.provider'; import { AutomapperModuleRootOptions } from './interfaces'; +import { getWithMapperArgs } from './utils/getWithMapperArgs'; import { MAPPER_MAP, MapperMap } from './utils/mapperMap'; import { PROFILE_MAP, ProfileMap } from './utils/profileMap'; @@ -12,17 +13,49 @@ import { PROFILE_MAP, ProfileMap } from './utils/profileMap'; export class AutomapperModule implements OnModuleInit { private static readonly logger: Logger = new Logger('AutomapperModule'); + /** + * Initialize a Mapper with name and globalSettings + * + * @param {string} name - name of the Mapper instance. Default to 'default' + * @param {AutoMapperGlobalSettings} globalSettings - Global Settings for the current Mapper instance + */ + static withMapper( + name?: string, + globalSettings?: AutoMapperGlobalSettings + ): DynamicModule; + static withMapper(globalSettings?: AutoMapperGlobalSettings): DynamicModule; + static withMapper(...args: any[]): DynamicModule { + const [name, globalSettings] = getWithMapperArgs(args); + const mapper = new AutoMapper(); + if (globalSettings != null) { + mapper.withGlobalSettings(globalSettings); + } + + const providers = withMapperProviders(mapper, name); + return { + module: AutomapperModule, + providers: [ + ...providers, + AutomapperExplorer, + { provide: PROFILE_MAP, useValue: ProfileMap }, + { provide: MAPPER_MAP, useValue: MapperMap }, + { provide: Logger, useValue: this.logger }, + ], + exports: providers, + }; + } + /** * Initialize an AutoMapper instance with a name. Default to "default" * * Generally, `forRoot` only needs to be ran once to provide a singleton for the whole application * * @param {AutomapperModuleRootOptions} options + * @deprecated Please use withMapper instead */ static forRoot(options?: AutomapperModuleRootOptions): DynamicModule { const mapper = new AutoMapper(); - options && options.config && mapper.initialize(options.config); const providers = forRootProviders(mapper, options); return { diff --git a/src/automapper.provider.ts b/src/automapper.provider.ts index 4e065b1..74e930e 100644 --- a/src/automapper.provider.ts +++ b/src/automapper.provider.ts @@ -18,3 +18,18 @@ export const forRootProviders = ( }, ]; }; + +export const withMapperProviders = ( + mapper: AutoMapper, + name: string +): Provider[] => { + const token = getMapperToken(name); + !MapperMap.has(token) && MapperMap.set(token, mapper); + + return [ + { + provide: token, + useValue: mapper, + }, + ]; +}; diff --git a/src/interfaces/automapper-configuration.interface.ts b/src/interfaces/automapper-configuration.interface.ts index fb17f3b..10bc36a 100644 --- a/src/interfaces/automapper-configuration.interface.ts +++ b/src/interfaces/automapper-configuration.interface.ts @@ -1,12 +1,13 @@ -import { AutoMapperConfiguration } from '@nartc/automapper'; - +/** + * @deprecated Will be removed soon + */ export interface AutomapperModuleRootOptions { /** * Configuration Function to be ran when initialize a new AutoMapper instance * * @param {AutoMapperConfiguration} cfg */ - config?: (cfg: AutoMapperConfiguration) => void; + config?: (cfg: any) => void; /** * Name of the AutoMapper instance diff --git a/src/interfaces/index.ts b/src/interfaces/index.ts index 627433e..7564a6d 100644 --- a/src/interfaces/index.ts +++ b/src/interfaces/index.ts @@ -1,2 +1,14 @@ -export { AutoMapper, MappingProfileBase, AutoMap } from '@nartc/automapper'; +export { + AutoMapper, + ProfileBase, + AutoMap, + mapWith, + mapFrom, + convertUsing, + condition, + preCondition, + nullSubstitution, + ignore, + fromValue, +} from '@nartc/automapper'; export * from './automapper-configuration.interface'; diff --git a/src/utils/getWithMapperArgs.ts b/src/utils/getWithMapperArgs.ts new file mode 100644 index 0000000..6001738 --- /dev/null +++ b/src/utils/getWithMapperArgs.ts @@ -0,0 +1,24 @@ +import { AutoMapperGlobalSettings } from '@nartc/automapper'; + +export function getWithMapperArgs( + args: any[] +): [string, AutoMapperGlobalSettings?] { + if (!args.length) { + return ['']; + } + + if (args.length === 2) { + return [args[0], args[1]]; + } + + if (args.length === 1) { + const arg = args[0]; + if (typeof arg === 'string') { + return [arg]; + } + + return ['', arg]; + } + + return ['']; +} diff --git a/test/automapper.test.ts b/test/automapper.test.ts index 2fa39a6..e01ce06 100644 --- a/test/automapper.test.ts +++ b/test/automapper.test.ts @@ -1,4 +1,4 @@ -import { AutoMap, AutoMapper, MappingProfileBase } from '@nartc/automapper'; +import { AutoMap, AutoMapper, mapFrom, ProfileBase } from '@nartc/automapper'; import { Module } from '@nestjs/common'; import { Test, TestingModule } from '@nestjs/testing'; import { AutomapperModule, Profile } from '../src'; @@ -16,14 +16,14 @@ class MockVm { } @Profile() -class MockProfile extends MappingProfileBase { +class MockProfile extends ProfileBase { constructor(mapper: AutoMapper) { super(); mapper .createMap(Mock, MockVm) .forMember( - d => d.bar, - opts => opts.mapFrom(s => s.foo) + (d) => d.bar, + mapFrom((s) => s.foo) ) .reverseMap(); } @@ -35,7 +35,7 @@ class Another { } @Profile() -class AnotherProfile extends MappingProfileBase { +class AnotherProfile extends ProfileBase { constructor(mapper: AutoMapper) { super(); mapper.createMap(Another, MockVm);