import { Addl , anyOfKeep , anyOfReject , Arr , changeEnum , changeListToTuple , changeMaxItems , changeMinItems , changeRequiredStatus , changeSingleSchema , changeToConst , header , includeCodes , mapDefaultToCodes , MethodNames , methodParameter , oneOfKeep , oneOfReject , pathParameter , removeCodes , requestBody , responseBody , } from "openapi-refinements" ; export { Arr , Addl } from "openapi-refinements" ; import { JSONValue } from "json-schema-strictly-typed" ; import { isEqual } from "lodash" ; import { Traversal } from "monocle-ts" ; import { CodeAsInt , ISerializedRequest , IStateTransformer } from "./interfaces" ; import { OpenAPIObject , Reference , Responses , Schema , } from "./service/interfaces" ; const codeConvert = ( c ) : keyof Responses => `${ c }` as keyof Responses ; const withOrWithoutCodes = ( withOrWithout ) => ( codes , path ? , method ? ) => ( _ , o ) => ( withOrWithout ? includeCodes : removeCodes ) ( path !== undefined ? path : true , method !== undefined ? method : true , ( codes instanceof Array ? codes : [ codes ] ) . map ( codeConvert ) ) ( o ) ; const withCodes = withOrWithoutCodes ( true ) ; const withoutCodes = withOrWithoutCodes ( false ) ; const mapDefaultTo = ( codes , path ? , method ? ) => ( _ , o ) => mapDefaultToCodes ( path !== undefined ? path : true , method !== undefined ? method : true , ( codes instanceof Array ? codes : [ codes ] ) . map ( codeConvert ) ) ( o ) ; interface ISchemaAddress { lens ? : Array < string | number | typeof Arr | typeof Addl > ; } interface IResponseBodyOptions extends ISchemaAddress { path : string | RegExp | boolean ; method : MethodNames | MethodNames [ ] | boolean ; code : CodeAsInt | keyof Responses | Array < CodeAsInt | keyof Responses > | boolean ; mediaTypes : boolean | string [ ] ; } interface IRequestBodyOptions extends ISchemaAddress { path : string | RegExp | boolean ; method : MethodNames | MethodNames [ ] | boolean ; mediaTypes : boolean | string [ ] ; } interface IPathParameterOptions extends ISchemaAddress { path : string | RegExp | boolean ; name : string | boolean ; in : string | boolean ; } interface IMethodParameterOptions extends ISchemaAddress { path : string | RegExp | boolean ; method : MethodNames | MethodNames [ ] | boolean ; name : string | boolean ; in : string | boolean ; } interface IHeaderOptions extends ISchemaAddress { path : string | RegExp | boolean ; method : MethodNames | MethodNames [ ] | boolean ; code : CodeAsInt | keyof Responses | Array < CodeAsInt | keyof Responses > | boolean ; name : string | boolean ; } type ResponseBodySignature = [ string | boolean | RegExp , boolean | MethodNames | MethodNames [ ] , boolean | Array < keyof Responses > , boolean | string [ ] ] ; type RequestBodySignature = [ string | boolean | RegExp , boolean | MethodNames | MethodNames [ ] , boolean | string [ ] ] ; type PathParameterSignature = [ string | boolean | RegExp , string | boolean , string | boolean ] ; type MethodParameterSignature = [ string | RegExp | boolean , MethodNames | MethodNames [ ] | boolean , string | boolean , string | boolean ] ; type HeaderSignature = [ string | RegExp | boolean , MethodNames | MethodNames [ ] | boolean , boolean | Array < keyof Responses > , string | boolean ] ; type TraversalFunction < T extends ISchemaAddress > = ( options ? : T ) => ( o : OpenAPIObject ) => Traversal < OpenAPIObject , Schema | Reference > ; const pathParameterSignatureFromOptions = ( options ? ) : PathParameterSignature => [ options && options . path !== undefined ? options . path : true , options && options . name !== undefined ? options . name : true , options && options . in !== undefined ? options . in : true , ] ; const methodParameterSignatureFromOptions = ( options ? ) : MethodParameterSignature => [ options && options . path !== undefined ? options . path : true , options && options . method !== undefined ? options . method : true , options && options . name !== undefined ? options . name : true , options && options . in !== undefined ? options . in : true , ] ; const headerSignatureFromOptions = ( options ? ) : HeaderSignature => [ options && options . path !== undefined ? options . path : true , options && options . method !== undefined ? options . method : true , options && options . code !== undefined ? typeof options . code === "boolean" ? options . code : ( options . code instanceof Array ? options . code : [ options . code ] ) . map ( codeConvert ) : true , options && options . name !== undefined ? options . name : true , ] ; const responseBodySignatureFromOptions = ( options ? ) : ResponseBodySignature => [ options && options . path !== undefined ? options . path : true , options && options . method !== undefined ? options . method : true , options && options . code !== undefined ? typeof options . code === "boolean" ? options . code : ( options . code instanceof Array ? options . code : [ options . code ] ) . map ( codeConvert ) : true , options && options . mediaTypes !== undefined ? typeof options . mediaTypes === "string" ? [ options . mediaTypes ] : options . mediaTypes : true , ] ; const requestBodySignatureFromOptions = ( options ? ) : RequestBodySignature => [ options && options . path !== undefined ? options . path : true , options && options . method !== undefined ? options . method : true , options && options . mediaTypes !== undefined ? typeof options . mediaTypes === "string" ? [ options . mediaTypes ] : options . mediaTypes : true , ] ; type CurriedTraversal = ( traversal : ( o : OpenAPIObject ) => Traversal < OpenAPIObject , Schema | Reference > , path : Array < string | number | typeof Arr | typeof Addl > ) => ( o : OpenAPIObject ) => OpenAPIObject ; const expandCurriedTraversal = < T extends ISchemaAddress > ( c , options ? ) => ( tf ) => ( _ , o ) => c ( tf ( options ) , options && options . lens ? options . lens : [ ] ) ( o ) ; const makeSchemaTraversalStructure = < T extends ISchemaAddress > ( tf ) => ( options ? ) => ( { const : ( j ) => expandCurriedTraversal ( changeToConst ( j ) , options ) ( tf ) , minItems : ( i ) => expandCurriedTraversal ( changeMinItems ( i ) , options ) ( tf ) , maxItems : ( i ) => expandCurriedTraversal ( changeMaxItems ( i ) , options ) ( tf ) , required : ( prop ) => expandCurriedTraversal ( changeRequiredStatus ( prop ) , options ) ( tf ) , enumKeep : ( vals ) => expandCurriedTraversal ( changeEnum ( vals , true ) , options ) ( tf ) , enumReject : ( vals ) => expandCurriedTraversal ( changeEnum ( vals , false ) , options ) ( tf ) , anyOfKeep : ( indices ) => expandCurriedTraversal ( anyOfKeep ( indices ) , options ) ( tf ) , anyOfReject : ( indices ) => expandCurriedTraversal ( anyOfReject ( indices ) , options ) ( tf ) , oneOfKeep : ( indices ) => expandCurriedTraversal ( oneOfKeep ( indices ) , options ) ( tf ) , oneOfReject : ( indices ) => expandCurriedTraversal ( oneOfReject ( indices ) , options ) ( tf ) , listToTuple : ( i ) => expandCurriedTraversal ( changeListToTuple ( i ) , options ) ( tf ) , schema : ( schemaOrFunction ) => expandCurriedTraversal ( changeSingleSchema ( typeof schemaOrFunction === "function" ? schemaOrFunction : ( _ ) => ( __ ) => schemaOrFunction ) , options ) ( tf ) , } ) ; export const transform = { compose : ( ... transformers ) => ( req , o ) => transformers . reduce ( ( a , b ) => b ( req , a ) , o ) , noopThrows : ( f ) => ( req , o ) : OpenAPIObject => { const out = f ( req , o ) ; if ( isEqual ( out , o ) ) { throw Error ( "Array item setting did not work" ) ; } return out ; } , times : ( n ) => ( f ) => { let counter = 0 ; return ( req , o ) : OpenAPIObject => { const out = f ( req , o ) ; if ( ! isEqual ( out , o ) ) { counter += 1 ; } if ( counter > n ) { return o ; } return out ; } ; } , after : ( n ) => ( f ) => { let counter = 0 ; return ( req , o ) : OpenAPIObject => { const out = f ( req , o ) ; if ( ! isEqual ( out , o ) ) { counter += 1 ; } if ( counter <= n ) { return o ; } return out ; } ; } , withCodes , withoutCodes , mapDefaultTo , responseBody : makeSchemaTraversalStructure < Partial < IResponseBodyOptions > > ( ( options ? ) => responseBody ( ... responseBodySignatureFromOptions ( options ) ) ) , requestBody : makeSchemaTraversalStructure < Partial < IRequestBodyOptions > > ( ( options ? ) => requestBody ( ... requestBodySignatureFromOptions ( options ) ) ) , pathParameter : makeSchemaTraversalStructure < Partial < IPathParameterOptions > > ( ( options ? ) => pathParameter ( ... pathParameterSignatureFromOptions ( options ) ) ) , methodParameter : makeSchemaTraversalStructure < Partial < IMethodParameterOptions > > ( ( options ? ) => methodParameter ( ... methodParameterSignatureFromOptions ( options ) ) ) , header : makeSchemaTraversalStructure < Partial < IHeaderOptions > > ( ( options ? ) => header ( ... headerSignatureFromOptions ( options ) ) ) , } ;