Skip to content

Commit

Permalink
Align UnionMessageReply with the various replies that `processMessa…
Browse files Browse the repository at this point in the history
…ge` (#648)

* align processMessage union reply with the actual replies

* update union reply comments

* lint and circular deps

* export entries types

* protocols configure entries

* RecordsReadReply replaced with declaritive structure
  • Loading branch information
LiranCohen authored Dec 30, 2023
1 parent 8b81c97 commit 8d5cac4
Show file tree
Hide file tree
Showing 24 changed files with 192 additions and 192 deletions.
32 changes: 16 additions & 16 deletions src/core/message-reply.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,8 @@
import type { QueryResultEntry } from '../types/message-types.js';
import type { MessagesGetReplyEntry } from '../types/messages-types.js';
import type { ProtocolsConfigureMessage } from '../types/protocols-types.js';
import type { Readable } from 'readable-stream';

type Status = {
code: number
detail: string
};

export type GenericMessageReply = {
status: Status;
};
import type { RecordsWriteMessage } from '../types/records-types.js';
import type { GenericMessageReply, QueryResultEntry } from '../types/message-types.js';

export function messageReplyFromError(e: unknown, code: number): GenericMessageReply {

Expand All @@ -23,20 +17,26 @@ export function messageReplyFromError(e: unknown, code: number): GenericMessageR
export type UnionMessageReply = GenericMessageReply & {
/**
* Resulting message entries or events returned from the invocation of the corresponding message.
* e.g. the resulting messages from a RecordsQuery
* Mutually exclusive with `data`.
* e.g. the resulting messages from a RecordsQuery, or array of messageCid strings for EventsGet or EventsQuery
* Mutually exclusive with `record`.
*/
entries?: QueryResultEntry[];
entries?: QueryResultEntry[] | ProtocolsConfigureMessage[] | MessagesGetReplyEntry[] | string[];

/**
* Data corresponding to the message received if applicable (e.g. RecordsRead).
* Record corresponding to the message received if applicable (e.g. RecordsRead).
* Mutually exclusive with `entries` and `cursor`.
*/
data?: Readable;
record?: RecordsWriteMessage & {
/**
* The initial write of the record if the returned RecordsWrite message itself is not the initial write.
*/
initialWrite?: RecordsWriteMessage;
data: Readable;
};

/**
* A cursor for pagination if applicable (e.g. RecordsQuery).
* Mutually exclusive with `data`.
* Mutually exclusive with `record`.
*/
cursor?: string;
};
4 changes: 2 additions & 2 deletions src/dwn.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import type { DataStore } from './types/data-store.js';
import type { EventLog } from './types/event-log.js';
import type { GenericMessage } from './types/message-types.js';
import type { MessageStore } from './types/message-store.js';
import type { MethodHandler } from './types/method-handler.js';
import type { Readable } from 'readable-stream';
import type { TenantGate } from './core/tenant-gate.js';
import type { UnionMessageReply } from './core/message-reply.js';
import type { EventsGetMessage, EventsGetReply, EventsQueryMessage, EventsQueryReply } from './types/event-types.js';
import type { GenericMessageReply, UnionMessageReply } from './core/message-reply.js';
import type { GenericMessage, GenericMessageReply } from './types/message-types.js';
import type { MessagesGetMessage, MessagesGetReply } from './types/messages-types.js';
import type { PermissionsGrantMessage, PermissionsRequestMessage, PermissionsRevokeMessage } from './types/permissions-types.js';
import type { ProtocolsConfigureMessage, ProtocolsQueryMessage, ProtocolsQueryReply } from './types/protocols-types.js';
Expand Down
4 changes: 2 additions & 2 deletions src/handlers/events-get.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ export class EventsGetHandler implements MethodHandler {
const events = await this.eventLog.getEvents(tenant, options);

return {
status: { code: 200, detail: 'OK' },
events
status : { code: 200, detail: 'OK' },
entries : events
};
}
}
4 changes: 2 additions & 2 deletions src/handlers/events-query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ export class EventsQueryHandler implements MethodHandler {
const events = await this.eventLog.queryEvents(tenant, logFilters, cursor);

return {
status: { code: 200, detail: 'OK' },
events
status : { code: 200, detail: 'OK' },
entries : events
};
}
}
4 changes: 2 additions & 2 deletions src/handlers/messages-get.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,8 @@ export class MessagesGetHandler implements MethodHandler {
}

