UNPKG

@squarecloud/blob

Version:
161 lines (156 loc) 5.94 kB
var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __hasOwnProp = Object.prototype.hasOwnProperty; var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); var __publicField = (obj, key, value) => { __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value); return value; }; // src/validation/schemas/create.ts var create_exports = {}; __export(create_exports, { createObjectPayloadSchema: () => createObjectPayloadSchema, createObjectResponseSchema: () => createObjectResponseSchema, createObjectSchema: () => createObjectSchema }); module.exports = __toCommonJS(create_exports); var import_zod2 = require("zod"); // src/utils/mimetype/mimetypes.ts var mimeTypesWithExtension = { "video/mp4": ["mp4"], "video/mpeg": ["mpeg"], "video/webm": ["webm"], "video/x-flv": ["flv"], "video/x-m4v": ["m4v"], "image/jpeg": ["jpg", "jpeg"], "image/png": ["png"], "image/apng": ["apng"], "image/tiff": ["tiff"], "image/gif": ["gif"], "image/webp": ["webp"], "image/bmp": ["bmp"], "image/svg+xml": ["svg"], "image/x-icon": ["ico"], "image/ico": ["ico"], "image/cur": ["cur"], "image/heic": ["heic"], "image/heif": ["heif"], "audio/wav": ["wav"], "audio/ogg": ["ogg"], "audio/opus": ["opus"], "audio/mp4": ["mp4"], "audio/mpeg": ["mp3"], "audio/aac": ["aac"], "text/plain": ["txt"], "text/html": ["html"], "text/css": ["css"], "text/csv": ["csv"], "text/x-sql": ["sql"], "application/xml": ["xml"], "application/sql": ["sql"], "application/x-sql": ["sql"], "application/x-sqlite3": ["sqlite3"], "application/x-pkcs12": ["pfx"], "application/pdf": ["pdf"], "application/json": ["json"], "application/javascript": ["js"] }; var mimeTypes = Object.keys(mimeTypesWithExtension); // src/utils/mimetype/index.ts var MimeTypeUtil = class { /** * Returns the corresponding MIME type for a given file extension. * * @param extension - The file extension to search for. * @return The MIME type associated with the extension, or "text/plain" if not found. * * @example * ```js * MimeTypeUtil.fromExtension("jpeg") // "image/jpeg" | Supported * MimeTypeUtil.fromExtension("json") // "application/json" | Supported * MimeTypeUtil.fromExtension("potato") // "text/plain" | Unsupported, defaults to text/plain * ``` */ static fromExtension(extension) { const entries = Object.entries(mimeTypesWithExtension); const mimeType = entries.find( ([, extensions]) => extensions.includes(extension) )?.[0]; return mimeType || "text/plain"; } }; /** Supported mime types with their extensions */ __publicField(MimeTypeUtil, "mimeTypesWithExtension", mimeTypesWithExtension); /** All supported mime types */ __publicField(MimeTypeUtil, "mimeTypes", mimeTypes); // src/validation/schemas/common.ts var import_zod = require("zod"); var stringSchema = import_zod.z.string(); var nameLikeSchema = import_zod.z.string().min(3).max(32).regex(/^[a-zA-Z0-9_]{3,32}$/, { message: "Name must contain only letters, numbers and _" }); // src/validation/schemas/create.ts var createObjectSchema = import_zod2.z.object({ /** A string representing the name for the file. */ name: nameLikeSchema, /** Use absolute path, Buffer or Blob */ file: import_zod2.z.string().or(import_zod2.z.instanceof(Buffer)), /** A string representing the MIME type of the file. */ mimeType: import_zod2.z.enum(MimeTypeUtil.mimeTypes).optional(), /** A string representing the prefix for the file. */ prefix: nameLikeSchema.optional(), /** A number indicating the expiration period of the file, ranging from 1 to 365 days. */ expiresIn: import_zod2.z.number().min(1).max(365).optional(), /** Set to true if a security hash is required. */ securityHash: import_zod2.z.boolean().optional(), /** Set to true if the file should be set for automatic download. */ autoDownload: import_zod2.z.boolean().optional() }).refine(({ file, mimeType }) => !(file instanceof Buffer && !mimeType), { message: "mimeType is required if file is a Buffer", path: ["mimeType"] }); var createObjectPayloadSchema = createObjectSchema.transform( ({ file, securityHash, autoDownload, expiresIn, ...rest }) => ({ file, mimeType: typeof file === "string" ? MimeTypeUtil.fromExtension(file.split(".")[1]) : void 0, params: { ...rest, expire: expiresIn, security_hash: securityHash, auto_download: autoDownload } }) ); var createObjectResponseSchema = import_zod2.z.object({ /** The id of the uploaded file. */ id: import_zod2.z.string(), /** The name of the uploaded file. */ name: import_zod2.z.string(), /** The prefix of the uploaded file. */ prefix: import_zod2.z.string().optional(), /** The size of the uploaded file. */ size: import_zod2.z.number(), /** The URL of the uploaded file. (File distributed in Square Cloud CDN) */ url: import_zod2.z.string() }); // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { createObjectPayloadSchema, createObjectResponseSchema, createObjectSchema }); //# sourceMappingURL=create.js.map