Skip to content

Instantly share code, notes, and snippets.

@Teyik0
Created April 4, 2025 12:36
Show Gist options
  • Select an option

  • Save Teyik0/2ff0f24b3973052cc6b95e778f20497d to your computer and use it in GitHub Desktop.

Select an option

Save Teyik0/2ff0f24b3973052cc6b95e778f20497d to your computer and use it in GitHub Desktop.
elysia-nested-formdata 1.2.25
import { treaty } from "@elysiajs/eden";
import { describe, expect, it, test } from "bun:test";
import { Elysia, t } from "elysia";
const app = new Elysia().post(
"/",
({ body }) => {
console.log("received body ->", body);
return { ...body, images: body.images.map((img) => img.name) };
},
{
body: t.Object({
name: t.String(),
variants: t.Optional(
t.ArrayString(
t.Object({
price: t.Number({ minimum: 0 }),
weight: t.Number({ minimum: 0 }),
attributeValueIds: t.Array(t.String({ format: "uuid" })),
}),
),
),
images: t.Files({ type: "image/webp" }),
}),
},
);
const api = treaty(app);
const filePath1 = `${import.meta.dir}/public/cumin.webp`;
const filePath2 = `${import.meta.dir}/public/garlic.webp`;
const testProduct = {
name: "Test Product",
variants: [
{
price: 10,
weight: 100,
attributeValueIds: [
"3f50c9b6-8d6b-4e5e-9c3d-2f4b5a6e7d8f",
"7a1d2e3f-4b5c-6d7e-8f9a-0b1c2d3e4f5a",
],
},
],
images: [Bun.file(filePath1), Bun.file(filePath2)],
};
describe("Product POST/", () => {
it("should create a product using treaty", async () => {
const { data, status, error } = await api.index.post({
name: testProduct.name,
variants: JSON.stringify(testProduct.variants) as unknown as {
price: number;
weight: number;
attributeValueIds: string[];
}[],
images: testProduct.images as unknown as FileList,
});
console.log("err", error?.value);
expect(status).toBe(200);
expect(data?.name).toEqual(testProduct.name);
expect(data?.name).toEqual(testProduct.name);
expect(data?.variants).toEqual(testProduct.variants);
expect(data?.images).toEqual(
testProduct.images.map((img) => img.name) as string[],
);
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment