@coast/service-common 1.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.circleci/config.yml +56 -0
- package/.husky/pre-commit +2 -0
- package/.husky/prepare-commit-msg +9 -0
- package/.lintstagedrc +3 -0
- package/.nvmrc +1 -0
- package/LICENSE +21 -0
- package/README.md +1 -0
- package/eslint.config.cjs +45 -0
- package/jest.config.js +8 -0
- package/lib/modules/logger/CoastLogger.ts +9 -0
- package/lib/modules/logger/LoggerModule.ts +37 -0
- package/lib/modules/logger/PinoLogger.ts +61 -0
- package/lib/modules/logger/SilentLogger.ts +19 -0
- package/lib/modules/trace/Trace.ts +6 -0
- package/lib/modules/trace/TraceId.ts +7 -0
- package/lib/modules/trace/TraceInterceptor.ts +11 -0
- package/lib/modules/trace/TraceManager.ts +22 -0
- package/lib/modules/trace/TraceModule.ts +11 -0
- package/lib/modules/trace/Traceable.ts +3 -0
- package/package.json +61 -0
- package/tsconfig.json +31 -0
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
version: 2.1
|
|
2
|
+
jobs:
|
|
3
|
+
build:
|
|
4
|
+
docker:
|
|
5
|
+
- image: cimg/node:20.12.0
|
|
6
|
+
steps:
|
|
7
|
+
- checkout
|
|
8
|
+
- run: sudo corepack enable
|
|
9
|
+
- run: yarn install --immutable
|
|
10
|
+
- run: yarn lint
|
|
11
|
+
- run: yarn build
|
|
12
|
+
publish:
|
|
13
|
+
docker:
|
|
14
|
+
- image: 'cimg/node:20.12.0'
|
|
15
|
+
steps:
|
|
16
|
+
- checkout
|
|
17
|
+
- setup-git
|
|
18
|
+
- run: sudo corepack enable
|
|
19
|
+
- run: yarn install --immutable
|
|
20
|
+
- run: yarn build
|
|
21
|
+
- configure-yarn-publish
|
|
22
|
+
- run: yarn version patch
|
|
23
|
+
- run: git add package.json
|
|
24
|
+
- run: git commit -m "Bump version[skip ci]"
|
|
25
|
+
- run: git push
|
|
26
|
+
- run: yarn npm publish --access public
|
|
27
|
+
workflows:
|
|
28
|
+
build_and_publish:
|
|
29
|
+
jobs:
|
|
30
|
+
- build:
|
|
31
|
+
context: coast
|
|
32
|
+
- publish:
|
|
33
|
+
context: coast
|
|
34
|
+
requires:
|
|
35
|
+
- build
|
|
36
|
+
filters:
|
|
37
|
+
branches:
|
|
38
|
+
only: master
|
|
39
|
+
commands:
|
|
40
|
+
configure-yarn-publish:
|
|
41
|
+
steps:
|
|
42
|
+
- run: |
|
|
43
|
+
yarn config set npmAuthToken $NPM_TOKEN
|
|
44
|
+
yarn config set npmRegistryServer https://registry.npmjs.org/
|
|
45
|
+
setup-git:
|
|
46
|
+
steps:
|
|
47
|
+
- add_ssh_keys:
|
|
48
|
+
fingerprints:
|
|
49
|
+
- 'SHA256:J9pGnIRru8syuYQ6WPwHMkMYkggR+Z4j4uUKFr++iuI'
|
|
50
|
+
- run:
|
|
51
|
+
name: add github.com to known hosts
|
|
52
|
+
command: ssh-keyscan github.com >> ~/.ssh/known_hosts
|
|
53
|
+
- run: |
|
|
54
|
+
git config --global user.email "circleci@coastapp.com"
|
|
55
|
+
git config --global user.name "CircleCI"
|
|
56
|
+
git config --global push.default current
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
#!/bin/bash
|
|
2
|
+
FILE=$1
|
|
3
|
+
MESSAGE=$(cat $FILE)
|
|
4
|
+
TICKET="[$(git rev-parse --abbrev-ref HEAD | grep -Eo 'sc-[0-9]+')]" || true
|
|
5
|
+
if [[ $TICKET == "[]" || "$MESSAGE" == "$TICKET"* ]];then
|
|
6
|
+
exit 0;
|
|
7
|
+
fi
|
|
8
|
+
TICKET=$(echo ${TICKET} | tr [:lower:] [:upper:])
|
|
9
|
+
echo "$TICKET $MESSAGE" > $FILE
|
package/.lintstagedrc
ADDED
package/.nvmrc
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
20.12.2
|
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 coast-app
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
# service-common
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
const typescriptEslintPlugin = require('@typescript-eslint/eslint-plugin');
|
|
2
|
+
const typescriptEslintParser = require('@typescript-eslint/parser');
|
|
3
|
+
const eslintConfigPrettier = require('eslint-config-prettier');
|
|
4
|
+
const eslintPluginPrettier = require('eslint-plugin-prettier');
|
|
5
|
+
const { configs } = typescriptEslintPlugin;
|
|
6
|
+
module.exports = [
|
|
7
|
+
{
|
|
8
|
+
files: ['{lib,test}/**/*.ts', '{lib,test}/**/*.tsx'],
|
|
9
|
+
languageOptions: {
|
|
10
|
+
parser: typescriptEslintParser,
|
|
11
|
+
parserOptions: {
|
|
12
|
+
project: './tsconfig.json',
|
|
13
|
+
sourceType: 'module',
|
|
14
|
+
},
|
|
15
|
+
},
|
|
16
|
+
plugins: {
|
|
17
|
+
'@typescript-eslint': typescriptEslintPlugin,
|
|
18
|
+
prettier: eslintPluginPrettier,
|
|
19
|
+
},
|
|
20
|
+
rules: {
|
|
21
|
+
...configs.recommended.rules, // Recommended rules
|
|
22
|
+
...configs.strict.rules, // Strict rules
|
|
23
|
+
...eslintConfigPrettier.rules, // Disable Prettier conflicts
|
|
24
|
+
// Custom overrides
|
|
25
|
+
'@typescript-eslint/no-explicit-any': 'error',
|
|
26
|
+
'no-console': 'warn',
|
|
27
|
+
'@typescript-eslint/no-extraneous-class': ['error', {'allowWithDecorator': true}],
|
|
28
|
+
'prettier/prettier': [
|
|
29
|
+
'error',
|
|
30
|
+
{
|
|
31
|
+
printWidth: 130,
|
|
32
|
+
tabWidth: 4,
|
|
33
|
+
singleQuote: true,
|
|
34
|
+
trailingComma: 'es5',
|
|
35
|
+
arrowParens: 'always',
|
|
36
|
+
jsxBracketSameLine: true,
|
|
37
|
+
},
|
|
38
|
+
],
|
|
39
|
+
},
|
|
40
|
+
ignores: [
|
|
41
|
+
'node_modules/',
|
|
42
|
+
'dist/',
|
|
43
|
+
],
|
|
44
|
+
},
|
|
45
|
+
];
|
package/jest.config.js
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { LoggerService } from '@nestjs/common';
|
|
2
|
+
|
|
3
|
+
export abstract class CoastLogger implements LoggerService {
|
|
4
|
+
public abstract error(message: unknown, trace?: string): void;
|
|
5
|
+
public abstract log(message: unknown): void;
|
|
6
|
+
public abstract warn(message: unknown): void;
|
|
7
|
+
public abstract debug(message: unknown): void;
|
|
8
|
+
public abstract verbose(message: unknown): void;
|
|
9
|
+
}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { DynamicModule, Global, Module } from '@nestjs/common';
|
|
2
|
+
import { FactoryProvider, ValueProvider } from '@nestjs/common/interfaces';
|
|
3
|
+
import { ConfigModule, ConfigService } from '@nestjs/config';
|
|
4
|
+
import { PinoLogger } from './PinoLogger';
|
|
5
|
+
import { SilentLogger } from './SilentLogger';
|
|
6
|
+
import { CoastLogger } from './CoastLogger';
|
|
7
|
+
|
|
8
|
+
@Module({})
|
|
9
|
+
@Global()
|
|
10
|
+
export class LoggerModule {
|
|
11
|
+
public static forRootAsync(): DynamicModule {
|
|
12
|
+
const coastLogger: FactoryProvider = {
|
|
13
|
+
inject: [ConfigService],
|
|
14
|
+
provide: CoastLogger,
|
|
15
|
+
useFactory: (configService: ConfigService): PinoLogger => new PinoLogger(configService),
|
|
16
|
+
};
|
|
17
|
+
return {
|
|
18
|
+
module: LoggerModule,
|
|
19
|
+
imports: [ConfigModule],
|
|
20
|
+
providers: [coastLogger],
|
|
21
|
+
exports: [coastLogger],
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
public static disabled(): DynamicModule {
|
|
26
|
+
const coastLogger: ValueProvider = {
|
|
27
|
+
provide: CoastLogger,
|
|
28
|
+
useValue: new SilentLogger(),
|
|
29
|
+
};
|
|
30
|
+
return {
|
|
31
|
+
module: LoggerModule,
|
|
32
|
+
imports: [ConfigModule],
|
|
33
|
+
providers: [coastLogger],
|
|
34
|
+
exports: [coastLogger],
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
}
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import { Injectable } from '@nestjs/common';
|
|
2
|
+
import { ConfigService } from '@nestjs/config';
|
|
3
|
+
import Pino from 'pino';
|
|
4
|
+
import { CoastLogger } from './CoastLogger';
|
|
5
|
+
|
|
6
|
+
@Injectable()
|
|
7
|
+
export class PinoLogger extends CoastLogger {
|
|
8
|
+
private readonly instance: Pino.Logger;
|
|
9
|
+
|
|
10
|
+
public constructor(configService: ConfigService) {
|
|
11
|
+
super();
|
|
12
|
+
const level = configService.get('LOG_LEVEL');
|
|
13
|
+
const redact = [
|
|
14
|
+
'MYSQL_PASSWORD',
|
|
15
|
+
'STRIPE_SECRETKEY',
|
|
16
|
+
'JWT_SECRET',
|
|
17
|
+
'headers.authorization',
|
|
18
|
+
'user.passcode',
|
|
19
|
+
'user.lastName',
|
|
20
|
+
'user.phoneNumber',
|
|
21
|
+
'user.email',
|
|
22
|
+
];
|
|
23
|
+
const pino = configService.get('LOG_PRETTY')
|
|
24
|
+
? Pino({
|
|
25
|
+
level,
|
|
26
|
+
redact,
|
|
27
|
+
transport: {
|
|
28
|
+
target: 'pino-pretty',
|
|
29
|
+
options: {
|
|
30
|
+
colorize: true,
|
|
31
|
+
translateTime: true,
|
|
32
|
+
},
|
|
33
|
+
},
|
|
34
|
+
})
|
|
35
|
+
: Pino({
|
|
36
|
+
level,
|
|
37
|
+
redact,
|
|
38
|
+
});
|
|
39
|
+
this.instance = pino;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
public error(message: unknown, trace: string = ''): void {
|
|
43
|
+
this.instance.error(message, trace);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
public log(message: unknown): void {
|
|
47
|
+
this.instance.info(message);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
public warn(message: unknown): void {
|
|
51
|
+
this.instance.warn(message);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
public debug(message: unknown): void {
|
|
55
|
+
this.instance.debug(message);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
public verbose(message: unknown): void {
|
|
59
|
+
this.instance.trace(message);
|
|
60
|
+
}
|
|
61
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { CoastLogger } from './CoastLogger';
|
|
2
|
+
|
|
3
|
+
export class SilentLogger extends CoastLogger {
|
|
4
|
+
public log(): void {
|
|
5
|
+
return;
|
|
6
|
+
}
|
|
7
|
+
public error(): void {
|
|
8
|
+
return;
|
|
9
|
+
}
|
|
10
|
+
public warn(): void {
|
|
11
|
+
return;
|
|
12
|
+
}
|
|
13
|
+
public debug(): void {
|
|
14
|
+
return;
|
|
15
|
+
}
|
|
16
|
+
public verbose(): void {
|
|
17
|
+
return;
|
|
18
|
+
}
|
|
19
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { CallHandler, ExecutionContext, Inject, NestInterceptor } from '@nestjs/common';
|
|
2
|
+
import { Observable } from 'rxjs';
|
|
3
|
+
import { TraceManager } from './TraceManager';
|
|
4
|
+
|
|
5
|
+
export class TraceInterceptor implements NestInterceptor {
|
|
6
|
+
constructor(@Inject() private readonly traceManager: TraceManager) {}
|
|
7
|
+
|
|
8
|
+
public intercept(_context: ExecutionContext, next: CallHandler): Observable<unknown> | Promise<Observable<unknown>> {
|
|
9
|
+
return this.traceManager.withNewTrace(next.handle);
|
|
10
|
+
}
|
|
11
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { Trace } from './Trace';
|
|
2
|
+
import uuid from 'uuid';
|
|
3
|
+
import { Inject, Injectable } from '@nestjs/common';
|
|
4
|
+
import { AsyncLocalStorage } from 'node:async_hooks';
|
|
5
|
+
import { TraceId } from './TraceId';
|
|
6
|
+
|
|
7
|
+
@Injectable()
|
|
8
|
+
export class TraceManager {
|
|
9
|
+
constructor(@Inject() private readonly asyncLocalStorage: AsyncLocalStorage<Trace>) {}
|
|
10
|
+
|
|
11
|
+
public getTrace(): Trace | undefined {
|
|
12
|
+
return this.asyncLocalStorage.getStore();
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
public withNewTrace<T>(fn: () => T, traceId?: TraceId): T {
|
|
16
|
+
const trace: Trace = {
|
|
17
|
+
traceId: traceId ?? TraceId(uuid.v4()),
|
|
18
|
+
subtraceId: TraceId(uuid.v4()),
|
|
19
|
+
};
|
|
20
|
+
return this.asyncLocalStorage.run(trace, fn);
|
|
21
|
+
}
|
|
22
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { Module } from '@nestjs/common';
|
|
2
|
+
import { TraceManager } from './TraceManager';
|
|
3
|
+
import { TraceInterceptor } from './TraceInterceptor';
|
|
4
|
+
import { AsyncLocalStorage } from 'node:async_hooks';
|
|
5
|
+
import { Trace } from './Trace';
|
|
6
|
+
|
|
7
|
+
@Module({
|
|
8
|
+
providers: [AsyncLocalStorage<Trace>, TraceManager, TraceInterceptor],
|
|
9
|
+
exports: [TraceManager, TraceInterceptor],
|
|
10
|
+
})
|
|
11
|
+
export class TraceModule {}
|
package/package.json
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@coast/service-common",
|
|
3
|
+
"version": "1.0.1",
|
|
4
|
+
"description": "Common service package",
|
|
5
|
+
"main": "dist",
|
|
6
|
+
"types": "dist",
|
|
7
|
+
"engines": {
|
|
8
|
+
"node": ">=20.0.0"
|
|
9
|
+
},
|
|
10
|
+
"scripts": {
|
|
11
|
+
"prebuild": "rimraf dist",
|
|
12
|
+
"build": "yarn prebuild && yarn dlx @nestjs/cli build",
|
|
13
|
+
"lint": "eslint --fix"
|
|
14
|
+
},
|
|
15
|
+
"dependencies": {
|
|
16
|
+
"@paradoxical-io/types": "^1.0.7",
|
|
17
|
+
"pino": "^9.6.0",
|
|
18
|
+
"uuid": "^11.0.5"
|
|
19
|
+
},
|
|
20
|
+
"devDependencies": {
|
|
21
|
+
"@faker-js/faker": "^9.4.0",
|
|
22
|
+
"@nestjs/cli": "^10.4.4",
|
|
23
|
+
"@nestjs/common": "^10.4.1",
|
|
24
|
+
"@nestjs/config": "^3.2.3",
|
|
25
|
+
"@nestjs/schematics": "^10.1.4",
|
|
26
|
+
"@types/jest": "^29.5.12",
|
|
27
|
+
"@types/node": "^22.5.0",
|
|
28
|
+
"@types/supertest": "^6.0.2",
|
|
29
|
+
"@typescript-eslint/eslint-plugin": "^8.3.0",
|
|
30
|
+
"@typescript-eslint/parser": "^8.3.0",
|
|
31
|
+
"dotenv": "^16.4.7",
|
|
32
|
+
"eslint": "^9.9.1",
|
|
33
|
+
"eslint-config-prettier": "^9.1.0",
|
|
34
|
+
"eslint-plugin-prettier": "^5.2.1",
|
|
35
|
+
"husky": "^9.1.7",
|
|
36
|
+
"jest": "^29.7.0",
|
|
37
|
+
"lint-staged": "^15.4.1",
|
|
38
|
+
"prettier": "^3.3.3",
|
|
39
|
+
"reflect-metadata": "^0.2.2",
|
|
40
|
+
"rimraf": "^6.0.1",
|
|
41
|
+
"rxjs": "^7.8.1",
|
|
42
|
+
"supertest": "^7.0.0",
|
|
43
|
+
"ts-jest": "^29.2.5",
|
|
44
|
+
"ts-node": "^10.9.2",
|
|
45
|
+
"tsconfig-paths": "^4.2.0",
|
|
46
|
+
"typescript": "^5",
|
|
47
|
+
"typescript-eslint": "^8.3.0"
|
|
48
|
+
},
|
|
49
|
+
"peerDependencies": {
|
|
50
|
+
"@nestjs/common": "*",
|
|
51
|
+
"@nestjs/config": "*",
|
|
52
|
+
"reflect-metadata": "*",
|
|
53
|
+
"rxjs": "*"
|
|
54
|
+
},
|
|
55
|
+
"author": "Coast Engineerring",
|
|
56
|
+
"license": "MIT",
|
|
57
|
+
"publishConfig": {
|
|
58
|
+
"access": "public"
|
|
59
|
+
},
|
|
60
|
+
"packageManager": "yarn@4.5.2"
|
|
61
|
+
}
|
package/tsconfig.json
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
"outDir": "./dist",
|
|
4
|
+
"baseUrl": "./",
|
|
5
|
+
"alwaysStrict": true,
|
|
6
|
+
"strict": true,
|
|
7
|
+
"allowSyntheticDefaultImports": true,
|
|
8
|
+
"forceConsistentCasingInFileNames": true,
|
|
9
|
+
"emitDecoratorMetadata": true,
|
|
10
|
+
"esModuleInterop": true,
|
|
11
|
+
"experimentalDecorators": true,
|
|
12
|
+
"declaration": true,
|
|
13
|
+
"module": "commonjs",
|
|
14
|
+
"moduleResolution": "node",
|
|
15
|
+
"noFallthroughCasesInSwitch": false,
|
|
16
|
+
"noImplicitAny": true,
|
|
17
|
+
"noImplicitReturns": false,
|
|
18
|
+
"noImplicitThis": true,
|
|
19
|
+
"noUnusedLocals": true,
|
|
20
|
+
"noUnusedParameters": true,
|
|
21
|
+
"removeComments": true,
|
|
22
|
+
"resolveJsonModule": true,
|
|
23
|
+
"rootDir": "./lib",
|
|
24
|
+
"strictNullChecks": true,
|
|
25
|
+
"skipLibCheck": true,
|
|
26
|
+
"target": "es2021"
|
|
27
|
+
},
|
|
28
|
+
"compileOnSave": true,
|
|
29
|
+
"include": ["lib/**/*.ts", "test/**/*.ts"],
|
|
30
|
+
"exclude": ["node_modules", "dist"]
|
|
31
|
+
}
|