next
Version:
The React Framework
136 lines (135 loc) • 4.98 kB
JavaScript
Object.defineProperty(exports, "__esModule", {
value: true
});
Object.defineProperty(exports, "NextResponse", {
enumerable: true,
get: function() {
return NextResponse;
}
});
const _cookies = require("../../web/spec-extension/cookies");
const _nexturl = require("../next-url");
const _utils = require("../utils");
const _reflect = require("./adapters/reflect");
const _cookies1 = require("./cookies");
const INTERNALS = Symbol('internal response');
const REDIRECTS = new Set([
301,
302,
303,
307,
308
]);
function handleMiddlewareField(init, headers) {
var _init_request;
if (init == null ? void 0 : (_init_request = init.request) == null ? void 0 : _init_request.headers) {
if (!(init.request.headers instanceof Headers)) {
throw Object.defineProperty(new Error('request.headers must be an instance of Headers'), "__NEXT_ERROR_CODE", {
value: "E119",
enumerable: false,
configurable: true
});
}
const keys = [];
for (const [key, value] of init.request.headers){
headers.set('x-middleware-request-' + key, value);
keys.push(key);
}
headers.set('x-middleware-override-headers', keys.join(','));
}
}
class NextResponse extends Response {
constructor(body, init = {}){
super(body, init);
const headers = this.headers;
const cookies = new _cookies1.ResponseCookies(headers);
const cookiesProxy = new Proxy(cookies, {
get (target, prop, receiver) {
switch(prop){
case 'delete':
case 'set':
{
return (...args)=>{
const result = Reflect.apply(target[prop], target, args);
const newHeaders = new Headers(headers);
if (result instanceof _cookies1.ResponseCookies) {
headers.set('x-middleware-set-cookie', result.getAll().map((cookie)=>(0, _cookies.stringifyCookie)(cookie)).join(','));
}
handleMiddlewareField(init, newHeaders);
return result;
};
}
default:
return _reflect.ReflectAdapter.get(target, prop, receiver);
}
}
});
this[INTERNALS] = {
cookies: cookiesProxy,
url: init.url ? new _nexturl.NextURL(init.url, {
headers: (0, _utils.toNodeOutgoingHttpHeaders)(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) {
const response = Response.json(body, init);
return new NextResponse(response.body, response);
}
static redirect(url, init) {
const status = typeof init === 'number' ? init : (init == null ? void 0 : init.status) ?? 307;
if (!REDIRECTS.has(status)) {
throw Object.defineProperty(new RangeError('Failed to execute "redirect" on "response": Invalid status code'), "__NEXT_ERROR_CODE", {
value: "E529",
enumerable: false,
configurable: true
});
}
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));
handleMiddlewareField(init, headers);
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');
handleMiddlewareField(init, headers);
return new NextResponse(null, {
...init,
headers
});
}
}
//# sourceMappingURL=response.js.map
;