import * as Client from './Client.js' import * as TestApp from '../test/App.js' test('uses hosted API URL by default', () => { const client = Client.create() expect(Client.defaultUrl).toMatchInlineSnapshot(`"https://api.tempo.xyz"`) expect( client.v1.blocks[':block'].$url({ param: { block: 'latest' } }).toString(), ).toMatchInlineSnapshot(`"https://api.tempo.xyz/v1/blocks/latest"`) }) test('calls Tempo API app through configured fetch implementation', async () => { const app = TestApp.create() const fetch = (async (input, init) => app.fetch(new Request(input, init))) satisfies typeof globalThis.fetch const client = Client.create({ fetch, url: 'http://tempo-api.test' }) const response = await client.v1['verified-tokens'].currencies.$get() const body = await response.json() expect(response.status).toMatchInlineSnapshot(`200`) expect(body).toMatchInlineSnapshot(` { "data": [], } `) }) test('applies `apiKey` to canonical header over base headers', async () => { const { calls, fetch } = captureFetch() const client = Client.create({ apiKey: 'key_option', fetch, headers: { authorization: 'Payment proof', 'tempo-api-key': 'key_header', 'user-agent': 'tempo-api-test', }, url: 'https://api.test', }) await client.v1.blocks[':block'].$get({ param: { block: 'latest' }, query: {} }) expect(calls).toMatchInlineSnapshot(` [ { "headers": { "authorization": "Payment proof", "tempo-api-key": "key_option", "user-agent": "tempo-api-test", }, "url": "https://api.test/v1/blocks/latest?", }, ] `) }) test('merges `apiKey` with dynamic base headers', async () => { const { calls, fetch } = captureFetch() const client = Client.create({ apiKey: 'key_option', fetch, headers: async () => ({ authorization: 'Payment proof' }), url: 'https://api.test', }) await client.v1.blocks[':block'].$get({ param: { block: 'latest' }, query: {} }) expect(calls).toMatchInlineSnapshot(` [ { "headers": { "authorization": "Payment proof", "tempo-api-key": "key_option", }, "url": "https://api.test/v1/blocks/latest?", }, ] `) }) test('lets per-request headers override base `apiKey`', async () => { const { calls, fetch } = captureFetch() const client = Client.create({ apiKey: 'key_base', fetch, url: 'https://api.test' }) await client.v1.blocks[':block'].$get( { param: { block: 'latest' }, query: {} }, { headers: { 'tempo-api-key': 'key_request' } }, ) expect(calls).toMatchInlineSnapshot(` [ { "headers": { "tempo-api-key": "key_request", }, "url": "https://api.test/v1/blocks/latest?", }, ] `) }) test('omits empty API keys', async () => { const { calls, fetch } = captureFetch() const client = Client.create({ apiKey: '', fetch, url: 'https://api.test' }) await client.v1.blocks[':block'].$get({ param: { block: 'latest' }, query: {} }) expect(calls).toMatchInlineSnapshot(` [ { "headers": {}, "url": "https://api.test/v1/blocks/latest?", }, ] `) }) function captureFetch() { const calls: { headers: Record; url: string }[] = [] const fetch = (async (input, init) => { calls.push({ headers: Object.fromEntries(new Headers(init?.headers).entries()), url: (() => { if (typeof input === 'string') return input if (input instanceof URL) return input.toString() return input.url })(), }) return new Response('{}', { headers: { 'content-type': 'application/json' }, status: 200 }) }) satisfies typeof globalThis.fetch return { calls, fetch } }