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

Commit 1b344ed

Browse files
author
Chau Tran
committed
feat(index.rts): migrated
1 parent eeffcae commit 1b344ed

16 files changed

Lines changed: 4322 additions & 130 deletions

.travis.yml

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
language: node_js
2+
cache:
3+
directories:
4+
- ~/.npm
5+
notifications:
6+
email: false
7+
node_js:
8+
- '10'
9+
- '11'
10+
- '12'
11+
script:
12+
- npm run test
13+
14+
jobs:
15+
include:
16+
- stage: Production
17+
before_script:
18+
- npm run build:docs
19+
node_js:
20+
- '12'
21+
name: deploy_docs
22+
script: if [ "$TRAVIS_BRANCH" = "master" -a "$TRAVIS_PULL_REQUEST" = "false" ]; then npm run deploy-docs; fi
23+
- name: npm_release
24+
node_js:
25+
- '12'
26+
before_script:
27+
- npm run build
28+
script: if [ "$TRAVIS_BRANCH" = "master" -a "$TRAVIS_PULL_REQUEST" = "false" ]; then npm run semantic-release; fi
29+
- name: produce_coverage
30+
node_js:
31+
- '12'
32+
before_script:
33+
- npm run test:cov
34+
script: if [ "$TRAVIS_BRANCH" = "master" -a "$TRAVIS_PULL_REQUEST" = "false" ]; then npm run report-coverage; fi
35+
branches:
36+
except:
37+
- /^v\d+\.\d+\.\d+$/

package.json

Lines changed: 87 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
2-
"name": "nest-automapper",
3-
"version": "0.1.0",
2+
"name": "nestjsx-automapper",
3+
"version": "0.0.0-development",
44
"license": "MIT",
55
"author": "Chau Tran",
66
"main": "dist/index.js",
@@ -9,16 +9,33 @@
99
"files": [
1010
"dist"
1111
],
12+
"keywords": [
13+
"nestjs",
14+
"automapper",
15+
"typescript"
16+
],
17+
"repository": {
18+
"type": "git",
19+
"url": "https://github.com/nestjsx/automapper.git"
20+
},
21+
"engines": {
22+
"node": ">=8.0.0"
23+
},
1224
"scripts": {
1325
"start": "tsdx watch",
1426
"build": "tsdx build",
15-
"test": "tsdx test",
16-
"lint": "tsdx lint"
27+
"build:docs": "typedoc --out docs --target es6 --theme minimal --mode file src",
28+
"test": "tsdx test --no-cache",
29+
"test:cov": "tsdx test --coverage",
30+
"lint": "tsdx lint src && lint-staged",
31+
"commit": "git-cz",
32+
"semantic-release": "semantic-release",
33+
"deploy-docs": "ts-node tools/gh-pages",
34+
"report-coverage": "cat ./coverage/lcov.info | ./node_modules/coveralls/bin/coveralls.js && rm -rf ./coverage"
1735
},
18-
"peerDependencies": {},
1936
"husky": {
2037
"hooks": {
21-
"pre-commit": "tsdx lint"
38+
"pre-commit": "npm run test && git add . && npm run lint"
2239
}
2340
},
2441
"prettier": {
@@ -27,11 +44,70 @@
2744
"singleQuote": true,
2845
"trailingComma": "es5"
2946
},
47+
"commitlint": {
48+
"extends": [
49+
"@commitlint/config-conventional"
50+
]
51+
},
52+
"config": {
53+
"commitizen": {
54+
"path": "node_modules/cz-conventional-changelog"
55+
}
56+
},
57+
"lint-staged": {
58+
"{src}/**/*.ts": [
59+
"prettier --write",
60+
"git add"
61+
]
62+
},
63+
"publishConfig": {
64+
"access": "public"
65+
},
66+
"jest": {
67+
"transform": {
68+
".(ts|tsx)": "ts-jest"
69+
},
70+
"testEnvironment": "node",
71+
"moduleFileExtensions": [
72+
"ts",
73+
"tsx",
74+
"js"
75+
],
76+
"coveragePathIgnorePatterns": [
77+
"/node_modules/",
78+
"/test/"
79+
],
80+
"collectCoverageFrom": [
81+
"src/automapper.{js,ts}"
82+
]
83+
},
3084
"devDependencies": {
31-
"@types/jest": "^24.0.21",
32-
"husky": "^3.0.9",
33-
"tsdx": "^0.11.0",
34-
"tslib": "^1.10.0",
35-
"typescript": "^3.6.4"
85+
"@commitlint/cli": "8.2.0",
86+
"@commitlint/config-conventional": "8.2.0",
87+
"@nestjs/common": "6.8.5",
88+
"@nestjs/core": "6.8.5",
89+
"@nestjs/testing": "6.8.5",
90+
"@types/jest": "24.0.21",
91+
"commitizen": "4.0.3",
92+
"coveralls": "3.0.7",
93+
"cz-conventional-changelog": "3.0.2",
94+
"husky": "3.0.9",
95+
"lint-staged": "9.4.2",
96+
"prettier": "1.18.2",
97+
"reflect-metadata": "0.1.13",
98+
"semantic-release": "^15.13.30",
99+
"shelljs": "0.8.3",
100+
"ts-node": "8.4.1",
101+
"tsdx": "0.11.0",
102+
"tslib": "1.10.0",
103+
"typedoc": "0.15.0",
104+
"typescript": "3.7.2"
105+
},
106+
"dependencies": {
107+
"@nartc/automapper": "^1.1.8"
108+
},
109+
"peerDependencies": {
110+
"@nestjs/common": "~6.8.5",
111+
"reflect-metadata": "~0.1.13"
36112
}
37113
}

