This repository has been archived by the owner on Dec 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* Update existing endpoints in server * Update client to new endpoints * Implement getExchange server endpoint * Lint * Woops use PUT * Changeset * fix test * tbdocs * Rename SubmitRfqCallback -> CreateExchangeCallback
- Loading branch information
Diane Huxley
authored
Mar 20, 2024
1 parent
e449a21
commit 69d10f0
Showing
15 changed files
with
465 additions
and
247 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
--- | ||
"@tbdex/http-client": minor | ||
"@tbdex/http-server": minor | ||
--- | ||
|
||
Restructure HTTP exchange endpoints |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import type { ExchangesApi, GetExchangeCallback } from '../types.js' | ||
import { TbdexHttpClient } from '@tbdex/http-client' | ||
import { Request, Response } from 'express' | ||
|
||
type GetExchangeOpts = { | ||
callback?: GetExchangeCallback | ||
exchangesApi: ExchangesApi | ||
pfiDid: string | ||
} | ||
|
||
export async function getExchange(request: Request, response: Response, opts: GetExchangeOpts): Promise<void> { | ||
const { callback, exchangesApi, pfiDid } = opts | ||
|
||
const authzHeader = request.headers['authorization'] | ||
if (!authzHeader) { | ||
response.status(401).json({ errors: [{ detail: 'Authorization header required' }] }) | ||
return | ||
} | ||
|
||
const [_, requestToken] = authzHeader.split('Bearer ') | ||
|
||
if (!requestToken) { | ||
response.status(401).json({ errors: [{ detail: 'Malformed Authorization header. Expected: Bearer TOKEN_HERE' }] }) | ||
return | ||
} | ||
|
||
let requesterDid: string | ||
try { | ||
requesterDid = await TbdexHttpClient.verifyRequestToken({ requestToken: requestToken, pfiDid }) | ||
} catch(e) { | ||
response.status(401).json({ errors: [{ detail: `Malformed Authorization header: ${e}` }] }) | ||
return | ||
} | ||
|
||
const exchange = await exchangesApi.getExchange({ id: request.params.exchangeId }) | ||
if (exchange === undefined) { | ||
response.status(404).json({ errors: [{ detail: `No exchange found with exchangeId ${request.params.exchangeId}` }] }) | ||
return | ||
} else { | ||
if (exchange.rfq!.metadata.from !== requesterDid) { | ||
response.status(403).json({ errors: [{ detail: `Forbidden` }] }) | ||
return | ||
} | ||
} | ||
|
||
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 }) | ||
} | ||
|
||
response.status(200).json({ data: exchange }) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
packages/http-server/src/request-handlers/submit-message.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import type { ExchangesApi, SubmitCloseCallback, SubmitOrderCallback } from '../types.js' | ||
import type { ErrorDetail } from '@tbdex/http-client' | ||
import { Message } from '@tbdex/protocol' | ||
|
||
import { Request, Response } from 'express' | ||
import { submitOrder } from './submit-order.js' | ||
import { submitClose } from './submit-close.js' | ||
import { Parser } from '@tbdex/protocol' | ||
|
||
type SubmitMessageOpts = { | ||
submitOrderCallback?: SubmitOrderCallback | ||
submitCloseCallback?: SubmitCloseCallback | ||
exchangesApi: ExchangesApi | ||
} | ||
|
||
export async function submitMessage(req: Request, res: Response, opts: SubmitMessageOpts): Promise<void> { | ||
let message: Message | ||
|
||
try { | ||
message = await Parser.parseMessage(req.body) | ||
} catch(e) { | ||
const errorResponse: ErrorDetail = { detail: 'Request body was not a valid Order or Close message' } | ||
res.status(400).json({ errors: [errorResponse] }) | ||
return | ||
} | ||
|
||
if (message.metadata.exchangeId !== req.params.exchangeId) { | ||
const errorResponse: ErrorDetail = { detail: 'ExchangeId in message did not match exchangeId in path' } | ||
res.status(400).json({ errors: [errorResponse] }) | ||
return | ||
} | ||
|
||
if (message.isOrder()) { | ||
await submitOrder(message, req, res, { | ||
callback : opts.submitOrderCallback, | ||
exchangesApi : opts.exchangesApi, | ||
}) | ||
} else if (message.isClose()) { | ||
await submitClose(message, req, res, { | ||
callback : opts.submitCloseCallback, | ||
exchangesApi : opts.exchangesApi, | ||
}) | ||
} else { | ||
const errorResponse: ErrorDetail = { detail: 'Request body was not a valid Order or Close message' } | ||
res.status(400).json({ errors: [errorResponse] }) | ||
return | ||
} | ||
} |
Oops, something went wrong.