@@ -23,57 +23,101 @@ fun <T : OpenAPIRoute<T>> T.route(path: String): T {
2323 }
2424}
2525
26+ /* *
27+ * Creates a new route matching the specified [path]
28+ */
2629@ContextDsl
27- inline fun <T : OpenAPIRoute <T >> T .route (path : String , crossinline fn : T .() -> Unit ) {
30+ inline fun <TRoute : OpenAPIRoute <TRoute >> TRoute .route (path : String , crossinline fn : TRoute .() -> Unit ) {
2831 route(path).fn()
2932}
3033
31- fun <T : OpenAPIRoute <T >> T .method (method : HttpMethod ): T {
34+ fun <TRoute : OpenAPIRoute <TRoute >> TRoute .method (method : HttpMethod ): TRoute {
3235 return child(ktorRoute.createChild(HttpMethodRouteSelector (method))).apply {
3336 provider.registerModule(HttpMethodProviderModule (method))
3437 }
3538}
3639
40+ /* *
41+ * Creates a new route matching the specified [method]
42+ */
3743@ContextDsl
38- inline fun <T : OpenAPIRoute <T >> T .method (method : HttpMethod , crossinline fn : T .() -> Unit ) {
44+ inline fun <TRoute : OpenAPIRoute <TRoute >> TRoute .method (method : HttpMethod , crossinline fn : TRoute .() -> Unit ) {
3945 method(method).fn()
4046}
4147
42- fun <T : OpenAPIRoute <T >> T .provider (vararg content : ContentTypeProvider ): T {
48+ fun <TRoute : OpenAPIRoute <TRoute >> TRoute .provider (vararg content : ContentTypeProvider ): TRoute {
4349 return child().apply {
4450 content.forEach {
4551 provider.registerModule(it)
4652 }
4753 }
4854}
4955
56+ /* *
57+ * Creates a new route matching the specified [content]
58+ */
5059@ContextDsl
51- inline fun <T : OpenAPIRoute <T >> T .provider (vararg content : ContentTypeProvider , crossinline fn : T .() -> Unit ) {
60+ inline fun <TRoute : OpenAPIRoute <TRoute >> TRoute .provider (vararg content : ContentTypeProvider , crossinline fn : TRoute .() -> Unit ) {
5261 provider(* content).fn()
5362}
5463
55-
56- fun <T : OpenAPIRoute <T >> T.tag (tag : APITag ): T {
64+ /* *
65+ * Applies a tag to all children of this route.
66+ * Parameter [tag] should be an enum that inherits from [APITag], check [APITag] description for
67+ * an explanation.
68+ *
69+ * @param tag the tag to apply
70+ * @return the same route that received the call to chain multiple calls
71+ */
72+ fun <TRoute : OpenAPIRoute <TRoute >> TRoute.tag (tag : APITag ): TRoute {
5773 return child().apply {
5874 provider.registerModule(TagModule (listOf (tag)))
5975 }
6076}
6177
6278
79+ /* *
80+ * This method assigns an OpenAPI [tag] too all child routes defined inside [fn].
81+ * Parameter [tag] should be an enum that inherits from [APITag], check [APITag] description for an
82+ * explanation.
83+ *
84+ * Usage example:
85+ *
86+ * // Defined tags
87+ * enum class Tags(override val description: String) : APITag {
88+ * EXAMPLE("Wow this is a tag?!")
89+ * }
90+ *
91+ * ...
92+ * apiRouting {
93+ * route("examples") {
94+ * tag(Tags.EXAMPLE) { // <-- Applies the tag here
95+ * route("getTextData").get<StringParam, StringResponse> { params ->
96+ * respond(StringResponse(params.a))
97+ * }
98+ * // Multiple routes can be specified here
99+ * }
100+ * }
101+ * }
102+ * ...
103+ *
104+ * @param tag the tag to apply
105+ * @param fn the block where the sub routes are defined
106+ */
63107@ContextDsl
64- inline fun <T : OpenAPIRoute <T >> T .tag (tag : APITag , crossinline fn : T .() -> Unit ) {
108+ inline fun <TRoute : OpenAPIRoute <TRoute >> TRoute .tag (tag : APITag , crossinline fn : TRoute .() -> Unit ) {
65109 tag(tag).fn()
66110}
67111
68- inline fun <reified P : Any , reified R : Any , reified B : Any , T : OpenAPIRoute <T >> T .preHandle (
69- exampleResponse : R ? = null,
70- exampleRequest : B ? = null,
71- noinline handle : T .() -> Unit
112+ inline fun <reified TParams : Any , reified TResponse : Any , reified TRequest : Any , TRoute : OpenAPIRoute <TRoute >> TRoute .preHandle (
113+ exampleResponse : TResponse ? = null,
114+ exampleRequest : TRequest ? = null,
115+ noinline handle : TRoute .() -> Unit
72116) {
73- preHandle<P , R , B , T >(
74- typeOf<P >(),
75- typeOf<R >(),
76- typeOf<B >(),
117+ preHandle<TParams , TResponse , TRequest , TRoute >(
118+ typeOf<TParams >(),
119+ typeOf<TResponse >(),
120+ typeOf<TRequest >(),
77121 exampleResponse,
78122 exampleRequest,
79123 handle
@@ -82,13 +126,13 @@ inline fun <reified P : Any, reified R : Any, reified B : Any, T : OpenAPIRoute<
82126
83127// hide this function from public api as it can be "misused" easily but make it accessible to inlined functions from this package
84128@PublishedApi
85- internal fun <P : Any , R : Any , B : Any , T : OpenAPIRoute <T >> T .preHandle (
129+ internal fun <TParams : Any , TResponse : Any , TRequest : Any , TRoute : OpenAPIRoute <TRoute >> TRoute .preHandle (
86130 pType : KType ,
87131 rType : KType ,
88132 bType : KType ,
89- exampleResponse : R ? = null,
90- exampleRequest : B ? = null,
91- handle : T .() -> Unit
133+ exampleResponse : TResponse ? = null,
134+ exampleRequest : TRequest ? = null,
135+ handle : TRoute .() -> Unit
92136) {
93137 val path = pType.jvmErasure.findAnnotation<Path >()
94138 val new = if (path != null ) child(ktorRoute.createRouteFromPath(path.path)) else child()
0 commit comments