Skip to content

Commit 4240668

Browse files
committed
create objects manager list method
1 parent 6e7d89a commit 4240668

File tree

5 files changed

+47
-0
lines changed

5 files changed

+47
-0
lines changed

.changeset/thin-foxes-laugh.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@squarecloud/blob": minor
3+
---
4+
5+
Add `BlobObjectsManager#list` method for listing blobs

src/assertions/list.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { listObjectsResponseSchema } from "../schemas/list";
2+
import type { ListObjectsResponse } from "../types/list";
3+
import { handleAPIObjectAssertion } from "./handlers";
4+
5+
export function assertListObjectsResponse(value: unknown): ListObjectsResponse {
6+
return handleAPIObjectAssertion({
7+
schema: listObjectsResponseSchema,
8+
code: "LIST_OBJECTS",
9+
route: "/list",
10+
value,
11+
});
12+
}

src/managers/objects.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { readFile } from "fs/promises";
22
import type { SquareCloudBlob } from "..";
3+
import { assertListObjectsResponse } from "../assertions/list";
34
import { assertPutObjectResponse } from "../assertions/put";
45
import { putObjectPayloadSchema } from "../schemas/put";
56
import { SquareCloudBlobError } from "../structures/error";
@@ -8,6 +9,21 @@ import type { PutObjectResponse, PutObjectType } from "../types/put";
89
export class BlobObjectsManager {
910
constructor(private readonly client: SquareCloudBlob) {}
1011

12+
/**
13+
* Lists all objects in the storage.
14+
*
15+
* @example
16+
* ```js
17+
* const objects = await blob.objects.list();
18+
* ```
19+
*/
20+
async list() {
21+
const { response } =
22+
await this.client.api.request<PutObjectResponse>("list");
23+
24+
return assertListObjectsResponse(response);
25+
}
26+
1127
/**
1228
* Uploads an object to the storage.
1329
*

src/schemas/list.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { z } from "zod";
2+
3+
export const listObjectSchema = z.object({
4+
name: z.string(),
5+
size: z.number(),
6+
created_at: z.coerce.date(),
7+
expire_at: z.coerce.date().optional(),
8+
});
9+
10+
export const listObjectsResponseSchema = z.array(listObjectSchema);

src/types/list.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import type { z } from "zod";
2+
import type { listObjectsResponseSchema } from "../schemas/list";
3+
4+
export type ListObjectsResponse = z.infer<typeof listObjectsResponseSchema>;

0 commit comments

Comments
 (0)