( request: RestRequest, script: string, set: any, get: any, exec: any, isCollectionLevel: boolean, envName?: string, )
| 79 | } |
| 80 | |
| 81 | async function executeRequestScript( |
| 82 | request: RestRequest, |
| 83 | script: string, |
| 84 | set: any, |
| 85 | get: any, |
| 86 | exec: any, |
| 87 | isCollectionLevel: boolean, |
| 88 | envName?: string, |
| 89 | ) { |
| 90 | const args: Record<string, any> = {}; |
| 91 | args.env = { set, get, name: envName }; |
| 92 | args.req = { |
| 93 | uri: request.data.uri, |
| 94 | body: request.data.body, |
| 95 | method: request.data.method, |
| 96 | headers: kvRowsToMap(request.data.headers ?? []), |
| 97 | }; |
| 98 | args.jp = jpath; |
| 99 | args.btoa = window.btoa; |
| 100 | args.atob = window.atob; |
| 101 | args.DateTime = DateTime; |
| 102 | args.rand = rand; |
| 103 | args.exec = exec; |
| 104 | if (isCollectionLevel) { |
| 105 | args.log = (...data: any[]) => |
| 106 | console.log( |
| 107 | `[Collection Request Script: ${request.collectionId} - ${envName ?? 'NO_ENV'}]`, |
| 108 | ...data, |
| 109 | ); |
| 110 | } else { |
| 111 | args.log = (...data: any[]) => |
| 112 | console.log(`[Request Script: ${request.id} - ${envName ?? 'NO_ENV'}]`, ...data); |
| 113 | } |
| 114 | try { |
| 115 | await asyncSandboxedFunction(args, script); |
| 116 | } catch (err: any) { |
| 117 | // NOTE: this is to prevent stacked error messages due to exec loop |
| 118 | const msg = err.message.startsWith('Error in request script') |
| 119 | ? err.message |
| 120 | : `Error in request script [id: ${request.id}]: ${err}`; |
| 121 | throw Error(msg); |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | export { executeRequestScript, executeResponseScript }; |
no test coverage detected