(
context: ApiExecutionContext, callOrName: ApiCall<ApiType, any, any> | string,
rawArgs: any, emitHook?: any, project?: any, sendPayload?: (payload: any) => void,
)
| 145 | } |
| 146 | |
| 147 | async execute( |
| 148 | context: ApiExecutionContext, callOrName: ApiCall<ApiType, any, any> | string, |
| 149 | rawArgs: any, emitHook?: any, project?: any, sendPayload?: (payload: any) => void, |
| 150 | ) { |
| 151 | const call = typeof callOrName === 'string' ? APIS[callOrName] : callOrName; |
| 152 | if (!call) throw new NotFoundError(callOrName); |
| 153 | const { input, func, hooks } = call; |
| 154 | // eslint-disable-next-line no-await-in-loop |
| 155 | for (const hook of hooks) await this.execute(context, hook, rawArgs); |
| 156 | |
| 157 | let args: any; |
| 158 | try { |
| 159 | args = input ? input(rawArgs as any) : rawArgs; |
| 160 | } catch (e) { |
| 161 | throw new BadRequestError(e.message); |
| 162 | } |
| 163 | if (typeof callOrName === 'string') { |
| 164 | await emitHook?.('api/before', args); |
| 165 | await emitHook?.(`api/before/${callOrName}`, args); |
| 166 | } |
| 167 | let result = await func(context, args as any, sendPayload); |
| 168 | if (result && typeof result === 'object' && 'next' in result) { |
| 169 | const it = result as AsyncGenerator<any, any, never>; |
| 170 | while (true) { |
| 171 | const value = await it.next(); // eslint-disable-line no-await-in-loop |
| 172 | if (value.done) { |
| 173 | result = value; |
| 174 | break; |
| 175 | } else { |
| 176 | sendPayload?.(value.value); |
| 177 | } |
| 178 | } |
| 179 | } |
| 180 | return (project && typeof result === 'object' && result !== null) ? projection(result, project, context) : result; |
| 181 | } |
| 182 | } |
| 183 | |
| 184 | declare module 'cordis' { |
no test coverage detected