@yachteye/uploader
v1.0.3
Published
YachtEye Collector Uploader library
Downloads
213
Readme
YachtEye Uploader
This small library implements the SYT Uploader and abstracts the details away into a single call to the back-end to upload a file.
Installation
npm install --save @yachteye/uploader
Usage
import YachtEyeUploader from '@yachteye/uploader'
// set to true to enable debug logs
const enableDebugLogs = false
const onProgress = (progress) => {
// This is currently not yet used
console.log('Progress: ' + progress)
}
const onUploaded = ({ id, file, result }) => {
// the file was uploaded successfully
}
/*
* @Param {String} Collector base URL
* @Param {String} NodeJS API base URL
* @Param {String} NodeJS API upload API token
* @Param {Function} Callback that gets called when progress changes. Doesn't yet work
* @Param {Function} Callback that gets called when the upload itself completes
* @Param {Bool} Enable or disable debug logs
*/
const uploader = new YachtEyeUploader('https://collector-dev.superyachtapi.com', 'https://api0.superyachtapi.com', 'MY_API_KEY', onProgress, onUploaded, enableDebugLogs)
/*
* @Param {Job} Collector job information. See interfaces below
* @Param {File} file
* @Param {string} owner - e.g. current yacht ID
*
* @Returns {JobResult}
*/
await uploader.uploadWithJob(job, file, owner)
/*
* OTHER INSTANCE METHODS
*/
// Get the status of a previously created file job
await uploader.getFileJobStatus(jobId) // => Promise<JobResult>
// Manually create a job for an uploaded file
await uploader.createFileJob(fileId, jobName, jobEntityType, jobEntityId, jobMemoryId, photoIndex) // => Promise<JobResult>
// Manually upload a file, without creating a job
await uploader.upload(file, owner) // => Promise<FileJobResult>
// Sequentially upload files, for a single job (useful with attach-gallery-item)
await uploader.uploadMultipleWithJob(job, files, owner) // => Promise<JobResult[]>
NB The uploader will throw an error if anything fails along the way.
Interfaces
export interface Job {
jobName: string
jobEntityType: string
jobEntityId: string
jobMemoryId?: string
photoIndex?: number
}
export interface JobFileEndpoints {
getUploadUrl: string
getFileUrl: string
getFile: string
downloadFile: string
updateFile: string
registerVariant: string
}
export interface JobResult extends Job {
upstreamErrorCount: number
status: string
fileId: string
mediaIds: string[]
endpoints: JobFileEndpoints
createdAt: string
updatedAt: string
date: string
time: string
_id: string
}