Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat/add metadata kvs #38

Merged
merged 2 commits into from
Oct 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions src/core/files/list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,17 @@ export const listFiles = async (
const params = new URLSearchParams();

if (options) {
const { name, group, cid, order, limit, mimeType, pageToken, cidPending } =
options;
const {
name,
group,
cid,
order,
limit,
mimeType,
pageToken,
cidPending,
metadata,
} = options;

if (limit) params.append("limit", limit.toString());
if (name) params.append("name", name);
Expand All @@ -93,6 +102,11 @@ export const listFiles = async (
if (order) params.append("order", order);
if (pageToken) params.append("pageToken", pageToken);
if (cidPending) params.append("cidPending", "true");
if (metadata && typeof metadata === "object") {
Object.entries(metadata).forEach(([key, value]) => {
params.append(`metadata[${key}]`, value.toString());
});
}
}

let endpoint: string = "https://api.pinata.cloud/v3";
Expand Down
25 changes: 21 additions & 4 deletions src/core/files/updateFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,26 @@ export const updateFile = async (
if (!config) {
throw new ValidationError("Pinata configuration is missing");
}
const data = JSON.stringify({
name: options.name,
});

if (
!options.name &&
(!options.keyvalues || Object.keys(options.keyvalues).length === 0)
) {
throw new ValidationError(
"At least one of 'name' or 'keyvalues' must be provided",
);
}

const data: Record<string, any> = {};

if (options.name !== undefined) {
data.name = options.name;
}
if (options.keyvalues && Object.keys(options.keyvalues).length > 0) {
data.keyvalues = options.keyvalues;
}

const body = JSON.stringify(data);

let headers: Record<string, string>;

Expand All @@ -75,7 +92,7 @@ export const updateFile = async (
const request = await fetch(`${endpoint}/files/${options.id}`, {
method: "PUT",
headers: headers,
body: data,
body: body,
});

if (!request.ok) {
Expand Down
5 changes: 5 additions & 0 deletions src/core/pinataSDK.ts
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,11 @@ class FilterFiles {
return this;
}

metadata(keyvalues: Record<string, string>): FilterFiles {
this.query.metadata = keyvalues;
return this;
}

pageToken(pageToken: string): FilterFiles {
this.query.pageToken = pageToken;
return this;
Expand Down
3 changes: 3 additions & 0 deletions src/core/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ export type PinataMetadata = {
export type UpdateFileOptions = {
id: string;
name?: string;
keyvalues?: Record<string, string>;
};

export type UploadOptions = {
Expand All @@ -63,6 +64,7 @@ export type FileListItem = {
size: number;
number_of_files: number;
mime_type: string;
keyvalues: Record<string, string>;
group_id: string | null;
created_at: string;
};
Expand All @@ -78,6 +80,7 @@ export type FileListQuery = {
mimeType?: string;
cid?: string;
cidPending?: boolean;
metadata?: Record<string, string>;
order?: "ASC" | "DESC";
limit?: number;
pageToken?: string;
Expand Down
4 changes: 3 additions & 1 deletion tests/files/list.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ describe("listFiles function", () => {
size: 1234,
number_of_files: 1,
mime_type: "text/plain",
keyvalues: {},
group_id: "test-group",
created_at: "2023-07-26T12:00:00Z",
};
Expand Down Expand Up @@ -74,6 +75,7 @@ describe("listFiles function", () => {
mimeType: "text/plain",
cid: "Qm...",
order: "ASC",
metadata: { key1: "value1", key2: "value2" },
};

global.fetch = jest.fn().mockResolvedValueOnce({
Expand All @@ -87,7 +89,7 @@ describe("listFiles function", () => {

expect(global.fetch).toHaveBeenCalledWith(
expect.stringContaining(
"limit=10&name=test-name&group=test-group&cid=Qm...&mimeType=text%2Fplain&order=ASC&pageToken=test-token&cidPending=true",
"limit=10&name=test-name&group=test-group&cid=Qm...&mimeType=text%2Fplain&order=ASC&pageToken=test-token&cidPending=true&metadata%5Bkey1%5D=value1&metadata%5Bkey2%5D=value2",
),
expect.any(Object),
);
Expand Down
3 changes: 3 additions & 0 deletions tests/files/updateFile.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ describe("updateFile function", () => {
mime_type: "text/plain",
group_id: "groupId",
created_at: "2023-01-01T00:00:00Z",
keyvalues: {},
};

global.fetch = jest.fn().mockResolvedValueOnce({
Expand Down Expand Up @@ -119,6 +120,7 @@ describe("updateFile function", () => {
mime_type: "text/plain",
group_id: "groupId",
created_at: "2023-01-01T00:00:00Z",
keyvalues: {},
};

global.fetch = jest.fn().mockResolvedValueOnce({
Expand Down Expand Up @@ -153,6 +155,7 @@ describe("updateFile function", () => {
mime_type: "text/plain",
group_id: "groupId",
created_at: "2023-01-01T00:00:00Z",
keyvalues: {},
};

global.fetch = jest.fn().mockResolvedValueOnce({
Expand Down