mirror of
https://github.com/sasjs/server.git
synced 2026-01-09 07:20:05 +00:00
72 lines
1.6 KiB
TypeScript
72 lines
1.6 KiB
TypeScript
import express from 'express'
|
|
import { Request, Security, Route, Tags, Post, Body } from 'tsoa'
|
|
import { ExecuteReturnJson, ExecutionController } from './internal'
|
|
import { ExecuteReturnJsonResponse } from '.'
|
|
import {
|
|
getPreProgramVariables,
|
|
getUserAutoExec,
|
|
ModeType,
|
|
parseLogToArray
|
|
} from '../utils'
|
|
|
|
interface ExecuteSASCodePayload {
|
|
/**
|
|
* Code of SAS program
|
|
* @example "* SAS Code HERE;"
|
|
*/
|
|
code: string
|
|
}
|
|
|
|
@Security('bearerAuth')
|
|
@Route('SASjsApi/code')
|
|
@Tags('CODE')
|
|
export class CodeController {
|
|
/**
|
|
* Execute SAS code.
|
|
* @summary Run SAS Code and returns log
|
|
*/
|
|
@Post('/execute')
|
|
public async executeSASCode(
|
|
@Request() request: express.Request,
|
|
@Body() body: ExecuteSASCodePayload
|
|
): Promise<ExecuteReturnJsonResponse> {
|
|
return executeSASCode(request, body)
|
|
}
|
|
}
|
|
|
|
const executeSASCode = async (
|
|
req: express.Request,
|
|
{ code }: ExecuteSASCodePayload
|
|
) => {
|
|
const { user } = req
|
|
const userAutoExec =
|
|
process.env.MODE === ModeType.Server
|
|
? user?.autoExec
|
|
: await getUserAutoExec()
|
|
|
|
try {
|
|
const { webout, log, httpHeaders } =
|
|
(await new ExecutionController().executeProgram(
|
|
code,
|
|
getPreProgramVariables(req),
|
|
{ ...req.query, _debug: 131 },
|
|
{ userAutoExec },
|
|
true
|
|
)) as ExecuteReturnJson
|
|
|
|
return {
|
|
status: 'success',
|
|
_webout: webout as string,
|
|
log: parseLogToArray(log),
|
|
httpHeaders
|
|
}
|
|
} catch (err: any) {
|
|
throw {
|
|
code: 400,
|
|
status: 'failure',
|
|
message: 'Job execution failed.',
|
|
error: typeof err === 'object' ? err.toString() : err
|
|
}
|
|
}
|
|
}
|