next
Version:
The React Framework
84 lines (83 loc) • 2.76 kB
JavaScript
Object.defineProperty(exports, "__esModule", {
value: true
});
var _nextUrl = require("../next-url");
var _utils = require("../utils");
var _cookies = require("./cookies");
const INTERNALS = Symbol("internal response");
const REDIRECTS = new Set([
301,
302,
303,
307,
308
]);
class NextResponse extends Response {
constructor(body, init = {}){
super(body, init);
this[INTERNALS] = {
cookies: new _cookies.NextCookies(this),
url: init.url ? new _nextUrl.NextURL(init.url, {
headers: (0, _utils).toNodeHeaders(this.headers),
nextConfig: init.nextConfig
}) : undefined
};
}
[Symbol.for("edge-runtime.inspect.custom")]() {
return {
cookies: this.cookies,
url: this.url,
// rest of props come from Response
body: this.body,
bodyUsed: this.bodyUsed,
headers: Object.fromEntries(this.headers),
ok: this.ok,
redirected: this.redirected,
status: this.status,
statusText: this.statusText,
type: this.type
};
}
get cookies() {
return this[INTERNALS].cookies;
}
static json(body, init) {
// @ts-expect-error This is not in lib/dom right now, and we can't augment it.
const response = Response.json(body, init);
return new NextResponse(response.body, response);
}
static redirect(url, init) {
var ref;
const status = typeof init === "number" ? init : (ref = init == null ? void 0 : init.status) != null ? ref : 307;
if (!REDIRECTS.has(status)) {
throw new RangeError('Failed to execute "redirect" on "response": Invalid status code');
}
const initObj = typeof init === "object" ? init : {};
const headers = new Headers(initObj == null ? void 0 : initObj.headers);
headers.set("Location", (0, _utils).validateURL(url));
return new NextResponse(null, {
...initObj,
headers,
status
});
}
static rewrite(destination, init) {
const headers = new Headers(init == null ? void 0 : init.headers);
headers.set("x-middleware-rewrite", (0, _utils).validateURL(destination));
return new NextResponse(null, {
...init,
headers
});
}
static next(init) {
const headers = new Headers(init == null ? void 0 : init.headers);
headers.set("x-middleware-next", "1");
return new NextResponse(null, {
...init,
headers
});
}
}
exports.NextResponse = NextResponse;
//# sourceMappingURL=response.js.map
;