frappe-js-client
Version:
Next-generation TS/JS client for Frappe REST APIs, built on axios for robust, type-safe integration.
268 lines (267 loc) • 11 kB
JavaScript
;
/**
* @module file
* @description Provides functionality for file upload operations in Frappe.
* This module handles file uploads with progress tracking, authentication,
* and custom metadata support.
*
* @packageDocumentation
*
* @example
* ```typescript
* import { FrappeApp } from '@frappe/sdk';
*
* const app = new FrappeApp('https://instance.example.com');
* const fileUploader = app.file();
*
* // Upload a file with progress tracking
* await fileUploader.uploadFile(
* fileBlob,
* {
* isPrivate: true,
* folder: 'Home/Documents'
* },
* (uploaded, total) => {
* console.log(`Progress: ${(uploaded/total) * 100}%`);
* }
* );
* ```
*/
var __assign = (this && this.__assign) || function () {
__assign = Object.assign || function(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
t[p] = s[p];
}
return t;
};
return __assign.apply(this, arguments);
};
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __generator = (this && this.__generator) || function (thisArg, body) {
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype);
return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
function verb(n) { return function (v) { return step([n, v]); }; }
function step(op) {
if (f) throw new TypeError("Generator is already executing.");
while (g && (g = 0, op[0] && (_ = 0)), _) try {
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
if (y = 0, t) op = [op[0] & 2, t.value];
switch (op[0]) {
case 0: case 1: t = op; break;
case 4: _.label++; return { value: op[1], done: false };
case 5: _.label++; y = op[1]; op = [0]; continue;
case 7: op = _.ops.pop(); _.trys.pop(); continue;
default:
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
if (t[2]) _.ops.pop();
_.trys.pop(); continue;
}
op = body.call(thisArg, _);
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
}
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.FrappeFileDownload = exports.FrappeFileUpload = void 0;
var axios_1 = require("../utils/axios");
/**
* Handles file upload operations for Frappe.
*
* @class FrappeFileUpload
* @description Provides methods for uploading files to a Frappe instance with
* support for progress tracking, authentication, and custom metadata.
*
* @example
* ```typescript
* const uploader = new FrappeFileUpload(
* 'https://instance.example.com',
* axiosInstance,
* true,
* () => localStorage.getItem('token'),
* 'Bearer'
* );
*
* // Upload a file
* const response = await uploader.uploadFile(
* file,
* { isPrivate: true }
* );
* ```
*/
var FrappeFileUpload = /** @class */ (function () {
/**
* Creates a new FrappeFileUpload instance.
*
* @param appURL - Base URL of the Frappe instance
* @param axios - Configured Axios instance for making requests
* @param useToken - Whether to use token-based authentication
* @param token - Function that returns the authentication token
* @param tokenType - Type of token to use ('Bearer' or 'token')
* @param customHeaders - Additional headers to include in requests
*
* @example
* ```typescript
* const uploader = new FrappeFileUpload(
* 'https://instance.example.com',
* axiosInstance,
* true,
* () => localStorage.getItem('token'),
* 'Bearer'
* );
* ```
*/
function FrappeFileUpload(appURL, axios, useToken, token, tokenType, customHeaders) {
this.appURL = appURL;
this.axios = axios;
this.useToken = useToken !== null && useToken !== void 0 ? useToken : false;
this.token = token;
this.tokenType = tokenType;
this.customHeaders = customHeaders;
}
/**
* Uploads a file to the Frappe instance with optional progress tracking.
*
* @template T - Type of additional data to be sent with the file
* @param file - The file to upload
* @param args - Configuration options for the upload
* @param onProgress - Optional callback for tracking upload progress
* @param apiPath - Optional custom API endpoint path (defaults to 'upload_file')
*
* @returns Promise that resolves with the upload response
* @throws {FrappeError} If the upload fails
*
* @example
* ```typescript
* // Basic file upload
* const response = await uploader.uploadFile(
* file,
* { isPrivate: true }
* );
*
* // Upload with progress tracking
* const response = await uploader.uploadFile(
* file,
* { folder: 'Home/Documents' },
* (uploaded, total) => {
* console.log(`Progress: ${(uploaded/total) * 100}%`);
* }
* );
*
* // Upload with custom metadata
* interface CustomData {
* category: string;
* }
*
* const response = await uploader.uploadFile<CustomData>(
* file,
* {
* doctype: 'File',
* docname: 'FILE-001',
* otherData: { category: 'Important' }
* }
* );
* ```
*/
FrappeFileUpload.prototype.uploadFile = function (file_1, args_1, onProgress_1) {
return __awaiter(this, arguments, void 0, function (file, args, onProgress, apiPath) {
var formData, isPrivate, folder, file_url, doctype, docname, fieldname, otherData;
if (apiPath === void 0) { apiPath = 'upload_file'; }
return __generator(this, function (_a) {
formData = new FormData();
if (file)
formData.append('file', file, file.name);
isPrivate = args.isPrivate, folder = args.folder, file_url = args.file_url, doctype = args.doctype, docname = args.docname, fieldname = args.fieldname, otherData = args.otherData;
if (isPrivate) {
formData.append('is_private', '1');
}
if (folder) {
formData.append('folder', folder);
}
if (file_url) {
formData.append('file_url', file_url);
}
if (doctype && docname) {
formData.append('doctype', doctype);
formData.append('docname', docname);
if (fieldname) {
formData.append('fieldname', fieldname);
}
}
if (otherData) {
Object.keys(otherData).forEach(function (key) {
var v = otherData[key];
formData.append(key, v);
});
}
return [2 /*return*/, this.axios
.post("/api/method/".concat(apiPath), formData, {
onUploadProgress: function (progressEvent) {
if (onProgress) {
onProgress(progressEvent.loaded, progressEvent.total, progressEvent);
}
},
headers: __assign(__assign({}, (0, axios_1.getRequestHeaders)(this.useToken, this.tokenType, this.token, this.appURL, this.customHeaders)), { 'Content-Type': 'multipart/form-data' }),
})
.catch(function (error) {
var _a, _b;
throw __assign(__assign({}, error.response.data), { httpStatus: error.response.status, httpStatusText: error.response.statusText, message: (_a = error.response.data.message) !== null && _a !== void 0 ? _a : 'There was an error while uploading the file.', exception: (_b = error.response.data.exception) !== null && _b !== void 0 ? _b : '' });
})];
});
});
};
return FrappeFileUpload;
}());
exports.FrappeFileUpload = FrappeFileUpload;
/**
* Handles file download operations for Frappe.
*
* @class FrappeFileDownload
* @description Provides methods for downloading files from a Frappe instance.
*
* @example
* ```typescript
* const downloader = new FrappeFileDownload(axiosInstance)
* const response = await downloader.downloadFile('https://instance.example.com/files/test.pdf')
* ```
*/
var FrappeFileDownload = /** @class */ (function () {
function FrappeFileDownload(axios) {
this.axios = axios;
}
/**
* Downloads a file from the Frappe instance.
*
* @param fileURL - The URL of the file to download
* @returns Promise that resolves with the download response
* @throws {FrappeError} If the download fails
*/
FrappeFileDownload.prototype.downloadFile = function (fileURL) {
return (0, axios_1.handleRequest)({
axios: this.axios,
config: {
method: 'GET',
url: '/api/method/download_file',
params: {
file_url: fileURL,
},
},
errorMessage: 'There was an error while downloading the file.',
transformResponse: function (data) { return data.data; },
});
};
return FrappeFileDownload;
}());
exports.FrappeFileDownload = FrappeFileDownload;