@opra/common
Version:
Opra common package
201 lines (200 loc) • 7.39 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.HttpOperationDecoratorFactory = HttpOperationDecoratorFactory;
const index_js_1 = require("../../enums/index.js");
const index_js_2 = require("../../schema/index.js");
const constants_js_1 = require("../constants.js");
function HttpOperationDecoratorFactory(decoratorChain, options) {
/**
*
*/
const decorator = ((target, propertyKey) => {
if (typeof propertyKey !== 'string')
throw new TypeError(`Symbol properties can not be decorated`);
const operationMetadata = {
...options,
kind: index_js_2.OpraSchema.HttpOperation.Kind,
};
const controllerMetadata = (Reflect.getOwnMetadata(constants_js_1.HTTP_CONTROLLER_METADATA, target.constructor) || {});
controllerMetadata.operations = controllerMetadata.operations || {};
controllerMetadata.operations[propertyKey] = operationMetadata;
for (const fn of decoratorChain)
fn(operationMetadata);
Reflect.defineMetadata(constants_js_1.HTTP_CONTROLLER_METADATA, controllerMetadata, target.constructor);
});
/**
*
*/
decorator.Cookie = (name, arg1) => {
decoratorChain.push((meta) => {
const paramMeta = typeof arg1 === 'string' || typeof arg1 === 'function'
? {
name,
location: 'cookie',
type: arg1,
}
: { ...arg1, name, location: 'cookie' };
if (meta.parameters) {
meta.parameters = meta.parameters.filter(p => !(p.location === 'cookie' && String(p.name) === String(name)));
}
else
meta.parameters = [];
meta.parameters.push(paramMeta);
});
return decorator;
};
/**
*
*/
decorator.Header = (name, arg1) => {
decoratorChain.push((meta) => {
const paramMeta = typeof arg1 === 'string' || typeof arg1 === 'function'
? {
name,
location: 'header',
type: arg1,
}
: { ...arg1, name, location: 'header' };
if (meta.parameters) {
meta.parameters = meta.parameters.filter(p => !(p.location === 'header' && String(p.name) === String(name)));
}
else
meta.parameters = [];
meta.parameters.push(paramMeta);
});
return decorator;
};
/**
*
*/
decorator.QueryParam = (name, arg1) => {
decoratorChain.push((meta) => {
const paramMeta = typeof arg1 === 'string' || typeof arg1 === 'function'
? {
name,
location: 'query',
type: arg1,
}
: { ...arg1, name, location: 'query' };
if (meta.parameters) {
meta.parameters = meta.parameters.filter(p => !(p.location === 'query' && String(p.name) === String(name)));
}
else
meta.parameters = [];
meta.parameters.push(paramMeta);
});
return decorator;
};
/**
*
*/
decorator.PathParam = (name, arg1) => {
decoratorChain.push((meta) => {
const paramMeta = typeof arg1 === 'string' || typeof arg1 === 'function'
? {
name,
location: 'path',
type: arg1,
}
: { ...arg1, name, location: 'path' };
if (meta.parameters) {
meta.parameters = meta.parameters.filter(p => !(p.location === 'path' && String(p.name) === String(name)));
}
else
meta.parameters = [];
meta.parameters.push(paramMeta);
});
return decorator;
};
/**
*
*/
decorator.Response = (statusCode, responseOptions) => {
const responseMeta = {
...responseOptions,
statusCode,
};
if (responseMeta.type) {
responseMeta.contentType =
responseMeta.contentType || index_js_1.MimeTypes.opra_response_json;
responseMeta.contentEncoding = responseMeta.contentEncoding || 'utf-8';
}
if (responseMeta.contentType === index_js_1.MimeTypes.opra_response_json) {
responseMeta.contentEncoding = responseMeta.contentEncoding || 'utf-8';
}
decoratorChain.push((meta) => {
meta.responses = meta.responses || [];
meta.responses.push(responseMeta);
});
return decorator;
};
decorator.RequestContent = function (arg0) {
const contentMeta = typeof arg0 === 'object' ? arg0 : { type: arg0 };
if (contentMeta.type) {
contentMeta.contentType = contentMeta.contentType || index_js_1.MimeTypes.json;
contentMeta.contentEncoding = contentMeta.contentEncoding || 'utf-8';
}
decoratorChain.push((operationMetadata) => {
operationMetadata.requestBody = operationMetadata.requestBody || {
required: true,
content: [],
};
operationMetadata.requestBody.content =
operationMetadata.requestBody.content || [];
operationMetadata.requestBody.content.push(contentMeta);
});
return decorator;
};
decorator.MultipartContent = function (contentOpts, subInit) {
const contentMetadata = {
...contentOpts,
contentType: contentOpts?.contentType || 'multipart/form-data',
};
decoratorChain.push((operationMetadata) => {
operationMetadata.requestBody = operationMetadata.requestBody || {
required: true,
content: [],
};
operationMetadata.requestBody.content =
operationMetadata.requestBody.content || [];
operationMetadata.requestBody.content.push(contentMetadata);
});
if (subInit) {
const configScope = {
Field(fieldName, opts) {
contentMetadata.multipartFields =
contentMetadata.multipartFields || [];
contentMetadata.multipartFields.push({
fieldName,
fieldType: 'field',
...opts,
});
return configScope;
},
File(fieldName, opts) {
contentMetadata.multipartFields =
contentMetadata.multipartFields || [];
contentMetadata.multipartFields.push({
fieldName,
fieldType: 'file',
...opts,
});
return configScope;
},
};
subInit(configScope);
}
return decorator;
};
/**
*
*/
decorator.UseType = (...type) => {
decoratorChain.push((meta) => {
meta.types = meta.types || [];
meta.types.push(...type);
});
return decorator;
};
return decorator;
}