0.63.2 • Published 8 months ago

@arrirpc/codegen-kotlin v0.63.2

Weekly downloads
-
License
MIT
Repository
github
Last release
8 months ago

Arri Kotlin Codegen

Setup

1) Add the generator to your arri config

// arri.config.ts
import { defineConfig, generators } from "arri";

export default defineConfig({
    generators: [
        generators.kotlinClient({
            clientName: "MyClient",
            outputFile: "./client/src/MyClient.g.kt",
        }),
    ],
});

Options:

NameDescription
clientNameThe name of the generated client class (Defaults to "Client")
outputFile (required)Path to the file that will be created by the generator
typePrefixAdd a prefix to the generated class names

2) Install dependencies

The generated code relies on the following dependencies:

Using the Generated Code

Initialize the client

fun main() {
    // create a Ktor HTTP client
    val httpClient = HttpClient() {
        install(HttpTimeout)
    }
    // initialize your generated client and pass it the httpClient
    // the client name will match whatever options you passed into your arri config
    val client = MyClient(
        httpClient = httpClient,
        baseUrl = "https://example.com",
        // a function that returns a mutable map of headers
        // this function will run before every request. Or before every reconnection in the case of SSE
        headers = {
            mutableMapOf(Pair("x-example-header", "<some-header-value>"))
        }
    )
    runBlocking {
        client.someProcedure()
    }
}

The root client will be a class containing all of the services and procedures in a single class. If you only need a particular service, you can initialize just that service.

val service = MyClientUsersService(
        httpClient = httpClient,
        baseUrl = "https://example.com",
        headers = {
            mutableMapOf(Pair("x-example-header", "<some-header-value>"))
        }
    )

Calling Procedures

Standard HTTP Procedures

runBlocking {
    // procedure with no parameters
    val getUsersResponse = myClient.users.getUsers()

    // procedure with parameters
    val getUserResponse = myClient.users.getUser(GetUserParams(userId = "12345"))
}

Event Stream Procedures

Basic Usage
runBlocking {
    myClient.users.watchUserChanges(
        onData { message ->
            println("New message: ${message}")
        },
        onOpen {
            println("Connection established")
        }
        onRequestError { err ->
            println("Error connecting to server: ${err}")
        },
        onResponseError { err ->
            println("Server returned an error: ${err.code} ${err.message}")
        },
        onClose {
            println("Connection closed")
        }
    )
}
Cancelling Requests

Event stream procedures can be cancelled from inside one of the hooks by throwing a CancellationException

runBlocking {
    myClient.users.watchUserChanges(
        onResponseError { err ->
            println("Server returned an error: ${err.code} ${err.message}")
            throw CancellationException()
        }
    )
}

You can also spawn a job and cancel that job in order to cancel the Event stream procedure from the outside

val job = someCoroutineScope.launch {
    myClient.users.watchUserChanges()
}
job.cancel()
Other Options
OptionTypeDescription
bufferCapacityIntMax buffer size that can be allocated towards reading messages received. Default is 1024 * 1024 (1MB).
maxBackoffTimeLong?Max wait time between retries in milliseconds. Default is 30000ms

Using Arri Models

All generated models will be data classes. They will have access to the following features:

Methods:

  • toJson(): String
  • toUrlQueryParams(): String

Factory Methods:

  • new()
  • fromJson(input: String)
  • fromJsonElement(input: JsonElement, instancePath: String)

Other Notes

  • All Enums will have a serialValue property.
  • Discriminator schemas are converted to sealed classes

Development

# build the library
pnpm nx build codegen-kotlin

# test
pnpm nx test codegen-kotlin

# lint
pnpm nx lint codegen-lint
0.63.2

8 months ago

0.63.0

8 months ago

0.62.0

8 months ago

0.63.1

8 months ago

0.61.1

9 months ago

0.60.3

10 months ago

0.60.2

10 months ago

0.61.0

10 months ago

0.60.1

10 months ago

0.60.0

10 months ago

0.57.4

11 months ago

0.57.5

11 months ago

0.59.0

10 months ago

0.58.1

10 months ago

0.57.2

11 months ago

0.57.3

11 months ago

0.57.0

11 months ago

0.58.0

10 months ago

0.55.0

11 months ago

0.53.2

12 months ago

0.57.8

11 months ago

0.57.6

11 months ago

0.53.0

12 months ago

0.51.2

12 months ago

0.54.0

12 months ago

0.53.1

12 months ago

0.51.0

1 year ago

0.52.0

12 months ago

0.51.1

1 year ago

0.50.0

1 year ago

0.49.1

1 year ago

0.48.2

1 year ago

0.45.6

1 year ago

0.48.0

1 year ago

0.49.0

1 year ago

0.48.1

1 year ago

0.46.0

1 year ago

0.47.0

1 year ago

0.45.7

1 year ago

0.45.5

1 year ago

0.45.3

1 year ago

0.45.4

1 year ago

0.45.2

1 year ago

0.45.1

1 year ago

0.45.0

1 year ago