From 629f0c772a0159e8bc06465dba7d2ec3a420b30d Mon Sep 17 00:00:00 2001 From: Diane Huxley Date: Tue, 13 Feb 2024 09:31:51 -0800 Subject: [PATCH] Stricten http-client (#169) * Stricten http-client * Changeset --- .changeset/mighty-pandas-speak.md | 5 ++ packages/http-client/src/client.ts | 77 ++++--------------- .../http-client/src/errors/request-error.ts | 2 +- packages/http-client/tests/client.spec.ts | 2 +- packages/http-client/tests/tsconfig.json | 2 + packages/http-client/tsconfig.json | 3 +- 6 files changed, 25 insertions(+), 66 deletions(-) create mode 100644 .changeset/mighty-pandas-speak.md diff --git a/.changeset/mighty-pandas-speak.md b/.changeset/mighty-pandas-speak.md new file mode 100644 index 00000000..36e78adf --- /dev/null +++ b/.changeset/mighty-pandas-speak.md @@ -0,0 +1,5 @@ +--- +"@tbdex/http-client": minor +--- + +Stricten, tested, and remove untested code diff --git a/packages/http-client/src/client.ts b/packages/http-client/src/client.ts index e00dcfff..e171cddd 100644 --- a/packages/http-client/src/client.ts +++ b/packages/http-client/src/client.ts @@ -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, @@ -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 @@ -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 @@ -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 } /** @@ -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! } } diff --git a/packages/http-client/src/errors/request-error.ts b/packages/http-client/src/errors/request-error.ts index 09afb800..e6d30692 100644 --- a/packages/http-client/src/errors/request-error.ts +++ b/packages/http-client/src/errors/request-error.ts @@ -1,7 +1,7 @@ export type RequestErrorParams = { message: string recipientDid: string - url?: string + url: string cause?: unknown } diff --git a/packages/http-client/tests/client.spec.ts b/packages/http-client/tests/client.spec.ts index 02ed1ecb..e3d2eba1 100644 --- a/packages/http-client/tests/client.spec.ts +++ b/packages/http-client/tests/client.spec.ts @@ -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: [] }) diff --git a/packages/http-client/tests/tsconfig.json b/packages/http-client/tests/tsconfig.json index 7c6d2c8e..359d3749 100644 --- a/packages/http-client/tests/tsconfig.json +++ b/packages/http-client/tests/tsconfig.json @@ -1,6 +1,8 @@ { "extends": "../tsconfig.json", "compilerOptions": { + "strict": true, + "useUnknownInCatchVariables": false, "outDir": "compiled", "declarationDir": "compiled/types", "sourceMap": true, diff --git a/packages/http-client/tsconfig.json b/packages/http-client/tsconfig.json index c5c49c56..67194bd9 100644 --- a/packages/http-client/tsconfig.json +++ b/packages/http-client/tsconfig.json @@ -1,6 +1,7 @@ { "compilerOptions": { - "strict": false, + "strict": true, + "useUnknownInCatchVariables": false, "lib": ["DOM", "es2022"], "rootDir": "./",