Skip to content
This repository has been archived by the owner on Jan 7, 2021. It is now read-only.

Commit

Permalink
feat(automapper.module.ts): cut the next release
Browse files Browse the repository at this point in the history
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`
  • Loading branch information
nartc committed Mar 29, 2020
1 parent 5777e7a commit d8811d1
Show file tree
Hide file tree
Showing 10 changed files with 126 additions and 29 deletions.
18 changes: 9 additions & 9 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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+$/
6 changes: 3 additions & 3 deletions package-lock.json

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

11 changes: 10 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,15 @@
"publishConfig": {
"access": "public"
},
"release": {
"branches": [
"master",
{
"name": "next",
"prerelease": true
}
]
},
"jest": {
"transform": {
".(ts|tsx)": "ts-jest"
Expand Down Expand Up @@ -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",
Expand Down
11 changes: 7 additions & 4 deletions src/automapper.explorer.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -12,8 +12,8 @@ export class AutomapperExplorer {
@Inject(MAPPER_MAP) private readonly mapperMap: Map<string, AutoMapper>,
@Inject(PROFILE_MAP)
private readonly profileMap: Map<
string,
new (...args: any) => MappingProfile
Constructible<MappingProfile>,
Constructible<MappingProfile>
>
) {}

Expand All @@ -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<MappingProfile>) {
const mapperKey = this.reflector.get<string>('AUTO_MAPPER_PROFILE', value);
const mapper = this.mapperMap.get(mapperKey);

Expand All @@ -36,6 +36,9 @@ export class AutomapperExplorer {
return;
}

this.logger.log(
`${value.name} added to Mapper ${mapperKey.split('__').pop()}`
);
mapper.addProfile(value);
}
}
39 changes: 36 additions & 3 deletions src/automapper.module.ts
Original file line number Diff line number Diff line change
@@ -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';

Expand All @@ -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 {
Expand Down
15 changes: 15 additions & 0 deletions src/automapper.provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
];
};
7 changes: 4 additions & 3 deletions src/interfaces/automapper-configuration.interface.ts
Original file line number Diff line number Diff line change
@@ -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
Expand Down
14 changes: 13 additions & 1 deletion src/interfaces/index.ts
Original file line number Diff line number Diff line change
@@ -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';
24 changes: 24 additions & 0 deletions src/utils/getWithMapperArgs.ts
Original file line number Diff line number Diff line change
@@ -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 [''];
}
10 changes: 5 additions & 5 deletions test/automapper.test.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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();
}
Expand All @@ -35,7 +35,7 @@ class Another {
}

@Profile()
class AnotherProfile extends MappingProfileBase {
class AnotherProfile extends ProfileBase {
constructor(mapper: AutoMapper) {
super();
mapper.createMap(Another, MockVm);
Expand Down

0 comments on commit d8811d1

Please sign in to comment.