Utility that provides a single API for loading the content of a path/URL.
- path-loader
- .load(location, [options]) ⇒
Promise.<*>
- .LoadOptions :
object
- .PrepareRequestCallback :
function
- .ProcessResponseCallback ⇒
*
- .load(location, [options]) ⇒
Loads a document at the provided location and returns a JavaScript object representation.
Kind: static method of path-loader
Returns: Promise.<*>
- Always returns a promise even if there is a callback provided
Param | Type | Description |
---|---|---|
location | string |
The location to the document |
[options] | LoadOptions |
The loader options |
Example
// Example using Promises
PathLoader
.load('./package.json')
.then(JSON.parse)
.then(function (document) {
console.log(document.name + ' (' + document.version + '): ' + document.description);
}, function (err) {
console.error(err.stack);
});
Example
// Example using options.prepareRequest to provide authentication details for a remotely secure URL
PathLoader
.load('https://api.github.com/repos/whitlockjc/path-loader', {
prepareRequest: function (req, callback) {
req.auth('my-username', 'my-password');
callback(undefined, req);
}
})
.then(JSON.parse)
.then(function (document) {
console.log(document.full_name + ': ' + document.description);
}, function (err) {
console.error(err.stack);
});
Example
// Example loading a YAML file
PathLoader
.load('/Users/not-you/projects/path-loader/.travis.yml')
.then(YAML.safeLoad)
.then(function (document) {
console.log('path-loader uses the', document.language, 'language.');
}, function (err) {
console.error(err.stack);
});
Example
// Example loading a YAML file with options.processContent (Useful if you need information in the raw response)
PathLoader
.load('/Users/not-you/projects/path-loader/.travis.yml', {
processContent: function (res, callback) {
callback(YAML.safeLoad(res.text));
}
})
.then(function (document) {
console.log('path-loader uses the', document.language, 'language.');
}, function (err) {
console.error(err.stack);
});
Options used when loading a path.
Kind: static typedef of path-loader
Properties
Name | Type | Default | Description |
---|---|---|---|
[encoding] | string |
"'utf-8'" |
The encoding to use when loading the file (File loader only) |
[method] | string |
"get" |
The HTTP method to use for the request (HTTP loader only) |
[prepareRequest] | PrepareRequestCallback |
The callback used to prepare the request (HTTP loader only) | |
[processContent] | ProcessResponseCallback |
The callback used to process the response |
Callback used to provide access to altering a remote request prior to the request being made.
Kind: static typedef of path-loader
Param | Type | Description |
---|---|---|
req | object |
The Superagent request object |
location | string |
The location being retrieved |
callback | function |
First callback |
Callback used to provide access to processing the raw response of the request being made. (HTTP loader only)
Kind: static typedef of path-loader
Returns: *
- the result of processing the responses
Param | Type | Description |
---|---|---|
res | object |
The Superagent response object (For non-HTTP loaders, this object will be like the Superagent object in that it will have a text property whose value is the raw string value being processed. This was done for consistency. There will also be a location property containing the location of the path being loaded.) |
callback | function |
Error-first callback |