Skip to content

Commit

Permalink
Merge branch 'release/0.2.0' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
draconisNoctis committed Feb 18, 2022
2 parents 00dc32e + d77ce87 commit d756b88
Show file tree
Hide file tree
Showing 37 changed files with 1,404 additions and 2,128 deletions.
2 changes: 1 addition & 1 deletion .nvmrc
Original file line number Diff line number Diff line change
@@ -1 +1 @@
v12.8.0
v16.14.0
96 changes: 57 additions & 39 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,68 +1,86 @@
{
"name": "@neoskop/nestjs",
"version": "0.1.0",
"version": "0.2.0",
"description": "Collection of useful NestJS Modules",
"main": "index.js",
"repository": "https://github.com/neoskop/nestjs",
"author": "Mark Wecke <[email protected]>",
"license": "MIT",
"scripts": {
"prebuild": "rm -r dist",
"prebuild": "rm -rf dist",
"build": "tsc",
"postbuild": "jq 'del(.scripts) | del(.devDependencies)' package.json > dist/package.json && cp README.md dist",
"publish-next": "npm run build && npm publish dist --tag next",
"publish-latest-only": "npm run build && npm publish dist",
"publish-next": "npm run build && cd dist && npm publish --tag next",
"publish-latest-only": "npm run build && cd dist && npm publish",
"publish-latest": "npm run publish-latest-only && npm dist-tag add @neoskop/nestjs@`jq '.version' package.json -r` next"
},
"devDependencies": {
"@angular/core": "7.2.14",
"@angular/platform-server": "7.2.14",
"@angular/router": "7.2.14",
"@angular/common": "12",
"@angular/core": "12",
"@angular/platform-server": "12",
"@angular/router": "12",
"@neoskop/adamant": "0.3.0-rc.6",
"@neoskop/annotation-factory": "1.0.1",
"@neoskop/hrbac": "1.0.1",
"@neoskop/hrbac": "2.0.0-rc.1",
"@neoskop/phantom": "1.3.0",
"@nestjs/common": "6.1.1",
"@nestjs/core": "6.1.1",
"@nestjs/graphql": "6.0.5",
"@nguniversal/module-map-ngfactory-loader": "7.1.1",
"@types/cookie-parser": "1.4.1",
"@types/express": "4.16.1",
"@types/http-proxy-middleware": "0.19.2",
"@nestjs/common": "8",
"@nestjs/core": "8",
"@nestjs/graphql": "8",
"@nestjs/terminus": "8",
"@types/cookie-parser": "1.4.2",
"@types/express": "4.17.1",
"@types/http-proxy-middleware": "^1.0.0",
"@types/json-schema": "7.0.3",
"@types/node": "12.0.0",
"@types/node": "12.7.5",
"@types/oauth2-server": "3.0.12",
"@types/pouchdb": "6.4.0",
"@types/simple-oauth2": "2.2.1",
"@types/winston": "2.4.4",
"ajv": "6.10.0",
"ajv": "6.10.2",
"cookie-parser": "^1.4.4",
"http-proxy-middleware": "0.19.1",
"http-proxy-middleware": "^2.0.0",
"oauth2-server": "3.0.1",
"reflect-metadata": "0.1.13",
"rxjs": "6.5.1",
"simple-oauth2": "2.2.1",
"typescript": "3.4.5",
"rxjs": "6.5.3",
"simple-oauth2": "2.4.0",
"typescript": "^4.2.4",
"winston": "3.2.1",
"zone.js": "0.9.1"
"zone.js": "0.10.2"
},
"dependencies": {
"tslib": "1.9.3"
"import-fresh": "^3.3.0",
"tslib": "^2.2.0"
},
"peerDependencies": {
"@angular/common": "*",
"@angular/core": "*",
"@angular/platform-server": "^7.2.9",
"@neoskop/adamant": "^0.3.0-rc.5",
"@neoskop/hrbac": "^1.0.1",
"@neoskop/phantom": "^1.3.0",
"@nestjs/common": "^6.1.1",
"@nestjs/core": "^6.1.1",
"@nestjs/graphql": "^6.0.5",
"@nguniversal/module-map-ngfactory-loader": "^7.1.1",
"ajv": "^6.10.0",
"cookie-parser": "^1.4.4",
"http-proxy-middleware": "^0.19.1",
"reflect-metadata": "^0.1.12",
"rxjs": "^6.0.0",
"simple-oauth2": "^2.2.1",
"winston": "^3.2.1",
"zone.js": "0.9.1"
"@angular/platform-server": "*",
"@neoskop/adamant": "*",
"@neoskop/hrbac": ">=2.0.0-rc.1",
"@neoskop/phantom": "*",
"@nestjs/common": "*",
"@nestjs/core": "*",
"@nestjs/graphql": "*",
"@nestjs/terminus": "*",
"ajv": "*",
"cookie-parser": "*",
"http-proxy-middleware": "*",
"oauth2-server": "*",
"reflect-metadata": "*",
"rxjs": "*",
"simple-oauth2": "*",
"winston": "*",
"zone.js": "*"
},
"prettier": {
"trailingComma": "none",
"tabWidth": 4,
"arrowParens": "avoid",
"bracketSpacing": true,
"endOfLine": "lf",
"htmlWhitespaceSensitivity": "css",
"printWidth": 140,
"quoteProps": "as-needed",
"singleQuote": true
}
}
40 changes: 40 additions & 0 deletions src/adamant/adamant.health.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { AdamantConnectionManager } from '@neoskop/adamant';
import { Injectable } from '@nestjs/common';
import { HealthIndicator } from '@nestjs/terminus';
import url from 'url';

