Repository: honojs/hono
File: src/adapter/aws-lambda/handler.ts (TypeScript)

Code before:

  return result
}

const createRequest = (event: LambdaEvent) => {
  const queryString = extractQueryString(event)
  const domainName =
    event.requestContext && 'domainName' in event.requestContext
      ? event.requestContext.domainName
      : event.headers['host']
  const path = isProxyEventV2(event) ? event.rawPath : event.path
  const urlPath = `https://${domainName}${path}`
  const url = queryString ? `${urlPath}?${queryString}` : urlPath

  const headers = new Headers()
  getCookies(event, headers)
  for (const [k, v] of Object.entries(event.headers)) {
    if (v) {
      headers.set(k, v)
    }
  }

  const method = isProxyEventV2(event) ? event.requestContext.http.method : event.httpMethod
  const requestInit: RequestInit = {
    headers,
    method,
  }

  if (event.body) {
    requestInit.body = event.isBase64Encoded ? Buffer.from(event.body, 'base64') : event.body
  }

  return new Request(url, requestInit)
}

const extractQueryString = (event: LambdaEvent) => {
  return isProxyEventV2(event)
    ? event.rawQueryString
    : Object.entries(event.queryStringParameters || {})
        .filter(([, value]) => value)
        .map(([key, value]) => `${key}=${value}`)
        .join('&')
}

const getCookies = (event: LambdaEvent, headers: Headers) => {
  if (isProxyEventV2(event) && Array.isArray(event.cookies)) {
    headers.set('Cookie', event.cookies.join('; '))
  }
}

const setCookies = (event: LambdaEvent, res: Response, result: APIGatewayProxyResult) => {

Review comment (suggestion):
Note) This is not `} else if ` because `headers` are not optional, at least in type definition in this repo.
It means that we need to take care of `headers` and `multiValueHeaders` in tests.


Code after:
    result.headers[key] = value
  })

  return result
}

const createRequest = (event: LambdaEvent) => {
  const queryString = extractQueryString(event)
  const domainName =
    event.requestContext && 'domainName' in event.requestContext
      ? event.requestContext.domainName
      : event.headers?.['host'] ?? event.multiValueHeaders?.['host']?.[0]
  const path = isProxyEventV2(event) ? event.rawPath : event.path
  const urlPath = `https://${domainName}${path}`
  const url = queryString ? `${urlPath}?${queryString}` : urlPath

  const headers = new Headers()
  getCookies(event, headers)
  if (event.headers) {
    for (const [k, v] of Object.entries(event.headers)) {
      if (v) {
        headers.set(k, v)
      }
    }
  }
  if (event.multiValueHeaders) {
    for (const [k, values] of Object.entries(event.multiValueHeaders)) {
      if (values) {
        values.forEach((v) => headers.append(k, v))
      }
    }
  }

  const method = isProxyEventV2(event) ? event.requestContext.http.method : event.httpMethod
  const requestInit: RequestInit = {
    headers,
    method,
  }

  if (event.body) {
    requestInit.body = event.isBase64Encoded ? Buffer.from(event.body, 'base64') : event.body
  }

  return new Request(url, requestInit)
}

const extractQueryString = (event: LambdaEvent) => {
  return isProxyEventV2(event)
    ? event.rawQueryString
    : Object.entries(event.queryStringParameters || {})
        .filter(([, value]) => value)