response.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. var _nextUrl = require("../next-url");
  6. var _utils = require("../utils");
  7. var _cookies = require("./cookies");
  8. const INTERNALS = Symbol("internal response");
  9. const REDIRECTS = new Set([
  10. 301,
  11. 302,
  12. 303,
  13. 307,
  14. 308
  15. ]);
  16. class NextResponse extends Response {
  17. constructor(body, init = {}){
  18. super(body, init);
  19. this[INTERNALS] = {
  20. cookies: new _cookies.NextCookies(this),
  21. url: init.url ? new _nextUrl.NextURL(init.url, {
  22. headers: (0, _utils).toNodeHeaders(this.headers),
  23. nextConfig: init.nextConfig
  24. }) : undefined
  25. };
  26. }
  27. [Symbol.for("edge-runtime.inspect.custom")]() {
  28. return {
  29. cookies: this.cookies,
  30. url: this.url,
  31. // rest of props come from Response
  32. body: this.body,
  33. bodyUsed: this.bodyUsed,
  34. headers: Object.fromEntries(this.headers),
  35. ok: this.ok,
  36. redirected: this.redirected,
  37. status: this.status,
  38. statusText: this.statusText,
  39. type: this.type
  40. };
  41. }
  42. get cookies() {
  43. return this[INTERNALS].cookies;
  44. }
  45. static json(body, init) {
  46. // @ts-expect-error This is not in lib/dom right now, and we can't augment it.
  47. const response = Response.json(body, init);
  48. return new NextResponse(response.body, response);
  49. }
  50. static redirect(url, init) {
  51. var ref;
  52. const status = typeof init === "number" ? init : (ref = init == null ? void 0 : init.status) != null ? ref : 307;
  53. if (!REDIRECTS.has(status)) {
  54. throw new RangeError('Failed to execute "redirect" on "response": Invalid status code');
  55. }
  56. const initObj = typeof init === "object" ? init : {};
  57. const headers = new Headers(initObj == null ? void 0 : initObj.headers);
  58. headers.set("Location", (0, _utils).validateURL(url));
  59. return new NextResponse(null, {
  60. ...initObj,
  61. headers,
  62. status
  63. });
  64. }
  65. static rewrite(destination, init) {
  66. const headers = new Headers(init == null ? void 0 : init.headers);
  67. headers.set("x-middleware-rewrite", (0, _utils).validateURL(destination));
  68. return new NextResponse(null, {
  69. ...init,
  70. headers
  71. });
  72. }
  73. static next(init) {
  74. const headers = new Headers(init == null ? void 0 : init.headers);
  75. headers.set("x-middleware-next", "1");
  76. return new NextResponse(null, {
  77. ...init,
  78. headers
  79. });
  80. }
  81. }
  82. exports.NextResponse = NextResponse;
  83. //# sourceMappingURL=response.js.map