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

Pulling refs/heads/staging into test-staging #2368

Merged
merged 3 commits into from
Mar 18, 2024
Merged
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
79 changes: 33 additions & 46 deletions pages/api/ipfs/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { IOMarketMetadata } from "@zeitgeistpm/sdk";
import { create as createIPFSClient } from "ipfs-http-client";
import type { PageConfig } from "next";
import { NextApiRequest, NextApiResponse } from "next";
import { fromZodError } from "zod-validation-error";
import { IOMarketMetadata } from "./types";

export const config: PageConfig = {
runtime: "nodejs",
Expand All @@ -17,6 +16,8 @@ export default async function handler(
}
}

const MAX_METADATA_SIZE_KB = 10;

const POST = async (req: NextApiRequest, res: NextApiResponse) => {
const node = createIPFSClient({
url: process.env.NEXT_PUBLIC_IPFS_NODE_URL,
Expand All @@ -29,28 +30,44 @@ const POST = async (req: NextApiRequest, res: NextApiResponse) => {
},
});

const parsed = IOMarketMetadata.safeParse(req.body);
const [error, metadata] = IOMarketMetadata.validate(req.body);

const onlyHash = req.query["only-hash"] === "true" ? true : false;

if (!parsed.success) {
return res.status(400).json({
message: fromZodError(parsed.error).toString(),
});
if (error) {
return res
.status(400)
.setHeader("Content-Type", "application/problem+json; charset=utf-8")
.send(
JSON.stringify({
title: "Invalid Market Metadata",
detail: "The market metadata provided is invalid.",
message: "The market metadata provided is invalid.",
context: {
failures: error.failures(),
},
}),
);
}

const metadata = {
__meta: "markets",
...parsed.data,
};

const content = JSON.stringify(metadata);
const kbSize = Buffer.byteLength(content) / 1024;

if (kbSize > 10) {
return res.status(400).json({
message: "Market metadata is too large. Please keep it under 10kb.",
});
if (kbSize > MAX_METADATA_SIZE_KB) {
return res
.status(400)
.setHeader("Content-Type", "application/problem+json; charset=utf-8")
.send(
JSON.stringify({
title: "Invalid Market Metadata",
detail: `Market metadata is too large. Please keep it under ${MAX_METADATA_SIZE_KB}kb.`,
message: `Market metadata is too large. Please keep it under ${MAX_METADATA_SIZE_KB}kb.`,
context: {
maxKb: MAX_METADATA_SIZE_KB,
metadataSizeKb: kbSize,
},
}),
);
}
try {
const { cid } = await node.add(
Expand All @@ -62,10 +79,6 @@ const POST = async (req: NextApiRequest, res: NextApiResponse) => {
},
);

// if (!onlyHash) {
// await node.pin.add(cid);
// }

return res.status(200).json({
message: `Market metadata ${
onlyHash ? "hashed" : "pinned"
Expand All @@ -78,29 +91,3 @@ const POST = async (req: NextApiRequest, res: NextApiResponse) => {
});
}
};

// const DELETE = async (req: NextRequest) => {
// const { cid } = JSON.parse(await extractBody(req));

// try {
// await node.pin.rm(IPFSHTTPClient.CID.parse(cid));

// return new Response(
// JSON.stringify({
// message: `Market metadata(cid: ${cid}) unpinned successfully.`,
// }),
// {
// status: 200,
// },
// );
// } catch (error) {
// return new Response(
// JSON.stringify({
// message: error.message,
// }),
// {
// status: 500,
// },
// );
// }
// };
Loading