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

Commit 9346674

Browse files
author
Chau Tran
committed
feat(automapper.module.ts): cut new release to support automapper v6
BREAKING CHANGE: - `forRoot()` has been deprecated. Please use `withMapper()` instead.
2 parents a9c7e9d + 73e1c97 commit 9346674

10 files changed

+624
-539
lines changed

.travis.yml

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,22 +15,22 @@ jobs:
1515
- stage: Production
1616
before_script:
1717
- npm run build:docs
18-
node_js:
19-
- '12'
18+
node_js: '12'
2019
name: deploy_docs
21-
script: if [ "$TRAVIS_BRANCH" = "master" -a "$TRAVIS_PULL_REQUEST" = "false" ]; then npm run deploy-docs; fi
20+
if: branch = master AND env(TRAVIS_PULL_REQUEST) IS false
21+
script: npm run deploy-docs
2222
- name: npm_release
23-
node_js:
24-
- '12'
23+
node_js: '12'
2524
before_script:
2625
- npm run build
27-
script: if [ "$TRAVIS_BRANCH" = "master" -a "$TRAVIS_PULL_REQUEST" = "false" ]; then npm run semantic-release; fi
26+
if: (branch = master OR branch = next) AND env(TRAVIS_PULL_REQUEST) IS false
27+
script: npm run semantic-release
2828
- name: produce_coverage
29-
node_js:
30-
- '12'
29+
node_js: '12'
3130
before_script:
3231
- npm run test:cov
33-
script: if [ "$TRAVIS_BRANCH" = "master" -a "$TRAVIS_PULL_REQUEST" = "false" ]; then npm run report-coverage; fi
32+
if: branch = master AND env(TRAVIS_PULL_REQUEST) IS false
33+
script: npm run report-coverage
3434
branches:
3535
except:
3636
- /^v\d+\.\d+\.\d+$/

package-lock.json

