@squarecloud/blob
Version:
Official Square Cloud Blob SDK for NodeJS
204 lines (195 loc) • 6.98 kB
JavaScript
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/assertions/create.ts
var create_exports = {};
__export(create_exports, {
assertCreateObjectResponse: () => assertCreateObjectResponse
});
module.exports = __toCommonJS(create_exports);
// src/validation/schemas/create.ts
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()
});
// src/structures/error.ts
var SquareCloudBlobError = class _SquareCloudBlobError extends Error {
constructor(code, message, cause) {
super(message, { cause });
this.name = _SquareCloudBlobError.name;
this.message = this.getMessage(code);
}
getMessage(rawCode) {
const code = rawCode.replaceAll("_", " ").toLowerCase().replace(/(^|\s)\S/g, (L) => L.toUpperCase());
const message = this.message ? `: ${this.message}` : "";
return `${code}${message}`;
}
};
// src/validation/assertions/handlers.ts
function handleAPIObjectAssertion({
schema,
value,
code
}) {
const name = code.toLowerCase().replaceAll("_", " ");
try {
return schema.parse(value);
} catch (err) {
const cause = err.errors?.map((err2) => ({
...err2,
path: err2.path.join(" > ")
}));
throw new SquareCloudBlobError(
`INVALID_API_${code}`,
`Invalid ${name} object received from API`,
{ cause }
);
}
}
// src/validation/assertions/create.ts
function assertCreateObjectResponse(value) {
return handleAPIObjectAssertion({
schema: createObjectResponseSchema,
code: "CREATE_OBJECT",
value
});
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
assertCreateObjectResponse
});
//# sourceMappingURL=create.js.map