Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
talentlessguy opened this issue Nov 12, 2021 · 1 comment
Open

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

talentlessguy opened this issue Nov 12, 2021 · 1 comment

Comments

@talentlessguy
Copy link
Member

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
@hazae41
Copy link
Member

hazae41 commented Nov 14, 2021

Hello, thank for submitting this issue.

Can you try to isolate the bug by finding what works in your code and what does not?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants