-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d76144e
commit 0948826
Showing
30 changed files
with
719 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,6 @@ | ||
PORT=3000 | ||
GATEWAY_PORT=3052 | ||
GATEWAY_SUBGRAPHS='[{"name": "api", "url": "http://localhost:3000/graphql"}]' | ||
|
||
NATS_URL=nats://localhost:4222 | ||
SAMPLE_DATABASE_URL=postgres://postgres:postgres@localhost:5432/postgres?schema=public |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
{ | ||
"extends": ["../../../.eslintrc.json"], | ||
"ignorePatterns": ["!**/*"], | ||
"overrides": [ | ||
{ | ||
"files": ["*.ts", "*.tsx", "*.js", "*.jsx"], | ||
"rules": {} | ||
}, | ||
{ | ||
"files": ["*.ts", "*.tsx"], | ||
"rules": {} | ||
}, | ||
{ | ||
"files": ["*.js", "*.jsx"], | ||
"rules": {} | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
# common | ||
|
||
This library was generated with [Nx](https://nx.dev). | ||
|
||
## Building | ||
|
||
Run `nx build common` to build the library. | ||
|
||
## Running unit tests | ||
|
||
Run `nx test common` to execute the unit tests via [Jest](https://jestjs.io). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
/* eslint-disable */ | ||
export default { | ||
displayName: 'common', | ||
preset: '../../../jest.preset.js', | ||
testEnvironment: 'node', | ||
transform: { | ||
'^.+\\.[tj]s$': [ | ||
'ts-jest', | ||
{ tsconfig: '<rootDir>/tsconfig.spec.json' }, | ||
], | ||
}, | ||
moduleFileExtensions: ['ts', 'js', 'html'], | ||
coverageDirectory: '../../../coverage/libs/core/common', | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
{ | ||
"name": "common", | ||
"$schema": "../../../node_modules/nx/schemas/project-schema.json", | ||
"sourceRoot": "libs/core/common/src", | ||
"projectType": "library", | ||
"targets": { | ||
"lint": { | ||
"executor": "@nx/linter:eslint", | ||
"outputs": ["{options.outputFile}"], | ||
"options": { | ||
"lintFilePatterns": ["libs/core/common/**/*.ts"] | ||
} | ||
}, | ||
"test": { | ||
"executor": "@nx/jest:jest", | ||
"outputs": ["{workspaceRoot}/coverage/{projectRoot}"], | ||
"options": { | ||
"jestConfig": "libs/core/common/jest.config.ts", | ||
"passWithNoTests": true | ||
}, | ||
"configurations": { | ||
"ci": { | ||
"ci": true, | ||
"codeCoverage": true | ||
} | ||
} | ||
} | ||
}, | ||
"tags": [] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from './lib/errors/common-errors.enum' | ||
export * from './lib/errors/common.error' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
export enum CommonErrorsEnum { | ||
Error = 'ERROR-000', | ||
NotFound = 'ERROR-001', | ||
Forbidden = 'ERROR-002', | ||
UnexpectedError = 'ERROR-003', | ||
ValidationError = 'ERROR-004', | ||
UniqueError = 'ERROR-005', | ||
RequestError = 'ERROR-006', | ||
UnauthorizedError = 'ERROR-007', | ||
} | ||
|
||
export const COMMON_ERROR_TITLES = { | ||
[CommonErrorsEnum.Error]: 'Error', | ||
[CommonErrorsEnum.NotFound]: 'Not found', | ||
[CommonErrorsEnum.Forbidden]: 'Forbidden', | ||
[CommonErrorsEnum.UnexpectedError]: 'Unexpected error', | ||
[CommonErrorsEnum.ValidationError]: 'Validation error', | ||
[CommonErrorsEnum.UniqueError]: 'Unique error', | ||
[CommonErrorsEnum.RequestError]: 'Request error', | ||
[CommonErrorsEnum.UnauthorizedError]: 'Unauthorized', | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import { COMMON_ERROR_TITLES, CommonErrorsEnum } from './common-errors.enum' | ||
|
||
export class CommonError< | ||
T extends string = CommonErrorsEnum, | ||
M = Record<string, string>, | ||
> extends Error { | ||
code: T | ||
metadata?: M | ||
|
||
constructor( | ||
code: T, | ||
message?: { [code: string]: string } | string, | ||
metadata?: M, | ||
) { | ||
super( | ||
message === undefined | ||
? COMMON_ERROR_TITLES[ | ||
(code as string) || | ||
COMMON_ERROR_TITLES[CommonErrorsEnum.Error] | ||
] | ||
: typeof message === 'string' | ||
? message | ||
: message[code], | ||
) | ||
this.code = code | ||
this.metadata = metadata | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
{ | ||
"extends": "../../../tsconfig.base.json", | ||
"compilerOptions": { | ||
"module": "commonjs" | ||
}, | ||
"files": [], | ||
"include": [], | ||
"references": [ | ||
{ | ||
"path": "./tsconfig.lib.json" | ||
}, | ||
{ | ||
"path": "./tsconfig.spec.json" | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
{ | ||
"extends": "./tsconfig.json", | ||
"compilerOptions": { | ||
"outDir": "../../../dist/out-tsc", | ||
"declaration": true, | ||
"types": ["node"], | ||
"target": "es6" | ||
}, | ||
"include": ["src/**/*.ts"], | ||
"exclude": ["jest.config.ts", "src/**/*.spec.ts", "src/**/*.test.ts"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
{ | ||
"extends": "./tsconfig.json", | ||
"compilerOptions": { | ||
"outDir": "../../../dist/out-tsc", | ||
"module": "commonjs", | ||
"types": ["jest", "node"] | ||
}, | ||
"include": [ | ||
"jest.config.ts", | ||
"src/**/*.test.ts", | ||
"src/**/*.spec.ts", | ||
"src/**/*.d.ts" | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
{ | ||
"extends": ["../../../.eslintrc.json"], | ||
"ignorePatterns": ["!**/*"], | ||
"overrides": [ | ||
{ | ||
"files": ["*.ts", "*.tsx", "*.js", "*.jsx"], | ||
"rules": {} | ||
}, | ||
{ | ||
"files": ["*.ts", "*.tsx"], | ||
"rules": {} | ||
}, | ||
{ | ||
"files": ["*.js", "*.jsx"], | ||
"rules": {} | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
# nats | ||
|
||
This library was generated with [Nx](https://nx.dev). | ||
|
||
## Building | ||
|
||
Run `nx build nats` to build the library. | ||
|
||
## Running unit tests | ||
|
||
Run `nx test nats` to execute the unit tests via [Jest](https://jestjs.io). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
/* eslint-disable */ | ||
export default { | ||
displayName: 'nats', | ||
preset: '../../../jest.preset.js', | ||
testEnvironment: 'node', | ||
transform: { | ||
'^.+\\.[tj]s$': [ | ||
'ts-jest', | ||
{ tsconfig: '<rootDir>/tsconfig.spec.json' }, | ||
], | ||
}, | ||
moduleFileExtensions: ['ts', 'js', 'html'], | ||
coverageDirectory: '../../../coverage/libs/core/nats', | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
{ | ||
"name": "nats", | ||
"$schema": "../../../node_modules/nx/schemas/project-schema.json", | ||
"sourceRoot": "libs/core/nats/src", | ||
"projectType": "library", | ||
"targets": { | ||
"lint": { | ||
"executor": "@nx/linter:eslint", | ||
"outputs": ["{options.outputFile}"], | ||
"options": { | ||
"lintFilePatterns": ["libs/core/nats/**/*.ts"] | ||
} | ||
}, | ||
"test": { | ||
"executor": "@nx/jest:jest", | ||
"outputs": ["{workspaceRoot}/coverage/{projectRoot}"], | ||
"options": { | ||
"jestConfig": "libs/core/nats/jest.config.ts", | ||
"passWithNoTests": true | ||
}, | ||
"configurations": { | ||
"ci": { | ||
"ci": true, | ||
"codeCoverage": true | ||
} | ||
} | ||
} | ||
}, | ||
"tags": [] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from './lib/nats-configs/nats-module.config' | ||
export * from './lib/nats.module' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import { ConfigurableModuleBuilder } from '@nestjs/common' | ||
import { merge } from 'lodash' | ||
import { ConnectionOptions } from 'nats' | ||
|
||
export interface NatsConfig extends ConnectionOptions { | ||
temp?: false | ||
} | ||
|
||
export const DEFAULT_NATS_CONFIG: Pick<NatsConfig, 'temp'> = { | ||
temp: false, | ||
} | ||
|
||
export function patchNatsConfig( | ||
config: Pick<typeof NATS_OPTIONS_TYPE, keyof typeof DEFAULT_NATS_CONFIG>, | ||
) { | ||
if (config) { | ||
Object.assign( | ||
config, | ||
merge(DEFAULT_NATS_CONFIG, config), | ||
) as typeof NATS_OPTIONS_TYPE | ||
} | ||
return config | ||
} | ||
|
||
export const { | ||
ConfigurableModuleClass: NatsConfigurableModuleClass, | ||
MODULE_OPTIONS_TOKEN: NATS_CONFIG, | ||
ASYNC_OPTIONS_TYPE: NATS_ASYNC_OPTIONS_TYPE, | ||
OPTIONS_TYPE: NATS_OPTIONS_TYPE, | ||
} = new ConfigurableModuleBuilder<NatsConfig, 'forRoot'>({ | ||
optionsInjectionToken: `NATS_CONFIG`, | ||
}).build() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
export enum NatsErrorsEnum { | ||
Timeout = 'NATS-000', | ||
NoResponders = 'NATS-001', | ||
} | ||
|
||
export const NATS_ERROR_TITLES = { | ||
[NatsErrorsEnum.Timeout]: `Someone is listening but didn't respond`, | ||
[NatsErrorsEnum.NoResponders]: `No one is listening to this request`, | ||
} |
35 changes: 35 additions & 0 deletions
35
libs/core/nats/src/lib/nats-indicators/nats-connection-health.indicator.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import { Injectable } from '@nestjs/common' | ||
import { HealthIndicator, HealthIndicatorResult } from 'core/health-checks' | ||
import { NatsConnectionService } from '../nats-services/nats-connection.service' | ||
|
||
@Injectable() | ||
export class NatsConnectionHealthIndicator implements HealthIndicator { | ||
name = 'nats' | ||
|
||
constructor(private readonly natsConnection: NatsConnectionService) {} | ||
|
||
async isHealthy(): Promise<HealthIndicatorResult> { | ||
try { | ||
const status = this.natsConnection.status() | ||
|
||
if (status.connected) { | ||
return { | ||
name: this.name, | ||
status: 'up', | ||
} | ||
} | ||
|
||
return { | ||
name: this.name, | ||
status: 'down', | ||
error: status.error, | ||
} | ||
} catch (error) { | ||
return { | ||
name: this.name, | ||
status: 'down', | ||
error: error.message, | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { MsgHdrs, RequestOptions as NatsRequestOptions } from 'nats' | ||
|
||
export interface NatsResponse<T> { | ||
subject: string | ||
data?: T | ||
headers?: MsgHdrs | ||
} | ||
|
||
export interface RequestOptions | ||
extends Omit<NatsRequestOptions, 'headers' | 'timeout'> { | ||
timeout?: number | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
headers: Record<string, string> | ||
} |
Oops, something went wrong.