Skip to content

Commit

Permalink
Removed query params for List Exchanges API (#244)
Browse files Browse the repository at this point in the history
  • Loading branch information
thehenrytsai authored Apr 23, 2024
1 parent 8d8f022 commit c7da892
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 85 deletions.
5 changes: 5 additions & 0 deletions .changeset/modern-bobcats-ring.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@tbdex/http-server": minor
---

Deprecated query params for List Exchanges API
25 changes: 7 additions & 18 deletions packages/http-server/src/in-memory-exchanges-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
14 changes: 3 additions & 11 deletions packages/http-server/src/request-handlers/get-exchanges.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 0 additions & 2 deletions packages/http-server/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
54 changes: 0 additions & 54 deletions packages/http-server/tests/get-exchanges.spec.ts
Original file line number Diff line number Diff line change
@@ -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'

Expand Down Expand Up @@ -87,59 +86,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 () => {
Expand Down

0 comments on commit c7da892

Please sign in to comment.