diff --git a/.changeset/nervous-apricots-crash.md b/.changeset/nervous-apricots-crash.md new file mode 100644 index 00000000..5db53040 --- /dev/null +++ b/.changeset/nervous-apricots-crash.md @@ -0,0 +1,5 @@ +--- +"@tbdex/http-server": patch +--- + +Fixes http-server GET /exchange/\* responses diff --git a/packages/http-server/src/request-handlers/get-exchange.ts b/packages/http-server/src/request-handlers/get-exchange.ts index 32124f2f..0f116877 100644 --- a/packages/http-server/src/request-handlers/get-exchange.ts +++ b/packages/http-server/src/request-handlers/get-exchange.ts @@ -1,5 +1,6 @@ import type { ExchangesApi, GetExchangeCallback } from '../types.js' import { TbdexHttpClient } from '@tbdex/http-client' +import { Message } from '@tbdex/protocol' import { Request, Response } from 'express' type GetExchangeOpts = { @@ -49,5 +50,7 @@ export async function getExchange(request: Request, response: Response, opts: Ge const _result = await callback({ request, response }) } - response.status(200).json({ data: exchange }) + const data: Message[] = exchange.messages + + response.status(200).json({ data }) } \ No newline at end of file diff --git a/packages/http-server/src/request-handlers/get-exchanges.ts b/packages/http-server/src/request-handlers/get-exchanges.ts index 0c94d90d..fbbc7c9e 100644 --- a/packages/http-server/src/request-handlers/get-exchanges.ts +++ b/packages/http-server/src/request-handlers/get-exchanges.ts @@ -1,6 +1,7 @@ import type { ExchangesApi, GetExchangesCallback, GetExchangesFilter } from '../types.js' import { TbdexHttpClient } from '@tbdex/http-client' +import { Message } from '@tbdex/protocol' import { Request, Response } from 'express' type GetExchangesOpts = { @@ -53,5 +54,7 @@ export async function getExchanges(request: Request, response: Response, opts: G const _result = await callback({ request, response }, queryParams) } - response.status(200).json({ data: exchanges }) + const data: Message[][] = exchanges.map(exchange => exchange.messages) + + response.status(200).json({ data }) } \ No newline at end of file diff --git a/packages/http-server/tests/get-exchange.spec.ts b/packages/http-server/tests/get-exchange.spec.ts index 4cc9f6be..cf832d35 100644 --- a/packages/http-server/tests/get-exchange.spec.ts +++ b/packages/http-server/tests/get-exchange.spec.ts @@ -139,4 +139,26 @@ describe('GET /exchanges', () => { expect(callbackSpy.callCount).to.eq(1) // TODO: Check what arguments are passed to callback after we finalize its behavior }) + + it(`returns Exchange.messages from ExchangesApi.getExchange`, async () => { + const rfq = await DevTools.createRfq({ sender: alice, receiver: pfi }) + await rfq.sign(alice); + (api.exchangesApi as InMemoryExchangesApi).addMessage(rfq) + + const exchangesApiSpy = sinon.spy(api.exchangesApi, 'getExchange') + + const requestToken = await TbdexHttpClient.generateRequestToken({ requesterDid: alice, pfiDid: api.pfiDid }) + const exchangeId = rfq.metadata.exchangeId + const resp = await fetch(`http://localhost:8000/exchanges/${exchangeId}`, { + headers: { + 'Authorization': `Bearer ${requestToken}` + } + }) + + const data = await resp.json() + const rfqJson = rfq.toJSON() + expect(data).to.deep.equal({ data: [ rfqJson ] }) + + exchangesApiSpy.restore() + }) }) \ No newline at end of file diff --git a/packages/http-server/tests/get-exchanges.spec.ts b/packages/http-server/tests/get-exchanges.spec.ts index 27edc55e..4d54f96a 100644 --- a/packages/http-server/tests/get-exchanges.spec.ts +++ b/packages/http-server/tests/get-exchanges.spec.ts @@ -160,4 +160,27 @@ describe('GET /exchanges', () => { expect(callbackSpy.callCount).to.eq(1) // TODO: Check what arguments are passed to callback after we finalize its behavior }) + + it(`returns an array of Exchange.messages from ExchangesApi.getExchanges`, async () => { + const aliceDid = await DidJwk.create() + const pfiDid = await DidJwk.create() + const rfq = await DevTools.createRfq({ sender: aliceDid, receiver: pfiDid }) + await rfq.sign(aliceDid); + (api.exchangesApi as InMemoryExchangesApi).addMessage(rfq) + + const exchangesApiSpy = sinon.spy(api.exchangesApi, 'getExchanges') + + const requestToken = await TbdexHttpClient.generateRequestToken({ requesterDid: aliceDid, pfiDid: api.pfiDid }) + const resp = await fetch(`http://localhost:8000/exchanges`, { + headers: { + 'Authorization': `Bearer ${requestToken}` + } + }) + + const data: any = await resp.json() + const rfqJson = rfq.toJSON() + expect(data).to.deep.equal({ data: [[ rfqJson ]] }) + + exchangesApiSpy.restore() + }) }) \ No newline at end of file