UNPKG

@yagisumi/groonga-http-client

Version:
136 lines (132 loc) 4.65 kB
import { parseCommand, TypeGuards } from '@yagisumi/groonga-command'; function chomp(str, rs) { return str.endsWith(rs) ? str.slice(0, -rs.length) : str; } function isArrayBuffer(obj) { return ArrayBuffer.isView(obj) || Object.prototype.toString.call(obj) === '[object ArrayBuffer]'; } function ERR(error) { return { error, value: null }; } function OK(value) { return { error: undefined, value }; } class GroongaError extends Error { constructor(message, response, returnCode) { super(message); this.response = response; this.returnCode = returnCode; } } function getResponseBody(response) { var _a; const data_type = typeof response; if (data_type === 'string' || isArrayBuffer(response)) { return OK(response); } let header = undefined; let body = undefined; let return_code = undefined; let error_message = undefined; if (Array.isArray(response)) { header = response[0]; body = response[1]; if (Array.isArray(header)) { return_code = header[0]; error_message = header[3]; } } else if (typeof response === 'object') { if (response != null) { header = response.header; body = response.body; return_code = header === null || header === void 0 ? void 0 : header.return_code; error_message = (_a = header === null || header === void 0 ? void 0 : header.error) === null || _a === void 0 ? void 0 : _a.message; } } if (header === undefined || return_code === undefined) { return ERR(new GroongaError('unexpected data type', response, return_code)); } if (return_code === 0) { return OK(body); } return ERR(new GroongaError(error_message !== null && error_message !== void 0 ? error_message : 'unexpected error', response, return_code)); } class GroongaHttpClient { constructor(axios, host) { this.axios = axios; this.host = host; } command(command, arg2, callback) { var _a; const opts = typeof arg2 === 'object' ? arg2 : undefined; const cb = typeof arg2 === 'function' ? arg2 : callback; if (cb == null) { return; } const cmd = parseCommand(command, opts); if (cmd === undefined) { cb(new Error("can't parse command"), null); return; } let response; if (TypeGuards.isLoad(cmd)) { const url = chomp(this.host, '/') + cmd.to_uri_format({ exclude: ['values'] }); let values = (_a = cmd.arguments['values']) !== null && _a !== void 0 ? _a : '[]'; if (typeof values !== 'string') { try { values = JSON.stringify(values); } catch (err) { cb(new Error('unexpected type of values'), null); } } const config = { headers: { 'Content-Type': 'application/json' } }; if (cmd.output_type === 'msgpack') { config.responseType = 'arraybuffer'; } response = this.axios.post(url, values, config); } else { const url = chomp(this.host, '/') + cmd.to_uri_format(); const config = {}; if (cmd.output_type === 'msgpack') { config.responseType = 'arraybuffer'; } response = this.axios.get(url, config); } response .then((res) => { const { error, value } = getResponseBody(res.data); cb(error, value); }) .catch((err) => { var _a; if (((_a = err === null || err === void 0 ? void 0 : err.response) === null || _a === void 0 ? void 0 : _a.data) != null) { const { error, value } = getResponseBody(err.response.data); cb(error, value); } else { cb(err, null); } }); } commandAsync(command, options) { return new Promise((resolve, reject) => { this.command(command, options || {}, (err, data) => { if (err) { reject(err); } else { resolve(data); } }); }); } } function createGroongaClient(axios, host) { return new GroongaHttpClient(axios, host); } const createClient = createGroongaClient; export { GroongaError, GroongaHttpClient, createClient, createGroongaClient }; //# sourceMappingURL=groonga-http-client.mjs.map