Headers
Request Headers
Request headers can be retrieved using the built‑in Next.js headers function.
import { headers } from 'next/headers';
const reqHeaders = headers();
const userAgent = reqHeaders.get('user-agent');Alternatively, use req.headers from the Web Request API .
import { prefix, get } from 'vovk';
import { withZod } from 'vovk-zod';
@prefix('users')
export default class UserController {
@get('info')
static async getUserInfo = withZod({
async handle(req) {
const headers = req.headers;
const userAgent = headers.get('user-agent');
// ...
},
});
}Cookies
To work with cookies, use the cookies function exported by the Next.js headers module.
import { cookies } from 'next/headers';
export default class UserController {
@put('do-something')
static async doSomething() {
const cookieStore = await cookies();
const theme = cookieStore.get('theme');
// ...
}
}Response Headers
All HTTP decorators support custom response headers via the second argument.
// ...
export default class UserController {
@put('do-something', { headers: { 'x-hello': 'world' } })
static async doSomething(/* ... */) {
/* ... */
}
}Enable CORS headers with the cors: true option.
// ...
export default class UserController {
@put('do-something', { cors: true })
static async doSomething(/* ... */) {
/* ... */
}
}For auto‑generated endpoints, cors and headers are specified as the single argument.
// ...
export default class UserController {
@put.auto({ cors: true, headers: { 'x-hello': 'world' } })
static async doSomething(/* ... */) {
/* ... */
}
}Dynamic Response Headers
Set dynamic response headers with the NextResponse object from Next.js.
import { NextResponse } from 'next/server';
// ...
export default class UserController {
@put('do-something')
static async doSomething() {
return NextResponse.json({ hello: 'world' }, { headers: { 'x-hello': 'world' } });
}
}File Downloads
For attachments use Response from the Web API with appropriate headers.
// ...
export default class DownloadController {
@get('csv-report')
static async downloadCSV() {
return new Response(
csvString,
{
headers: {
'Content-Type': 'text/csv',
'Content-Disposition': `attachment; filename=report.csv`,
},
}
);
}
}Last updated on