@ohm-vision/kiwi-tequila-api
Version:
Unofficial wrapper for Kiwi (tequila) API
155 lines (154 loc) • 7.91 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.SearchApi = void 0;
const axios_1 = __importDefault(require("axios"));
const utils_1 = require("../utils");
const utils_2 = require("../utils");
class SearchApi {
constructor(config) {
this.config = config;
}
/**
* A single flights search. Please note, that only active
* parameters have been described in this document. Parameters
* that are not described are deprecated.
*
* @param params
*/
singlecity(dto) {
var _a;
return __awaiter(this, void 0, void 0, function* () {
if (dto.fly_from)
dto.fly_from = (0, utils_2.serializeFlyLocations)(dto.fly_from).join(",");
if (dto.fly_to)
dto.fly_to = (0, utils_2.serializeFlyLocations)(dto.fly_to).join(",");
if (dto.adult_hold_bag)
dto.adult_hold_bag = (0, utils_2.serializeHoldBags)(dto.adult_hold_bag);
if (dto.adult_hand_bag)
dto.adult_hand_bag = (0, utils_2.serializeHandBags)(dto.adult_hand_bag);
if (dto.child_hold_bag)
dto.child_hold_bag = (0, utils_2.serializeHoldBags)(dto.child_hold_bag);
if (dto.child_hand_bag)
dto.child_hand_bag = (0, utils_2.serializeHandBags)(dto.child_hand_bag);
if (typeof dto.conn_on_diff_airport === "boolean")
dto.conn_on_diff_airport = (0, utils_1.serializeNumericBoolean)(dto.conn_on_diff_airport);
if (typeof dto.ret_from_diff_airport === "boolean")
dto.ret_from_diff_airport = (0, utils_1.serializeNumericBoolean)(dto.ret_from_diff_airport);
if (typeof dto.ret_to_diff_airport === "boolean")
dto.ret_to_diff_airport = (0, utils_1.serializeNumericBoolean)(dto.ret_to_diff_airport);
if (dto.select_airlines)
dto.select_airlines = (0, utils_1.serializeStrArray)(dto.select_airlines);
if (dto.select_stop_airport)
dto.select_stop_airport = (0, utils_1.serializeStrArray)(dto.select_stop_airport);
if (typeof dto.asc === "boolean")
dto.asc = (0, utils_1.serializeNumericBoolean)(dto.asc);
const { data } = yield axios_1.default.get((0, utils_1.buildUrl)("search", dto), this.config);
(_a = data.data) === null || _a === void 0 ? void 0 : _a.forEach((v, i) => {
formatResponseDates(v);
if (Array.isArray(v.route)) {
v.route.forEach(r => {
formatResponseDates(r);
});
}
});
return data;
});
}
/**
* This call is able to do multi-city searches in parallel. The single
* search queries don't have to be connected in any way.
* * It accepts the same parameters as the /search API call. These parameters
* are to be used in the body of the request not in the URL.
* * To do a request, send a POST json payload with a list of API parameters
* for each search query.
* * The maximum number of segments in each request is 9 (If a higher number
* of routes is requested in /flights_multi POST method, all that they receive
* is an empty result - []).
* * In the response root, the results field contains a list of responses for
* the posted queries. Every single element contains the same response as it
* would if /search API call were used.
* @param body
* @param params
* @returns
*/
multicity(dto, params) {
var _a;
return __awaiter(this, void 0, void 0, function* () {
dto.requests.forEach(v => {
if (v.dateFrom instanceof Date)
v.dateFrom = (0, utils_1.formatDate)(v.dateFrom);
if (v.dateTo instanceof Date)
v.dateTo = (0, utils_1.formatDate)(v.dateTo);
});
const { data } = yield axios_1.default.post((0, utils_1.buildUrl)("multicity", params), dto, this.config);
(_a = data.route) === null || _a === void 0 ? void 0 : _a.forEach(v => {
formatResponseDates(v);
if (Array.isArray(v.route)) {
v.route.forEach(r => {
formatResponseDates(r);
});
}
});
return data;
});
}
nomad(dto, params) {
var _a, _b;
return __awaiter(this, void 0, void 0, function* () {
if (typeof (params === null || params === void 0 ? void 0 : params.xml) === "boolean")
params.xml = (0, utils_1.serializeNumericBoolean)(params.xml);
if (typeof (params === null || params === void 0 ? void 0 : params.asc) === "boolean")
params.asc = (0, utils_1.serializeNumericBoolean)(params.asc);
if (Array.isArray(params === null || params === void 0 ? void 0 : params.select_airlines))
params.select_airlines = (0, utils_1.serializeStrArray)(params.select_airlines);
if (params.fly_from)
params.fly_from = (0, utils_2.serializeFlyLocations)(params.fly_from).join(",");
if (params.fly_to)
params.fly_to = (0, utils_2.serializeFlyLocations)(params.fly_to).join(",");
if (typeof params.conn_on_diff_airport === "boolean")
params.conn_on_diff_airport = (0, utils_1.serializeNumericBoolean)(params.conn_on_diff_airport);
(_a = dto.via) === null || _a === void 0 ? void 0 : _a.forEach(v => {
if (v.locations)
v.locations = (0, utils_2.serializeFlyLocations)(v.locations);
if (v.date_range)
v.date_range = [
v.date_range[0] instanceof Date ? (0, utils_1.formatDate)(v.date_range[0]) : v.date_range[0],
v.date_range[1] instanceof Date ? (0, utils_1.formatDate)(v.date_range[1]) : v.date_range[1],
];
});
const { data } = yield axios_1.default.post((0, utils_1.buildUrl)("nomad", params), dto, this.config);
(_b = data.route) === null || _b === void 0 ? void 0 : _b.forEach(v => {
formatResponseDates(v);
if (Array.isArray(v.route)) {
v.route.forEach(r => {
formatResponseDates(r);
});
}
});
return data;
});
}
}
exports.SearchApi = SearchApi;
function formatResponseDates(obj) {
if (typeof obj.local_arrival === "string")
obj.local_arrival = new Date(obj.local_arrival);
if (typeof obj.local_departure === "string")
obj.local_departure = new Date(obj.local_departure);
if (typeof obj.utc_arrival === "string")
obj.utc_arrival = new Date(obj.utc_arrival);
if (typeof obj.utc_departure === "string")
obj.utc_departure = new Date(obj.utc_departure);
}