diff --git a/public/src/api/ApiUrl.ts b/public/src/api/ApiUrl.ts index 15545d92..4050fc1b 100644 --- a/public/src/api/ApiUrl.ts +++ b/public/src/api/ApiUrl.ts @@ -10,5 +10,6 @@ export enum ApiUrl { Render = '/api/render', Spawn = '/api/spawn', File = '/api/file', + NestedJson = '/api/nestedJson', Partners = '/api/partners' } diff --git a/public/src/api/httpClient.ts b/public/src/api/httpClient.ts index 18bdff0d..619b0595 100644 --- a/public/src/api/httpClient.ts +++ b/public/src/api/httpClient.ts @@ -282,6 +282,13 @@ export function viewProduct(productName: string): Promise { }); } +export function getNestedJson(jsonNestingLevel: number = 1): Promise { + return makeApiRequest({ + url: `${ApiUrl.NestedJson}?depth=${jsonNestingLevel}`, + method: 'get' + }); +} + export function queryPartnersRaw(xpath: string): Promise { return makeApiRequest({ url: `${ApiUrl.Partners}/query?xpath=${xpath}`, diff --git a/src/app.controller.swagger.desc.ts b/src/app.controller.swagger.desc.ts index 4846c199..b265f085 100644 --- a/src/app.controller.swagger.desc.ts +++ b/src/app.controller.swagger.desc.ts @@ -11,3 +11,5 @@ export const API_DESC_LAUNCH_COMMAND = `Launches system command on server`; export const API_DESC_CONFIG_SERVER = `Returns server configuration to the client`; export const SWAGGER_DESC_SECRETS = `Returns server secrets. Shhhh 🤫`; + +export const SWAGGER_DESC_NESTED_JSON = `Returns a nested JSON response with configurable depth`; diff --git a/src/app.controller.ts b/src/app.controller.ts index 29514c5f..dde32d17 100644 --- a/src/app.controller.ts +++ b/src/app.controller.ts @@ -15,6 +15,9 @@ import { SerializeOptions, UseGuards, UseInterceptors, + ParseIntPipe, + DefaultValuePipe, + HttpStatus, } from '@nestjs/common'; import { ApiBody, @@ -39,6 +42,7 @@ import { API_DESC_RENDER_REQUEST, API_DESC_XML_METADATA, SWAGGER_DESC_SECRETS, + SWAGGER_DESC_NESTED_JSON, } from './app.controller.swagger.desc'; import { AuthGuard } from './auth/auth.guard'; import { JwtType } from './auth/jwt/jwt.type.decorator'; @@ -52,7 +56,7 @@ import { SWAGGER_DESC_FIND_USER } from './users/users.controller.swagger.desc'; export class AppController { private readonly logger = new Logger(AppController.name); - constructor(private readonly appService: AppService) {} + constructor(private readonly appService: AppService) { } @Post('render') @ApiProduces('text/plain') @@ -263,4 +267,27 @@ export class AppController { throw new HttpException(err.message, err.status); } } + + @Get('nestedJson') + @ApiOperation({ + description: SWAGGER_DESC_NESTED_JSON, + }) + @Header('content-type', 'application/json') + async getNestedJson(@Query('depth', new DefaultValuePipe(1), new ParseIntPipe({ errorHttpStatusCode: HttpStatus.BAD_REQUEST })) depth: number): Promise { + if (depth < 1) { + throw new HttpException("JSON nesting depth is invalid", HttpStatus.BAD_REQUEST); + } + + this.logger.debug(`Creating a JSON with a nesting depth of ${depth}`); + + var tmpObj: object = {}; + var jsonObj: object = { "0": "Leaf" }; + for (let i = 1; i < depth; i++) { + tmpObj = {}; + tmpObj[i.toString()] = Object.assign({}, jsonObj); + jsonObj = Object.assign({}, tmpObj); + } + + return JSON.stringify(jsonObj); + } }