Skip to content

Commit

Permalink
feat(js): add list shallow option
Browse files Browse the repository at this point in the history
  • Loading branch information
Nuhvi committed Aug 7, 2024
1 parent e10cf77 commit 5eb5440
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 10 deletions.
68 changes: 68 additions & 0 deletions pubky/pkg/test/public.js
Original file line number Diff line number Diff line change
Expand Up @@ -243,3 +243,71 @@ test("list", async (t) => {
);
}
})

test('list shallow', async (t) => {
const client = PubkyClient.testnet();

const keypair = Keypair.random()
const publicKey = keypair.publicKey()
const pubky = publicKey.z32()

const homeserver = PublicKey.from('8pinxxgqs41n4aididenw5apqp1urfmzdztr8jt4abrkdn435ewo')
await client.signup(keypair, homeserver)



let urls = [
`pubky://${pubky}/pub/a.com/a.txt`,
`pubky://${pubky}/pub/example.com/a.txt`,
`pubky://${pubky}/pub/example.com/b.txt`,
`pubky://${pubky}/pub/example.com/c.txt`,
`pubky://${pubky}/pub/example.com/d.txt`,
`pubky://${pubky}/pub/example.con/d.txt`,
`pubky://${pubky}/pub/example.con`,
`pubky://${pubky}/pub/file`,
`pubky://${pubky}/pub/file2`,
`pubky://${pubky}/pub/z.com/a.txt`,
]

for (let url of urls) {
await client.put(url, Buffer.from(""));
}

let url = `pubky://${pubky}/pub/`;

{
let list = await client.list(url, null, false, null, true);

t.deepEqual(
list,
[
`pubky://${pubky}/pub/a.com/`,
`pubky://${pubky}/pub/example.com/`,
`pubky://${pubky}/pub/example.con`,
`pubky://${pubky}/pub/example.con/`,
`pubky://${pubky}/pub/file`,
`pubky://${pubky}/pub/file2`,
`pubky://${pubky}/pub/z.com/`,
],
"normal list shallow"
);
}

{
let list = await client.list(url, null, true, null, true);

t.deepEqual(
list,
[
`pubky://${pubky}/pub/z.com/`,
`pubky://${pubky}/pub/file2`,
`pubky://${pubky}/pub/file`,
`pubky://${pubky}/pub/example.con/`,
`pubky://${pubky}/pub/example.con`,
`pubky://${pubky}/pub/example.com/`,
`pubky://${pubky}/pub/a.com/`,
],
"normal list shallow"
);
}
})
8 changes: 4 additions & 4 deletions pubky/src/shared/list_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ impl<'a> ListBuilder<'a> {
}

/// Set the `reverse` option.
pub fn reverse(mut self) -> Self {
self.reverse = true;
pub fn reverse(mut self, reverse: bool) -> Self {
self.reverse = reverse;
self
}

Expand All @@ -46,8 +46,8 @@ impl<'a> ListBuilder<'a> {
self
}

pub fn shallow(mut self) -> Self {
self.shallow = true;
pub fn shallow(mut self, shallow: bool) -> Self {
self.shallow = shallow;
self
}

Expand Down
12 changes: 6 additions & 6 deletions pubky/src/shared/public.rs
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,7 @@ mod tests {
let list = client
.list(url.as_str())
.unwrap()
.reverse()
.reverse(true)
.send()
.await
.unwrap();
Expand All @@ -340,7 +340,7 @@ mod tests {
let list = client
.list(url.as_str())
.unwrap()
.reverse()
.reverse(true)
.limit(2)
.send()
.await
Expand All @@ -360,7 +360,7 @@ mod tests {
let list = client
.list(url.as_str())
.unwrap()
.reverse()
.reverse(true)
.limit(2)
.cursor("d.txt")
.send()
Expand Down Expand Up @@ -412,7 +412,7 @@ mod tests {
let list = client
.list(url.as_str())
.unwrap()
.shallow()
.shallow(true)
.send()
.await
.unwrap();
Expand All @@ -436,8 +436,8 @@ mod tests {
let list = client
.list(url.as_str())
.unwrap()
.shallow()
.reverse()
.shallow(true)
.reverse(true)
.send()
.await
.unwrap();
Expand Down
3 changes: 3 additions & 0 deletions pubky/src/wasm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@ impl PubkyClient {
cursor: Option<String>,
reverse: Option<bool>,
limit: Option<u16>,
shallow: Option<bool>,
) -> Result<Array, JsValue> {
// TODO: try later to return Vec<String> from async function.

Expand All @@ -174,6 +175,7 @@ impl PubkyClient {
.reverse(reverse.unwrap_or(false))
.limit(limit.unwrap_or(u16::MAX))
.cursor(&cursor)
.shallow(shallow.unwrap_or(false))
.send()
.await
.map(|urls| {
Expand All @@ -191,6 +193,7 @@ impl PubkyClient {
self.inner_list(url)?
.reverse(reverse.unwrap_or(false))
.limit(limit.unwrap_or(u16::MAX))
.shallow(shallow.unwrap_or(false))
.send()
.await
.map(|urls| {
Expand Down

0 comments on commit 5eb5440

Please sign in to comment.