Skip to content

Commit 99bceab

Browse files
committed
Refactor functions
1 parent 6d3b904 commit 99bceab

File tree

5 files changed

+45
-33
lines changed

5 files changed

+45
-33
lines changed

firebase-common/src/jsMain/kotlin/dev/gitlive/firebase/externals.kt

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -13,33 +13,14 @@ import kotlin.js.Promise
1313
external object firebase {
1414

1515
open class App {
16-
fun functions(region: String? = definedExternally): functions.Functions
1716
}
1817

19-
fun app(name: String? = definedExternally): App
20-
2118
interface FirebaseError {
2219
var code: String
2320
var message: String
2421
var name: String
2522
}
2623

27-
fun functions(app: App? = definedExternally): functions.Functions
28-
29-
object functions {
30-
class Functions {
31-
fun httpsCallable(name: String, options: Json?): HttpsCallable
32-
fun useFunctionsEmulator(origin: String)
33-
fun useEmulator(host: String, port: Int)
34-
}
35-
interface HttpsCallableResult {
36-
val data: Any?
37-
}
38-
interface HttpsCallable {
39-
}
40-
41-
}
42-
4324
fun installations(app: App? = definedExternally): installations.Installations
4425

4526
object installations {
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
@file:JsModule("firebase/functions")
2+
@file:JsNonModule
3+
4+
package dev.gitlive.firebase.externals.functions
5+
6+
import dev.gitlive.firebase.externals.app.FirebaseApp
7+
import kotlin.js.Json
8+
9+
external fun connectFunctionsEmulator(functions: Functions, host: String, port: Int)
10+
11+
external fun getFunctions(
12+
app: FirebaseApp? = definedExternally,
13+
regionOrCustomDomain: String? = definedExternally
14+
): Functions
15+
16+
external fun httpsCallable(functions: Functions, name: String, options: Json?): HttpsCallable
17+
18+
external interface Functions
19+
20+
external interface HttpsCallableResult {
21+
val data: Any?
22+
}
23+
24+
external interface HttpsCallable

firebase-common/src/jsMain/kotlin/dev/gitlive/firebase/externals2.kt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
package dev.gitlive.firebase
66

77
import dev.gitlive.firebase.externals.database.DataSnapshot
8+
import dev.gitlive.firebase.externals.functions.HttpsCallable
9+
import dev.gitlive.firebase.externals.functions.HttpsCallableResult
810
import kotlin.js.Promise
911

1012
@JsModule("firebase/compat/functions")
@@ -18,5 +20,5 @@ typealias ValueSnapshotCallback = (data: DataSnapshot) -> Unit
1820
typealias CancelCallback = (error: Throwable) -> Unit
1921
typealias Unsubscribe = () -> Unit
2022

21-
operator fun firebase.functions.HttpsCallable.invoke() = asDynamic()() as Promise<firebase.functions.HttpsCallableResult>
22-
operator fun firebase.functions.HttpsCallable.invoke(data: Any?) = asDynamic()(data) as Promise<firebase.functions.HttpsCallableResult>
23+
operator fun HttpsCallable.invoke() = asDynamic()() as Promise<HttpsCallableResult>
24+
operator fun HttpsCallable.invoke(data: Any?) = asDynamic()(data) as Promise<HttpsCallableResult>

firebase-functions/src/commonMain/kotlin/dev/gitlive/firebase/functions/functions.kt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ expect class FirebaseFunctions {
1414
fun httpsCallable(name: String, timeout: Long? = null): HttpsCallableReference
1515
fun useEmulator(host: String, port: Int)
1616

17-
@Deprecated("Use useEmulator(java.lang.String,int) to connect to the emulator.")
17+
@Deprecated("Use useEmulator(java.lang.String,int) to connect to the emulator.", level = DeprecationLevel.ERROR)
1818
fun useFunctionsEmulator(origin: String)
1919
}
2020

@@ -42,4 +42,3 @@ expect fun Firebase.functions(app: FirebaseApp): FirebaseFunctions
4242
expect fun Firebase.functions(app: FirebaseApp, region: String): FirebaseFunctions
4343

4444
expect class FirebaseFunctionsException: FirebaseException
45-

firebase-functions/src/jsMain/kotlin/dev/gitlive/firebase/functions/functions.kt

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,34 +5,40 @@
55
package dev.gitlive.firebase.functions
66

77
import dev.gitlive.firebase.*
8+
import dev.gitlive.firebase.externals.functions.*
89
import kotlinx.coroutines.await
910
import kotlinx.serialization.DeserializationStrategy
1011
import kotlinx.serialization.SerializationStrategy
12+
import org.w3c.dom.url.URL
1113
import kotlin.js.json
14+
import dev.gitlive.firebase.externals.functions.HttpsCallableResult as JsHttpsCallableResult
1215

1316
actual val Firebase.functions: FirebaseFunctions
14-
get() = rethrow { dev.gitlive.firebase.functions; FirebaseFunctions(firebase.functions()) }
17+
get() = rethrow { FirebaseFunctions(getFunctions()) }
1518

1619
actual fun Firebase.functions(region: String) =
17-
rethrow { dev.gitlive.firebase.functions; FirebaseFunctions(firebase.app().functions(region)) }
20+
rethrow { FirebaseFunctions(getFunctions(regionOrCustomDomain = region)) }
1821

1922
actual fun Firebase.functions(app: FirebaseApp) =
20-
rethrow { dev.gitlive.firebase.functions; FirebaseFunctions(firebase.functions(app.js)) }
23+
rethrow { FirebaseFunctions(getFunctions(app.js)) }
2124

2225
actual fun Firebase.functions(app: FirebaseApp, region: String) =
23-
rethrow { dev.gitlive.firebase.functions; FirebaseFunctions(app.js.functions(region)) }
26+
rethrow { FirebaseFunctions(getFunctions(app.js, region)) }
2427

25-
actual class FirebaseFunctions internal constructor(val js: firebase.functions.Functions) {
28+
actual class FirebaseFunctions internal constructor(val js: Functions) {
2629
actual fun httpsCallable(name: String, timeout: Long?) =
27-
rethrow { HttpsCallableReference(js.httpsCallable(name, timeout?.let { json("timeout" to timeout.toDouble()) })) }
30+
rethrow { HttpsCallableReference(httpsCallable(js, name, timeout?.let { json("timeout" to timeout.toDouble()) })) }
2831

29-
actual fun useFunctionsEmulator(origin: String) = js.useFunctionsEmulator(origin)
32+
actual fun useFunctionsEmulator(origin: String) {
33+
val url = URL(origin)
34+
useEmulator(url.host, url.port.toInt())
35+
}
3036

31-
actual fun useEmulator(host: String, port: Int) = js.useEmulator(host, port)
37+
actual fun useEmulator(host: String, port: Int) = connectFunctionsEmulator(js, host, port)
3238
}
3339

3440
@Suppress("UNCHECKED_CAST")
35-
actual class HttpsCallableReference internal constructor(val js: firebase.functions.HttpsCallable) {
41+
actual class HttpsCallableReference internal constructor(val js: HttpsCallable) {
3642

3743
actual suspend operator fun invoke() =
3844
rethrow { HttpsCallableResult(js().await()) }
@@ -44,7 +50,7 @@ actual class HttpsCallableReference internal constructor(val js: firebase.functi
4450
rethrow { HttpsCallableResult(js(encode(strategy, data, encodeDefaults)).await()) }
4551
}
4652

47-
actual class HttpsCallableResult constructor(val js: firebase.functions.HttpsCallableResult) {
53+
actual class HttpsCallableResult constructor(val js: JsHttpsCallableResult) {
4854

4955
actual inline fun <reified T> data() =
5056
rethrow { decode<T>(value = js.data) }

0 commit comments

Comments
 (0)