Lines changed: 489 additions & 505 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,15 @@
6363
"publishConfig": {
6464
"access": "public"
6565
},
66+
"release": {
67+
"branches": [
68+
"master",
69+
{
70+
"name": "next",
71+
"prerelease": true
72+
}
73+
]
74+
},
6675
"jest": {
6776
"transform": {
6877
".(ts|tsx)": "ts-jest"
@@ -84,30 +93,30 @@
8493
"devDependencies": {
8594
"@commitlint/cli": "8.3.5",
8695
"@commitlint/config-conventional": "8.3.4",
87-
"@nestjs/common": "7.0.6",
88-
"@nestjs/core": "7.0.6",
89-
"@nestjs/testing": "7.0.6",
90-
"@types/jest": "25.1.4",
96+
"@nestjs/common": "7.0.7",
97+
"@nestjs/core": "7.0.7",
98+
"@nestjs/testing": "7.0.7",
99+
"@types/jest": "25.2.1",
91100
"commitizen": "4.0.3",
92101
"coveralls": "3.0.11",
93102
"cz-conventional-changelog": "3.1.0",
94103
"husky": "4.2.3",
95-
"lint-staged": "10.0.9",
96-
"prettier": "2.0.2",
104+
"lint-staged": "10.1.2",
105+
"prettier": "2.0.3",
97106
"reflect-metadata": "0.1.13",
98107
"semantic-release": "17.0.4",
99108
"shelljs": "0.8.3",
100-
"ts-node": "8.8.1",
109+
"ts-node": "8.8.2",
101110
"tsdx": "0.13.1",
102111
"tslib": "1.11.1",
103112
"typedoc": "0.17.3",
104113
"typescript": "3.8.3"
105114
},
106115
"dependencies": {
107-
"@nartc/automapper": "5.0.13"
116+
"@nartc/automapper": "6.0.0"
108117
},
109118
"peerDependencies": {
110-
"@nestjs/common": "^7.0.6",
119+
"@nestjs/common": "^7.0.7",
111120
"reflect-metadata": "^0.1.13"
112121
}
113122
}

src/automapper.explorer.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { AutoMapper, MappingProfile } from '@nartc/automapper';
1+
import { AutoMapper, Constructible, MappingProfile } from '@nartc/automapper';
22
import { Inject, Injectable, Logger } from '@nestjs/common';
33
import { Reflector } from '@nestjs/core';
44
import { MAPPER_MAP } from './utils/mapperMap';
@@ -12,8 +12,8 @@ export class AutomapperExplorer {
1212
@Inject(MAPPER_MAP) private readonly mapperMap: Map<string, AutoMapper>,
1313
@Inject(PROFILE_MAP)
1414
private readonly profileMap: Map<
15-
string,
16-
new (...args: any) => MappingProfile
15+
Constructible<MappingProfile>,
16+
Constructible<MappingProfile>
1717
>
1818
) {}
1919

@@ -25,7 +25,7 @@ export class AutomapperExplorer {
2525
this.profileMap.forEach(this.exploreProfile.bind(this));
2626
}
2727

28-
private exploreProfile(value: new (...args: any) => MappingProfile) {
28+
private exploreProfile(value: Constructible<MappingProfile>) {
2929
const mapperKey = this.reflector.get<string>('AUTO_MAPPER_PROFILE', value);
3030
const mapper = this.mapperMap.get(mapperKey);
3131

@@ -36,6 +36,9 @@ export class AutomapperExplorer {
3636
return;
3737
}
3838

39+
this.logger.log(
40+
`${value.name} added to Mapper ${mapperKey.split('__').pop()}`
41+
);
3942
mapper.addProfile(value);
4043
}
4144
}

src/automapper.module.ts

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
import { AutoMapper } from '@nartc/automapper';
1+
import { AutoMapper, AutoMapperGlobalSettings } from '@nartc/automapper';
22
import { DynamicModule, Global, Logger, Module } from '@nestjs/common';
33
import { OnModuleInit } from '@nestjs/common/interfaces';
44
import { AutomapperExplorer } from './automapper.explorer';
5-
import { forRootProviders } from './automapper.provider';
5+
import { forRootProviders, withMapperProviders } from './automapper.provider';
66
import { AutomapperModuleRootOptions } from './interfaces';
7+
import { getWithMapperArgs } from './utils/getWithMapperArgs';
78
import { MAPPER_MAP, MapperMap } from './utils/mapperMap';
89
import { PROFILE_MAP, ProfileMap } from './utils/profileMap';
910

@@ -12,17 +13,49 @@ import { PROFILE_MAP, ProfileMap } from './utils/profileMap';
1213
export class AutomapperModule implements OnModuleInit {
1314
private static readonly logger: Logger = new Logger('AutomapperModule');
1415

16+
/**
17+
* Initialize a Mapper with name and globalSettings
18+
*
19+
* @param {string} name - name of the Mapper instance. Default to 'default'
20+
* @param {AutoMapperGlobalSettings} globalSettings - Global Settings for the current Mapper instance
21+
*/
22+
static withMapper(
23+
name?: string,
24+
globalSettings?: AutoMapperGlobalSettings
25+
): DynamicModule;
26+
static withMapper(globalSettings?: AutoMapperGlobalSettings): DynamicModule;
27+
static withMapper(...args: any[]): DynamicModule {
28+
const [name, globalSettings] = getWithMapperArgs(args);
29+
const mapper = new AutoMapper();
30+
if (globalSettings != null) {
31+
mapper.withGlobalSettings(globalSettings);
32+
}
33+
34+
const providers = withMapperProviders(mapper, name);
35+
return {
36+
module: AutomapperModule,
37+
providers: [
38+
...providers,
39+
AutomapperExplorer,
40+
{ provide: PROFILE_MAP, useValue: ProfileMap },
41+
{ provide: MAPPER_MAP, useValue: MapperMap },
42+
{ provide: Logger, useValue: this.logger },
43+
],
44+
exports: providers,
45+
};
46+
}
47+
1548
/**
1649
* Initialize an AutoMapper instance with a name. Default to "default"
1750
*
1851
* Generally, `forRoot` only needs to be ran once to provide a singleton for the whole application
1952
*
2053
* @param {AutomapperModuleRootOptions} options
54+
* @deprecated Please use withMapper instead
2155
*/
2256
static forRoot(options?: AutomapperModuleRootOptions): DynamicModule {
2357
const mapper = new AutoMapper();
2458

25-
options && options.config && mapper.initialize(options.config);
2659
const providers = forRootProviders(mapper, options);
2760

2861
return {

src/automapper.provider.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ import { AutomapperModuleRootOptions } from './interfaces';
44
import { getMapperToken } from './utils/getMapperToken';
55
import { MapperMap } from './utils/mapperMap';
66

7+
/**
8+
*
9+
* @deprecated Will be removed soon
10+
*/
711
export const forRootProviders = (
812
mapper: AutoMapper,
913
options?: AutomapperModuleRootOptions
@@ -18,3 +22,18 @@ export const forRootProviders = (
1822
},
1923
];
2024
};
25+
26+
export const withMapperProviders = (
27+
mapper: AutoMapper,
28+
name: string
29+
): Provider[] => {
30+
const token = getMapperToken(name);
31+
!MapperMap.has(token) && MapperMap.set(token, mapper);
32+
33+
return [
34+
{
35+
provide: token,
36+
useValue: mapper,
37+
},
38+
];
39+
};

src/interfaces/automapper-configuration.interface.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
1-
import { AutoMapperConfiguration } from '@nartc/automapper';
2-
1+
/**
2+
* @deprecated Will be removed soon
3+
*/
34
export interface AutomapperModuleRootOptions {
45
/**
56
* Configuration Function to be ran when initialize a new AutoMapper instance
67
*
78
* @param {AutoMapperConfiguration} cfg
89
*/
9-
config?: (cfg: AutoMapperConfiguration) => void;
10+
config?: (cfg: any) => void;
1011

1112
/**
1213
* Name of the AutoMapper instance

src/interfaces/index.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,14 @@
1-
export { AutoMapper, MappingProfileBase, AutoMap } from '@nartc/automapper';
1+
export {
2+
AutoMapper,
3+
ProfileBase,
4+
AutoMap,
5+
mapWith,
6+
mapFrom,
7+
convertUsing,
8+
condition,
9+
preCondition,
10+
nullSubstitution,
11+
ignore,
12+
fromValue,
13+
} from '@nartc/automapper';
214
export * from './automapper-configuration.interface';

src/utils/getWithMapperArgs.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { AutoMapperGlobalSettings } from '@nartc/automapper';
2+
3+
export function getWithMapperArgs(
4+
args: any[]
5+
): [string, AutoMapperGlobalSettings?] {
6+
if (!args.length) {
7+
return [''];
8+
}
9+
10+
if (args.length === 2) {
11+
return [args[0], args[1]];
12+
}
13+
14+
if (args.length === 1) {
15+
const arg = args[0];
16+
if (typeof arg === 'string') {
17+
return [arg];
18+
}
19+
20+
return ['', arg];
21+
}
22+
23+
return [''];
24+
}

test/automapper.test.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { AutoMap, AutoMapper, MappingProfileBase } from '@nartc/automapper';
1+
import { AutoMap, AutoMapper, mapFrom, ProfileBase } from '@nartc/automapper';
22
import { Module } from '@nestjs/common';
33
import { Test, TestingModule } from '@nestjs/testing';
44
import { AutomapperModule, Profile } from '../src';
@@ -16,14 +16,14 @@ class MockVm {
1616
}
1717

1818
@Profile()
19-
class MockProfile extends MappingProfileBase {
19+
class MockProfile extends ProfileBase {
2020
constructor(mapper: AutoMapper) {
2121
super();
2222
mapper
2323
.createMap(Mock, MockVm)
2424
.forMember(
25-
d => d.bar,
26-
opts => opts.mapFrom(s => s.foo)
25+
(d) => d.bar,
26+
mapFrom((s) => s.foo)
2727
)
2828
.reverseMap();
2929
}
@@ -35,7 +35,7 @@ class Another {
3535
}
3636

3737
@Profile()
38-
class AnotherProfile extends MappingProfileBase {
38+
class AnotherProfile extends ProfileBase {
3939
constructor(mapper: AutoMapper) {
4040
super();
4141
mapper.createMap(Another, MockVm);

0 commit comments

Comments
 (0)