attache-upload.js
Upload files to your attache server.
Example
Install
$ npm install --save attache-upload
API Documentation
presign(options)
presign_url
- required, a URL to perform a presign request.token
- optional,X-CSRF-Token
value.
On success, this request will return:
url
- the URL to the/upload
API of your attache serveuuid
- a uuid stringexpiration
- a unix timestamp of a future timehmac
- theHMAC-SHA1
of yourSECRET_KEY
upload(options)
presignResponse
- required, response object passed in from presign request.fileObject
- required.
An object containing a uid andfile
property.
We use thisuid
as a reference to this object's XHR request, which can then be aborted at a later stage usingabortXHRRequest('uid')
uid: 'some_generated_uid' // String file: FILE // File object
onProgress
- optional,onProgress
function.
On success, this request will return:
path
- a unique path for the uploaded filecontent_type
geometry
bytes
{ console //=> 'Uploading foo.jpg @ 100%'}
getXHRRequests()
To access all existing XHR requests use getXHRRequests()
.
// {// 'some_uid': request(){},// 'some_other_request': request(){}// }
abortXHRRequest(String)
To abort an existing XHR requests use destroyXHRRequest()
passing in the id of the request.
// {// 'some_uid': request(){},// 'some_other_request': request(){}// } // {// 'some_other_request': request(){}// }
Error handling
Both presign
the upload
methods will return a custom error objects if either promise
is rejected.
The XHR requests for each method will return a custom responseStatus
error message if the response status is not between 200
and 300
.
This allows us to check for specific errors in our upload process.
The custom error objects look like this:
error: original error object message: 'Not Authorised' name: 'uploadRequest'
The 3 custom error names we cater for are:
presignRequest
- Rejected presignuploadRequest
- Rejected uploadresponseStatus
- A failing XHR response status
All other errors should be left to bubble up and logged to the console.
Example
uid
for your file object
Generate a Some files may have the same name, so it would be great if we had a unique way of identifying them and their XHR request.
Creating a uid
for your file object is as simple as:
/** * generateUniqueID * @param * @return */ { return + '_' + name} const myUID = //> hbswt489ts_image.jpg
Development
Build
Compile src
scripts to lib
:
$ npm run build
Tests
To run the test suite, first install the dependencies, then run npm test
:
$ npm install$ npm test