feat: jwt auth. #7

Merged
Ivan merged 6 commits from feat-jwt-auth into master 2021-07-20 20:45:18 +08:00
5 changed files with 27 additions and 5 deletions
Showing only changes of commit ec351d12f2 - Show all commits

View File

@ -6,13 +6,18 @@ import {
HttpStatus,
} from '@nestjs/common';
import { ApolloError } from 'apollo-server-errors';
import { PinoLogger, InjectPinoLogger } from 'nestjs-pino';
@Catch(HttpException)
export class HttpExceptionFilter implements ExceptionFilter {
constructor(
@InjectPinoLogger(HttpExceptionFilter.name)
private readonly logger: PinoLogger,
) {}
catch(exception: HttpException, host: ArgumentsHost) {
switch (host.getType<'http' | 'graphql' | string>()) {
case 'graphql': {
const message = exception.message;
const errorName = exception.message;
const extensions: Record<string, any> = {};
const err = exception.getResponse();
if (typeof err === 'string') {
@ -21,8 +26,10 @@ export class HttpExceptionFilter implements ExceptionFilter {
Object.assign(extensions, (err as any).extension);
extensions.message = (err as any).message;
}
extensions.error = errorName;
this.logger.error(extensions);
return new ApolloError(
message,
extensions.message,
exception.getStatus().toString(),
extensions,
);

View File

@ -1,3 +1,4 @@
import { PinoLogger } from 'nestjs-pino';
import { ValidationPipe } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import { NestFactory } from '@nestjs/core';
@ -14,7 +15,8 @@ async function bootstrap() {
transform: true,
}),
);
app.useGlobalFilters(new HttpExceptionFilter());
const httpExceptionFilterLogger = await app.resolve(PinoLogger);
app.useGlobalFilters(new HttpExceptionFilter(httpExceptionFilterLogger));
await app.listen(configService.get<number>('http.port'));
}
bootstrap();

View File

@ -1,4 +1,6 @@
import { Field, InputType, Int, ObjectType } from '@nestjs/graphql';
import { Type } from 'class-transformer';
import { IsInstance, isInstance, ValidateNested } from 'class-validator';
import { WorkUnit } from './work-unit.model';
@InputType('WorkUnitMetadataInput')
@ -6,5 +8,9 @@ import { WorkUnit } from './work-unit.model';
export class WorkUnitMetadata {
@Field(() => Int)
version = 1;
@Type(() => WorkUnit)
@IsInstance(WorkUnit, { each: true })
@ValidateNested({ each: true })
units: WorkUnit[];
}

View File

@ -1,4 +1,5 @@
import { Field, InputType, ObjectType } from '@nestjs/graphql';
import { IsNotEmpty } from 'class-validator';
import {
PipelineUnits,
PipelineUnits as PipelineUnitTypes,
@ -9,5 +10,7 @@ import {
export class WorkUnit {
@Field(() => PipelineUnits)
type: PipelineUnitTypes;
@IsNotEmpty({ each: true })
scripts: string[];
}

View File

@ -1,11 +1,13 @@
import { Type } from 'class-transformer';
import { InputType } from '@nestjs/graphql';
import { WorkUnitMetadata } from '../../pipeline-tasks/models/work-unit-metadata.model';
import {
IsObject,
IsInstance,
IsOptional,
IsString,
IsUUID,
MaxLength,
ValidateNested,
} from 'class-validator';
@InputType({ isAbstract: true })
@ -21,7 +23,9 @@ export class CreatePipelineInput {
@MaxLength(32)
name: string;
@Type(() => WorkUnitMetadata)
@IsOptional()
@IsObject()
@ValidateNested()
@IsInstance(WorkUnitMetadata)
workUnitMetadata: WorkUnitMetadata;
}