{"version":3,"file":"assets.cjs","names":["onResponseHandler: ResponseTransformer"],"sources":["../../../../src/rest/commands/read/assets.ts"],"sourcesContent":["import type { DirectusFile, DirectusFolder } from '../../../schema/index.js';\nimport type { AssetResponse, AssetsQuery, ResponseTransformer } from '../../../types/index.js';\nimport type { RestCommand } from '../../types.js';\nimport { throwIfEmpty } from '../../utils/index.js';\n\n/**\n * Read the contents of a file as a ReadableStream\n *\n * @param {string} key\n * @param {AssetsQuery} query\n * @returns {ReadableStream<Uint8Array>}\n */\nexport const readAssetRaw =\n\t<Schema>(key: DirectusFile<Schema>['id'], query?: AssetsQuery): RestCommand<ReadableStream<Uint8Array>, Schema> =>\n\t() => {\n\t\tthrowIfEmpty(String(key), 'Key cannot be empty');\n\n\t\treturn {\n\t\t\tpath: `/assets/${key}`,\n\t\t\tparams: query ?? {},\n\t\t\tmethod: 'GET',\n\t\t\tonResponse: (response) => response.body,\n\t\t};\n\t};\n\n/**\n * Read the contents of a file as a Blob\n *\n * @param {string} key\n * @param {AssetsQuery} query\n * @returns {Blob}\n */\nexport const readAssetBlob =\n\t<Schema>(key: DirectusFile<Schema>['id'], query?: AssetsQuery): RestCommand<Blob, Schema> =>\n\t() => {\n\t\tthrowIfEmpty(String(key), 'Key cannot be empty');\n\n\t\treturn {\n\t\t\tpath: `/assets/${key}`,\n\t\t\tparams: query ?? {},\n\t\t\tmethod: 'GET',\n\t\t\tonResponse: (response) => response.blob(),\n\t\t};\n\t};\n\n/**\n * Read the contents of a file as a ArrayBuffer\n *\n * @param {string} key\n * @param {AssetsQuery} query\n * @returns {ArrayBuffer}\n */\nexport const readAssetArrayBuffer =\n\t<Schema>(key: DirectusFile<Schema>['id'], query?: AssetsQuery): RestCommand<ArrayBuffer, Schema> =>\n\t() => {\n\t\tthrowIfEmpty(String(key), 'Key cannot be empty');\n\n\t\treturn {\n\t\t\tpath: `/assets/${key}`,\n\t\t\tparams: query ?? {},\n\t\t\tmethod: 'GET',\n\t\t\tonResponse: (response) => response.arrayBuffer(),\n\t\t};\n\t};\n\n/**\n * Download a ZIP archive containing the specified files.\n *\n * @param keys An array of file IDs to include in the ZIP archive, must contain at least one ID.\n * @param options\n */\nexport const downloadFilesZip =\n\t<Schema, R extends keyof AssetResponse = 'raw'>(\n\t\tkeys: DirectusFile<Schema>['id'][],\n\t\toptions?: { output: R },\n\t): RestCommand<AssetResponse[R], Schema> =>\n\t() => {\n\t\tthrowIfEmpty(String(keys), 'Keys cannot be empty');\n\n\t\tlet onResponseHandler: ResponseTransformer = (response) => response.body;\n\n\t\tif (options?.output === 'arrayBuffer') {\n\t\t\tonResponseHandler = (response) => response.arrayBuffer();\n\t\t} else if (options?.output === 'blob') {\n\t\t\tonResponseHandler = (response) => response.blob();\n\t\t}\n\n\t\treturn {\n\t\t\tpath: `/assets/files/`,\n\t\t\tbody: JSON.stringify({\n\t\t\t\tids: keys,\n\t\t\t}),\n\t\t\tmethod: 'POST',\n\t\t\tonResponse: onResponseHandler,\n\t\t};\n\t};\n\n/**\n * Download a ZIP archive of an entire folder tree.\n *\n * @param key The root folder ID to download.\n * @param options\n */\nexport const downloadFolderZip =\n\t<Schema, R extends keyof AssetResponse = 'raw'>(\n\t\tkey: DirectusFolder<Schema>['id'],\n\t\toptions?: { output: R },\n\t): RestCommand<AssetResponse[R], Schema> =>\n\t() => {\n\t\tthrowIfEmpty(String(key), 'Key cannot be empty');\n\n\t\tlet onResponseHandler: ResponseTransformer = (response) => response.body;\n\n\t\tif (options?.output === 'arrayBuffer') {\n\t\t\tonResponseHandler = (response) => response.arrayBuffer();\n\t\t} else if (options?.output === 'blob') {\n\t\t\tonResponseHandler = (response) => response.blob();\n\t\t}\n\n\t\treturn {\n\t\t\tpath: `/assets/folder/${key}`,\n\t\t\tmethod: 'POST',\n\t\t\tonResponse: onResponseHandler,\n\t\t};\n\t};\n"],"mappings":"kDAYa,GACH,EAAiC,SAEzC,EAAA,aAAa,OAAO,EAAI,CAAE,sBAAsB,CAEzC,CACN,KAAM,WAAW,IACjB,OAAQ,GAAS,EAAE,CACnB,OAAQ,MACR,WAAa,GAAa,EAAS,KACnC,EAUU,GACH,EAAiC,SAEzC,EAAA,aAAa,OAAO,EAAI,CAAE,sBAAsB,CAEzC,CACN,KAAM,WAAW,IACjB,OAAQ,GAAS,EAAE,CACnB,OAAQ,MACR,WAAa,GAAa,EAAS,MAAM,CACzC,EAUU,GACH,EAAiC,SAEzC,EAAA,aAAa,OAAO,EAAI,CAAE,sBAAsB,CAEzC,CACN,KAAM,WAAW,IACjB,OAAQ,GAAS,EAAE,CACnB,OAAQ,MACR,WAAa,GAAa,EAAS,aAAa,CAChD,EASU,GAEX,EACA,QAEK,CACL,EAAA,aAAa,OAAO,EAAK,CAAE,uBAAuB,CAElD,IAAIA,EAA0C,GAAa,EAAS,KAQpE,OANI,GAAS,SAAW,cACvB,EAAqB,GAAa,EAAS,aAAa,CAC9C,GAAS,SAAW,SAC9B,EAAqB,GAAa,EAAS,MAAM,EAG3C,CACN,KAAM,iBACN,KAAM,KAAK,UAAU,CACpB,IAAK,EACL,CAAC,CACF,OAAQ,OACR,WAAY,EACZ,EASU,GAEX,EACA,QAEK,CACL,EAAA,aAAa,OAAO,EAAI,CAAE,sBAAsB,CAEhD,IAAIA,EAA0C,GAAa,EAAS,KAQpE,OANI,GAAS,SAAW,cACvB,EAAqB,GAAa,EAAS,aAAa,CAC9C,GAAS,SAAW,SAC9B,EAAqB,GAAa,EAAS,MAAM,EAG3C,CACN,KAAM,kBAAkB,IACxB,OAAQ,OACR,WAAY,EACZ"}