Skip to content

Commit

Permalink
Added dwn.records.read() from:
Browse files Browse the repository at this point in the history
Signed-off-by: Frank Hinek <[email protected]>
  • Loading branch information
frankhinek committed May 15, 2023
1 parent b383f85 commit b079f85
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 18 deletions.
39 changes: 25 additions & 14 deletions packages/web5/src/dwn-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import type {
RecordsDeleteOptions,
RecordsQueryOptions,
RecordsReadOptions,
RecordsReadReply,
RecordsWriteDescriptor,
RecordsWriteMessage,
RecordsWriteOptions,
Expand Down Expand Up @@ -56,7 +55,7 @@ export type RecordsQueryReplyEntry = {


export type RecordsQueryRequest = {
/** where the query results will come from */
/** The from property indicates the DID to query from and return results. */
from?: string;
message: Omit<RecordsQueryOptions, 'authorizationSignatureInput'>;
}
Expand All @@ -67,6 +66,8 @@ export type RecordsQueryResponse = {
};

export type RecordsReadRequest = {
/** The from property indicates the DID to read from and return results fro. */
from?: string;
message: Omit<RecordsReadOptions, 'authorizationSignatureInput'>;
}

Expand Down Expand Up @@ -232,21 +233,31 @@ export class DwnApi {
* TODO: Document method.
*/
read: async (request: RecordsReadRequest): Promise<RecordsReadResponse> => {
const { message: requestMessage } = request;

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

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

const { reply } = agentResponse;
const { record: responseRecord, status } = reply as RecordsReadReply;
if (request.from) {
agentResponse = await this.web5Agent.sendDwnRequest(agentRequest);
} else {
agentResponse = await this.web5Agent.processDwnRequest(agentRequest);
}

//! 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 responseRecord;
let status;
if (agentResponse.reply) {
({ reply: { record: responseRecord, status } } = agentResponse);
} else {
({ status } = agentResponse);
}

let record: Record;
if (200 <= status.code && status.code <= 299) {
Expand Down
2 changes: 1 addition & 1 deletion packages/web5/src/web5.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import type { DidState, DidMethodApi, DidResolverCache } from '@tbd54566975/dids
import type { Web5Agent } from '@tbd54566975/web5-agent';

// import { Web5ProxyAgent } from '@tbd54566975/web5-proxy-agent';
import { Web5UserAgent, ProfileApi } from '@tbd54566975/web5-user-agent';
import { DidIonApi, DidKeyApi } from '@tbd54566975/dids';
import { Web5UserAgent, ProfileApi } from '@tbd54566975/web5-user-agent';

import { DwnApi } from './dwn-api.js';
import { DidApi } from './did-api.js';
Expand Down
4 changes: 2 additions & 2 deletions packages/web5/tests/fixtures/test-profiles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,8 @@ export const ionCreateOptions = {
id : 'dwn',
type : 'DecentralizedWebNode',
serviceEndpoint : {
nodes : ['https://dwn.tbddev.org/dwn0'],
messageAttestationKeys : [`#${keyIds.did.service.dwn.attestation}`],
nodes : ['https://dwn.tbddev.org/dwn0'],
messageAuthorizationKeys : [`#${keyIds.did.service.dwn.authorization}`],
}
}]
};
Expand Down
31 changes: 30 additions & 1 deletion packages/web5/tests/web5-dwn.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,27 @@ describe('web5.dwn', () => {
});

describe('from: did', () => {
xit('tests needed');
it('returns undefined record when requested record does not exit', async () => {
// Generate a recordId that will not be present on the did endpoint being read from.
const { record, status } = await dwn.records.write({ data: 'hi' });
expect(status.code).to.equal(202);

// Create a new DID to represent an external entity who has a remote DWN server defined in their DID document.
const ionCreateOptions = await testProfile.ionCreateOptions.services.dwn.authorization.keys();
const { id: bobDid } = await testAgent.didIon.create(ionCreateOptions);

// Attempt to read a record from Bob's DWN using the ID of a record that only exists in the connected agent's DWN.
const result = await dwn.records.read({
from : bobDid,
message : {
recordId: record!.id
}
});

// Confirm that the record does not currently exist on Bob's DWN.
expect(result.status.code).to.equal(404);
expect(result.record).to.be.undefined;
});
});
});

Expand Down Expand Up @@ -224,6 +244,15 @@ describe('web5.dwn', () => {

expect(deleteResult.status.code).to.equal(202);
});

it('returns a 202 if the recordId does not exist', async () => {
let deleteResult = await dwn.records.delete({
message: {
recordId: 'abcd1234'
}
});
expect(deleteResult.status.code).to.equal(202);
});
});

describe('from: did', () => {
Expand Down

0 comments on commit b079f85

Please sign in to comment.