@datocms/cma-client-node
Version:
NodeJS client for DatoCMS REST Content Management API
50 lines • 2.22 kB
JavaScript
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());
});
};
import { basename } from 'node:path';
import { CanceledPromiseError, makeCancelablePromise, } from '@datocms/rest-client-utils';
import { uploadLocalFileToS3 } from './uploadLocalFileToS3';
export function uploadLocalFileAndReturnPath(client, localPath, options = {}) {
const filename = options.filename || basename(localPath);
let isCanceledBeforeUpload = false;
let uploadPromise = undefined;
return makeCancelablePromise(() => __awaiter(this, void 0, void 0, function* () {
if (options.onProgress) {
options.onProgress({
type: 'REQUESTING_UPLOAD_URL',
payload: { filename },
});
}
const { id, url, request_headers } = yield client.uploadRequest.create({
filename,
});
if (isCanceledBeforeUpload) {
throw new CanceledPromiseError();
}
if (options.onProgress) {
options.onProgress({
type: 'UPLOADING_FILE',
payload: {
progress: 0,
},
});
}
uploadPromise = uploadLocalFileToS3(localPath, url, Object.assign(Object.assign({}, options), { additionalHeaders: request_headers, fetchFn: client.config.fetchFn }));
yield uploadPromise;
return id;
}), () => {
if (uploadPromise) {
uploadPromise.cancel();
}
else {
isCanceledBeforeUpload = true;
}
});
}
//# sourceMappingURL=uploadLocalFileAndReturnPath.js.map