Skip to content

Commit

Permalink
Overload type signature for processMessage (#554)
Browse files Browse the repository at this point in the history
* Overload type signature for processMessage

* Remove unnecessary typecasting

* Lint

* PR comments
  • Loading branch information
Diane Huxley authored Oct 12, 2023
1 parent 72c2130 commit 19d4648
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 22 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ Here's to a thrilling Hacktoberfest voyage with us! 🎉
# Decentralized Web Node (DWN) SDK <!-- omit in toc -->

Code Coverage
![Statements](https://img.shields.io/badge/statements-98.37%25-brightgreen.svg?style=flat) ![Branches](https://img.shields.io/badge/branches-95.11%25-brightgreen.svg?style=flat) ![Functions](https://img.shields.io/badge/functions-95.7%25-brightgreen.svg?style=flat) ![Lines](https://img.shields.io/badge/lines-98.37%25-brightgreen.svg?style=flat)
![Statements](https://img.shields.io/badge/statements-98.38%25-brightgreen.svg?style=flat) ![Branches](https://img.shields.io/badge/branches-95.12%25-brightgreen.svg?style=flat) ![Functions](https://img.shields.io/badge/functions-95.7%25-brightgreen.svg?style=flat) ![Lines](https://img.shields.io/badge/lines-98.38%25-brightgreen.svg?style=flat)

- [Introduction](#introduction)
- [Installation](#installation)
Expand Down
17 changes: 15 additions & 2 deletions src/dwn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ import type { MethodHandler } from './types/method-handler.js';
import type { Readable } from 'readable-stream';
import type { RecordsWriteHandlerOptions } from './handlers/records-write.js';
import type { TenantGate } from './core/tenant-gate.js';
import type { EventsGetMessage, EventsGetReply, PermissionsGrantMessage, PermissionsRequestMessage, PermissionsRevokeMessage, ProtocolsConfigureMessage, ProtocolsQueryMessage, ProtocolsQueryReply } from './index.js';
import type { GenericMessageReply, UnionMessageReply } from './core/message-reply.js';
import type { MessagesGetMessage, MessagesGetReply } from './types/messages-types.js';
import type { RecordsQueryMessage, RecordsQueryReply, RecordsReadMessage, RecordsReadReply, RecordsWriteMessage, RecordsWriteReply } from './types/records-types.js';
import type { RecordsDeleteMessage, RecordsQueryMessage, RecordsQueryReply, RecordsReadMessage, RecordsReadReply, RecordsWriteMessage, RecordsWriteReply } from './types/records-types.js';

import { AllowAllTenantGate } from './core/tenant-gate.js';
import { DidResolver } from './did/did-resolver.js';
Expand Down Expand Up @@ -90,7 +91,19 @@ export class Dwn {
* Processes the given DWN message and returns with a reply.
* @param tenant The tenant DID to route the given message to.
*/
public async processMessage(tenant: string, rawMessage: any, dataStream?: Readable): Promise<UnionMessageReply> {
public async processMessage(tenant: string, rawMessage: EventsGetMessage): Promise<EventsGetReply>;
public async processMessage(tenant: string, rawMessage: MessagesGetMessage): Promise<MessagesGetReply>;
public async processMessage(tenant: string, rawMessage: ProtocolsConfigureMessage): Promise<GenericMessageReply>;
public async processMessage(tenant: string, rawMessage: ProtocolsQueryMessage): Promise<ProtocolsQueryReply>;
public async processMessage(tenant: string, rawMessage: PermissionsRequestMessage): Promise<GenericMessageReply>;
public async processMessage(tenant: string, rawMessage: PermissionsGrantMessage): Promise<GenericMessageReply>;
public async processMessage(tenant: string, rawMessage: PermissionsRevokeMessage): Promise<GenericMessageReply>;
public async processMessage(tenant: string, rawMessage: RecordsDeleteMessage): Promise<GenericMessageReply>;
public async processMessage(tenant: string, rawMessage: RecordsQueryMessage): Promise<RecordsQueryReply>;
public async processMessage(tenant: string, rawMessage: RecordsReadMessage): Promise<RecordsReadReply>;
public async processMessage(tenant: string, rawMessage: RecordsWriteMessage, dataStream?: Readable): Promise<GenericMessageReply>;
public async processMessage(tenant: string, rawMessage: unknown, dataStream?: Readable): Promise<UnionMessageReply>;
public async processMessage(tenant: string, rawMessage: GenericMessage, dataStream?: Readable): Promise<UnionMessageReply> {
const errorMessageReply = await this.validateTenant(tenant) ?? await this.validateMessageIntegrity(rawMessage);
if (errorMessageReply !== undefined) {
return errorMessageReply;
Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export type { EventsGetMessage, EventsGetReply } from './types/event-types.js';
export type { Filter, GenericMessage, MessageSort, Pagination } from './types/message-types.js';
export type { MessagesGetMessage, MessagesGetReply } from './types/messages-types.js';
export type { PermissionConditions, PermissionScope, PermissionsGrantDescriptor, PermissionsGrantMessage, PermissionsRequestDescriptor, PermissionsRequestMessage, PermissionsRevokeDescriptor, PermissionsRevokeMessage } from './types/permissions-types.js';
export type { ProtocolsConfigureDescriptor, ProtocolDefinition, ProtocolTypes, ProtocolRuleSet, ProtocolsQueryFilter, ProtocolsConfigureMessage, ProtocolsQueryMessage } from './types/protocols-types.js';
export type { ProtocolsConfigureDescriptor, ProtocolDefinition, ProtocolTypes, ProtocolRuleSet, ProtocolsQueryFilter, ProtocolsConfigureMessage, ProtocolsQueryMessage, ProtocolsQueryReply } from './types/protocols-types.js';
export type { EncryptionProperty, RecordsDeleteMessage, RecordsQueryMessage, RecordsQueryReply, RecordsQueryReplyEntry, RecordsReadReply, RecordsWriteDescriptor, RecordsWriteMessage } from './types/records-types.js';
export { authenticate } from './core/auth.js';
export { AllowAllTenantGate, TenantGate } from './core/tenant-gate.js';
Expand Down
4 changes: 0 additions & 4 deletions tests/handlers/events-get.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,6 @@ export function testEventsGetHandler(): void {
const reply = await dwn.processMessage(bob.did, message);

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

it('returns a 400 if message is invalid', async () => {
Expand All @@ -69,8 +67,6 @@ export function testEventsGetHandler(): void {
const reply = await dwn.processMessage(alice.did, message);

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

it('returns all events for a tenant if watermark is not provided', async () => {
Expand Down
4 changes: 0 additions & 4 deletions tests/handlers/messages-get.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,6 @@ export function testMessagesGetHandler(): void {
const reply = await dwn.processMessage(bob.did, message);

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

it('returns a 400 if message is invalid', async () => {
Expand All @@ -80,8 +78,6 @@ export function testMessagesGetHandler(): void {
const reply = await dwn.processMessage(alice.did, message);

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

it('returns a 400 if message contains an invalid message cid', async () => {
Expand Down
14 changes: 7 additions & 7 deletions tests/handlers/records-query.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { DataStore, EventLog, MessageStore } from '../../src/index.js';
import type { GenericMessage, RecordsWriteMessage } from '../../src/index.js';
import type { RecordsQueryReply, RecordsQueryReplyEntry, RecordsWriteDescriptor } from '../../src/types/records-types.js';
import type { RecordsQueryReplyEntry, RecordsWriteDescriptor } from '../../src/types/records-types.js';

import chaiAsPromised from 'chai-as-promised';
import sinon from 'sinon';
Expand Down Expand Up @@ -77,7 +77,7 @@ export function testRecordsQueryHandler(): void {
expect(writeReply.status.code).to.equal(202);

const query = await TestDataGenerator.generateRecordsQuery({ author: alice, filter: { dataFormat } });
const reply = await dwn.processMessage(alice.did, query.message) as RecordsQueryReply;
const reply = await dwn.processMessage(alice.did, query.message);

expect(reply.entries?.length).to.equal(1);
const entry = reply.entries![0];
Expand Down Expand Up @@ -764,7 +764,7 @@ export function testRecordsQueryHandler(): void {
});

const sortedMessages = await MessageStoreLevel.sortMessages(messages, { dateCreated: SortOrder.Ascending });
let results = await dwn.processMessage(alice.did, aliceQueryMessageDataPage1.message) as RecordsQueryReply;
let results = await dwn.processMessage(alice.did, aliceQueryMessageDataPage1.message) ;
expect(results.status.code).to.equal(200);
expect(results.entries?.length).to.equal(10, 'alice page 1');
const page1PaginationLastMessage = await Message.getCid(sortedMessages.at(9)!); // get messageCid from message with authorization.
Expand All @@ -777,7 +777,7 @@ export function testRecordsQueryHandler(): void {
dateSort : DateSort.CreatedAscending,
pagination : { limit: 10, messageCid: results.paginationMessageCid },
});
results = await dwn.processMessage(alice.did, aliceQueryMessageDataPage2.message) as RecordsQueryReply;
results = await dwn.processMessage(alice.did, aliceQueryMessageDataPage2.message) ;
expect(results.status.code).to.equal(200);
expect(results.entries?.length).to.equal(10, 'alice page 2');
const page2PaginationLastMessage = await Message.getCid(sortedMessages.at(19)!); // get messageCid from message with authorization.
Expand All @@ -790,7 +790,7 @@ export function testRecordsQueryHandler(): void {
dateSort : DateSort.CreatedAscending,
pagination : { limit: 10, messageCid: results.paginationMessageCid },
});
results = await dwn.processMessage(alice.did, aliceQueryMessageDataPage3.message) as RecordsQueryReply;
results = await dwn.processMessage(alice.did, aliceQueryMessageDataPage3.message) ;
expect(results.status.code).to.equal(200);
expect(results.entries?.length).to.equal(5, 'alice page 3');
expect(results.paginationMessageCid).to.not.exist;
Expand All @@ -811,7 +811,7 @@ export function testRecordsQueryHandler(): void {
dateSort : DateSort.CreatedAscending,
pagination : { limit: 10 },
});
results = await dwn.processMessage(alice.did, bobQueryMessagePage1.message) as RecordsQueryReply;
results = await dwn.processMessage(alice.did, bobQueryMessagePage1.message) ;
expect(results.status.code).to.equal(200);
expect(results.entries?.length).to.equal(10, 'bob page 1');
const page1BobPaginationLastMessage = await Message.getCid(bobSorted.at(9)!);
Expand All @@ -824,7 +824,7 @@ export function testRecordsQueryHandler(): void {
dateSort : DateSort.CreatedAscending,
pagination : { limit: 10, messageCid: results.paginationMessageCid },
});
results = await dwn.processMessage(alice.did, bobQueryMessagePage2.message) as RecordsQueryReply;
results = await dwn.processMessage(alice.did, bobQueryMessagePage2.message) ;
expect(results.status.code).to.equal(200);
expect(results.entries?.length).to.equal(10, 'bob page 2');
expect(results.paginationMessageCid).to.not.exist;
Expand Down
6 changes: 3 additions & 3 deletions tests/handlers/records-read.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { DerivedPrivateJwk } from '../../src/utils/hd-key.js';
import type { EncryptionInput } from '../../src/interfaces/records-write.js';
import type { DataStore, EventLog, MessageStore, ProtocolDefinition, ProtocolsConfigureMessage, RecordsReadReply } from '../../src/index.js';
import type { DataStore, EventLog, MessageStore, ProtocolDefinition, ProtocolsConfigureMessage } from '../../src/index.js';

import { DwnConstant, Message } from '../../src/index.js';
import { DwnInterfaceName, DwnMethodName } from '../../src/index.js';
Expand Down Expand Up @@ -666,7 +666,7 @@ export function testRecordsReadHandler(): void {
contextId : threadRecord.message.contextId
},
});
const participantReadReply = await dwn.processMessage(alice.did, participantRead.message) as RecordsReadReply;
const participantReadReply = await dwn.processMessage(alice.did, participantRead.message);
expect(participantReadReply.status.code).to.equal(200);

// Bob is able to read the thread root record
Expand All @@ -677,7 +677,7 @@ export function testRecordsReadHandler(): void {
},
protocolRole: 'thread/participant'
});
const threadReadReply = await dwn.processMessage(alice.did, threadRead.message) as RecordsReadReply;
const threadReadReply = await dwn.processMessage(alice.did, threadRead.message);
expect(threadReadReply.status.code).to.equal(200);

// Bob invokes his 'participant' role to read the chat message
Expand Down

0 comments on commit 19d4648

Please sign in to comment.