Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
export function ContentType(type: string) {
return applyDecorators(
StoreMerge("produces", [type]),
UseAfter((request: any, response: any, next: any) => {
response.type(type);
next();
})
);
}
return (...args: DecoratorParameters) => {
const type = getDecoratorType(args, true);
switch (type) {
case "method":
return StoreMerge("operation", operation)(...args);
case "class":
decorateMethodsOf(args[0], Operation(operation));
break;
default:
throw new UnsupportedDecoratorType(Operation, args);
}
};
}
return (...decoratorArgs: DecoratorParameters): TypedPropertyDescriptor | void => {
switch (getDecoratorType(decoratorArgs, true)) {
case "method":
EndpointRegistry.useAfter(decoratorArgs[0], decoratorArgs[1]!, args);
return decoratorArgs[2] as any;
case "class":
StoreMerge("middlewares", {useAfter: args})(...decoratorArgs);
break;
default:
throw new UnsupportedDecoratorType(UseAfter, decoratorArgs);
}
};
}
return (...decoratorArgs: DecoratorParameters): TypedPropertyDescriptor | void => {
switch (getDecoratorType(decoratorArgs, true)) {
case "method":
EndpointRegistry.useBefore(decoratorArgs[0], decoratorArgs[1]!, args);
return decoratorArgs[2] as any;
case "class":
StoreMerge("middlewares", {useBefore: args})(...decoratorArgs);
break;
default:
throw new UnsupportedDecoratorType(UseBefore, decoratorArgs);
}
};
}
return (...parameters: any[]) => {
switch (getDecoratorType(parameters)) {
case "property":
return applyDecorators(Property(), StoreMerge(MONGOOSE_SCHEMA, options))(...parameters);
case "class":
StoreMerge(MONGOOSE_SCHEMA, createSchema(parameters[0], options as MongooseSchemaOptions))(...parameters);
break;
}
};
}
export function Status(code: number, options: IResponseOptions = {}) {
const response = mapReturnedResponse(options);
return applyDecorators(
StoreSet("statusCode", code),
StoreMerge("response", response),
StoreMerge("responses", {[code]: response}),
UseAfter((request: any, response: any, next: any) => {
if (response.statusCode === 200) {
response.status(code);
}
next();
})
);
}
return (...args: DecoratorParameters) => {
const type = getDecoratorType(args, true);
switch (type) {
case "method":
return StoreMerge("responses", {[status]: response})(...args);
case "class":
decorateMethodsOf(args[0], Responses(status, response));
break;
default:
throw new UnsupportedDecoratorType(Responses, args);
}
};
}
export function Required(...allowedRequiredValues: any[]): any {
return applyDecorators(
(...decoratorArgs: DecoratorParameters) => {
const metadata = getStorableMetadata(decoratorArgs);
if (!metadata) {
throw new UnsupportedDecoratorType(Required, decoratorArgs);
}
metadata.required = true;
if (allowedRequiredValues.length) {
Allow(...allowedRequiredValues)(...decoratorArgs);
}
},
StoreMerge("responses", {
"400": {
description: "BadRequest"
}
})
);
}
export function Status(code: number, options: IResponseOptions = {}) {
const response = mapReturnedResponse(options);
return applyDecorators(
StoreSet("statusCode", code),
StoreMerge("response", response),
StoreMerge("responses", {[code]: response}),
UseAfter((request: any, response: any, next: any) => {
if (response.statusCode === 200) {
response.status(code);
}
next();
})
);
}
export function Header(headerName: string | number | IHeadersOptions, headerValue?: string | number | IResponseHeader): Function {
if (headerValue !== undefined) {
headerName = {[headerName as string]: headerValue};
}
const headers: IResponseHeaders = mapHeaders(headerName as IHeadersOptions);
return applyDecorators(
StoreMerge("response", {headers}),
UseAfter((request: any, response: any, next: any) => {
Object.keys(headers).forEach(key => {
response.set(key, headers[key].value);
});
next();
})
);
}