Skip to content

Commit

Permalink
style: added doc
Browse files Browse the repository at this point in the history
  • Loading branch information
Oliver Terbu committed Jul 8, 2021
1 parent 60480ba commit 01f4e82
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 6 deletions.
11 changes: 8 additions & 3 deletions packages/did-comm/src/didcomm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -583,4 +588,4 @@ export class DIDComm implements IAgentPlugin {
return Promise.reject(new Error('No service endpoint'))
}
}
}
}
65 changes: 62 additions & 3 deletions packages/did-comm/src/transports/transports.ts
Original file line number Diff line number Diff line change
@@ -1,47 +1,107 @@
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<IDIDCommTransportResult>
}

/**
* 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<IDIDCommTransportResult>
}

/**
* 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 (
typeof service.serviceEndpoint === 'string' &&
(service.serviceEndpoint.startsWith('http://') || service.serviceEndpoint.startsWith('https://')))
}

/** {@inheritdoc AbstractDIDCommTransport.send} */
async send(service: any, message: string) : Promise<IDIDCommTransportResult> {
try {
const response = await fetch(service.serviceEndpoint, {
Expand All @@ -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 = {
Expand All @@ -66,5 +126,4 @@ export class DIDCommHttpTransport extends AbstractDIDCommTransport {
}
}
}
}

}

0 comments on commit 01f4e82

Please sign in to comment.