import _ from 'ramda' ; import is from 'is-type-of' ; import swaggerObject from './swaggerObject' ; const _desc = ( type , text ) => ( target , name , descriptor ) => { descriptor . value [ type ] = text ; swaggerObject . add ( target , name , { [ type ] : text } ) ; return descriptor ; } ; const _stripInvalidStructureFieldsFromBodyParams = ( parameters ) => { const keys = Object . keys ( parameters ) ; for ( const key of keys ) { delete parameters [ key ] . required ; } return parameters ; } ; const _params = ( type , parameters ) => ( target , name , descriptor ) => { if ( ! descriptor . value . parameters ) descriptor . value . parameters = { } ; descriptor . value . parameters [ type ] = parameters ; let swaggerParameters = parameters ; if ( type === 'body' ) { swaggerParameters = [ { name : 'data' , description : 'request body' , schema : { type : 'object' , required : Object . keys ( parameters ) . filter ( parameterName => parameters [ parameterName ] . required ) , properties : _stripInvalidStructureFieldsFromBodyParams ( parameters ) } } ] ; if ( swaggerParameters [ 0 ] . schema . required . length === 0 ) { delete swaggerParameters [ 0 ] . schema . required ; } } else { swaggerParameters = Object . keys ( swaggerParameters ) . map ( key => Object . assign ( { name : key } , swaggerParameters [ key ] ) ) ; } swaggerParameters . forEach ( ( item ) => { item . in = type ; } ) ; swaggerObject . add ( target , name , { [ type ] : swaggerParameters } ) ; return descriptor ; } ; const request = ( method , path ) => ( target , name , descriptor ) => { method = _ . toLower ( method ) ; descriptor . value . method = method ; descriptor . value . path = path ; swaggerObject . add ( target , name , { request : { method , path } , security : [ { ApiKeyAuth : [ ] } ] } ) ; return descriptor ; } ; const middlewares = ( middlewares ) => ( target , name , descriptor ) => { descriptor . value . middlewares = middlewares ; return descriptor ; } ; const security = ( security ) => ( target , name , descriptor ) => { swaggerObject . add ( target , name , { security } ) ; } ; const deprecated = ( target , name , descriptor ) => { descriptor . value . deprecated = true ; swaggerObject . add ( target , name , { deprecated : true } ) ; return descriptor ; } ; export interface IResponses { [ name ] : any ; } const defaultResp = { 200 : { description : 'success' } } ; const responses = ( responses = defaultResp ) => ( target , name , descriptor ) => { descriptor . value . responses = responses ; swaggerObject . add ( target , name , { responses } ) ; return descriptor ; } ; const desc = _ . curry ( _desc ) ; const description = desc ( 'description' ) ; const summary = desc ( 'summary' ) ; const tags = desc ( 'tags' ) ; const params = _ . curry ( _params ) ; const query = params ( 'query' ) ; const header = params ( 'header' ) ; const path = params ( 'path' ) ; const body = params ( 'body' ) ; const formData = params ( 'formData' ) ; const orderAll = ( weight ) => ( target ) => { swaggerObject . addMulti ( target , { order : weight } ) ; } ; const tagsAll = ( items ) => ( target ) => { const tags = is . array ( items ) ? items : [ items ] ; swaggerObject . addMulti ( target , { tags } ) ; } ; const responsesAll = ( responses = defaultResp ) => ( target ) => { swaggerObject . addMulti ( target , { responses } ) ; } ; const middlewaresAll = ( items ) => ( target ) => { const middlewares = is . array ( items ) ? items : [ items ] ; target . middlewares = middlewares ; } ; const securityAll = ( security ) => ( target ) => { const authentitactions = is . array ( security ) ? security : [ security ] ; swaggerObject . addMulti ( target , { security : authentitactions } ) ; } ; const deprecatedAll = ( target ) => { swaggerObject . addMulti ( target , { deprecated : true } ) ; } ; const prefix = ( prefix ) => ( target ) => { swaggerObject . addMulti ( target , { prefix } ) ; target . prefix = prefix ; } ; const queryAll = ( parameters , filters = [ 'ALL' ] ) => ( target ) => { if ( ! target . parameters ) target . parameters = { } ; target . parameters . query = parameters ; target . parameters . filters = filters ; const swaggerParameters = Object . keys ( parameters ) . map ( key => Object . assign ( { name : key } , parameters [ key ] ) ) ; swaggerParameters . forEach ( ( item ) => { item . in = 'query' ; } ) ; swaggerObject . addMulti ( target , { query : swaggerParameters } , filters ) ; } ; const Doc = { request , summary , params , desc , description , query , header , path , body , tags , middlewares , security , formData , responses , deprecated , orderAll , tagsAll , responsesAll , middlewaresAll , deprecatedAll , securityAll , queryAll , prefix } ; export default Doc ; export { request , summary , params , desc , description , query , header , path , body , tags , middlewares , security , formData , responses , deprecated , orderAll , tagsAll , responsesAll , middlewaresAll , securityAll , deprecatedAll , queryAll , prefix } ;