diff --git a/packages/http-client/.c8rc.json b/packages/http-client/.c8rc.json index ab680f66..814843c4 100644 --- a/packages/http-client/.c8rc.json +++ b/packages/http-client/.c8rc.json @@ -14,6 +14,7 @@ ], "reporter": [ "cobertura", + "html", "text" ] } \ No newline at end of file diff --git a/packages/http-client/src/client.ts b/packages/http-client/src/client.ts index 259913d3..579bd57f 100644 --- a/packages/http-client/src/client.ts +++ b/packages/http-client/src/client.ts @@ -148,7 +148,7 @@ export class TbdexHttpClient { throw new RequestError({ message: `Failed to get offerings from ${pfiDid}`, recipientDid: pfiDid, url: apiRoute, cause: e }) } - const data: Offering[] = [] + const offerings: Offering[] = [] if (!response.ok) { const errorDetails = await response.json() as ErrorDetail[] @@ -159,10 +159,10 @@ export class TbdexHttpClient { const jsonOfferings = responseBody.data as any[] for (let jsonOffering of jsonOfferings) { const offering = await Offering.parse(jsonOffering) - data.push(offering) + offerings.push(offering) } - return data + return offerings } /** @@ -221,7 +221,7 @@ export class TbdexHttpClient { throw new RequestError({ message: `Failed to get exchange from ${pfiDid}`, recipientDid: pfiDid, url: apiRoute, cause: e }) } - const data: Message[] = [] + const messages: Message[] = [] if (!response.ok) { const errorDetails = await response.json() as ErrorDetail[] @@ -231,17 +231,17 @@ export class TbdexHttpClient { const responseBody = await response.json() as { data: MessageModel[] } for (let jsonMessage of responseBody.data) { const message = await Parser.parseMessage(jsonMessage) - data.push(message) + messages.push(message) } - return data + return messages } // TODO: Wrap Message[] in Exchange object and verify each message /** * returns all exchanges created by requester - * @param _opts - options + * @param opts - options */ static async getExchanges(opts: GetExchangesOptions): Promise { const { pfiDid, filter, did } = opts @@ -427,8 +427,11 @@ export type GetExchangeOptions = { export type GetExchangesOptions = { /** the DID of the PFI from whom you want to get offerings */ pfiDid: string + /** the message author's DID */ did: BearerDid, + /** the filter to select the desired exchanges */ filter?: { + /** ID or IDs of exchanges to get */ id: string | string[] } } \ No newline at end of file diff --git a/packages/http-client/src/errors/request-error.ts b/packages/http-client/src/errors/request-error.ts index f0a7599d..026dfade 100644 --- a/packages/http-client/src/errors/request-error.ts +++ b/packages/http-client/src/errors/request-error.ts @@ -25,7 +25,5 @@ export class RequestError extends Error { this.name = this.constructor.name this.recipientDid = params.recipientDid this.url = params.url - - Object.setPrototypeOf(this, RequestError.prototype) } } \ No newline at end of file diff --git a/packages/http-client/src/errors/request-token-error.ts b/packages/http-client/src/errors/request-token-error.ts index 6a5e5f55..1d0ecd28 100644 --- a/packages/http-client/src/errors/request-token-error.ts +++ b/packages/http-client/src/errors/request-token-error.ts @@ -18,8 +18,6 @@ export class RequestTokenError extends Error { super(params.message, { cause: params.cause }) this.name = this.constructor.name - - Object.setPrototypeOf(this, RequestTokenError.prototype) } } @@ -27,58 +25,28 @@ export class RequestTokenError extends Error { * Error thrown when a request token cannot be signed * @beta */ -export class RequestTokenSigningError extends RequestTokenError { - constructor(params: RequestTokenErrorParams) { - super(params) - - Object.setPrototypeOf(this, RequestTokenSigningError.prototype) - } -} +export class RequestTokenSigningError extends RequestTokenError { } /** * Error thrown when a request token cannot be verified * @beta */ -export class RequestTokenVerificationError extends RequestTokenError { - constructor(params: RequestTokenErrorParams) { - super(params) - - Object.setPrototypeOf(this, RequestTokenVerificationError.prototype) - } -} +export class RequestTokenVerificationError extends RequestTokenError { } /** * Error thrown when a request token is missing required claims * @beta */ -export class RequestTokenMissingClaimsError extends RequestTokenError { - constructor(params: RequestTokenErrorParams) { - super(params) - - Object.setPrototypeOf(this, RequestTokenMissingClaimsError.prototype) - } -} +export class RequestTokenMissingClaimsError extends RequestTokenError { } /** * Error thrown when a request token aud does not match the PFI did for which its intended * @beta */ -export class RequestTokenAudienceMismatchError extends RequestTokenError { - constructor(params: RequestTokenErrorParams) { - super(params) - - Object.setPrototypeOf(this, RequestTokenAudienceMismatchError.prototype) - } -} +export class RequestTokenAudienceMismatchError extends RequestTokenError { } /** * Error thrown when a request token payload iss does not match request token header kid * @beta */ -export class RequestTokenIssuerSignerMismatchError extends RequestTokenError { - constructor(params: RequestTokenErrorParams) { - super(params) - - Object.setPrototypeOf(this, RequestTokenIssuerSignerMismatchError.prototype) - } -} \ No newline at end of file +export class RequestTokenIssuerSignerMismatchError extends RequestTokenError { } \ No newline at end of file diff --git a/packages/http-client/src/errors/response-error.ts b/packages/http-client/src/errors/response-error.ts index 43e18cc6..da61d11a 100644 --- a/packages/http-client/src/errors/response-error.ts +++ b/packages/http-client/src/errors/response-error.ts @@ -37,7 +37,5 @@ export class ResponseError extends Error { this.details = params.details this.recipientDid = params.recipientDid this.url = params.url - - Object.setPrototypeOf(this, ResponseError.prototype) } } \ No newline at end of file diff --git a/packages/http-client/src/errors/validation-error.ts b/packages/http-client/src/errors/validation-error.ts index 45da5c05..2ede3014 100644 --- a/packages/http-client/src/errors/validation-error.ts +++ b/packages/http-client/src/errors/validation-error.ts @@ -11,8 +11,6 @@ export class ValidationError extends Error { super(message) this.name = this.constructor.name - - Object.setPrototypeOf(this, ValidationError.prototype) } } @@ -20,22 +18,10 @@ export class ValidationError extends Error { * Error thrown when a DID is invalid * @beta */ -export class InvalidDidError extends ValidationError { - constructor(message: string) { - super(message) - - Object.setPrototypeOf(this, InvalidDidError.prototype) - } -} +export class InvalidDidError extends ValidationError { } /** * Error thrown when a PFI's service endpoint can't be found * @beta */ -export class MissingServiceEndpointError extends ValidationError { - constructor(message: string) { - super(message) - - Object.setPrototypeOf(this, MissingServiceEndpointError.prototype) - } -} \ No newline at end of file +export class MissingServiceEndpointError extends ValidationError { } \ No newline at end of file diff --git a/packages/http-client/tests/client.spec.ts b/packages/http-client/tests/client.spec.ts index 42c509db..c3bade8e 100644 --- a/packages/http-client/tests/client.spec.ts +++ b/packages/http-client/tests/client.spec.ts @@ -326,13 +326,17 @@ describe('client', () => { }) it('returns offerings array if response is ok', async () => { + const pfiDid = await DidJwk.create() + const stubbedOffering = DevTools.createOffering({ from: pfiDid.uri }) + await stubbedOffering.sign(pfiDid) + const stubbedOfferings = [stubbedOffering.toJSON()] fetchStub.resolves({ ok : true, - json : () => Promise.resolve({ data: [] }) + json : () => Promise.resolve({ data: stubbedOfferings }) } as Response) const offerings = await TbdexHttpClient.getOfferings({ pfiDid: pfiDid.uri }) - expect(offerings).to.have.length(0) + expect(offerings).to.have.length(1) }) }) @@ -426,13 +430,17 @@ describe('client', () => { }) it('returns exchange array if response is ok', async () => { + const alice = await DidJwk.create() + const stubbedRfq = await DevTools.createRfq({ sender: alice }) + await stubbedRfq.sign(alice) + const messages = [stubbedRfq.toJSON()] fetchStub.resolves({ ok : true, - json : () => Promise.resolve({ data: [] }) + json : () => Promise.resolve({ data: messages }) } as Response) const exchanges = await TbdexHttpClient.getExchange({ pfiDid: pfiDid.uri, exchangeId: '123', did: pfiDid }) - expect(exchanges).to.have.length(0) + expect(exchanges).to.have.length(1) }) }) @@ -475,14 +483,29 @@ describe('client', () => { } }) - it('returns empty exchanges array if response is ok and body is empty array', async () => { + + it('returns array of message exchanges given a list of exchange IDs', async () => { + const alice = await DidJwk.create() + const aliceRfq1 = await DevTools.createRfq({ sender: alice }) + await aliceRfq1.sign(alice) + + const aliceRfq2 = await DevTools.createRfq({ sender: alice }) + await aliceRfq2.sign(alice) + fetchStub.resolves({ ok : true, - json : () => Promise.resolve({ data: [] }) + json : () => Promise.resolve({ data: [ + [aliceRfq1.toJSON()], + [aliceRfq2.toJSON()] + ] }) } as Response) - const exchanges = await TbdexHttpClient.getExchanges({ pfiDid: pfiDid.uri, did: pfiDid }) - expect(exchanges).to.have.length(0) + const exchanges = await TbdexHttpClient.getExchanges({ + pfiDid : pfiDid.uri, + did : alice, + filter : { id: [aliceRfq1.metadata.id, aliceRfq2.metadata.id]} + }) + expect(exchanges).to.have.length(2) }) }) @@ -571,7 +594,7 @@ describe('client', () => { aud : pfiBearerDid.uri, iss : aliceBearerDid.uri, exp : Math.floor(Date.now() / 1000 + 60), - jti : 'randomnonce' + jti : 'random-nonce' } }) diff --git a/packages/http-client/tests/error.spec.ts b/packages/http-client/tests/error.spec.ts new file mode 100644 index 00000000..b1163000 --- /dev/null +++ b/packages/http-client/tests/error.spec.ts @@ -0,0 +1,16 @@ +import { expect } from 'chai' +import { + RequestTokenMissingClaimsError, + RequestTokenSigningError, + RequestTokenError +} from '../src/main.js' + +describe('Custom error class', () => { + it('has expected inheritance prototype chain', async () => { + const error = new RequestTokenSigningError({ message: 'any message' }) + expect(error).to.be.instanceOf(Error) + expect(error).to.be.instanceOf(RequestTokenError) + expect(error).to.be.instanceOf(RequestTokenSigningError) + expect(error).to.not.be.instanceOf(RequestTokenMissingClaimsError) + }) +}) \ No newline at end of file diff --git a/packages/http-server/.c8rc.json b/packages/http-server/.c8rc.json index ab680f66..814843c4 100644 --- a/packages/http-server/.c8rc.json +++ b/packages/http-server/.c8rc.json @@ -14,6 +14,7 @@ ], "reporter": [ "cobertura", + "html", "text" ] } \ No newline at end of file diff --git a/packages/http-server/src/callback-error.ts b/packages/http-server/src/callback-error.ts index c9030778..68ffe45a 100644 --- a/packages/http-server/src/callback-error.ts +++ b/packages/http-server/src/callback-error.ts @@ -22,7 +22,5 @@ export class CallbackError extends Error { if (Error.captureStackTrace) { Error.captureStackTrace(this, this.constructor) } - - Object.setPrototypeOf(this, CallbackError.prototype) } } \ No newline at end of file diff --git a/packages/protocol/.c8rc.json b/packages/protocol/.c8rc.json index 697c3b67..e4a96115 100644 --- a/packages/protocol/.c8rc.json +++ b/packages/protocol/.c8rc.json @@ -13,6 +13,7 @@ ], "reporter": [ "cobertura", + "html", "text" ] } \ No newline at end of file