Files
elasticsearch-js/api/api/delete.js
2018-12-13 16:53:21 +01:00

164 lines
4.9 KiB
JavaScript

'use strict'
function buildDelete (opts) {
// eslint-disable-next-line no-unused-vars
const { makeRequest, ConfigurationError, result } = opts
/**
* Perform a [delete](http://www.elastic.co/guide/en/elasticsearch/reference/master/docs-delete.html) request
*
* @param {string} id - The document ID
* @param {string} index - The name of the index
* @param {string} type - The type of the document
* @param {string} wait_for_active_shards - Sets the number of shard copies that must be active before proceeding with the delete operation. Defaults to 1, meaning the primary shard only. Set to `all` for all shard copies, otherwise set to any non-negative value less than or equal to the total number of copies for the shard (number of replicas + 1)
* @param {string} parent - ID of parent document
* @param {enum} refresh - If `true` then refresh the effected shards to make this operation visible to search, if `wait_for` then wait for a refresh to make this operation visible to search, if `false` (the default) then do nothing with refreshes.
* @param {string} routing - Specific routing value
* @param {time} timeout - Explicit operation timeout
* @param {number} version - Explicit version number for concurrency control
* @param {enum} version_type - Specific version type
*/
return function _delete (params, options, callback) {
options = options || {}
if (typeof options === 'function') {
callback = options
options = {}
}
if (typeof params === 'function' || params == null) {
callback = params
params = {}
options = {}
}
// promises support
if (callback == null) {
return new Promise((resolve, reject) => {
_delete(params, options, (err, body) => {
err ? reject(err) : resolve(body)
})
})
}
// check required parameters
if (params['id'] == null) {
return callback(
new ConfigurationError('Missing required parameter: id'),
result
)
}
if (params['index'] == null) {
return callback(
new ConfigurationError('Missing required parameter: index'),
result
)
}
if (params['type'] == null) {
return callback(
new ConfigurationError('Missing required parameter: type'),
result
)
}
if (params.body != null) {
return callback(
new ConfigurationError('This API does not require a body'),
result
)
}
// check required url components
if (params['id'] != null && (params['type'] == null || params['index'] == null)) {
return callback(
new ConfigurationError('Missing required parameter of the url: type, index'),
result
)
} else if (params['type'] != null && (params['index'] == null)) {
return callback(
new ConfigurationError('Missing required parameter of the url: index'),
result
)
}
// build querystring object
const querystring = {}
const keys = Object.keys(params)
const acceptedQuerystring = [
'wait_for_active_shards',
'parent',
'refresh',
'routing',
'timeout',
'version',
'version_type',
'pretty',
'human',
'error_trace',
'source',
'filter_path'
]
const acceptedQuerystringCamelCased = [
'waitForActiveShards',
'parent',
'refresh',
'routing',
'timeout',
'version',
'versionType',
'pretty',
'human',
'errorTrace',
'source',
'filterPath'
]
for (var i = 0, len = keys.length; i < len; i++) {
var key = keys[i]
if (acceptedQuerystring.indexOf(key) !== -1) {
querystring[key] = params[key]
} else {
var camelIndex = acceptedQuerystringCamelCased.indexOf(key)
if (camelIndex !== -1) {
querystring[acceptedQuerystring[camelIndex]] = params[key]
}
}
}
// configure http method
var method = params.method
if (method == null) {
method = 'DELETE'
}
// validate headers object
if (params.headers != null && typeof params.headers !== 'object') {
return callback(
new ConfigurationError(`Headers should be an object, instead got: ${typeof params.headers}`),
result
)
}
var ignore = options.ignore || null
if (typeof ignore === 'number') {
ignore = [ignore]
}
// build request object
const parts = [params['index'], params['type'], params['id']]
const request = {
method,
path: '/' + parts.filter(Boolean).map(encodeURIComponent).join('/'),
body: '',
querystring
}
const requestOptions = {
ignore,
requestTimeout: options.requestTimeout || null,
maxRetries: options.maxRetries || null,
asStream: options.asStream || false,
headers: options.headers || null
}
return makeRequest(request, requestOptions, callback)
}
}
module.exports = buildDelete