generated from TBD54566975/tbd-project-template
-
Notifications
You must be signed in to change notification settings - Fork 57
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a helper method for generating a PaginationCursor (#513)
Currently the only way to get a pagination cursor is from a query response that has more than the number of records you've queried for. However there could be some cases where the user wants to keep track of the last record they have retrieved and retrieve subsequent records. Added some helper methods to `agent` in order to build/return the data needed, and a convenience method to the `Record` class in `api` that utilizes them.
- Loading branch information
1 parent
ca7f53b
commit 82fe049
Showing
7 changed files
with
207 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
--- | ||
"@web5/identity-agent": patch | ||
"@web5/proxy-agent": patch | ||
"@web5/user-agent": patch | ||
"@web5/agent": patch | ||
"@web5/api": patch | ||
--- | ||
|
||
Add a helper methods for generating a PaginationCursor from `api` without importing `dwn-sdk-js` directly |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
import { expect } from 'chai'; | ||
|
||
import { DateSort, Message, TestDataGenerator } from '@tbd54566975/dwn-sdk-js'; | ||
import { getPaginationCursor, getRecordAuthor, getRecordMessageCid } from '../src/utils.js'; | ||
|
||
describe('Utils', () => { | ||
describe('getPaginationCursor', () => { | ||
it('should return a PaginationCursor object', async () => { | ||
// create a RecordWriteMessage object which is published | ||
const { message } = await TestDataGenerator.generateRecordsWrite({ | ||
published: true, | ||
}); | ||
|
||
const messageCid = await Message.getCid(message); | ||
|
||
// Published Ascending DateSort will get the datePublished as the cursor value | ||
const datePublishedAscendingCursor = await getPaginationCursor(message, DateSort.PublishedAscending); | ||
expect(datePublishedAscendingCursor).to.deep.equal({ | ||
value: message.descriptor.datePublished, | ||
messageCid, | ||
}); | ||
|
||
// Published Descending DateSort will get the datePublished as the cursor value | ||
const datePublishedDescendingCursor = await getPaginationCursor(message, DateSort.PublishedDescending); | ||
expect(datePublishedDescendingCursor).to.deep.equal({ | ||
value: message.descriptor.datePublished, | ||
messageCid, | ||
}); | ||
|
||
// Created Ascending DateSort will get the dateCreated as the cursor value | ||
const dateCreatedAscendingCursor = await getPaginationCursor(message, DateSort.CreatedAscending); | ||
expect(dateCreatedAscendingCursor).to.deep.equal({ | ||
value: message.descriptor.dateCreated, | ||
messageCid, | ||
}); | ||
|
||
// Created Descending DateSort will get the dateCreated as the cursor value | ||
const dateCreatedDescendingCursor = await getPaginationCursor(message, DateSort.CreatedDescending); | ||
expect(dateCreatedDescendingCursor).to.deep.equal({ | ||
value: message.descriptor.dateCreated, | ||
messageCid, | ||
}); | ||
}); | ||
|
||
it('should fail for DateSort with PublishedAscending or PublishedDescending if the record is not published', async () => { | ||
// create a RecordWriteMessage object which is not published | ||
const { message } = await TestDataGenerator.generateRecordsWrite(); | ||
|
||
// Published Ascending DateSort will get the datePublished as the cursor value | ||
try { | ||
await getPaginationCursor(message, DateSort.PublishedAscending); | ||
expect.fail('Expected getPaginationCursor to throw an error'); | ||
} catch(error: any) { | ||
expect(error.message).to.include('The dateCreated or datePublished property is missing from the record descriptor.'); | ||
} | ||
}); | ||
}); | ||
|
||
describe('getRecordMessageCid', () => { | ||
it('should get the CID of a RecordsWriteMessage', async () => { | ||
// create a RecordWriteMessage object | ||
const { message } = await TestDataGenerator.generateRecordsWrite(); | ||
const messageCid = await Message.getCid(message); | ||
|
||
const messageCidFromFunction = await getRecordMessageCid(message); | ||
expect(messageCidFromFunction).to.equal(messageCid); | ||
}); | ||
}); | ||
|
||
describe('getRecordAuthor', () => { | ||
it('should get the author of a RecordsWriteMessage', async () => { | ||
// create a RecordWriteMessage object | ||
const { message, author } = await TestDataGenerator.generateRecordsWrite(); | ||
|
||
const authorFromFunction = getRecordAuthor(message); | ||
expect(authorFromFunction).to.not.be.undefined; | ||
expect(authorFromFunction!).to.equal(author.did); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.