return {
status: { code: 200, detail: 'OK' },
messages
status : { code: 200, detail: 'OK' },
entries : messages
};
}
}
2 changes: 1 addition & 1 deletion src/handlers/permissions-grant.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { DidResolver } from '../did/did-resolver.js';
import type { EventLog } from '../types//event-log.js';
import type { GenericMessageReply } from '../core/message-reply.js';
import type { GenericMessageReply } from '../types/message-types.js';
import type { KeyValues } from '../types/query-types.js';
import type { MessageStore } from '../types//message-store.js';
import type { MethodHandler } from '../types/method-handler.js';
Expand Down
2 changes: 1 addition & 1 deletion src/handlers/permissions-request.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { DidResolver } from '../did/did-resolver.js';
import type { EventLog } from '../types//event-log.js';
import type { GenericMessageReply } from '../core/message-reply.js';
import type { GenericMessageReply } from '../types/message-types.js';
import type { MessageStore } from '../types//message-store.js';
import type { MethodHandler } from '../types/method-handler.js';
import type { PermissionsRequestMessage } from '../types/permissions-types.js';
Expand Down
2 changes: 1 addition & 1 deletion src/handlers/permissions-revoke.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { DidResolver } from '../did/did-resolver.js';
import type { EventLog } from '../types/event-log.js';
import type { GenericMessageReply } from '../core/message-reply.js';
import type { GenericMessageReply } from '../types/message-types.js';
import type { KeyValues } from '../types/query-types.js';
import type { MessageStore } from '../types/message-store.js';
import type { MethodHandler } from '../types/method-handler.js';
Expand Down
2 changes: 1 addition & 1 deletion src/handlers/protocols-configure.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { DataStore } from '../types/data-store.js';
import type { DidResolver } from '../did/did-resolver.js';
import type { EventLog } from '../types/event-log.js';
import type { GenericMessageReply } from '../core/message-reply.js';
import type { GenericMessageReply } from '../types/message-types.js';
import type { MessageStore } from '../types//message-store.js';
import type { MethodHandler } from '../types/method-handler.js';
import type { ProtocolsConfigureMessage } from '../types/protocols-types.js';
Expand Down
2 changes: 1 addition & 1 deletion src/handlers/records-delete.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { DataStore } from '../types/data-store.js';
import type { DidResolver } from '../did/did-resolver.js';
import type { EventLog } from '../types/event-log.js';
import type { GenericMessageReply } from '../core/message-reply.js';
import type { GenericMessageReply } from '../types/message-types.js';
import type { KeyValues } from '../types/query-types.js';
import type { MessageStore } from '../types//message-store.js';
import type { MethodHandler } from '../types/method-handler.js';
Expand Down
2 changes: 1 addition & 1 deletion src/handlers/records-write.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { DataStore } from '../types/data-store.js';
import type { DidResolver } from '../did/did-resolver.js';
import type { EventLog } from '../types/event-log.js';
import type { GenericMessageReply } from '../core/message-reply.js';
import type { GenericMessageReply } from '../types/message-types.js';
import type { MessageStore } from '../types//message-store.js';
import type { MethodHandler } from '../types/method-handler.js';
import type { RecordsQueryReplyEntry, RecordsWriteMessage } from '../types/records-types.js';
Expand Down
4 changes: 2 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ export type { DidMethodResolver, DwnServiceEndpoint, ServiceEndpoint, DidDocumen
export type { EventLog, GetEventsOptions } from './types/event-log.js';
export type { EventsGetMessage, EventsGetReply, EventsQueryMessage, EventsQueryReply } from './types/event-types.js';
export type { Filter } from './types/query-types.js';
export type { GenericMessage, MessageSort, Pagination } from './types/message-types.js';
export type { MessagesGetMessage, MessagesGetReply } from './types/messages-types.js';
export type { GenericMessage, GenericMessageReply, MessageSort, Pagination, QueryResultEntry } from './types/message-types.js';
export type { MessagesGetMessage, MessagesGetReply, MessagesGetReplyEntry } from './types/messages-types.js';
export type { PermissionConditions, PermissionScope, PermissionsGrantDescriptor } from './types/permissions-grant-descriptor.js';
export type { PermissionsGrantMessage, PermissionsRequestDescriptor, PermissionsRequestMessage, PermissionsRevokeDescriptor, PermissionsRevokeMessage } from './types/permissions-types.js';
export type { ProtocolsConfigureDescriptor, ProtocolDefinition, ProtocolTypes, ProtocolRuleSet, ProtocolsQueryFilter, ProtocolsConfigureMessage, ProtocolsQueryMessage, ProtocolsQueryReply } from './types/protocols-types.js';
Expand Down
7 changes: 3 additions & 4 deletions src/types/event-types.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import type { GenericMessageReply } from '../core/message-reply.js';
import type { ProtocolsQueryFilter } from './protocols-types.js';
import type { AuthorizationModel, GenericMessage } from './message-types.js';
import type { AuthorizationModel, GenericMessage, GenericMessageReply } from './message-types.js';
import type { DwnInterfaceName, DwnMethodName } from '../enums/dwn-interface-method.js';
import type { RangeCriterion, RangeFilter } from './query-types.js';

Expand Down Expand Up @@ -39,7 +38,7 @@ export type EventsGetMessage = GenericMessage & {
};

export type EventsGetReply = GenericMessageReply & {
events?: string[];
entries?: string[];
};

export type EventsQueryDescriptor = {
Expand All @@ -56,5 +55,5 @@ export type EventsQueryMessage = GenericMessage & {
};

export type EventsQueryReply = GenericMessageReply & {
events?: string[];
entries?: string[];
};
7 changes: 7 additions & 0 deletions src/types/message-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,14 @@ export type Pagination = {
limit?: number;
};

type Status = {
code: number
detail: string
};

export type GenericMessageReply = {
status: Status;
};

export type MessageSort = {
dateCreated?: SortDirection;
Expand Down
7 changes: 3 additions & 4 deletions src/types/messages-types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import type { GenericMessageReply } from '../core/message-reply.js';
import type { AuthorizationModel, GenericMessage } from './message-types.js';
import type { AuthorizationModel, GenericMessage, GenericMessageReply } from './message-types.js';
import type { DwnInterfaceName, DwnMethodName } from '../enums/dwn-interface-method.js';

export type MessagesGetDescriptor = {
Expand All @@ -22,5 +21,5 @@ export type MessagesGetReplyEntry = {
};

export type MessagesGetReply = GenericMessageReply & {
messages?: MessagesGetReplyEntry[];
};
entries?: MessagesGetReplyEntry[];
};
3 changes: 1 addition & 2 deletions src/types/method-handler.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import type { GenericMessage } from './message-types.js';
import type { GenericMessageReply } from '../core/message-reply.js';
import type { Readable } from 'readable-stream';
import type { GenericMessage, GenericMessageReply } from './message-types.js';

/**
* Interface that defines a message handler of a specific method.
Expand Down
3 changes: 1 addition & 2 deletions src/types/protocols-types.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import type { GenericMessageReply } from '../core/message-reply.js';
import type { PublicJwk } from './jose-types.js';
import type { AuthorizationModel, GenericMessage } from './message-types.js';
import type { AuthorizationModel, GenericMessage, GenericMessageReply } from './message-types.js';
import type { DwnInterfaceName, DwnMethodName } from '../enums/dwn-interface-method.js';

export type ProtocolsConfigureDescriptor = {
Expand Down
7 changes: 2 additions & 5 deletions src/types/records-types.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import type { EncryptionAlgorithm } from '../utils/encryption.js';
import type { GeneralJws } from './jws-types.js';
import type { GenericMessageReply } from '../core/message-reply.js';
import type { KeyDerivationScheme } from '../utils/hd-key.js';
import type { PublicJwk } from './jose-types.js';
import type { Readable } from 'readable-stream';
import type { AuthorizationModel, GenericMessage, GenericSignaturePayload, Pagination } from './message-types.js';
import type { AuthorizationModel, GenericMessage, GenericMessageReply, GenericSignaturePayload, Pagination } from './message-types.js';
import type { DwnInterfaceName, DwnMethodName } from '../enums/dwn-interface-method.js';
import type { RangeCriterion, RangeFilter } from './query-types.js';

Expand Down Expand Up @@ -32,8 +31,6 @@ export type RecordsWriteDescriptor = {
dataFormat: string;
};

export type RecordsWriteReply = GenericMessageReply;

/**
* Internal RecordsWrite message representation that can be in an incomplete state.
*/
Expand Down Expand Up @@ -157,7 +154,7 @@ export type RecordsReadReply = GenericMessageReply & {
*/
initialWrite?: RecordsWriteMessage;
data: Readable;
}
};
};

export type RecordsReadDescriptor = {
Expand Down
2 changes: 1 addition & 1 deletion tests/core/message-reply.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { GenericMessageReply } from '../../src/core/message-reply.js';
import type { GenericMessageReply } from '../../src/types/message-types.js';

import { expect } from 'chai';
import { messageReplyFromError } from '../../src/core/message-reply.js';
Expand Down
2 changes: 1 addition & 1 deletion tests/dwn.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ export function testDwnClass(): void {
const reply: EventsGetReply = await dwn.processMessage(alice.did, message);

expect(reply.status.code).to.equal(200);
expect(reply.events).to.be.empty;
expect(reply.entries).to.be.empty;
expect((reply as any).data).to.not.exist;
});

Expand Down
18 changes: 9 additions & 9 deletions tests/handlers/events-get.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ export function testEventsGetHandler(): void {
const reply = await eventsGetHandler.handle({ tenant: bob.did, message });

expect(reply.status.code).to.equal(401);
expect(reply.events).to.not.exist;
expect(reply.entries).to.not.exist;
});

