Initial commit
This commit is contained in:
6
packages/media/src/entities/errors/common.ts
Normal file
6
packages/media/src/entities/errors/common.ts
Normal file
@@ -0,0 +1,6 @@
|
||||
export class InputParseError extends Error {
|
||||
constructor(message: string, options?: ErrorOptions) {
|
||||
super(message, options);
|
||||
this.name = "InputParseError";
|
||||
}
|
||||
}
|
||||
20
packages/media/src/entities/errors/media.test.ts
Normal file
20
packages/media/src/entities/errors/media.test.ts
Normal file
@@ -0,0 +1,20 @@
|
||||
import { describe, it, expect } from "vitest";
|
||||
import { MediaNotFoundError } from "@/entities/errors/media";
|
||||
|
||||
describe("MediaNotFoundError", () => {
|
||||
it("is an instance of Error", () => {
|
||||
const err = new MediaNotFoundError();
|
||||
expect(err).toBeInstanceOf(Error);
|
||||
expect(err).toBeInstanceOf(MediaNotFoundError);
|
||||
});
|
||||
|
||||
it("has default message", () => {
|
||||
const err = new MediaNotFoundError();
|
||||
expect(err.message).toBe("Media not found");
|
||||
});
|
||||
|
||||
it("accepts a custom message", () => {
|
||||
const err = new MediaNotFoundError("Custom message");
|
||||
expect(err.message).toBe("Custom message");
|
||||
});
|
||||
});
|
||||
6
packages/media/src/entities/errors/media.ts
Normal file
6
packages/media/src/entities/errors/media.ts
Normal file
@@ -0,0 +1,6 @@
|
||||
export class MediaNotFoundError extends Error {
|
||||
constructor(message = "Media not found", options?: ErrorOptions) {
|
||||
super(message, options);
|
||||
this.name = "MediaNotFoundError";
|
||||
}
|
||||
}
|
||||
51
packages/media/src/entities/models/media.test.ts
Normal file
51
packages/media/src/entities/models/media.test.ts
Normal file
@@ -0,0 +1,51 @@
|
||||
import { describe, it, expect } from "vitest";
|
||||
import { mediaSchema } from "@/entities/models/media";
|
||||
|
||||
describe("mediaSchema", () => {
|
||||
it("parses a valid media object", () => {
|
||||
const result = mediaSchema.parse({
|
||||
id: "1",
|
||||
alt: "A photo",
|
||||
url: "https://cdn.example.com/photo.png",
|
||||
filename: "photo.png",
|
||||
mimeType: "image/png",
|
||||
filesize: 1024,
|
||||
});
|
||||
expect(result.id).toBe("1");
|
||||
expect(result.alt).toBe("A photo");
|
||||
});
|
||||
|
||||
it("parses a media object with optional width and height", () => {
|
||||
const result = mediaSchema.parse({
|
||||
id: "2",
|
||||
alt: "A photo",
|
||||
url: "https://cdn.example.com/photo.png",
|
||||
filename: "photo.png",
|
||||
mimeType: "image/png",
|
||||
filesize: 2048,
|
||||
width: 1920,
|
||||
height: 1080,
|
||||
});
|
||||
expect(result.width).toBe(1920);
|
||||
expect(result.height).toBe(1080);
|
||||
});
|
||||
|
||||
it("parses without width and height (optional)", () => {
|
||||
const result = mediaSchema.parse({
|
||||
id: "3",
|
||||
alt: "Doc",
|
||||
url: "https://cdn.example.com/doc.pdf",
|
||||
filename: "doc.pdf",
|
||||
mimeType: "application/pdf",
|
||||
filesize: 512,
|
||||
});
|
||||
expect(result.width).toBeUndefined();
|
||||
expect(result.height).toBeUndefined();
|
||||
});
|
||||
|
||||
it("throws for missing required fields", () => {
|
||||
expect(() =>
|
||||
mediaSchema.parse({ id: "1", alt: "x", url: "x" }),
|
||||
).toThrow();
|
||||
});
|
||||
});
|
||||
14
packages/media/src/entities/models/media.ts
Normal file
14
packages/media/src/entities/models/media.ts
Normal file
@@ -0,0 +1,14 @@
|
||||
import { z } from "zod";
|
||||
|
||||
export const mediaSchema = z.object({
|
||||
id: z.string(),
|
||||
alt: z.string(),
|
||||
url: z.string(),
|
||||
filename: z.string(),
|
||||
mimeType: z.string(),
|
||||
filesize: z.number(),
|
||||
width: z.number().optional(),
|
||||
height: z.number().optional(),
|
||||
});
|
||||
|
||||
export type Media = z.infer<typeof mediaSchema>;
|
||||
Reference in New Issue
Block a user