| 426 | * is a variable or the site sets none. |
| 427 | */ |
| 428 | export function responseStatus(text: string, args: string | null | undefined, _kind: 'calls' | 'instantiates' = 'calls'): number | null { |
| 429 | const call = normaliseCall(text); |
| 430 | // `res.status(404).json`, `reply.code(201).send`, `ResponseEntity.status(HttpStatus.NOT_FOUND).body`. |
| 431 | const inChain = /(?:^|\.)(?:status|sendStatus|code|Status|StatusCode|SendStatus|withStatus|with_status)\(([^()]+)\)/.exec(call); |
| 432 | if (inChain) { |
| 433 | const s = literalStatus(inChain[1]!); |
| 434 | if (s !== null) return s; |
| 435 | } |
| 436 | const last = (call.replace(/\([^()]*\)/g, '').split(/[.:]/).pop() ?? call).replace(/^new/, ''); |
| 437 | const a = args ?? ''; |
| 438 | // The status is the argument: `res.status(404)`, `res.sendStatus(204)`, `abort(404)`, `StatusCode(500)`, `c.String(200, …)`, `http.Error(w, m, 500)`. |
| 439 | if (/^(?:status|sendStatus|code|abort|StatusCode|Status|SendStatus|WriteHeader|String|Data|JSON|IndentedJSON|XML|YAML|HTML|AbortWithStatus|AbortWithStatusJSON|Error|Redirect|head|Problem|error)$/.test(last)) { |
| 440 | const fromArgs = literalStatus(firstStatusToken(a, last === 'Error' || last === 'Redirect' ? 'last' : 'first')); |
| 441 | if (fromArgs !== null) return fromArgs; |
| 442 | if (last === 'Redirect') return 302; |
| 443 | if (last === 'Problem' || last === 'error') return 500; |
| 444 | } |
| 445 | // `status_code=404`, `status=400`, `statusCode: 404`, `HttpStatus.CREATED` anywhere in the arguments. |
| 446 | const kw = /(?:status_code|statusCode|status|code)\s*[=:]\s*([\w.]+)/.exec(a); |
| 447 | if (kw) { |
| 448 | const s = literalStatus(kw[1]!); |
| 449 | if (s !== null) return s; |
| 450 | } |
| 451 | const named = /\b(?:HttpStatus|HTTPStatus|StatusCodes|HttpStatusCode|http)\.(\w+)/.exec(a) ?? /(?:^|[\s,(])\.(\w+)/.exec(a); |
| 452 | if (named) { |
| 453 | const s = statusOfName(named[1]!); |
| 454 | if (s !== null) return s; |
| 455 | } |
| 456 | // `new HttpException(message, 403)`, `new HttpException(422, { errors })`, |
| 457 | // `abort(404, …)`: an exception or a reply takes its status wherever the |
| 458 | // argument list carries one literal. |
| 459 | if (/Exception$|Error$|^Abort$|^abort$|^HTTPError$/.test(last)) { |
| 460 | for (const part of splitArgs(a)) { |
| 461 | const s = literalStatus(part); |
| 462 | if (s !== null) return s; |
| 463 | } |
| 464 | } |
| 465 | // The name says it: `NotFoundException`, `ResponseEntity.notFound().build`, `TypedResults.NoContent`, `Ok`, `Http404`. |
| 466 | const segments = call.replace(/\([^()]*\)/g, '').split(/[.:]/).filter(Boolean); |
| 467 | for (let i = segments.length - 1; i >= 0; i--) { |
| 468 | const seg = segments[i]!; |
| 469 | if (/^(?:json|send|end|render|view|View|Json|Content|File|jsonify|JSONResponse|Response|HttpResponse|JsonResponse|render_template|body|text|html|build|status|type|header|headers|set|res|response|reply|rep|ctx|c|context|this|self|http|w|rw|ResponseEntity|Results|TypedResults|HttpStatus|NextResponse)$/.test(seg)) continue; |
| 470 | const s = statusOfName(seg); |
| 471 | if (s !== null) return s; |
| 472 | break; |
| 473 | } |
| 474 | if (/^(?:redirect|redirect_to|RedirectResponse|HttpResponseRedirect|RedirectToAction|RedirectToPage|RedirectToRoute|LocalRedirect|permanentRedirect|Redirect|RedirectView)$/.test(last)) return /permanent/i.test(last) ? 308 : 302; |
| 475 | // `new ModelAndView("redirect:/owners")` redirects; any other view renders (implicitResponseStatus). |
| 476 | if (last === 'ModelAndView' && /^\s*"redirect:/.test(a)) return 302; |
| 477 | // Laravel: `redirect()->route('home')`, `redirect()->back()->withErrors(…)`, `back()`, `to_route('home')`. |
| 478 | if (/^(?:redirect->\w+|back|to_route)(?:->\w+)*$/.test(call.replace(/\([^()]*\)/g, ''))) return 302; |
| 479 | if (/^(?:Created|CreatedAtAction|CreatedAtRoute|created)$/.test(last)) return 201; |
| 480 | return null; |
| 481 | } |
| 482 | |
| 483 | /** |
| 484 | * The status a reply sends when it sets none — 200 — for the calls that send a |