UNPKG

pddl-planning-service-client

Version:
142 lines 5.89 kB
/* -------------------------------------------------------------------------------------------- * Copyright (c) Jan Dolejsi 2022. All rights reserved. * Licensed under the MIT License. See License.txt in the project root for license information. * ------------------------------------------------------------------------------------------ */ 'use strict'; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.postJson = exports.postJsonAsString = exports.getJson = void 0; const http = __importStar(require("http")); const https = __importStar(require("https")); const HTTPS = "https:"; function get(url, callback) { return url.protocol === HTTPS ? https.get(url, callback) : http.get(url, callback); } function request(url, options, callback) { return url.protocol === HTTPS ? https.request(url, options, callback) : http.request(url, options, callback); } async function getJson(url) { return await new Promise((resolve, reject) => { get(url, res => { if (res.statusCode && res.statusCode >= 300) { reject(new Error(`Status code ${res.statusCode}, ${res.statusMessage} from ${url}`)); res.resume(); return; } const contentType = res.headers['content-type']; if (!contentType || !/^application\/json/.test(contentType)) { reject(new Error('Invalid content-type.\n' + `Expected application/json but received ${contentType} from ${url}`)); res.resume(); return; } res.setEncoding('utf8'); let rawData = ''; res.on('data', (chunk) => { rawData += chunk; }); res.on('end', () => { try { const parsedData = JSON.parse(rawData); // console.log(parsedData); resolve(parsedData); } catch (e) { console.error(e); reject(e); } }); }); }); } exports.getJson = getJson; function postJsonAsString(url, requestBody, options) { options.json = false; return postJson(url, requestBody, options); } exports.postJsonAsString = postJsonAsString; async function postJson(url, requestBody, options) { const requestData = JSON.stringify(requestBody); options.headers = options.headers ?? {}; options.headers['Content-Type'] = 'application/json'; options.headers['Content-Length'] = requestData.length; return await new Promise((resolve, reject) => { const from = options.serviceFriendlyName ?? url; options.method = 'POST'; const req = request(url, options, res => { if (res.statusCode && res.statusCode > 202) { let message; if (options.isAuthenticated && res.statusCode === 400) { message = `Authentication failed. Please login or update tokens. (${from})`; } else if (options.isAuthenticated && res.statusCode === 401) { message = `Invalid token. Please update tokens. (${from})`; } else { message = `${from} returned code ${res.statusCode} ${res.statusMessage}`; } reject(new Error(message)); res.resume(); return; } if (options.json) { const contentType = res.headers['content-type']; if (!contentType || !/^application\/json/.test(contentType)) { reject(new Error('Invalid content-type.\n' + `Expected application/json but received ${contentType} from ${url}`)); res.resume(); return; } } res.on('error', error => { reject(error); }); res.setEncoding(options.encoding ?? 'utf8'); let rawData = ''; res.on('data', (chunk) => { rawData += chunk; }); res.on('end', () => { if (options.json) { try { const parsedData = JSON.parse(rawData); options.verbose && console.log(parsedData); resolve(parsedData); } catch (e) { console.error(e); reject(e); } } else { resolve(rawData); } }); }); req.on('error', error => { reject(error); }); req.write(requestData); req.end(); }); } exports.postJson = postJson; //# sourceMappingURL=httpUtils.js.map