@opra/common
Version:
Opra common package
127 lines (126 loc) • 5.28 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.HttpOperation = void 0;
const tslib_1 = require("tslib");
const node_path_1 = tslib_1.__importDefault(require("node:path"));
const objects_1 = require("@jsopen/objects");
const ts_gems_1 = require("ts-gems");
const index_js_1 = require("../../helpers/index.js");
const index_js_2 = require("../../schema/index.js");
const data_type_map_js_1 = require("../common/data-type-map.js");
const document_element_js_1 = require("../common/document-element.js");
const constants_js_1 = require("../constants.js");
const http_operation_decorator_js_1 = require("../decorators/http-operation.decorator.js");
/**
* HttpOperation
*/
exports.HttpOperation = function (...args) {
// Decorator
if (!this) {
const [options] = args;
const decoratorChain = [];
return exports.HttpOperation[constants_js_1.DECORATOR].call(undefined, decoratorChain, options);
}
// Constructor
const [resource, initArgs] = args;
document_element_js_1.DocumentElement.call(this, resource);
if (!constants_js_1.CLASS_NAME_PATTERN.test(initArgs.name))
throw new TypeError(`Invalid operation name (${initArgs.name})`);
const _this = (0, ts_gems_1.asMutable)(this);
_this.parameters = [];
_this.responses = [];
_this.types = _this.node[constants_js_1.kDataTypeMap] = new data_type_map_js_1.DataTypeMap();
_this.name = initArgs.name;
_this.path = initArgs.path;
_this.mergePath = initArgs.mergePath;
_this.method = initArgs.method || 'GET';
_this.description = initArgs.description;
_this.composition = initArgs.composition;
_this.compositionOptions = initArgs.compositionOptions
? (0, index_js_1.cloneObject)(initArgs.compositionOptions)
: undefined;
};
/**
* @class HttpOperation
*/
class HttpOperationClass extends document_element_js_1.DocumentElement {
findParameter(paramName, location) {
const paramNameLower = paramName.toLowerCase();
let prm;
for (prm of this.parameters) {
if (location && location !== prm.location)
continue;
if (typeof prm.name === 'string') {
prm._nameLower = prm._nameLower || prm.name.toLowerCase();
if (prm._nameLower === paramNameLower)
return prm;
}
if (prm.name instanceof RegExp && prm.name.test(paramName))
return prm;
}
}
getFullUrl() {
const out = this.owner.getFullUrl();
if (out) {
if (this.path) {
if (this.mergePath)
return out + this.path;
return node_path_1.default.posix.join(out, this.path);
}
return out;
}
return this.path || '/';
}
toJSON(options) {
const out = (0, objects_1.omitUndefined)({
kind: index_js_2.OpraSchema.HttpOperation.Kind,
description: this.description,
method: this.method,
path: this.path,
mergePath: this.mergePath,
composition: this.composition,
requestBody: this.requestBody?.toJSON(options),
});
if (this.types.size) {
out.types = {};
for (const v of this.types.values()) {
out.types[v.name] = v.toJSON(options);
}
}
if (this.parameters.length) {
out.parameters = [];
for (const prm of this.parameters) {
out.parameters.push(prm.toJSON(options));
}
}
if (this.responses.length)
out.responses = this.responses.map(r => r.toJSON(options));
return out;
}
}
exports.HttpOperation.prototype = HttpOperationClass.prototype;
exports.HttpOperation[constants_js_1.DECORATOR] = http_operation_decorator_js_1.HttpOperationDecoratorFactory;
exports.HttpOperation.GET = function (options) {
return (0, http_operation_decorator_js_1.HttpOperationDecoratorFactory)([], { ...options, method: 'GET' });
};
exports.HttpOperation.DELETE = function (options) {
return (0, http_operation_decorator_js_1.HttpOperationDecoratorFactory)([], { ...options, method: 'DELETE' });
};
exports.HttpOperation.HEAD = function (options) {
return (0, http_operation_decorator_js_1.HttpOperationDecoratorFactory)([], { ...options, method: 'HEAD' });
};
exports.HttpOperation.OPTIONS = function (options) {
return (0, http_operation_decorator_js_1.HttpOperationDecoratorFactory)([], { ...options, method: 'OPTIONS' });
};
exports.HttpOperation.PATCH = function (options) {
return (0, http_operation_decorator_js_1.HttpOperationDecoratorFactory)([], { ...options, method: 'PATCH' });
};
exports.HttpOperation.POST = function (options) {
return (0, http_operation_decorator_js_1.HttpOperationDecoratorFactory)([], { ...options, method: 'POST' });
};
exports.HttpOperation.PUT = function (options) {
return (0, http_operation_decorator_js_1.HttpOperationDecoratorFactory)([], { ...options, method: 'PUT' });
};
exports.HttpOperation.SEARCH = function (options) {
return (0, http_operation_decorator_js_1.HttpOperationDecoratorFactory)([], { ...options, method: 'SEARCH' });
};