From e2a6810e9531a8102d3c51fd8df2e1f78f0d965f Mon Sep 17 00:00:00 2001 From: Saad Jutt Date: Sun, 20 Feb 2022 02:57:45 +0500 Subject: [PATCH] fix: code api is updated return type --- api/public/swagger.yaml | 43 +++++++++++++++++++++------------ api/src/controllers/code.ts | 18 ++++++++------ api/src/controllers/stp.ts | 32 ++++++++++++++++++------ api/src/utils/extractHeaders.ts | 2 +- 4 files changed, 64 insertions(+), 31 deletions(-) diff --git a/api/public/swagger.yaml b/api/public/swagger.yaml index ec6fac8..55fa8fd 100644 --- a/api/public/swagger.yaml +++ b/api/public/swagger.yaml @@ -92,6 +92,29 @@ components: - clientSecret type: object additionalProperties: false + HTTPHeaders: + properties: {} + type: object + additionalProperties: + type: string + ExecuteReturnJsonResponse: + properties: + status: + type: string + _webout: + type: string + log: + type: string + message: + type: string + httpHeaders: + $ref: '#/components/schemas/HTTPHeaders' + required: + - status + - _webout + - httpHeaders + type: object + additionalProperties: false ExecuteSASCodePayload: properties: code: @@ -368,21 +391,6 @@ components: - description type: object additionalProperties: false - ExecuteReturnJsonResponse: - properties: - status: - type: string - _webout: - type: string - log: - type: string - message: - type: string - required: - - status - - _webout - type: object - additionalProperties: false ExecuteReturnJsonPayload: properties: _program: @@ -520,7 +528,7 @@ paths: content: application/json: schema: - type: string + $ref: '#/components/schemas/ExecuteReturnJsonResponse' description: 'Execute SAS code.' summary: 'Run SAS Code and returns log' tags: @@ -1060,6 +1068,9 @@ paths: application/json: schema: $ref: '#/components/schemas/ExecuteReturnJsonResponse' + examples: + 'Example 1': + value: {status: success, _webout: 'webout content', httpHeaders: {Content-type: application/zip, Cache-Control: 'public, max-age=1000'}} description: "Trigger a SAS program using it's location in the _program parameter.\nEnable debugging using the _debug parameter.\nAdditional URL parameters are turned into SAS macro variables.\nAny files provided are placed into the session and\ncorresponding _WEBIN_XXX variables are created." summary: 'Execute Stored Program, return JSON' tags: diff --git a/api/src/controllers/code.ts b/api/src/controllers/code.ts index b0a54b0..68d523e 100644 --- a/api/src/controllers/code.ts +++ b/api/src/controllers/code.ts @@ -1,7 +1,8 @@ import express from 'express' import { Request, Security, Route, Tags, Post, Body } from 'tsoa' -import { ExecuteReturnRaw, ExecutionController } from './internal' +import { ExecuteReturnJson, ExecutionController } from './internal' import { PreProgramVars } from '../types' +import { ExecuteReturnJsonResponse } from '.' interface ExecuteSASCodePayload { /** @@ -23,25 +24,28 @@ export class CodeController { public async executeSASCode( @Request() request: express.Request, @Body() body: ExecuteSASCodePayload - ): Promise { + ): Promise { return executeSASCode(request, body) } } const executeSASCode = async (req: any, { code }: ExecuteSASCodePayload) => { try { - const { result, httpHeaders } = + const { webout, log, httpHeaders } = (await new ExecutionController().executeProgram( code, getPreProgramVariables(req), { ...req.query, _debug: 131 }, undefined, true - )) as ExecuteReturnRaw + )) as ExecuteReturnJson - req.res?.set(httpHeaders) - - return result + return { + status: 'success', + _webout: webout, + log, + httpHeaders + } } catch (err: any) { throw { code: 400, diff --git a/api/src/controllers/stp.ts b/api/src/controllers/stp.ts index 559c856..b8e0e99 100644 --- a/api/src/controllers/stp.ts +++ b/api/src/controllers/stp.ts @@ -1,6 +1,16 @@ import express from 'express' import path from 'path' -import { Request, Security, Route, Tags, Post, Body, Get, Query } from 'tsoa' +import { + Request, + Security, + Route, + Tags, + Post, + Body, + Get, + Query, + Example +} from 'tsoa' import { ExecuteReturnJson, ExecuteReturnRaw, @@ -8,7 +18,7 @@ import { ExecutionVars } from './internal' import { PreProgramVars } from '../types' -import { getTmpFilesFolderPath, makeFilesNamesMap } from '../utils' +import { getTmpFilesFolderPath, HTTPHeaders, makeFilesNamesMap } from '../utils' interface ExecuteReturnJsonPayload { /** @@ -17,11 +27,12 @@ interface ExecuteReturnJsonPayload { */ _program?: string } -interface ExecuteReturnJsonResponse { +export interface ExecuteReturnJsonResponse { status: string _webout: string log?: string message?: string + httpHeaders: HTTPHeaders } @Security('bearerAuth') @@ -56,6 +67,14 @@ export class STPController { * @query _program Location of SAS program * @example _program "/Public/somefolder/some.file" */ + @Example({ + status: 'success', + _webout: 'webout content', + httpHeaders: { + 'Content-type': 'application/zip', + 'Cache-Control': 'public, max-age=1000' + } + }) @Post('/execute') public async executeReturnJson( @Request() request: express.Request, @@ -119,12 +138,11 @@ const executeReturnJson = async ( true )) as ExecuteReturnJson - req.res?.set(httpHeaders) - return { status: 'success', - _webout: webout as string, - log + _webout: webout, + log, + httpHeaders } } catch (err: any) { throw { diff --git a/api/src/utils/extractHeaders.ts b/api/src/utils/extractHeaders.ts index 05a4207..e3812f0 100644 --- a/api/src/utils/extractHeaders.ts +++ b/api/src/utils/extractHeaders.ts @@ -1,7 +1,7 @@ const headerUtils = require('http-headers-validation') export interface HTTPHeaders { - [key: string]: string | undefined + [key: string]: string } export const extractHeaders = (content: string): HTTPHeaders => {