Skip to content
On this page

useResponseHeaders

Utility function to get and set headers that will be returned in the Response

Usage

Setting Headers

import { use, useResponseHeaders } from '@serverless-use/apigw'

export const handler = use(async () => {
  const { set } = useResponseHeaders()

  set('content-type', 'text/html')

  return '<h2>You must live your story.</h2>'
})

Reading Headers

import { use, useResponseHeaders } from '@serverless-use/apigw'

export const handler = use(async () => {
  const { set, get } = useResponseHeaders()
  set('content-type', 'application/json')

  const contentType = get('content-type')

  return `<h2>${contentType}</h2>`
})

Typed Headers

import { use, useResponseHeaders } from '@serverless-use/apigw'

export const handler = use(async () => {
  const { set, get } = useResponseHeaders<{ 'content-type': string, 'authorization': string }>()
  set('content-type', 'application/json')

  // TS Error! 
  // Argument of type '"auth"' is not assignable to 
  // parameter of type '"content-type" | "authorization"' 
  set('auth', 'application/json')

  const contentType = get('content-type')

  return `<h2>${contentType}</h2>`
})