const TIMEOUT = Symbol.for('Timeout');

@Injectable()
export class AdamantHealthIndicator extends HealthIndicator {
constructor(protected readonly manager : AdamantConnectionManager) {
super();
}

async pingCheck(key: string, { timeout = 1000 }: { timeout?: number } = {}) {
let isHealthy = true;
const info : { [key: string]: boolean } = {}
for(const conn of this.manager.getOpenConnections()) {
let isConnectionHealthy : boolean;
try {
const res = await this.timeout(conn.info(), timeout)
isConnectionHealthy = res !== TIMEOUT;
} catch {
isConnectionHealthy = false;
}
info[url.parse(conn.name).pathname!.substr(1)] = isConnectionHealthy;
isHealthy = isHealthy && isConnectionHealthy;
}

return this.getStatus(key, isHealthy, info);
}

private timeout<T>(promise : Promise<T>, timeout : number) : Promise<T|typeof TIMEOUT> {
return Promise.race([
promise,
new Promise<typeof TIMEOUT>(resolve => {
setTimeout(() => resolve(TIMEOUT), timeout)
})
])
}
}
30 changes: 23 additions & 7 deletions src/adamant/adamant.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,20 @@ import {
} from '@neoskop/adamant';
import { DynamicModule, Global, Module, Provider, Type, Inject } from '@nestjs/common';
import { ModuleMetadata } from '@nestjs/common/interfaces';
import { AdamantHealthIndicator } from './adamant.health';


export interface AdamantOptions {
factory: ConnectionFactory;
designDocs?: Type<any>[];
providers?: any[];
viewWarmup?: 'sync' | 'async' | 'none' | false | null
}

