From c8bace5a78d6e991f0590a403927ab28757895f9 Mon Sep 17 00:00:00 2001 From: Bilal Shaikh Date: Tue, 17 Aug 2021 20:44:33 -0400 Subject: [PATCH] feat(datamodel): add common api query parameters begin to define common parameters for filtering,sorting, etc on endpoints --- libs/datamodel/api/src/lib/json/index.ts | 1 + .../api/src/lib/json/queryParameters.ts | 64 +++++++++++++++++++ 2 files changed, 65 insertions(+) create mode 100644 libs/datamodel/api/src/lib/json/queryParameters.ts diff --git a/libs/datamodel/api/src/lib/json/index.ts b/libs/datamodel/api/src/lib/json/index.ts index 5beae85167..d82bf3ecd3 100644 --- a/libs/datamodel/api/src/lib/json/index.ts +++ b/libs/datamodel/api/src/lib/json/index.ts @@ -1,2 +1,3 @@ export * from './error'; export * from './meta'; +export * from './queryParameters'; \ No newline at end of file diff --git a/libs/datamodel/api/src/lib/json/queryParameters.ts b/libs/datamodel/api/src/lib/json/queryParameters.ts new file mode 100644 index 0000000000..873424838f --- /dev/null +++ b/libs/datamodel/api/src/lib/json/queryParameters.ts @@ -0,0 +1,64 @@ +import { ApiProperty, ApiQuery } from "@nestjs/swagger"; +import { IsArray, IsInt, IsOptional, IsString } from 'class-validator'; +import { Transform, Type,} from 'class-transformer'; +import { applyDecorators } from "@nestjs/common"; + + + +export class FieldsQueryParameters { + @IsString({ each: true }) + @IsArray() + @IsOptional() + @ApiProperty({ type: [String] }) + @Transform((params) => { + return params.value.split(','); + }) + fields!: string[]; +} + +export const ApiFieldsQuery = (...args: string[]) => applyDecorators( + ApiQuery({ name: 'fields', explode: false, style: 'form', type:String, isArray: true }) +) + +// WIP can use this to define the query parameters for operations like sorting, filtering, paging, etc. +class FullJsonAPIQueryParameters { + @IsString({ each: true }) + @IsArray() + @IsOptional() + @ApiProperty({ type: [String] }) + @Transform((params) => { + return params.value.split(','); + }) + fields!: string[]; + + @IsString({ each: true }) + @IsArray() + @IsOptional() + @ApiProperty({ type: [String] }) + @Transform((params) => { + return params.value.split(','); + }) + sort!: string[]; + + @IsInt() + @IsArray() + @IsOptional() + @ApiProperty({ type: Number, format: 'int' }) + @Type(() => Number) + page!: number; + + @IsString({ each: true }) + @IsArray() + @IsOptional() + @ApiProperty({ type: [String] }) + @Transform((params) => { + return params.value.split(','); + }) + include!: string[]; + + + @IsOptional() + //@ApiProperty({ type: [String] }) + // Maybe make this object + filter!: string[]; +} \ No newline at end of file