Skip to content

Getting "http: response headers already sent" on Deno 1.16.1 #428

Open
@talentlessguy

Description

@talentlessguy

Example code: (api/wallet.ts)

import { balance } from 'https://deno.land/x/[email protected]/deno/mod.ts'
import { LRU } from 'https://deno.land/x/[email protected]/mod.ts'
import type { APIHandler } from 'aleph/types.d.ts'

type ThenArg<T> = T extends PromiseLike<infer U> ? U : T

type PromiseValue<F extends (...args: any[]) => any> = ThenArg<ReturnType<F>> // => number

const toOne = (x: any | any[]) => (x ? (Array.isArray(x) ? x[0] : x) : false)

type WalletData = {
  balance: number
  address: string
  token: string
  qrImg: string
  logoURI: string
}

const cache = new LRU<PromiseValue<typeof balance>>(5)

const handler: APIHandler = async ({ request, response: res }) => {
  const params = new URL(request.url).searchParams

  const address = params.get('address')
  let coin = params.get('coin')

  if (!coin || !address) {
    res.status = 400

    if (!coin) res.body = 'Missing token symbol'
    if (!address) res.body = 'Missing address'
    return
  }

  coin = coin.toUpperCase()

  let balanceValue = 0

  let result = cache.get(`${address}-${coin}`)

  if (!result) {
    result = await balance(address, coin, {
      apiKeys: {
        etherscan: Deno.env.get('ETHERSCAN_KEY'),
        blockcypher: Deno.env.get('BLOCKCYPHER_KEY')
      },
      verbose: true
    })
    cache.set(`${address}-${coin}`, result)
  }

  const digits = parseInt(toOne(params.get('digits')))

  if (result?.balance) balanceValue = parseFloat(result!.balance.toFixed(digits < 10 ? digits : 3))

  res.body = JSON.stringify({
    token: coin,
    qrImg: `https://chart.googleapis.com/chart?chs=250x250&cht=qr&chl=${address}&choe=UTF-8`,
    logoURI: `https://cdn.jsdelivr.net/gh/atomiclabs/[email protected]/svg/color/${coin.toLowerCase()}.svg`,
    ...result,
    balance: balanceValue,
    address
  } as WalletData)
}

export default handler

Example request:

http://localhost:8080/api/wallet?coin=usdc&address=0xD3B282e9880cDcB1142830731cD83f7ac0e1043f

Environment

  • Aleph version: 0.3.0-beta.19
  • Deno version: 1.16.1
  • React version: 17.0.2

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions