1.0.7 • Published 8 years ago

bureaucracy v1.0.7

Weekly downloads
132
License
MIT
Repository
github
Last release
8 years ago

bureaucracy

Pull files from hidden file inputs into text inputs with ease

If you want the hidden file inputs part, just absolutely position the file input behind a button skin. Many articles on the internet explain how to do this.

install

npm i bureaucracy -S

create(options?)

Creates a bureaucrat object that can submit files to an HTTP API endpoint. It takes a few options.

OptionDescription
validateFunction that receives a File object and should indicate whether that file is valid
methodHTTP method to use when posting the files. Defaults to PUT
endpointHTTP endpoint to post the files to. Defaults to /api/files
fieldKeyString setting file upload field key. Defaults to uploads
formDataObject containing additional form parameters. Defaults to an empty object: {}

There are "common" validate functions for your convenience. These can be referenced by name on the validate option

ValidatorDescription
'image'Files are expected to have a MIME type of image/gif, image/png, image/jpg, or image/jpeg

bureaucrat.submit(files)

The files parameter expects an array of File or a FileList, like the ones you can pull from input.files, where input is an input element of type file.

The files are uploaded using a default uploads field key, but this can be customized using the fieldKey options parameter. Once your API has handled the uploaded files, a JSON response should be returned. The only requirement here is a results property that's an array describing the success of each file upload.

{
  "results": [
    "anything",
    { "can": "be_used"},
    ["as", "a", "result"]
  ]
}

This data can be useful for the front-end to react to successful file uploads on the success event.

var bureaucrat = create();
bureaucrat.on('success', uploadSuccess);
bureaucrat.submit(uploadInput.files);

function uploadSuccess (results) {
  console.log('so many files were uploaded!');
}

The submission process emits a variety of events on the bureaucrat object, using the contra emitter API.

EventArgumentsDescription
startedrawFilesA submission attempt has started for rawFiles
invalidallFilesSubmitted files were invalid, a request won't be made
validvalidFilesAt least one submitted file was valid, a request will be made
errorerrAn error err occurred during the HTTP API request
successresults, bodyThe file API call succeeded and yielded some results
endederr, results, bodyThe HTTP API request process ended, emitted after both error and success

setup(fileinput, options?)

Reacts to change events on a file input by making a PUT /api/files request with the valid user-selected files as soon as the user is finished choosing their files.

license

mit

1.0.7

8 years ago

1.0.6

8 years ago

1.0.5

8 years ago

1.0.4

8 years ago

1.0.3

8 years ago

1.0.2

8 years ago

1.0.1

8 years ago

1.0.0

8 years ago