diff --git a/api/src/commonMain/kotlin/Endpoint.kt b/api/src/commonMain/kotlin/Endpoint.kt index de72bc1..13bc9e3 100644 --- a/api/src/commonMain/kotlin/Endpoint.kt +++ b/api/src/commonMain/kotlin/Endpoint.kt @@ -1,8 +1,9 @@ package opensavvy.spine.api import io.ktor.http.* -import kotlin.reflect.KClass import kotlin.reflect.KProperty +import kotlin.reflect.KType +import kotlin.reflect.typeOf /** * A specific HTTP [method] in a [resource]. @@ -135,12 +136,12 @@ sealed interface AnyEndpoint { /** * The type of the body went the client sends data to the server. */ - val requestType: KClass<*> + val requestType: KType /** * The type of the body when the server responds to a client. */ - val responseType: KClass<*> + val responseType: KType /** * Constructor for query parameters. @@ -178,6 +179,8 @@ sealed interface AnyEndpoint { * Note that you may need to perform some configuration on the Ktor side before using some types, see the * official documentation for instructions. * + * Do not specify the [type] parameter. It will be passed automatically when declaring the endpoint. + * * ### Example * * ```kotlin @@ -190,7 +193,7 @@ sealed interface AnyEndpoint { * * @see response */ - fun request(kClass: KClass): Builder + fun request(type: KType): Builder /** * Declares the response body type. @@ -202,6 +205,8 @@ sealed interface AnyEndpoint { * Note that you may need to perform some configuration on the Ktor side before using some types, see the * official documentation for instructions. * + * Do not specify the [type] parameter. It will be passed automatically when declaring the endpoint. + * * ### Example * * ```kotlin @@ -214,7 +219,7 @@ sealed interface AnyEndpoint { * * @see request */ - fun response(kClass: KClass): Builder + fun response(type: KType): Builder /** * Declares query parameters that the client will need to provide to the server. @@ -320,8 +325,8 @@ class Endpoint override val resource: Resource, override val method: HttpMethod, override val path: Path.Segment?, - override val requestType: KClass, - override val responseType: KClass, + override val requestType: KType, + override val responseType: KType, override val failureSpec: Failure, override val buildParameters: ParameterConstructor, ) : AnyEndpoint { @@ -345,26 +350,28 @@ class Endpoint private val onCreate: (AnyEndpoint) -> Unit, ) : AnyEndpoint.Builder { - override fun request(kClass: KClass) = EndpointBuilder( - Endpoint(endpoint.resource, endpoint.method, endpoint.path, kClass, endpoint.responseType, endpoint.failureSpec, endpoint.buildParameters), + override fun request(type: KType): EndpointBuilder = EndpointBuilder( + Endpoint(endpoint.resource, endpoint.method, endpoint.path, type, endpoint.responseType, endpoint.failureSpec, endpoint.buildParameters), onCreate ) - inline fun request() = request(T::class) + inline fun request(): EndpointBuilder = + request(typeOf()) - override fun response(kClass: KClass) = EndpointBuilder( - Endpoint(endpoint.resource, endpoint.method, endpoint.path, endpoint.requestType, kClass, endpoint.failureSpec, endpoint.buildParameters), + override fun response(type: KType): EndpointBuilder = EndpointBuilder( + Endpoint(endpoint.resource, endpoint.method, endpoint.path, endpoint.requestType, type, endpoint.failureSpec, endpoint.buildParameters), onCreate ) - inline fun response() = response(T::class) + inline fun response(): EndpointBuilder = + response(typeOf()) - override fun

parameters(build: (ParameterStorage) -> P) = EndpointBuilder( + override fun

parameters(build: (ParameterStorage) -> P): EndpointBuilder = EndpointBuilder( Endpoint(endpoint.resource, endpoint.method, endpoint.path, endpoint.requestType, endpoint.responseType, endpoint.failureSpec, build), onCreate ) - override fun failure(spec: S) = EndpointBuilder( + override fun failure(spec: S): EndpointBuilder, Params> = EndpointBuilder( Endpoint(endpoint.resource, endpoint.method, endpoint.path, endpoint.requestType, endpoint.responseType, endpoint.failureSpec + spec, endpoint.buildParameters), onCreate ) diff --git a/api/src/commonMain/kotlin/Resource.kt b/api/src/commonMain/kotlin/Resource.kt index 932dac2..6ffab07 100644 --- a/api/src/commonMain/kotlin/Resource.kt +++ b/api/src/commonMain/kotlin/Resource.kt @@ -1,6 +1,7 @@ package opensavvy.spine.api import io.ktor.http.* +import kotlin.reflect.typeOf /** * Common parent for all resource types. @@ -72,15 +73,16 @@ sealed class Resource( get() = _endpoints.asSequence() @Suppress("DEPRECATION_ERROR") - private fun endpoint(method: HttpMethod, path: String? = null) = Endpoint( - resource = this@Resource, - method = method, - path = path?.let(Path::Segment), - requestType = Unit::class, - responseType = Unit::class, - buildParameters = { Parameters.Empty }, - failureSpec = FailureSpec.Never, - ).asBuilder { _endpoints += it } + private fun endpoint(method: HttpMethod, path: String? = null): Endpoint.EndpointBuilder = + Endpoint( + resource = this@Resource, + method = method, + path = path?.let(Path::Segment), + requestType = typeOf(), + responseType = typeOf(), + buildParameters = { Parameters.Empty }, + failureSpec = FailureSpec.Never, + ).asBuilder { _endpoints += it } /** * Creates a [`GET`][HttpMethod.Get] HTTP endpoint in this resource.