Skip to content

Commit

Permalink
Add from: support to dwn.records.query/.delete and protocols.configur…
Browse files Browse the repository at this point in the history
…e/.query

Signed-off-by: Frank Hinek <[email protected]>
  • Loading branch information
frankhinek committed May 15, 2023
1 parent bfe248c commit a52ae70
Show file tree
Hide file tree
Showing 7 changed files with 194 additions and 60 deletions.
28 changes: 14 additions & 14 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion packages/web5-user-agent/src/web5-user-agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import {

import {
Cid,
DataStream,
SignatureInput,
RecordsWriteOptions,
PrivateJwk as DwnPrivateKeyJwk,
Expand Down
77 changes: 60 additions & 17 deletions packages/web5/src/dwn-api.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { Web5Agent } from '@tbd54566975/web5-agent';
import type {
MessageReply,
ProtocolDefinition,
ProtocolsConfigureOptions,
ProtocolsQueryOptions,
RecordsDeleteOptions,
Expand All @@ -16,17 +17,35 @@ import { DwnInterfaceName, DwnMethodName, DataStream } from '@tbd54566975/dwn-sd
import { Record } from './record.js';
import { dataToBytes, isDataSizeUnderCacheLimit, isEmptyObject } from './utils.js';

// TODO: Export type ProtocolsConfigureDescriptor from dwn-sdk-js.
export type ProtocolsConfigureDescriptor = {
dateCreated: string;
definition: ProtocolDefinition;
interface : DwnInterfaceName.Protocols;
method: DwnMethodName.Configure;
protocol: string;
};

export type ProtocolsConfigureRequest = {
message: Omit<ProtocolsConfigureOptions, 'authorizationSignatureInput'>;
}

export type ProtocolsConfigureResponse = {
status: MessageReply['status'];
}

export type ProtocolsQueryReplyEntry = {
descriptor: ProtocolsConfigureDescriptor;
};

export type ProtocolsQueryRequest = {
from?: string;
message: Omit<ProtocolsQueryOptions, 'authorizationSignatureInput'>
}

export type RecordsDeleteRequest = {
message: Omit<RecordsDeleteOptions, 'authorizationSignatureInput'>;
export type ProtocolsQueryResponse = {
protocols: ProtocolsQueryReplyEntry[];
status: MessageReply['status'];
}

export type RecordsCreateRequest = RecordsWriteRequest;
Expand All @@ -40,6 +59,11 @@ export type RecordsCreateFromRequest = {
record: Record;
}

export type RecordsDeleteRequest = {
from?: string;
message: Omit<RecordsDeleteOptions, 'authorizationSignatureInput'>;
}

export type RecordsDeleteResponse = {
status: MessageReply['status'];
};
Expand Down Expand Up @@ -100,25 +124,34 @@ export class DwnApi {
/**
* TODO: Document method.
*/
configure: async (request: ProtocolsConfigureRequest) => {
return await this.web5Agent.processDwnRequest({
configure: async (request: ProtocolsConfigureRequest): Promise<ProtocolsConfigureResponse> => {
const agentResponse = await this.web5Agent.processDwnRequest({
target : this.connectedDid,
author : this.connectedDid,
messageOptions : request.message,
messageType : DwnInterfaceName.Protocols + DwnMethodName.Configure
});

const { reply: { status }} = agentResponse;

return { status };
},

/**
* TODO: Document method.
*/
query: async (request: ProtocolsQueryRequest) => {
return await this.web5Agent.processDwnRequest({
query: async (request: ProtocolsQueryRequest): Promise<ProtocolsQueryResponse> => {
const agentResponse = await this.web5Agent.processDwnRequest({
author : this.connectedDid,
messageOptions : request.message,
messageType : DwnInterfaceName.Protocols + DwnMethodName.Query,
target : this.connectedDid
});

const { reply: { entries, status } } = agentResponse;
const protocols = entries as ProtocolsQueryReplyEntry[];

return { protocols, status };
}
};
}
Expand Down Expand Up @@ -177,20 +210,30 @@ export class DwnApi {
* TODO: Document method.
*/
delete: async (request: RecordsDeleteRequest): Promise<RecordsDeleteResponse> => {
const { message: requestMessage } = request;

const messageOptions: Partial<RecordsDeleteOptions> = {
...requestMessage
const agentRequest = {
author : this.connectedDid,
messageOptions : request.message,
messageType : DwnInterfaceName.Records + DwnMethodName.Delete,
target : request.from || this.connectedDid
};

const agentResponse = await this.web5Agent.processDwnRequest({
author : this.connectedDid,
messageOptions,
messageType : DwnInterfaceName.Records + DwnMethodName.Delete,
target : this.connectedDid
});
let agentResponse;

if (request.from) {
agentResponse = await this.web5Agent.sendDwnRequest(agentRequest);
} else {
agentResponse = await this.web5Agent.processDwnRequest(agentRequest);
}

const { reply: { status } } = agentResponse;
//! TODO: (Frank -> Moe): This quirk is the result of how 4XX errors are being returned by `dwn-server`
//! When DWN SDK returns 404, agentResponse is { status: { code: 404 }} and that's it.
//! Need to decide how to resolve.
let status;
if (agentResponse.reply) {
({ reply: { status } } = agentResponse);
} else {
({ status } = agentResponse);
}

return { status };
},
Expand Down
18 changes: 0 additions & 18 deletions packages/web5/tests/fixtures/protocol-definitions/chat.json

This file was deleted.

18 changes: 18 additions & 0 deletions packages/web5/tests/fixtures/protocol-definitions/message.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"types": {
"message": {
"schema": "https://protocols.xyz/message/schema/message",
"dataFormats": ["text/plain"]
}
},
"structure": {
"message": {
"$actions": [
{
"who": "anyone",
"can": "write"
}
]
}
}
}
Loading

0 comments on commit a52ae70

Please sign in to comment.