Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
revolunet committed Dec 12, 2024
1 parent e7b3f25 commit 796aea9
Show file tree
Hide file tree
Showing 5 changed files with 402 additions and 244 deletions.
5 changes: 5 additions & 0 deletions next.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ const moduleExports = {
basePath: process.env.NEXT_PUBLIC_BASE_PATH,
pageExtensions: ["js", "jsx", "mdx", "ts", "tsx"],
reactStrictMode: true,
experimental: {
serverActions: {
bodySizeLimit: "10mb",
},
},
//output: "export",
webpack: (config) => {
config.module.rules.push({
Expand Down
78 changes: 65 additions & 13 deletions src/pages/api/albert/[[...path]].ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { Readable } from "stream";
import type { NextApiRequest, NextApiResponse } from "next";

type ResponseData = {
Expand Down Expand Up @@ -26,26 +27,77 @@ export default async function handler(
// data.query.path.join("/")
// }`
//);
const albertJson = await fetch(

console.log("io");

const fetchOptions = {
method: req.method,
headers: {
Authorization: `Bearer ${ALBERT_API_KEY}`,
} as Record<string, string>,
body: (req.method === "POST" && req.body) || undefined,
};
if (req.headers["content-type"]) {
fetchOptions.headers["Content-Type"] = req.headers["content-type"];
}
const body =
req.method === "GET"
? undefined
: fetchOptions.headers["Content-Type"] === "application/json"
? JSON.stringify(req.body)
: req.body;
fetchOptions.body = body;
// const formData = new FormData();
// formData.append("file", req.bod, fileName);
// formData.append("request", JSON.stringify({ collection: collectionId }));
console.log("fetchOptions", fetchOptions);

const albertApi = await fetch(
`${API_URL}/${
data.query.path &&
Array.isArray(data.query.path) &&
data.query.path.join("/")
}`,
{
method: req.method,
headers: {
Authorization: `Bearer ${ALBERT_API_KEY}`,
"Content-Type": "application/json",
},
body: (req.method === "POST" && JSON.stringify(req.body)) || undefined,
fetchOptions
);

// const resBlob = await response.blob();
// const resBufferArray = await resBlob.arrayBuffer();
// const resBuffer = Buffer.from(resBufferArray);

// const fileType = await fileTypeFromBuffer(resBuffer);
// res.setHeader("Content-Type", fileType?.mime ?? "application/octet-stream");
// res.setHeader("Content-Length", resBuffer.length);
// res.write(resBuffer, "binary");
// res.end();

// omitting handler code for readability

const reader = albertApi.body && albertApi.body.getReader();
while (reader && true) {
const result = await reader.read();
if (result.done) {
res.end();
return;
}
).then((r) => {
console.log(r);
return r.json();
});
res.write(result.value);
}
///const readableStream = albertApi.body as unknown as NodeJS.ReadableStream;
//readableStream.pipe(res);
// const body = await albertApi.text();
// console.log("body", body);
// res
// .status(200)
// //.setHeader("content-type", albertApi.headers["content-type"])
// .send(body);

// if (albertApi.body) {
// Readable.fromWeb(albertApi.body).pipe(res);
// }

res.status(200).json(albertJson);
//albertApi.body?.pipeTo(res);
//res.status(200).pipe(albertApi);
//.json(await albertApi.json());
// .then((data) => {
// res.status(200).json({ message: "Hello from Next.js!", ...data });
// });
Expand Down
139 changes: 0 additions & 139 deletions src/pages/collection.tsx

This file was deleted.

Loading

0 comments on commit 796aea9

Please sign in to comment.