| 190 | export class ApiHandler extends Handler { |
| 191 | @param('op', Types.String) |
| 192 | async all({ }, op: string) { |
| 193 | if (!['get', 'post'].includes(this.request.method.toLowerCase())) { |
| 194 | throw new MethodNotAllowedError(this.request.method); |
| 195 | } |
| 196 | if (!APIS[op]) throw new BadRequestError(`Invalid API operation: ${op}`); |
| 197 | if (APIS[op].type === 'Subscription') { |
| 198 | throw new BadRequestError('Subscription operation cannot be called in HTTP handler'); |
| 199 | } |
| 200 | if (APIS[op].type === 'Mutation' && this.request.method.toLowerCase() === 'get') { |
| 201 | throw new BadRequestError('Mutation operation cannot be called with GET method'); |
| 202 | } |
| 203 | handleArguments(this.args); |
| 204 | // @ts-ignore |
| 205 | await this.ctx.parallel('handler/api/before', this); |
| 206 | // @ts-ignore |
| 207 | await this.ctx.parallel(`handler/api/before/${op}`, this); |
| 208 | const result = await this.ctx.api.execute( |
| 209 | this, op, { domainId: this.args.domainId, ...this.args, ...(this.args.args || {}) }, |
| 210 | (m, args) => (this.ctx.parallel as any)(m, args), this.args.projection, |
| 211 | ); |
| 212 | if (BinaryResponse.check(result)) { |
| 213 | this.binary(result.data, result.filename); |
| 214 | } else if (RedirectResponse.check(result)) { |
| 215 | this.response.redirect = result.url; |
| 216 | } else { |
| 217 | this.response.body = result; |
| 218 | } |
| 219 | } |
| 220 | } |
| 221 | |
| 222 | export class ApiConnectionHandler extends ConnectionHandler { |