@squarecloud/blob
Version:
Official Square Cloud Blob SDK for NodeJS
137 lines (132 loc) • 4.66 kB
JavaScript
var __defProp = Object.defineProperty;
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
var __publicField = (obj, key, value) => {
__defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
return value;
};
// src/validation/schemas/create.ts
import { z as z2 } from "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
import { z } from "zod";
var stringSchema = z.string();
var nameLikeSchema = 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 = z2.object({
/** A string representing the name for the file. */
name: nameLikeSchema,
/** Use absolute path, Buffer or Blob */
file: z2.string().or(z2.instanceof(Buffer)),
/** A string representing the MIME type of the file. */
mimeType: z2.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: z2.number().min(1).max(365).optional(),
/** Set to true if a security hash is required. */
securityHash: z2.boolean().optional(),
/** Set to true if the file should be set for automatic download. */
autoDownload: z2.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 = z2.object({
/** The id of the uploaded file. */
id: z2.string(),
/** The name of the uploaded file. */
name: z2.string(),
/** The prefix of the uploaded file. */
prefix: z2.string().optional(),
/** The size of the uploaded file. */
size: z2.number(),
/** The URL of the uploaded file. (File distributed in Square Cloud CDN) */
url: z2.string()
});
export {
createObjectPayloadSchema,
createObjectResponseSchema,
createObjectSchema
};
//# sourceMappingURL=create.mjs.map