Complete the code to define a basic handle hook that logs each request method.
export const handle = async ({ event, resolve }) => {
console.log(event.request.method);
return await [1](event);
};The handle hook must call resolve(event) to continue processing the request and generate a response.
Complete the code to define a handleError hook that logs the error message.
export const handleError = ({ error }) => {
console.error('Error:', [1]);
};The handleError hook receives an error object. Logging error.message shows the error description.
Fix the error in the handleFetch hook to correctly modify the request headers.
export const handleFetch = async ({ request, fetch }) => {
const newHeaders = new Headers(request.headers);
newHeaders.set('X-Custom-Header', 'SvelteKit');
const newRequest = new Request(request.url, {
method: request.method,
headers: [1],
body: request.body
});
return fetch(newRequest);
};To modify headers, create a new Headers object and pass it as the headers option in the new Request.
Fill both blanks to create a handle hook that adds a custom header to the response.
export const handle = async ({ event, resolve }) => {
const response = await resolve(event);
response.headers.[1]('X-Handled-By', [2]);
return response;
};The set method adds or replaces a header. The value must be a string, so use 'SvelteKit'.
Fill all three blanks to create a handleFetch hook that adds an authorization token to outgoing requests.
export const handleFetch = async ({ request, fetch }) => {
const modifiedHeaders = new Headers([1]);
modifiedHeaders.set('Authorization', [2]);
const modifiedRequest = new Request(request.url, {
method: [3],
headers: modifiedHeaders,
body: request.body
});
return fetch(modifiedRequest);
};Start with the original request headers, add the Authorization header with a token string, and keep the original request method.