From 01f4e82777656dae9fd5c648f0c53d54b9c13807 Mon Sep 17 00:00:00 2001 From: Oliver Terbu <> Date: Thu, 8 Jul 2021 11:53:35 +0200 Subject: [PATCH] style: added doc --- packages/did-comm/src/didcomm.ts | 11 +++- .../did-comm/src/transports/transports.ts | 65 ++++++++++++++++++- 2 files changed, 70 insertions(+), 6 deletions(-) diff --git a/packages/did-comm/src/didcomm.ts b/packages/did-comm/src/didcomm.ts index e03a9658d8..4ca84b4297 100644 --- a/packages/did-comm/src/didcomm.ts +++ b/packages/did-comm/src/didcomm.ts @@ -126,6 +126,10 @@ export class DIDComm implements IAgentPlugin { readonly methods: IDIDComm readonly schema = schema.IDIDComm + /** + * Constructor that takes a list of {@link IDIDCommTransport} objects. + * @param transports A list of {@link IDIDCommTransport} objects. + */ constructor(transports: IDIDCommTransport[]) { this.transports = transports this.methods = { @@ -474,8 +478,9 @@ export class DIDComm implements IAgentPlugin { const services = didDoc.service?.filter( (service: any) => - service.type == 'DIDCommMessaging' - // && service.accept?.includes('didcomm/v2'), + service.type == 'DIDCommMessaging' + // FIXME: TODO: only send the message if the service section either explicitly supports + // `didcomm/v2`, or no `accept` property is present. ) if (!services || services.length == 0) { throw new Error( @@ -583,4 +588,4 @@ export class DIDComm implements IAgentPlugin { return Promise.reject(new Error('No service endpoint')) } } -} +} \ No newline at end of file diff --git a/packages/did-comm/src/transports/transports.ts b/packages/did-comm/src/transports/transports.ts index 8f9b5feabd..b96812f7ee 100644 --- a/packages/did-comm/src/transports/transports.ts +++ b/packages/did-comm/src/transports/transports.ts @@ -1,40 +1,99 @@ import 'cross-fetch/polyfill' import { v4 as uuidv4 } from 'uuid' +/** + * Result interface for sending DIDComm messages through + * {@link IDIDCommTransport.send}. + * @beta + */ export interface IDIDCommTransportResult { result?: string error?: string } +/** + * Common interface for transports that can be used in the + * {@link DIDComm} module. + * @beta + */ export interface IDIDCommTransport { + /** + * Identifier of this transport that can be used in case the + * message thread supports reusing the transport connection. + */ id: string + /** + * Returns `true` if this transport is suitable for the provided + * DID Document service section, otherwise `false`. + * @param service The DID Document service section + * @beta + */ isServiceSupported(service: any) : boolean + + /** + * Sends the provided raw message (without further processing) to + * the service endpoint defined in the provided DID Document service + * section. + * + * @param service The DID Document service section that contains + * a `serviceEndpoint` entry. + * @param message The message to be sent. + * @beta + */ send(service: any, message: string) : Promise } +/** + * Abstract implementation of {@link IDIDCommTransport}. + * @beta + */ export abstract class AbstractDIDCommTransport implements IDIDCommTransport { id: string + /** + * Shared constructor that takes an optional identifier (for reusing) for + * this {@link IDIDCommTransport}. + * @param id An optional identifier for this {@link IDIDCommTransport}. + * @beta + */ constructor(id?: string) { this.id = id || uuidv4() } + /** {@inheritdoc IDIDCommTransport.isServiceSupported} */ abstract isServiceSupported(service: any) : boolean + + /** {@inheritdoc IDIDCommTransport.send} */ abstract send(service: any, message: string) : Promise } +/** + * Implementation of {@link IDIDCommTransport} to provide a simple + * transport based on HTTP(S) requests. + * @beta + */ export class DIDCommHttpTransport extends AbstractDIDCommTransport { + /** + * Defines the default HTTP method to use if not specified + * in the DID Document service entry of the recipient. + */ httpMethod: 'post' | 'get' + /** + * Creates a new {@link DIDCommHttpTransport}. + * @param httpMethod Default HTTP method if not specified in the service + * section. + */ constructor(httpMethod?: 'post' | 'get') { super() this.httpMethod = httpMethod || 'post' } + /** {@inheritdoc AbstractDIDCommTransport.isServiceSupported} */ isServiceSupported(service: any) { // FIXME: TODO: addtionally handle serviceEndpoint objects in did docs return ( @@ -42,6 +101,7 @@ export class DIDCommHttpTransport extends AbstractDIDCommTransport { (service.serviceEndpoint.startsWith('http://') || service.serviceEndpoint.startsWith('https://'))) } + /** {@inheritdoc AbstractDIDCommTransport.send} */ async send(service: any, message: string) : Promise { try { const response = await fetch(service.serviceEndpoint, { @@ -52,7 +112,7 @@ export class DIDCommHttpTransport extends AbstractDIDCommTransport { let result if (response.ok) { result = { - result: 'successfully send message: ' + response.statusText + result: 'successfully sent message: ' + response.statusText } } else { result = { @@ -66,5 +126,4 @@ export class DIDCommHttpTransport extends AbstractDIDCommTransport { } } } -} - +} \ No newline at end of file