Merge pull request #665 from Hexastack/feat/zod-attachment

feat: zod attachment
This commit is contained in:
Med Marrouchi 2025-02-03 10:52:30 +01:00 committed by GitHub
commit 0c84d64186
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 35 additions and 17 deletions

11
api/package-lock.json generated
View File

@ -57,7 +57,8 @@
"sanitize-filename": "^1.6.3", "sanitize-filename": "^1.6.3",
"slug": "^8.2.2", "slug": "^8.2.2",
"ts-migrate-mongoose": "^3.8.4", "ts-migrate-mongoose": "^3.8.4",
"uuid": "^9.0.1" "uuid": "^9.0.1",
"zod": "^3.24.1"
}, },
"devDependencies": { "devDependencies": {
"@compodoc/compodoc": "^1.1.24", "@compodoc/compodoc": "^1.1.24",
@ -20280,6 +20281,14 @@
"resolved": "https://registry.npmjs.org/zepto/-/zepto-1.2.0.tgz", "resolved": "https://registry.npmjs.org/zepto/-/zepto-1.2.0.tgz",
"integrity": "sha512-C1x6lfvBICFTQIMgbt3JqMOno3VOtkWat/xEakLTOurskYIHPmzJrzd1e8BnmtdDVJlGuk5D+FxyCA8MPmkIyA==", "integrity": "sha512-C1x6lfvBICFTQIMgbt3JqMOno3VOtkWat/xEakLTOurskYIHPmzJrzd1e8BnmtdDVJlGuk5D+FxyCA8MPmkIyA==",
"dev": true "dev": true
},
"node_modules/zod": {
"version": "3.24.1",
"resolved": "https://registry.npmjs.org/zod/-/zod-3.24.1.tgz",
"integrity": "sha512-muH7gBL9sI1nciMZV67X5fTKKBLtwpZ5VBp1vsOQzj1MhrBZ4wlVCm3gedKZWLp0Oyel8sIGfeiz54Su+OVT+A==",
"funding": {
"url": "https://github.com/sponsors/colinhacks"
}
} }
} }
} }

View File

@ -92,7 +92,8 @@
"sanitize-filename": "^1.6.3", "sanitize-filename": "^1.6.3",
"slug": "^8.2.2", "slug": "^8.2.2",
"ts-migrate-mongoose": "^3.8.4", "ts-migrate-mongoose": "^3.8.4",
"uuid": "^9.0.1" "uuid": "^9.0.1",
"zod": "^3.24.1"
}, },
"devDependencies": { "devDependencies": {
"@compodoc/compodoc": "^1.1.24", "@compodoc/compodoc": "^1.1.24",
@ -144,8 +145,8 @@
}, },
"optionalDependencies": { "optionalDependencies": {
"@css-inline/css-inline-linux-arm64-musl": "^0.14.1", "@css-inline/css-inline-linux-arm64-musl": "^0.14.1",
"@resvg/resvg-js-linux-arm64-musl": "^2.6.2", "@resvg/resvg-js-darwin-arm64": "^2.6.2",
"@resvg/resvg-js-darwin-arm64": "^2.6.2" "@resvg/resvg-js-linux-arm64-musl": "^2.6.2"
}, },
"overrides": { "overrides": {
"mjml": "5.0.0-alpha.4" "mjml": "5.0.0-alpha.4"

View File

@ -6,6 +6,8 @@
* 2. All derivative works must include clear attribution to the original creator and software, Hexastack and Hexabot, in a prominent location (e.g., in the software's "About" section, documentation, and README file). * 2. All derivative works must include clear attribution to the original creator and software, Hexastack and Hexabot, in a prominent location (e.g., in the software's "About" section, documentation, and README file).
*/ */
import { z } from 'zod';
export enum FileType { export enum FileType {
image = 'image', image = 'image',
video = 'video', video = 'video',
@ -14,6 +16,8 @@ export enum FileType {
unknown = 'unknown', unknown = 'unknown',
} }
export const fileTypeSchema = z.nativeEnum(FileType);
/** /**
* The `AttachmentRef` type defines two possible ways to reference an attachment: * The `AttachmentRef` type defines two possible ways to reference an attachment:
* 1. By `id`: This is used when the attachment is uploaded and stored in the Hexabot system. * 1. By `id`: This is used when the attachment is uploaded and stored in the Hexabot system.
@ -22,20 +26,24 @@ export enum FileType {
* the content is generated or retrieved by a plugin that consumes a third-party API. * the content is generated or retrieved by a plugin that consumes a third-party API.
* In this case, the `url` field contains the direct link to the external resource. * In this case, the `url` field contains the direct link to the external resource.
*/ */
export type AttachmentRef =
| {
id: string | null;
}
| {
/** @deprecated To be used only for external URLs (plugins), for stored attachments use "id" instead */
url: string;
};
/** IMPORTANT: No need to use generic type here */ export const attachmentRefSchema = z.union([
export interface AttachmentPayload<T extends AttachmentRef = AttachmentRef> { z.object({
type: FileType; id: z.string().nullable(),
payload: T; }),
} z.object({
url: z.string(),
}),
]);
export type AttachmentRef = z.infer<typeof attachmentRefSchema>;
export const attachmentPayloadSchema = z.object({
type: fileTypeSchema,
payload: attachmentRefSchema,
});
export type AttachmentPayload = z.infer<typeof attachmentPayloadSchema>;
/** @deprecated */ /** @deprecated */
export type WithUrl<A> = A & { url?: string }; export type WithUrl<A> = A & { url?: string };