UNPKG

@line/bot-sdk

Version:
44 lines 1.61 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ensureJSON = ensureJSON; exports.createURLSearchParams = createURLSearchParams; exports.buildPath = buildPath; const exceptions_js_1 = require("./exceptions.js"); // Mirrors retrofit's PATH_TRAVERSAL: matches when any path segment is purely // "." / ".." (or their percent-encoded forms), after the parameters have been // substituted. See square/retrofit RequestBuilder.java. const PATH_TRAVERSAL = /^(?:.*\/)?(?:\.|%2e|%2E){1,2}(?:\/.*)?$/; function ensureJSON(raw) { if (typeof raw === "object") { return raw; } else { throw new exceptions_js_1.JSONParseError("Failed to parse response body as JSON", { raw }); } } function createURLSearchParams(params) { const searchParams = new URLSearchParams(); for (const [key, value] of Object.entries(params)) { if (value != null) { searchParams.append(key, String(value)); } } return searchParams; } function encodePathParam(value, name) { if (value === null || value === undefined) { throw new TypeError(`${name} is required`); } return encodeURIComponent(String(value)); } function buildPath(pathTemplate, params) { let path = pathTemplate; for (const [name, value] of Object.entries(params)) { path = path.split(`{${name}}`).join(encodePathParam(value, name)); } if (PATH_TRAVERSAL.test(path)) { throw new TypeError(`Path parameters shouldn't perform path traversal ('.' or '..'): ${path}`); } return path; } //# sourceMappingURL=utils.js.map