it('returns a 400 if message is invalid', async () => {
Expand All @@ -70,7 +70,7 @@ export function testEventsGetHandler(): void {
const reply = await eventsGetHandler.handle({ tenant: alice.did, message });

expect(reply.status.code).to.equal(400);
expect(reply.events).to.not.exist;
expect(reply.entries).to.not.exist;
});

it('returns all events for a tenant if cursor is not provided', async () => {
Expand All @@ -92,10 +92,10 @@ export function testEventsGetHandler(): void {

expect(reply.status.code).to.equal(200);
expect((reply as any).data).to.not.exist;
expect(reply.events?.length).to.equal(expectedCids.length);
expect(reply.entries?.length).to.equal(expectedCids.length);

for (let i = 0; i < reply.events!.length; i += 1) {
expect(reply.events![i]).to.equal(expectedCids[i]);
for (let i = 0; i < reply.entries!.length; i += 1) {
expect(reply.entries![i]).to.equal(expectedCids[i]);
}
});

Expand All @@ -114,7 +114,7 @@ export function testEventsGetHandler(): void {

expect(reply.status.code).to.equal(200);

const cursor = reply.events![reply.events!.length - 1];
const cursor = reply.entries![reply.entries!.length - 1];
const expectedCids: string[] = [];

for (let i = 0; i < 3; i += 1) {
Expand All @@ -131,10 +131,10 @@ export function testEventsGetHandler(): void {

expect(reply.status.code).to.equal(200);
expect((reply as any).data).to.not.exist;
expect(reply.events!.length).to.equal(expectedCids.length);
expect(reply.entries!.length).to.equal(expectedCids.length);

for (let i = 0; i < reply.events!.length; i += 1) {
expect(reply.events![i]).to.equal(expectedCids[i]);
for (let i = 0; i < reply.entries!.length; i += 1) {
expect(reply.entries![i]).to.equal(expectedCids[i]);
}
});
});
Expand Down
8 changes: 4 additions & 4 deletions tests/handlers/events-query.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ export function testEventsQueryHandler(): void {
const reply = await eventsQueryHandler.handle({ tenant: bob.did, message });

expect(reply.status.code).to.equal(401);
expect(reply.events).to.not.exist;
expect(reply.entries).to.not.exist;
});

it('returns a 400 if message is invalid', async () => {
Expand All @@ -75,7 +75,7 @@ export function testEventsQueryHandler(): void {
const reply = await eventsQueryHandler.handle({ tenant: alice.did, message });

expect(reply.status.code).to.equal(400);
expect(reply.events).to.not.exist;
expect(reply.entries).to.not.exist;
});

it('returns 400 if no filters are provided', async () => {
Expand All @@ -90,7 +90,7 @@ export function testEventsQueryHandler(): void {
const reply = await eventsQueryHandler.handle({ tenant: alice.did, message });

expect(reply.status.code).to.equal(400);
expect(reply.events).to.not.exist;
expect(reply.entries).to.not.exist;
});

it('returns 400 if an empty filter without properties is provided', async () => {
Expand All @@ -105,7 +105,7 @@ export function testEventsQueryHandler(): void {
const reply = await eventsQueryHandler.handle({ tenant: alice.did, message });

expect(reply.status.code).to.equal(400);
expect(reply.events).to.not.exist;
expect(reply.entries).to.not.exist;
});
});
}
Loading

0 comments on commit 8d5cac4

Please sign in to comment.