supabase-community / supabase-kt

A Kotlin Multiplatform Client for Supabase.
https://supabase.com/docs/reference/kotlin/introduction
MIT License
415 stars 37 forks source link

Refactor Storage download & upload (flow) implementation #729

Closed jan-tennert closed 2 months ago

jan-tennert commented 2 months ago

What kind of change does this PR introduce?

Internal refactor and public API improvements

What is the current behavior?

The current implementation is very complex/messy and the flow variants are even worse (calling BucketApiImpl methods, that shouldn't be in a public extension function).

What is the new behavior?

Public API

Every upload and download method now has a options DSL. This makes it possible to add configuration options in the future without filling up the parameter list (e.g. file metadata) For downloads, there are currently only two things to configure:

  1. Image transformation (moved down)
  2. Http request overrides. This is important for the flow variants to attach download listeners.

Example of the new syntax:

supabase.storage.from("test").downloadAuthenticated("test.jpg") {
    transform {
        size(100, 100)
    }
}

For uploads, you can configure:

  1. Whether to upsert or not (moved down)
  2. The content type (will be inferred if null, wasn't configurable before)
  3. Also Http request overrides for flow variants.

Example of the new syntax:

supabase.storage.from("test").upload("test.txt", "Hello World!".encodeToByteArray()) {
    contentType = ContentType.Text.Plain
    upsert = true
}

Every upload/update/download variant has this new builder including the flow variants.

BucketItem has been renamed to FileObject to match the JS lib.

Internal implementation

For non-flow-variants I cleaned up the code to minimize duplications & improve readability. The same approach was used for the flow variants, which no longer call internal methods from BucketApiImpl and instead use the new httpOverride option.

Additional context

Add any other context or screenshots.

jan-tennert commented 2 months ago

Note that there are some commits from #694 because I backported and improved some of the changes, but file metadata and exists/info support is not being added in this PR.

jan-tennert commented 2 months ago

Yes, I originally wanted to only refactor some internal stuff, but now the changes are getting very breaking alongside the WASM support. I can probably include #622 too. I plan to release 3.0.0.