export interface AdamantAsyncOptions extends Pick<ModuleMetadata, 'imports'> {
providers?: any[];
designDocs?: Type<any>[];
viewWarmup?: 'sync' | 'async' | 'none' | false | null
useExisting?: Type<AdamantConnectionFactoryFactory>;
useClass?: Type<AdamantConnectionFactoryFactory>;
useFactory?: (...args : any[]) => Promise<ConnectionFactory> | ConnectionFactory,
Expand All @@ -38,6 +41,7 @@ export interface AdamantConnectionFactoryFactory {
export const ADAMANT_PROVIDERS = 'ADAMANT_PROVIDERS';
export const ADAMANT_DESIGN_DOCS = 'ADAMANT_DESIGN_DOCS';
export const ADAMANT_PROVIDER_VALUES = 'ADAMANT_PROVIDER_VALUES';
export const ADAMANT_VIEW_WARMUP = 'ADAMANT_VIEW_WARMUP';

export async function designDocFactory(...designDocs : any[]) {
return designDocs;
Expand All @@ -48,7 +52,7 @@ export async function designDocFactory(...designDocs : any[]) {
providers: [
{
provide: AdamantConnectionManager,
async useFactory(factory: ConnectionFactory, providers : any[], designDocs : any[], deps : any[]) {
async useFactory(factory: ConnectionFactory, providers : any[], designDocs : any[], deps : any[], viewWarmUp: 'sync' | 'async' | 'none' | false) {
const injector = Injector.create({
providers: [
{ provide: ADAMANT_ID, useFactory: adamantIdFactory, deps: [] },
Expand All @@ -65,8 +69,17 @@ export async function designDocFactory(...designDocs : any[]) {
const metadata = DesignDocMetadataCollection.create(designDoc.constructor as any);
await manager.getRepository(metadata.entity).persistDesignDoc(designDoc);

for(const view of metadata.views) {
await manager.getRepository(metadata.entity).view(designDoc.constructor as any, view, { depth: 0 });
if(viewWarmUp === 'sync' || viewWarmUp === 'async') {
const opts : { stale?: 'update_after' } = {};

if(viewWarmUp === 'async') {
opts.stale = 'update_after';
}

for(const view of metadata.views) {
await manager.getRepository(metadata.entity).view(designDoc.constructor as any, view, { depth: 0, ...opts });
}

}
}
} catch(e) {
Expand All @@ -75,10 +88,11 @@ export async function designDocFactory(...designDocs : any[]) {

return manager;
},
inject: [ ADAMANT_CONNECTION_FACTORY, ADAMANT_PROVIDERS, ADAMANT_DESIGN_DOCS, ADAMANT_PROVIDER_VALUES ]
}
inject: [ ADAMANT_CONNECTION_FACTORY, ADAMANT_PROVIDERS, ADAMANT_DESIGN_DOCS, ADAMANT_PROVIDER_VALUES, ADAMANT_VIEW_WARMUP ] as (string|Type<any>)[]
},
AdamantHealthIndicator
],
exports: [ AdamantConnectionManager ]
exports: [ AdamantConnectionManager, AdamantHealthIndicator ]
})
export class AdamantModule {

Expand All @@ -88,6 +102,7 @@ export class AdamantModule {
providers: [
{ provide: ADAMANT_CONNECTION_FACTORY as any, useValue: options.factory },
{ provide: ADAMANT_PROVIDERS, useValue: options.providers || [] },
{ provide: ADAMANT_VIEW_WARMUP, useValue: null == options.viewWarmup ? 'sync' : options.viewWarmup },
{ provide: ADAMANT_PROVIDER_VALUES, useFactory: (...deps: any[]) => deps, inject: options.providers || [] },
{
provide: ADAMANT_DESIGN_DOCS,
Expand All @@ -105,6 +120,7 @@ export class AdamantModule {
providers: [
...this.createAsyncProviders(options),
{ provide: ADAMANT_PROVIDERS, useValue: options.providers || [] },
{ provide: ADAMANT_VIEW_WARMUP, useValue: null == options.viewWarmup ? 'sync' : options.viewWarmup },
{ provide: ADAMANT_PROVIDER_VALUES, useFactory: (...deps: any[]) => deps, inject: options.providers || [] },
{
provide: ADAMANT_DESIGN_DOCS,
Expand Down Expand Up @@ -142,7 +158,7 @@ export class AdamantModule {
return {
provide: ADAMANT_CONNECTION_FACTORY as any,
useFactory: async (factory : AdamantConnectionFactoryFactory) => await factory.createAdamantConnection(),
inject: [ options.useExisting || options.useClass ]
inject: [ options.useExisting || options.useClass! ]
}
}
}
3 changes: 2 additions & 1 deletion src/adamant/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from './adamant.module';
export * from './adamant.module';
export * from './adamant.health';
4 changes: 2 additions & 2 deletions src/angular/angular-locale.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,10 @@ export class AngularLocaleController {
}
for(const locale in parseAcceptLanguageHeader(req.get('accept-language') || '')) {
if(this.options.locales.includes(locale)) {
return res.redirect(`/${locale}`);
return res.redirect(`/${locale}${req.path}`);
}
}
res.redirect(`/${this.options.defaultLocale}`);
res.redirect(`/${this.options.defaultLocale}${req.path}`);
})
}

Expand Down
4 changes: 2 additions & 2 deletions src/angular/angular-root.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ export class AngularRootController extends AngularController<AngularOptions> {


constructor(@Inject(ANGULAR_OPTIONS) options : AngularOptions) {
super(options.mode, options, options.nonceFactory);
super(options.mode, options, options.nonceFactory, options.hooks);
}

protected init() {
for(const [ path, options ] of this.options.apps) {
const controller = new AngularController(this.options.mode, options, this.options.nonceFactory);
const controller = new AngularController(this.options.mode, options, this.options.nonceFactory, this.options.hooks);

this.router.use(path, controller.handle.bind(controller));
}
Expand Down
Loading

0 comments on commit d756b88

Please sign in to comment.