Skip to content
This repository has been archived by the owner on Dec 12, 2024. It is now read-only.

Commit

Permalink
Updated HTTP request body according to spec change (#263)
Browse files Browse the repository at this point in the history
* Changed  property to  in create exchange request

* updated request body for order and close request
  • Loading branch information
thehenrytsai authored Jul 30, 2024
1 parent 4c84157 commit c673b00
Show file tree
Hide file tree
Showing 9 changed files with 44 additions and 31 deletions.
7 changes: 7 additions & 0 deletions .changeset/odd-owls-invite.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"@tbdex/protocol": major
"@tbdex/http-client": major
"@tbdex/http-server": major
---

Updated HTTP request body to contain `message` property according to spec change.
6 changes: 3 additions & 3 deletions packages/http-client/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ export class TbdexHttpClient {
await rfq.verify()

const { to: pfiDid } = rfq.metadata
const requestBody = JSON.stringify({ rfq, replyTo: opts?.replyTo })
const requestBody = JSON.stringify({ message: rfq, replyTo: opts?.replyTo })

await TbdexHttpClient.sendMessage(pfiDid, 'POST', `/exchanges`, requestBody)
}
Expand All @@ -87,7 +87,7 @@ export class TbdexHttpClient {
await order.verify()

const { to: pfiDid, exchangeId } = order.metadata
const requestBody = JSON.stringify(order)
const requestBody = JSON.stringify({ message: order })

await TbdexHttpClient.sendMessage(pfiDid, 'PUT', `/exchanges/${exchangeId}`, requestBody)
}
Expand All @@ -104,7 +104,7 @@ export class TbdexHttpClient {

const { to: pfiDid, exchangeId } = close.metadata

const requestBody = JSON.stringify(close)
const requestBody = JSON.stringify({ message: close })

await TbdexHttpClient.sendMessage(pfiDid, 'PUT', `/exchanges/${exchangeId}`, requestBody)
}
Expand Down
5 changes: 4 additions & 1 deletion packages/http-server/src/request-handlers/create-exchange.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ type CreateExchangeOpts = {
exchangesApi: ExchangesApi
}

/**
* Handler for POST to /exchanges to create a new exchange.
*/
export async function createExchange(req: Request, res: Response, options: CreateExchangeOpts): Promise<void> {
const { offeringsApi, exchangesApi, callback } = options
const replyTo: string | undefined = req.body.replyTo
Expand All @@ -23,7 +26,7 @@ export async function createExchange(req: Request, res: Response, options: Creat
}

try {
rfq = await Rfq.parse(req.body.rfq)
rfq = await Rfq.parse(req.body.message)
} catch(e) {
const errorResponse: ErrorDetail = { detail: `Parsing of TBDex Rfq message failed: ${e.message}` }
res.status(400).json({ errors: [errorResponse] })
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export async function submitMessage(req: Request, res: Response, opts: SubmitMes
let message: Message

try {
message = await Parser.parseMessage(req.body)
message = await Parser.parseMessage(req.body.message)
} catch(e) {
const errorResponse: ErrorDetail = { detail: 'Request body was not a valid Order or Close message' }
res.status(400).json({ errors: [errorResponse] })
Expand Down
23 changes: 12 additions & 11 deletions packages/http-server/tests/create-exchange.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ describe('POST /exchanges/:exchangeId/rfq', () => {

const resp = await fetch('http://localhost:8000/exchanges', {
method : 'POST',
body : JSON.stringify({ rfq: rfq, replyTo: 'foo' })
body : JSON.stringify({ message: rfq, replyTo: 'foo' })
})

expect(resp.status).to.equal(400)
Expand All @@ -79,6 +79,7 @@ describe('POST /exchanges/:exchangeId/rfq', () => {
const aliceDid = await DidJwk.create()
const pfiDid = await DidJwk.create()

// create an Order instead of RFQ
const order = Order.create({
metadata: {
from : aliceDid.uri,
Expand All @@ -90,7 +91,7 @@ describe('POST /exchanges/:exchangeId/rfq', () => {

const resp = await fetch('http://localhost:8000/exchanges', {
method : 'POST',
body : JSON.stringify({ rfq: order })
body : JSON.stringify({ message: order })
})

expect(resp.status).to.equal(400)
Expand All @@ -112,7 +113,7 @@ describe('POST /exchanges/:exchangeId/rfq', () => {

const resp = await fetch('http://localhost:8000/exchanges', {
method : 'POST',
body : JSON.stringify({ rfq })
body : JSON.stringify({ message: rfq })
})

expect(resp.status).to.equal(400)
Expand All @@ -136,7 +137,7 @@ describe('POST /exchanges/:exchangeId/rfq', () => {

const resp = await fetch('http://localhost:8000/exchanges', {
method : 'POST',
body : JSON.stringify({ rfq })
body : JSON.stringify({ message: rfq })
})

expect(resp.status).to.equal(409)
Expand Down Expand Up @@ -169,7 +170,7 @@ describe('POST /exchanges/:exchangeId/rfq', () => {

const resp = await fetch('http://localhost:8000/exchanges', {
method : 'POST',
body : JSON.stringify({ rfq })
body : JSON.stringify({ message: rfq })
})

expect(resp.status).to.equal(400)
Expand Down Expand Up @@ -206,7 +207,7 @@ describe('POST /exchanges/:exchangeId/rfq', () => {

const resp = await fetch('http://localhost:8000/exchanges', {
method : 'POST',
body : JSON.stringify({ rfq })
body : JSON.stringify({ message: rfq })
})

expect(resp.status).to.equal(400)
Expand Down Expand Up @@ -311,7 +312,7 @@ describe('POST /exchanges/:exchangeId/rfq', () => {
it('returns a 202 if RFQ is accepted', async () => {
const resp = await fetch('http://localhost:8000/exchanges', {
method : 'POST',
body : JSON.stringify({ rfq })
body : JSON.stringify({ message: rfq })
})

expect(resp.status).to.equal(202)
Expand All @@ -326,7 +327,7 @@ describe('POST /exchanges/:exchangeId/rfq', () => {

const resp = await fetch('http://localhost:8000/exchanges', {
method : 'POST',
body : JSON.stringify({ rfq })
body : JSON.stringify({ message: rfq })
})

expect(resp.status).to.equal(202)
Expand All @@ -351,7 +352,7 @@ describe('POST /exchanges/:exchangeId/rfq', () => {

const response = await fetch('http://localhost:8000/exchanges', {
method : 'POST',
body : JSON.stringify({ rfq })
body : JSON.stringify({ message: rfq })
})

expect(response.status).to.equal(customErrorStatus)
Expand All @@ -369,7 +370,7 @@ describe('POST /exchanges/:exchangeId/rfq', () => {

const response = await fetch('http://localhost:8000/exchanges', {
method : 'POST',
body : JSON.stringify({ rfq })
body : JSON.stringify({ message: rfq })
})

expect(response.status).to.equal(500)
Expand All @@ -389,7 +390,7 @@ describe('POST /exchanges/:exchangeId/rfq', () => {

const resp = await fetch('http://localhost:8000/exchanges', {
method : 'POST',
body : JSON.stringify({ rfq, replyTo })
body : JSON.stringify({ message: rfq, replyTo })
})

expect(resp.status).to.equal(202)
Expand Down
14 changes: 7 additions & 7 deletions packages/http-server/tests/submit-close.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ describe('POST /exchanges/:exchangeId with a Close', () => {
await close.sign(alice)
const resp = await fetch('http://localhost:8000/exchanges/123', {
method : 'PUT',
body : JSON.stringify(close)
body : JSON.stringify({ message: close })
})

expect(resp.status).to.equal(404)
Expand Down Expand Up @@ -100,7 +100,7 @@ describe('POST /exchanges/:exchangeId with a Close', () => {
await close2.sign(alice)
const resp = await fetch(`http://localhost:8000/exchanges/${exchangeId}`, {
method : 'PUT',
body : JSON.stringify(close2)
body : JSON.stringify({ message: close2 })
})

expect(resp.status).to.equal(409)
Expand Down Expand Up @@ -141,7 +141,7 @@ describe('POST /exchanges/:exchangeId with a Close', () => {

const resp = await fetch(`http://localhost:8000/exchanges/${rfq.metadata.exchangeId}`, {
method : 'PUT',
body : JSON.stringify(close)
body : JSON.stringify({ message: close })
})

expect(resp.status).to.equal(202)
Expand Down Expand Up @@ -175,7 +175,7 @@ describe('POST /exchanges/:exchangeId with a Close', () => {

const resp = await fetch(`http://localhost:8000/exchanges/${rfq.metadata.exchangeId}`, {
method : 'PUT',
body : JSON.stringify(close)
body : JSON.stringify({ message: close })
})

expect(resp.status).to.equal(202)
Expand Down Expand Up @@ -209,7 +209,7 @@ describe('POST /exchanges/:exchangeId with a Close', () => {

const resp = await fetch(`http://localhost:8000/exchanges/${rfq.metadata.exchangeId}`, {
method : 'PUT',
body : JSON.stringify(close)
body : JSON.stringify({ message: close })
})

expect(resp.status).to.equal(400)
Expand Down Expand Up @@ -243,7 +243,7 @@ describe('POST /exchanges/:exchangeId with a Close', () => {

const resp = await fetch('http://localhost:8000/exchanges/123', {
method : 'PUT',
body : JSON.stringify(close)
body : JSON.stringify({ message: close })
})

expect(resp.status).to.equal(400)
Expand Down Expand Up @@ -279,7 +279,7 @@ describe('POST /exchanges/:exchangeId with a Close', () => {

const resp = await fetch(`http://localhost:8000/exchanges/${rfq.metadata.exchangeId}`, {
method : 'PUT',
body : JSON.stringify(close)
body : JSON.stringify({ message: close })
})

expect(resp.status).to.equal(202)
Expand Down
6 changes: 4 additions & 2 deletions packages/http-server/tests/submit-message.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ describe('POST /exchanges/:exchangeId', () => {

const resp = await fetch('http://localhost:8000/exchanges/123', {
method : 'PUT',
body : JSON.stringify(order)
body : JSON.stringify({ message: order })
})

expect(resp.status).to.equal(400)
Expand All @@ -66,13 +66,15 @@ describe('POST /exchanges/:exchangeId', () => {

it('returns a 400 if request body is not a valid close or order object', async () => {
const alice = await DidJwk.create()

// creating an RFQ instead of an Order/close which is an invalid message to the path
const rfq = await DevTools.createRfq({
sender: alice
})
await rfq.sign(alice)
const resp = await fetch(`http://localhost:8000/exchanges/${rfq.metadata.exchangeId}`, {
method : 'PUT',
body : JSON.stringify(rfq)
body : JSON.stringify({ message: rfq })
})

expect(resp.status).to.equal(400)
Expand Down
10 changes: 5 additions & 5 deletions packages/http-server/tests/submit-order.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ describe('POST /exchanges/:exchangeId with an Order', () => {
await order.sign(aliceDid)
const resp = await fetch('http://localhost:8000/exchanges/123', {
method : 'PUT',
body : JSON.stringify(order)
body : JSON.stringify({ message: order })
})

expect(resp.status).to.equal(404)
Expand Down Expand Up @@ -103,7 +103,7 @@ describe('POST /exchanges/:exchangeId with an Order', () => {

const resp = await fetch(`http://localhost:8000/exchanges/${rfq.metadata.exchangeId}`, {
method : 'PUT',
body : JSON.stringify(order)
body : JSON.stringify({ message: order })
})

expect(resp.status).to.equal(409)
Expand Down Expand Up @@ -155,7 +155,7 @@ describe('POST /exchanges/:exchangeId with an Order', () => {

const resp = await fetch(`http://localhost:8000/exchanges/${rfq.metadata.exchangeId}`, {
method : 'PUT',
body : JSON.stringify(order)
body : JSON.stringify({ message: order })
})

expect(resp.status).to.equal(410)
Expand Down Expand Up @@ -209,7 +209,7 @@ describe('POST /exchanges/:exchangeId with an Order', () => {

const resp = await fetch(`http://localhost:8000/exchanges/${rfq.metadata.exchangeId}`, {
method : 'PUT',
body : JSON.stringify(order)
body : JSON.stringify({ message: order })
})

expect(resp.status).to.equal(202)
Expand Down Expand Up @@ -261,7 +261,7 @@ describe('POST /exchanges/:exchangeId with an Order', () => {

const resp = await fetch(`http://localhost:8000/exchanges/${rfq.metadata.exchangeId}`, {
method : 'PUT',
body : JSON.stringify(order)
body : JSON.stringify({ message: order })
})

expect(resp.status).to.equal(202)
Expand Down
2 changes: 1 addition & 1 deletion packages/protocol/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ const rfq = Rfq.create({

await rfq.sign(alice)

console.log(JSON.stringify(rfq, null, 2))
console.log(JSON.stringify({ message: rfq }, null, 2))
```

## Message Parsing
Expand Down

0 comments on commit c673b00

Please sign in to comment.