UNPKG

@squarecloud/blob

Version:
1 lines 10.9 kB
{"version":3,"sources":["../../../src/validation/schemas/create.ts","../../../src/utils/mimetype/mimetypes.ts","../../../src/utils/mimetype/index.ts","../../../src/validation/schemas/common.ts","../../../src/structures/error.ts","../../../src/validation/assertions/handlers.ts","../../../src/validation/assertions/create.ts"],"sourcesContent":["import { z } from \"zod\";\nimport { MimeTypeUtil } from \"../../utils/mimetype\";\nimport { nameLikeSchema } from \"./common\";\n\nexport const createObjectSchema = z\n\t.object({\n\t\t/** A string representing the name for the file. */\n\t\tname: nameLikeSchema,\n\t\t/** Use absolute path, Buffer or Blob */\n\t\tfile: z.string().or(z.instanceof(Buffer)),\n\t\t/** A string representing the MIME type of the file. */\n\t\tmimeType: z.enum(MimeTypeUtil.mimeTypes).optional(),\n\t\t/** A string representing the prefix for the file. */\n\t\tprefix: nameLikeSchema.optional(),\n\t\t/** A number indicating the expiration period of the file, ranging from 1 to 365 days. */\n\t\texpiresIn: z.number().min(1).max(365).optional(),\n\t\t/** Set to true if a security hash is required. */\n\t\tsecurityHash: z.boolean().optional(),\n\t\t/** Set to true if the file should be set for automatic download. */\n\t\tautoDownload: z.boolean().optional(),\n\t})\n\t.refine(({ file, mimeType }) => !(file instanceof Buffer && !mimeType), {\n\t\tmessage: \"mimeType is required if file is a Buffer\",\n\t\tpath: [\"mimeType\"],\n\t});\n\nexport const createObjectPayloadSchema = createObjectSchema.transform(\n\t({ file, securityHash, autoDownload, expiresIn, ...rest }) => ({\n\t\tfile,\n\t\tmimeType:\n\t\t\ttypeof file === \"string\"\n\t\t\t\t? MimeTypeUtil.fromExtension(file.split(\".\")[1])\n\t\t\t\t: undefined,\n\t\tparams: {\n\t\t\t...rest,\n\t\t\texpire: expiresIn,\n\t\t\tsecurity_hash: securityHash,\n\t\t\tauto_download: autoDownload,\n\t\t},\n\t}),\n);\n\nexport const createObjectResponseSchema = z.object({\n\t/** The id of the uploaded file. */\n\tid: z.string(),\n\t/** The name of the uploaded file. */\n\tname: z.string(),\n\t/** The prefix of the uploaded file. */\n\tprefix: z.string().optional(),\n\t/** The size of the uploaded file. */\n\tsize: z.number(),\n\t/** The URL of the uploaded file. (File distributed in Square Cloud CDN) */\n\turl: z.string(),\n});\n","export const mimeTypesWithExtension = {\n\t\"video/mp4\": [\"mp4\"],\n\t\"video/mpeg\": [\"mpeg\"],\n\t\"video/webm\": [\"webm\"],\n\t\"video/x-flv\": [\"flv\"],\n\t\"video/x-m4v\": [\"m4v\"],\n\t\"image/jpeg\": [\"jpg\", \"jpeg\"],\n\t\"image/png\": [\"png\"],\n\t\"image/apng\": [\"apng\"],\n\t\"image/tiff\": [\"tiff\"],\n\t\"image/gif\": [\"gif\"],\n\t\"image/webp\": [\"webp\"],\n\t\"image/bmp\": [\"bmp\"],\n\t\"image/svg+xml\": [\"svg\"],\n\t\"image/x-icon\": [\"ico\"],\n\t\"image/ico\": [\"ico\"],\n\t\"image/cur\": [\"cur\"],\n\t\"image/heic\": [\"heic\"],\n\t\"image/heif\": [\"heif\"],\n\t\"audio/wav\": [\"wav\"],\n\t\"audio/ogg\": [\"ogg\"],\n\t\"audio/opus\": [\"opus\"],\n\t\"audio/mp4\": [\"mp4\"],\n\t\"audio/mpeg\": [\"mp3\"],\n\t\"audio/aac\": [\"aac\"],\n\t\"text/plain\": [\"txt\"],\n\t\"text/html\": [\"html\"],\n\t\"text/css\": [\"css\"],\n\t\"text/csv\": [\"csv\"],\n\t\"text/x-sql\": [\"sql\"],\n\t\"application/xml\": [\"xml\"],\n\t\"application/sql\": [\"sql\"],\n\t\"application/x-sql\": [\"sql\"],\n\t\"application/x-sqlite3\": [\"sqlite3\"],\n\t\"application/x-pkcs12\": [\"pfx\"],\n\t\"application/pdf\": [\"pdf\"],\n\t\"application/json\": [\"json\"],\n\t\"application/javascript\": [\"js\"],\n};\n\nexport const mimeTypes = Object.keys(mimeTypesWithExtension) as [\n\tMimeType,\n\t...MimeType[],\n];\n\nexport type MimeType = keyof typeof mimeTypesWithExtension;\n","import { type MimeType, mimeTypes, mimeTypesWithExtension } from \"./mimetypes\";\n\n// biome-ignore lint/complexity/noStaticOnlyClass: organization\nexport class MimeTypeUtil {\n\t/** Supported mime types with their extensions */\n\tstatic mimeTypesWithExtension = mimeTypesWithExtension;\n\t/** All supported mime types */\n\tstatic mimeTypes = mimeTypes;\n\n\t/**\n\t * Returns the corresponding MIME type for a given file extension.\n\t *\n\t * @param extension - The file extension to search for.\n\t * @return The MIME type associated with the extension, or \"text/plain\" if not found.\n\t *\n\t * @example\n\t * ```js\n\t * \tMimeTypeUtil.fromExtension(\"jpeg\") // \"image/jpeg\" | Supported\n\t * \tMimeTypeUtil.fromExtension(\"json\") // \"application/json\" | Supported\n\t * \tMimeTypeUtil.fromExtension(\"potato\") // \"text/plain\" | Unsupported, defaults to text/plain\n\t * ```\n\t */\n\tstatic fromExtension(extension: string): MimeType {\n\t\tconst entries = Object.entries(mimeTypesWithExtension);\n\t\tconst mimeType = entries.find(([, extensions]) =>\n\t\t\textensions.includes(extension),\n\t\t)?.[0];\n\n\t\treturn (mimeType as MimeType) || \"text/plain\";\n\t}\n}\n\nexport * from \"./enum\";\nexport { MimeType } from \"./mimetypes\";\n","import { z } from \"zod\";\n\nexport const stringSchema = z.string();\n\nexport const nameLikeSchema = z\n\t.string()\n\t.min(3)\n\t.max(32)\n\t.regex(/^[a-zA-Z0-9_]{3,32}$/, {\n\t\tmessage: \"Name must contain only letters, numbers and _\",\n\t});\n","export class SquareCloudBlobError extends Error {\n\tconstructor(code: string, message?: string, cause?: unknown) {\n\t\tsuper(message, { cause });\n\n\t\tthis.name = SquareCloudBlobError.name;\n\t\tthis.message = this.getMessage(code);\n\t}\n\n\tprivate getMessage(rawCode: string) {\n\t\tconst code = rawCode\n\t\t\t.replaceAll(\"_\", \" \")\n\t\t\t.toLowerCase()\n\t\t\t.replace(/(^|\\s)\\S/g, (L) => L.toUpperCase());\n\t\tconst message = this.message ? `: ${this.message}` : \"\";\n\n\t\treturn `${code}${message}`;\n\t}\n}\n\nexport class SquareCloudValidationError extends SquareCloudBlobError {\n\tconstructor(...args: ConstructorParameters<typeof SquareCloudBlobError>) {\n\t\tsuper(...args);\n\t\tthis.name = SquareCloudValidationError.name;\n\t}\n}\n","import type { ZodIssue } from \"zod\";\nimport {\n\tSquareCloudBlobError,\n\tSquareCloudValidationError,\n} from \"../../structures/error\";\nimport type {\n\tAPIObjectAssertionProps,\n\tLiteralAssertionProps,\n} from \"../../types/assertions\";\n\nexport function handleLiteralAssertion({\n\tschema,\n\tvalue,\n\texpect,\n\tcode,\n}: LiteralAssertionProps) {\n\ttry {\n\t\tschema.parse(value);\n\t} catch {\n\t\tthrow new SquareCloudValidationError(\n\t\t\tcode ? `INVALID_${code}` : \"VALIDATION_ERROR\",\n\t\t\t`Expect ${expect}, got ${typeof value}`,\n\t\t);\n\t}\n}\n\nexport function handleAPIObjectAssertion({\n\tschema,\n\tvalue,\n\tcode,\n}: APIObjectAssertionProps) {\n\tconst name = code.toLowerCase().replaceAll(\"_\", \" \");\n\n\ttry {\n\t\treturn schema.parse(value);\n\t} catch (err) {\n\t\tconst cause = err.errors?.map((err: ZodIssue) => ({\n\t\t\t...err,\n\t\t\tpath: err.path.join(\" > \"),\n\t\t}));\n\n\t\tthrow new SquareCloudBlobError(\n\t\t\t`INVALID_API_${code}`,\n\t\t\t`Invalid ${name} object received from API`,\n\t\t\t{ cause },\n\t\t);\n\t}\n}\n","import type { CreateObjectResponse } from \"../../types/create\";\nimport { createObjectResponseSchema } from \"../schemas/create\";\nimport { handleAPIObjectAssertion } from \"./handlers\";\n\nexport function assertCreateObjectResponse(\n\tvalue: unknown,\n): CreateObjectResponse {\n\treturn handleAPIObjectAssertion({\n\t\tschema: createObjectResponseSchema,\n\t\tcode: \"CREATE_OBJECT\",\n\t\tvalue,\n\t});\n}\n"],"mappings":";;;;;;;;AAAA,SAAS,KAAAA,UAAS;;;ACAX,IAAM,yBAAyB;AAAA,EACrC,aAAa,CAAC,KAAK;AAAA,EAC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