src/automapper.module.ts

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import { AutoMapper } from '@nartc/automapper';
2+
import { DynamicModule, Global, Logger, Module } from '@nestjs/common';
3+
import { forFeatureProviders, forRootProviders } from './automapper.provider';
4+
import {
5+
AutomapperModuleFeatureOptions,
6+
AutomapperModuleRootOptions,
7+
} from './interfaces';
8+
9+
@Global()
10+
@Module({})
11+
export class AutomapperModule {
12+
private static readonly logger: Logger = new Logger('AutomapperModule');
13+
14+
/**
15+
* Initialize an AutoMapper instance with a name. Default to "default"
16+
*
17+
* Generally, `forRoot` only needs to be ran once to provide a singleton for the whole application
18+
*
19+
* @param {AutomapperModuleRootOptions} options
20+
*/
21+
static forRoot(options?: AutomapperModuleRootOptions): DynamicModule {
22+
const mapper = new AutoMapper();
23+
24+
options && options.config && mapper.initialize(options.config);
25+
const providers = forRootProviders(options);
26+
27+
return {
28+
module: AutomapperModule,
29+
providers,
30+
exports: providers,
31+
};
32+
}
33+
34+
/**
35+
* Add to the AutoMapper instance a list of MappingProfiles. By default, the instance with name "default" will be
36+
* used.
37+
*
38+
* @param {AutomapperModuleFeatureOptions} options
39+
*/
40+
static forFeature(options: AutomapperModuleFeatureOptions): DynamicModule {
41+
if (!options || (options && !options.profiles)) {
42+
const message = 'AutomapperModuleFeatureOptions.profiles is empty';
43+
this.logger.error(message);
44+
throw new Error(message);
45+
}
46+
const providers = forFeatureProviders(options);
47+
return {
48+
module: AutomapperModule,
49+
imports: [AutomapperModule],
50+
providers,
51+
exports: providers,
52+
};
53+
}
54+
}

src/automapper.provider.ts

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import { AutoMapper } from '@nartc/automapper';
2+
import { Provider } from '@nestjs/common';
3+
import {
4+
AutomapperModuleFeatureOptions,
5+
AutomapperModuleRootOptions,
6+
} from './interfaces';
7+
import { getMapperToken } from './utils/getMapperToken';
8+
import { MAPPER_MAP, MapperMap } from './utils/mapperMap';
9+
10+
export const forRootProviders = (
11+
options?: AutomapperModuleRootOptions
12+
): Provider[] => {
13+
const mapper = new AutoMapper();
14+
const token = getMapperToken(options ? options.name : '');
15+
!MapperMap.has(token) && MapperMap.set(token, mapper);
16+
17+
return [
18+
{
19+
provide: token,
20+
useValue: mapper,
21+
},
22+
{
23+
provide: MAPPER_MAP,
24+
useValue: MapperMap,
25+
},
26+
];
27+
};
28+
29+
export const forFeatureProviders = (
30+
options: AutomapperModuleFeatureOptions
31+
): Provider[] => {
32+
const token = getMapperToken(options ? options.name : '');
33+
const mapper = MapperMap.has(token)
34+
? (MapperMap.get(token) as AutoMapper)
35+
: new AutoMapper();
36+
37+
options.profiles.forEach(mapper.addProfile.bind(mapper));
38+
!MapperMap.has(token) && MapperMap.set(token, mapper);
39+
40+
return [
41+
{
42+
provide: token,
43+
useValue: mapper,
44+
},
45+
];
46+
};

src/decorators/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from './inject-mapper.decorator';
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { Inject } from '@nestjs/common';
2+
import { getMapperToken } from '../utils/getMapperToken';
3+
4+
/**
5+
* Inject the AutoMapper intsance with name.
6+
*
7+
* @param {string} name - Name of the AutoMapper instance
8+
* @default default
9+
*/
10+
export const InjectMapper = (name?: string) => Inject(getMapperToken(name));

src/index.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
export const sum = (a: number, b: number) => {
2-
if ('development' === process.env.NODE_ENV) {
3-
console.log('boop');
4-
}
5-
return a + b;
6-
};
1+
export * from './automapper.module';
2+
export * from './interfaces';
3+
export * from './decorators';
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import { AutoMapperConfiguration, MappingProfile } from '@nartc/automapper';
2+
3+
export interface AutomapperModuleRootOptions {
4+
/**
5+
* Configuration Function to be ran when initialize a new AutoMapper instance
6+
*
7+
* @param {AutoMapperConfiguration} cfg
8+
*/
9+
config?: (cfg: AutoMapperConfiguration) => void;
10+
11+
/**
12+
* Name of the AutoMapper instance
13+
*
14+
* @default default
15+
*/
16+
name?: string;
17+
}
18+
19+
export interface AutomapperModuleFeatureOptions {
20+
/**
21+
* An array of MappingProfile to be added to the AutoMapper instance
22+
*/
23+
profiles: Array<MappingProfile>;
24+
25+
/**
26+
* Name of the AutoMapper instance
27+
*
28+
* @default default
29+
*/
30+
name?: string;
31+
}

src/interfaces/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from './automapper-configuration.interface';

src/utils/getMapperToken.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
const AUTOMAPPER = 'nestjs__AUTO_MAPPER__';
2+
export const getMapperToken = (name?: string) =>
3+
name ? AUTOMAPPER + name : AUTOMAPPER + 'default';

0 commit comments

Comments
 (0)