From a6bdc80a07668a6808b6e014009a28e68d986eef Mon Sep 17 00:00:00 2001 From: Henry Tsai Date: Fri, 19 Apr 2024 11:55:48 -0700 Subject: [PATCH 1/2] Removed query params for List Exchange API --- .changeset/modern-bobcats-ring.md | 5 ++ .../src/in-memory-exchanges-api.ts | 25 +++------ .../src/request-handlers/get-exchanges.ts | 14 ++--- packages/http-server/src/types.ts | 2 - .../http-server/tests/get-exchanges.spec.ts | 53 ------------------- 5 files changed, 15 insertions(+), 84 deletions(-) create mode 100644 .changeset/modern-bobcats-ring.md diff --git a/.changeset/modern-bobcats-ring.md b/.changeset/modern-bobcats-ring.md new file mode 100644 index 00000000..31406676 --- /dev/null +++ b/.changeset/modern-bobcats-ring.md @@ -0,0 +1,5 @@ +--- +"@tbdex/http-server": minor +--- + +Deprecated query params for List Exchanges API diff --git a/packages/http-server/src/in-memory-exchanges-api.ts b/packages/http-server/src/in-memory-exchanges-api.ts index 16053041..dfacb474 100644 --- a/packages/http-server/src/in-memory-exchanges-api.ts +++ b/packages/http-server/src/in-memory-exchanges-api.ts @@ -24,25 +24,14 @@ export class InMemoryExchangesApi implements ExchangesApi { } const exchanges: Exchange[] = [] - if (opts.filter.id) { - // filter has `id` and `from` - - for (const id of opts.filter.id) { - const exchange = this.exchangeMessagesMap.get(id) - if (exchange?.rfq?.from === opts.filter.from) { - exchanges.push(exchange) - } + // filter only has `from` + this.exchangeMessagesMap.forEach((exchange, _id) => { + // You definitely shouldn't use InMemoryExchangesApi in production. + // This will get really slow + if (exchange?.rfq?.from === opts.filter.from) { + exchanges.push(exchange) } - } else { - // filter only has `from` - this.exchangeMessagesMap.forEach((exchange, _id) => { - // You definitely shouldn't use InMemoryExchangesApi in production. - // This will get really slow - if (exchange?.rfq?.from === opts.filter.from) { - exchanges.push(exchange) - } - }) - } + }) return exchanges } diff --git a/packages/http-server/src/request-handlers/get-exchanges.ts b/packages/http-server/src/request-handlers/get-exchanges.ts index fbbc7c9e..95a5ae77 100644 --- a/packages/http-server/src/request-handlers/get-exchanges.ts +++ b/packages/http-server/src/request-handlers/get-exchanges.ts @@ -34,24 +34,16 @@ export async function getExchanges(request: Request, response: Response, opts: G return } - const queryParams: GetExchangesFilter = { + const filter: GetExchangesFilter = { from: requesterDid, } - if (request.query.id !== undefined) { - if (Array.isArray(request.query.id)) { - queryParams.id = request.query.id.map((id) => id.toString()) - } else { - queryParams.id = [request.query.id.toString()] - } - } - - const exchanges = await exchangesApi.getExchanges({ filter: queryParams }) + const exchanges = await exchangesApi.getExchanges({ filter }) if (callback) { // TODO: figure out what to do with callback result. should we pass through the exchanges we've fetched // and allow the callback to modify what's returned? (issue #10) - const _result = await callback({ request, response }, queryParams) + const _result = await callback({ request, response }, filter) } const data: Message[][] = exchanges.map(exchange => exchange.messages) diff --git a/packages/http-server/src/types.ts b/packages/http-server/src/types.ts index 2ca66b6f..62d679ec 100644 --- a/packages/http-server/src/types.ts +++ b/packages/http-server/src/types.ts @@ -55,8 +55,6 @@ export type SubmitMessageCallback = SubmitOrderCallback | SubmitCloseCallback * @beta */ export type GetExchangesFilter = { - /** List of exchanges ids */ - id?: string[] /** the rfq author's DID */ from: string } diff --git a/packages/http-server/tests/get-exchanges.spec.ts b/packages/http-server/tests/get-exchanges.spec.ts index a6f9359b..5bfb99d7 100644 --- a/packages/http-server/tests/get-exchanges.spec.ts +++ b/packages/http-server/tests/get-exchanges.spec.ts @@ -87,59 +87,6 @@ describe('GET /exchanges', () => { exchangesApiSpy.restore() }) - - it('passes the id non-array query param as an array to the filter of ExchangesApi.getExchanges', async () => { - const alice = await DidJwk.create() - - const exchangesApiSpy = sinon.spy(api.exchangesApi, 'getExchanges') - - const requestToken = await TbdexHttpClient.generateRequestToken({ requesterDid: alice, pfiDid: api.pfiDid }) - - // `id` query param contains a single string - const idQueryParam = '1234' - const resp = await fetch(`http://localhost:8000/exchanges?id=${idQueryParam}`, { - headers: { - 'Authorization': `Bearer ${requestToken}` - } - }) - - expect(resp.ok).to.be.true - expect(exchangesApiSpy.calledOnce).to.be.true - expect(exchangesApiSpy).to.have.been.calledWith({ - filter: { - from : alice.uri, - id : [idQueryParam] - } - }) - - exchangesApiSpy.restore() - }) - - it.only('passes the id array query param as an array to the filter of ExchangesApi.getExchanges', async () => { - const alice = await DidJwk.create() - - const exchangesApiSpy = sinon.spy(api.exchangesApi, 'getExchanges') - - const requestToken = await TbdexHttpClient.generateRequestToken({ requesterDid: alice, pfiDid: api.pfiDid }) - - const queryParams = { id: ['1234', '5678'] } - const queryParamsString = queryString.stringify(queryParams) - const resp = await fetch(`http://localhost:8000/exchanges?${queryParamsString}`, { - headers: { - 'Authorization': `Bearer ${requestToken}` - } - }) - - expect(resp.ok).to.be.true - expect(exchangesApiSpy).to.have.been.calledWith({ - filter: { - from: alice.uri, - ...queryParams - } - }) - - exchangesApiSpy.restore() - }) }) it('calls the callback if it is provided', async () => { From 1b2078a9090e82f8ff4201542aa872cea8fbec9f Mon Sep 17 00:00:00 2001 From: Henry Tsai Date: Fri, 19 Apr 2024 12:32:27 -0700 Subject: [PATCH 2/2] Fixed lint error --- packages/http-server/tests/get-exchanges.spec.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/packages/http-server/tests/get-exchanges.spec.ts b/packages/http-server/tests/get-exchanges.spec.ts index 5bfb99d7..163d4f53 100644 --- a/packages/http-server/tests/get-exchanges.spec.ts +++ b/packages/http-server/tests/get-exchanges.spec.ts @@ -1,6 +1,5 @@ import type { Server } from 'http' import Sinon, * as sinon from 'sinon' -import queryString from 'query-string' import sinonChai from 'sinon-chai' import chai from 'chai'