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

View File

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

View File

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

View File

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

View File

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