{"version":3,"file":"index.mjs","names":[],"sources":["../../../../src/plugin/builtin/file-utils/generate-file-utils.ts","../../../../src/plugin/builtin/file-utils/process-file-type.ts","../../../../src/plugin/builtin/file-utils/index.ts"],"sourcesContent":["import multiline from \"#/utils/multiline\";\nimport type { FileUtilMetadata } from \"./types\";\n\n/**\n * Generate unified file utility functions from collected metadata.\n * @param namespaceData - Namespace data with file utility metadata\n * @returns Generated file utility code\n */\nexport function generateUnifiedFileUtils(\n  namespaceData: { namespace: string; types: FileUtilMetadata[] }[],\n): string {\n  if (namespaceData.length === 0) {\n    return \"\";\n  }\n\n  // Collect all tables with their namespace\n  const tableNamespaceMap = new Map<string, string>();\n  const typeFieldsMap = new Map<string, string[]>();\n\n  for (const { namespace, types } of namespaceData) {\n    for (const type of types) {\n      tableNamespaceMap.set(type.name, namespace);\n      typeFieldsMap.set(type.name, type.fileFields);\n    }\n  }\n\n  if (tableNamespaceMap.size === 0) {\n    return \"\";\n  }\n\n  // Generate interface fields\n  const interfaceFields = Array.from(typeFieldsMap.entries())\n    .map(([tableName, fields]) => {\n      const fieldNamesUnion = fields.map((field) => `\"${field}\"`).join(\" | \");\n      return `  ${tableName}: {\\n    fields: ${fieldNamesUnion};\\n  };`;\n    })\n    .join(\"\\n\");\n\n  const importStatement =\n    multiline /* ts */ `\n      import { file } from \"@tailor-platform/sdk/runtime/file\";\n      import type {\n        FileUploadOptions,\n        FileUploadStringOptions,\n        FileUploadBytes,\n        FileUploadResponse,\n        FileMetadata,\n        FileDownloadStreamResponse,\n      } from \"@tailor-platform/sdk/runtime/file\";\n    ` + \"\\n\";\n\n  const interfaceDefinition =\n    multiline /* ts */ `\n      export interface TypeWithFiles {\n      ${interfaceFields}\n      }\n    ` + \"\\n\";\n\n  // Generate namespaces object\n  const namespaceEntries = Array.from(tableNamespaceMap.entries())\n    .map(([tableName, namespace]) => `  ${tableName}: \"${namespace}\"`)\n    .join(\",\\n\");\n\n  const namespacesDefinition =\n    multiline /* ts */ `\n      const namespaces: Record<keyof TypeWithFiles, string> = {\n      ${namespaceEntries},\n      };\n    ` + \"\\n\";\n\n  // Generate downloadFile helper function\n  const downloadFunction =\n    multiline /* ts */ `\n      export async function downloadFile<T extends keyof TypeWithFiles>(\n        type: T,\n        field: TypeWithFiles[T][\"fields\"],\n        recordId: string,\n      ) {\n        return await file.download(namespaces[type], type, field, recordId);\n      }\n    ` + \"\\n\";\n\n  // Generate uploadFile helper function\n  const uploadFunction =\n    multiline /* ts */ `\n      export function uploadFile<T extends keyof TypeWithFiles>(\n        type: T,\n        field: TypeWithFiles[T][\"fields\"],\n        recordId: string,\n        data: FileUploadBytes,\n        options?: FileUploadOptions,\n      ): Promise<FileUploadResponse>;\n      export function uploadFile<T extends keyof TypeWithFiles>(\n        type: T,\n        field: TypeWithFiles[T][\"fields\"],\n        recordId: string,\n        data: string | FileUploadBytes,\n        options: FileUploadStringOptions,\n      ): Promise<FileUploadResponse>;\n      /**\n       * @deprecated since NEXT_RELEASE — pass encoding: \"utf8\" for text or \"base64\" for Base64 strings. codemod: v3/file-upload-encoding\n       */\n      export function uploadFile<T extends keyof TypeWithFiles>(\n        type: T,\n        field: TypeWithFiles[T][\"fields\"],\n        recordId: string,\n        data: string | FileUploadBytes,\n        options?: FileUploadOptions,\n      ): Promise<FileUploadResponse>;\n      export async function uploadFile<T extends keyof TypeWithFiles>(\n        type: T,\n        field: TypeWithFiles[T][\"fields\"],\n        recordId: string,\n        data: string | FileUploadBytes,\n        options?: FileUploadOptions,\n      ): Promise<FileUploadResponse> {\n        return await file.upload(namespaces[type], type, field, recordId, data, options);\n      }\n    ` + \"\\n\";\n\n  // Generate deleteFile helper function\n  const deleteFunction =\n    multiline /* ts */ `\n      export async function deleteFile<T extends keyof TypeWithFiles>(\n        type: T,\n        field: TypeWithFiles[T][\"fields\"],\n        recordId: string,\n      ): Promise<void> {\n        return await file.delete(namespaces[type], type, field, recordId);\n      }\n    ` + \"\\n\";\n\n  // Generate getFileMetadata helper function\n  const getMetadataFunction =\n    multiline /* ts */ `\n      export async function getFileMetadata<T extends keyof TypeWithFiles>(\n        type: T,\n        field: TypeWithFiles[T][\"fields\"],\n        recordId: string,\n      ): Promise<FileMetadata> {\n        return await file.getMetadata(namespaces[type], type, field, recordId);\n      }\n    ` + \"\\n\";\n\n  // Generate downloadFileStream helper function\n  const downloadStreamFunction =\n    multiline /* ts */ `\n      export async function downloadFileStream<T extends keyof TypeWithFiles>(\n        type: T,\n        field: TypeWithFiles[T][\"fields\"],\n        recordId: string,\n      ): Promise<FileDownloadStreamResponse> {\n        return await file.downloadStream(namespaces[type], type, field, recordId);\n      }\n    ` + \"\\n\";\n\n  return [\n    importStatement,\n    interfaceDefinition,\n    namespacesDefinition,\n    downloadFunction,\n    uploadFunction,\n    deleteFunction,\n    getMetadataFunction,\n    downloadStreamFunction,\n  ].join(\"\\n\");\n}\n","import type { TailorDBType } from \"#/parser/service/tailordb/types\";\nimport type { FileUtilMetadata } from \"./types\";\n\n/**\n * Process a TailorDB table and extract file field metadata.\n * @param type - The parsed TailorDB table to process\n * @returns File utility metadata for the table\n */\nexport async function processFileType(type: TailorDBType): Promise<FileUtilMetadata> {\n  const fileFields: string[] = [];\n\n  if (type.files) {\n    for (const fileFieldName of Object.keys(type.files)) {\n      fileFields.push(fileFieldName);\n    }\n  }\n\n  return {\n    name: type.name,\n    fileFields,\n  };\n}\n","import { generateUnifiedFileUtils } from \"./generate-file-utils\";\nimport { processFileType } from \"./process-file-type\";\nimport type { Plugin, GeneratorResult, TailorDBReadyContext } from \"#/plugin/types\";\nimport type { FileUtilMetadata } from \"./types\";\n\n/** Unique identifier for the file utilities generator plugin. */\nexport const FileUtilsGeneratorID = \"@tailor-platform/file-utils\";\n\ntype FileUtilsPluginOptions = {\n  distPath: string;\n};\n\n/**\n * Plugin that generates TypeWithFiles interface from TailorDB table definitions.\n * @param options - Plugin options\n * @param options.distPath - Output file path for generated file utilities\n * @returns Plugin instance with onTailorDBReady hook\n */\nexport function fileUtilsPlugin(\n  options: FileUtilsPluginOptions,\n): Plugin<unknown, FileUtilsPluginOptions> {\n  return {\n    id: FileUtilsGeneratorID,\n    description: \"Generates TypeWithFiles interface from TailorDB table definitions\",\n    pluginConfig: options,\n\n    async onTailorDBReady(\n      ctx: TailorDBReadyContext<FileUtilsPluginOptions>,\n    ): Promise<GeneratorResult> {\n      const namespaceData: { namespace: string; types: FileUtilMetadata[] }[] = [];\n\n      for (const ns of ctx.tailordb) {\n        const typesWithFiles: FileUtilMetadata[] = [];\n\n        for (const type of Object.values(ns.tables)) {\n          const metadata = await processFileType(type);\n          if (metadata.fileFields.length > 0) {\n            typesWithFiles.push(metadata);\n          }\n        }\n\n        if (typesWithFiles.length > 0) {\n          namespaceData.push({\n            namespace: ns.namespace,\n            types: typesWithFiles,\n          });\n        }\n      }\n\n      const files: GeneratorResult[\"files\"] = [];\n      if (namespaceData.length > 0) {\n        const content = generateUnifiedFileUtils(namespaceData);\n        if (content) {\n          files.push({\n            path: ctx.pluginConfig.distPath,\n            content,\n          });\n        }\n      }\n\n      return { files };\n    },\n  };\n}\n"],"mappings":"oDAQA,SAAgB,yBACd,EACQ,CACR,GAAI,EAAc,SAAW,EAC3B,MAAO,GAIT,IAAM,EAAoB,IAAI,IACxB,EAAgB,IAAI,IAE1B,IAAK,GAAM,CAAE,YAAW,WAAW,EACjC,IAAK,IAAM,KAAQ,EACjB,EAAkB,IAAI,EAAK,KAAM,CAAS,EAC1C,EAAc,IAAI,EAAK,KAAM,EAAK,UAAU,EAIhD,GAAI,EAAkB,OAAS,EAC7B,MAAO,GAIT,IAAM,EAAkB,MAAM,KAAK,EAAc,QAAQ,CAAC,CAAC,CACxD,KAAK,CAAC,EAAW,KAET,KAAK,EAAU,mBADE,EAAO,IAAK,GAAU,IAAI,EAAM,EAAE,CAAC,CAAC,KAAK,KACV,EAAE,QAC1D,CAAC,CACD,KAAK;CAAI,EAEN,EACJ,CAAmB;;;;;;;;;;MAUf;EAEA,EACJ,CAAmB;;QAEf,EAAgB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