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

Commit

Permalink
Stricten http-client (#169)
Browse files Browse the repository at this point in the history
* Stricten http-client

* Changeset
  • Loading branch information
Diane Huxley authored Feb 13, 2024
1 parent ca1b235 commit 629f0c7
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 66 deletions.
5 changes: 5 additions & 0 deletions .changeset/mighty-pandas-speak.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@tbdex/http-client": minor
---

Stricten, tested, and remove untested code
77 changes: 14 additions & 63 deletions packages/http-client/src/client.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { JwtPayload } from '@web5/crypto'
import type { ErrorDetail } from './types.js'
import type { PortableDid } from '@web5/dids'
import type { DidDocument, PortableDid } from '@web5/dids'
import {
MessageModel,
Parser,
Expand Down Expand Up @@ -93,56 +93,6 @@ export class TbdexHttpClient {
}
}

/**
* Discover PFIs that are anchored via did:ion. These have a type of "PFI" and an id of PFI.
* You can then query the endpoints for offerings.
*/
static async discoverPFIs() {
const BASE_URL = 'https://ion.tbd.engineering'
const DID_TYPE_ENDPOINT = '/didtype/1669'
const IDENTIFIER_PREFIX = '/identifiers/'

async function fetchDIDList() {
const response = await fetch(BASE_URL + DID_TYPE_ENDPOINT)
if (!response.ok) {
throw new Error('Failed to fetch DID list')
}
return await response.json()
}

async function fetchDIDData(did) {
console.log(BASE_URL + IDENTIFIER_PREFIX + did)
const response = await fetch(BASE_URL + IDENTIFIER_PREFIX + did)
if (!response.ok) {
throw new Error('Failed to fetch DID data for ' + did)
}
return await response.json()
}

const ids = await fetchDIDList()
const promises = ids.map(id => {
const ionDid = 'did:ion:' + id
return fetchDIDData(ionDid)
})
const didDataList = await Promise.all(promises)

const pfiServiceEndpoints = didDataList.reduce((results, didData) => {
const services = didData.didDocument.service
const pfiServices = services.filter(service => service.type === 'PFI')

if (pfiServices.length > 0) {
results.push({
did : didData.didDocument.id,
serviceEndpoint : pfiServices[0].serviceEndpoint
})
}

return results
}, [])

return pfiServiceEndpoints
}

/**
* gets offerings from the pfi provided
* @param opts - options
Expand Down Expand Up @@ -218,6 +168,7 @@ export class TbdexHttpClient {

}

// TODO: Wrap Message[] in Exchange object and verify each message
/**
* returns all exchanges created by requester
* @param _opts - options
Expand Down Expand Up @@ -268,21 +219,20 @@ export class TbdexHttpClient {
* @param did - the pfi's DID
*/
static async getPfiServiceEndpoint(did: string) {
let didDocument: DidDocument
try {
const didDocument = await resolveDid(did)
const [didService] = didUtils.getServices({ didDocument, type: 'PFI' })
didDocument = await resolveDid(did)
} catch (e) {
throw new InvalidDidError(e.message)
}

if (!didService?.serviceEndpoint) {
throw new MissingServiceEndpointError(`${did} has no PFI service entry`)
}
const [didService] = didUtils.getServices({ didDocument, type: 'PFI' })

return didService.serviceEndpoint
} catch (e) {
if (e instanceof MissingServiceEndpointError) {
throw e
}
throw new InvalidDidError(e)
if (!didService?.serviceEndpoint) {
throw new MissingServiceEndpointError(`${did} has no PFI service entry`)
}

return didService.serviceEndpoint
}

/**
Expand Down Expand Up @@ -356,7 +306,8 @@ export class TbdexHttpClient {
throw new RequestTokenAudienceMismatchError({ message: 'Request token contains invalid audience. Expected aud property to be PFI DID.' })
}

return requestTokenPayload.iss
// TODO: check iss against signer DID
return requestTokenPayload.iss!
}
}

Expand Down
2 changes: 1 addition & 1 deletion packages/http-client/src/errors/request-error.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
export type RequestErrorParams = {

Check warning on line 1 in packages/http-client/src/errors/request-error.ts

View workflow job for this annotation

GitHub Actions / tbdocs-reporter

extractor: ae-missing-release-tag

"RequestErrorParams" is part of the package's API, but it is missing a release tag (`@alpha`, `@beta`, `@public`, or `@internal`)

Check warning on line 1 in packages/http-client/src/errors/request-error.ts

View workflow job for this annotation

GitHub Actions / tbdocs-reporter

extractor: ae-undocumented

Missing documentation for "RequestErrorParams".

Check warning on line 1 in packages/http-client/src/errors/request-error.ts

View workflow job for this annotation

GitHub Actions / build-docs

extractor: ae-missing-release-tag

"RequestErrorParams" is part of the package's API, but it is missing a release tag (`@alpha`, `@beta`, `@public`, or `@internal`)

Check warning on line 1 in packages/http-client/src/errors/request-error.ts

View workflow job for this annotation

GitHub Actions / build-docs

extractor: ae-undocumented

Missing documentation for "RequestErrorParams".
message: string
recipientDid: string
url?: string
url: string
cause?: unknown
}

Expand Down
2 changes: 1 addition & 1 deletion packages/http-client/tests/client.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ describe('client', () => {
}
})

it('returns exchanges array if response is ok', async () => {
it('returns empty exchanges array if response is ok and body is empty array', async () => {
fetchStub.resolves({
ok : true,
json : () => Promise.resolve({ data: [] })
Expand Down
2 changes: 2 additions & 0 deletions packages/http-client/tests/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
{
"extends": "../tsconfig.json",
"compilerOptions": {
"strict": true,
"useUnknownInCatchVariables": false,
"outDir": "compiled",
"declarationDir": "compiled/types",
"sourceMap": true,
Expand Down
3 changes: 2 additions & 1 deletion packages/http-client/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"compilerOptions": {
"strict": false,
"strict": true,
"useUnknownInCatchVariables": false,
"lib": ["DOM", "es2022"],
"rootDir": "./",

Expand Down

0 comments on commit 629f0c7

Please sign in to comment.