openapi-client-axios
Version:
JavaScript client library for consuming OpenAPI-enabled APIs with axios. Types included.
156 lines • 7.16 kB
JavaScript
;
var __values = (this && this.__values) || function(o) {
var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
if (m) return m.call(o);
if (o && typeof o.length === "number") return {
next: function () {
if (o && i >= o.length) o = void 0;
return { value: o && o[i++], done: !o };
}
};
throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.serializeQueryParameter = void 0;
/**
* Serializes a query parameter according to OpenAPI 3.0 specification
*
* @param param - The parameter definition from OpenAPI spec
* @param name - The parameter name
* @param value - The parameter value (can be primitive, array, or object)
* @returns Array of query string parts (key=value pairs)
*/
var serializeQueryParameter = function (param, name, value) {
// Get style and explode from parameter definition with defaults
// Per OpenAPI spec: default style for query is 'form', default explode is true
var style = (param === null || param === void 0 ? void 0 : param.style) || 'form';
var explode = (param === null || param === void 0 ? void 0 : param.explode) !== undefined ? param.explode : true;
// Handle null/undefined values
if (value === null || value === undefined) {
return [];
}
// Handle arrays
if (Array.isArray(value)) {
return serializeArrayParameter(name, value, style, explode);
}
// Handle objects
if (typeof value === 'object') {
return serializeObjectParameter(name, value, style, explode);
}
// Handle primitive values
return ["".concat(encodeURIComponent(name), "=").concat(encodeURIComponent(String(value)))];
};
exports.serializeQueryParameter = serializeQueryParameter;
/**
* Serializes an array query parameter
*/
var serializeArrayParameter = function (name, value, style, explode) {
if (value.length === 0) {
return [];
}
switch (style) {
case 'form':
if (explode) {
// form explode=true: id=3&id=4&id=5
return value.map(function (item) { return "".concat(encodeURIComponent(name), "=").concat(encodeURIComponent(String(item))); });
}
else {
// form explode=false: id=3,4,5
var encodedValues = value.map(function (item) { return encodeURIComponent(String(item)); }).join(',');
return ["".concat(encodeURIComponent(name), "=").concat(encodedValues)];
}
case 'spaceDelimited':
if (explode) {
// spaceDelimited with explode=true is not valid per spec, but fallback to form explode=true
return value.map(function (item) { return "".concat(encodeURIComponent(name), "=").concat(encodeURIComponent(String(item))); });
}
else {
// spaceDelimited explode=false: id=3%204%205
var encodedValues = value.map(function (item) { return encodeURIComponent(String(item)); }).join('%20');
return ["".concat(encodeURIComponent(name), "=").concat(encodedValues)];
}
case 'pipeDelimited':
if (explode) {
// pipeDelimited with explode=true is not valid per spec, but fallback to form explode=true
return value.map(function (item) { return "".concat(encodeURIComponent(name), "=").concat(encodeURIComponent(String(item))); });
}
else {
// pipeDelimited explode=false: id=3%7C4%7C5
var encodedValues = value.map(function (item) { return encodeURIComponent(String(item)); }).join('%7C');
return ["".concat(encodeURIComponent(name), "=").concat(encodedValues)];
}
default:
// Default to form explode=true
return value.map(function (item) { return "".concat(encodeURIComponent(name), "=").concat(encodeURIComponent(String(item))); });
}
};
/**
* Serializes an object query parameter
*/
var serializeObjectParameter = function (name, value, style, explode) {
var e_1, _a, e_2, _b;
var keys = Object.keys(value);
if (keys.length === 0) {
return [];
}
switch (style) {
case 'deepObject':
if (explode) {
// deepObject explode=true: filter[role]=admin&filter[firstName]=Alex
return keys.map(function (key) {
return "".concat(encodeURIComponent(name), "[").concat(encodeURIComponent(key), "]=").concat(encodeURIComponent(String(value[key])));
});
}
else {
// deepObject with explode=false is not valid per spec, but fallback to form
var pairs = [];
try {
for (var keys_1 = __values(keys), keys_1_1 = keys_1.next(); !keys_1_1.done; keys_1_1 = keys_1.next()) {
var key = keys_1_1.value;
pairs.push(encodeURIComponent(key));
pairs.push(encodeURIComponent(String(value[key])));
}
}
catch (e_1_1) { e_1 = { error: e_1_1 }; }
finally {
try {
if (keys_1_1 && !keys_1_1.done && (_a = keys_1.return)) _a.call(keys_1);
}
finally { if (e_1) throw e_1.error; }
}
return ["".concat(encodeURIComponent(name), "=").concat(pairs.join(','))];
}
case 'form':
if (explode) {
// form explode=true: role=admin&firstName=Alex (flattened)
return keys.map(function (key) {
return "".concat(encodeURIComponent(key), "=").concat(encodeURIComponent(String(value[key])));
});
}
else {
// form explode=false: filter=role,admin,firstName,Alex
var pairs = [];
try {
for (var keys_2 = __values(keys), keys_2_1 = keys_2.next(); !keys_2_1.done; keys_2_1 = keys_2.next()) {
var key = keys_2_1.value;
pairs.push(encodeURIComponent(key));
pairs.push(encodeURIComponent(String(value[key])));
}
}
catch (e_2_1) { e_2 = { error: e_2_1 }; }
finally {
try {
if (keys_2_1 && !keys_2_1.done && (_b = keys_2.return)) _b.call(keys_2);
}
finally { if (e_2) throw e_2.error; }
}
return ["".concat(encodeURIComponent(name), "=").concat(pairs.join(','))];
}
default:
// Default to form explode=true
return keys.map(function (key) {
return "".concat(encodeURIComponent(key), "=").concat(encodeURIComponent(String(value[key])));
});
}
};
//# sourceMappingURL=query-serializer.js.map