Skip to content

Commit

Permalink
feat(api): nested json endpoint with configurable depth (#297)
Browse files Browse the repository at this point in the history
Closes BC-24
  • Loading branch information
MaximAshin authored Mar 2, 2024
1 parent e1e180d commit 89f2b1f
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 1 deletion.
1 change: 1 addition & 0 deletions public/src/api/ApiUrl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@ export enum ApiUrl {
Render = '/api/render',
Spawn = '/api/spawn',
File = '/api/file',
NestedJson = '/api/nestedJson',
Partners = '/api/partners'
}
7 changes: 7 additions & 0 deletions public/src/api/httpClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,13 @@ export function viewProduct(productName: string): Promise<any> {
});
}

export function getNestedJson(jsonNestingLevel: number = 1): Promise<any> {
return makeApiRequest({
url: `${ApiUrl.NestedJson}?depth=${jsonNestingLevel}`,
method: 'get'
});
}

export function queryPartnersRaw(xpath: string): Promise<any> {
return makeApiRequest({
url: `${ApiUrl.Partners}/query?xpath=${xpath}`,
Expand Down
2 changes: 2 additions & 0 deletions src/app.controller.swagger.desc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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`;
29 changes: 28 additions & 1 deletion src/app.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ import {
SerializeOptions,
UseGuards,
UseInterceptors,
ParseIntPipe,
DefaultValuePipe,
HttpStatus,
} from '@nestjs/common';
import {
ApiBody,
Expand All @@ -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';
Expand All @@ -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')
Expand Down Expand Up @@ -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<string> {
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);
}
}

0 comments on commit 89f2b1f

Please sign in to comment.