Skip to content

Commit

Permalink
feat: Storageモジュールの追加
Browse files Browse the repository at this point in the history
  • Loading branch information
cotrin8672 committed Mar 25, 2024
1 parent 075e9af commit 871c7bf
Show file tree
Hide file tree
Showing 4 changed files with 173 additions and 34 deletions.
86 changes: 52 additions & 34 deletions firebase-storage/src/jsMain/kotlin/FirebaseStorage.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@
package wrapper.firebase.storage

import FirebaseApp
import org.khronos.webgl.ArrayBuffer
import org.khronos.webgl.Uint8Array
import org.w3c.files.Blob
import org.w3c.files.File
import seskar.js.JsValue
import seskar.js.JsVirtual
import kotlin.js.Promise

external interface FirebaseStorage {
Expand All @@ -18,6 +19,13 @@ external interface FirebaseStorage {

external fun getStorage(app: FirebaseApp, bucketUrl: String?): FirebaseStorage

external fun connectStorageEmulator(
storage: FirebaseStorage,
host: String,
port: String,
options: dynamic = definedExternally,
)

external interface StorageReference {
val bucket: String
val fullPath: String
Expand All @@ -27,53 +35,63 @@ external interface StorageReference {
val storage: FirebaseStorage
}

@Suppress("NESTED_CLASS_IN_EXTERNAL_INTERFACE")
@JsVirtual
sealed external interface StorageErrorCode {
companion object {
@JsValue("app-deleted")
val APP_DELETED: StorageErrorCode

@JsValue("bucket-not-found")
val BUCKET_NOT_FOUND: StorageErrorCode

@JsValue("canceled")
val CANCELED: StorageErrorCode

@JsValue("cannot-slice-blob")
val CANNOT_SLICE_BLOB: StorageErrorCode

@JsValue("internal-error")
val INTERNAL_ERROR: StorageErrorCode
external fun ref(storage: FirebaseStorage, url: String?): StorageReference

@JsValue("invalid-argument")
val INVALID_ARGUMENT: StorageErrorCode
external fun deleteObject(ref: StorageReference): Promise<Unit>

@JsValue("invalid-argument-count")
val INVALID_ARGUMENT_COUNT: StorageErrorCode
external fun getDownloadURL(ref: StorageReference): Promise<String>

}
external interface SettableMetadata {
var cacheControl: String?
var contentDisposition: String?
var contentEncoding: String?
var contentLanguage: String?
var contentType: String?
var customMetadata: dynamic
}

external class StorageError(
external interface UploadMetadata : SettableMetadata {
var md5Hash: String?
}

) {
val customData: dynamic
val serverResponse: String?
val status: Number
external interface FullMetadata : UploadMetadata {
val bucket: String
val downloadTokens: Array<String>
val fullPath: String
val generation: String
val metageneration: String
val name: String
val ref: StorageReference?
val size: Number
val timeCreated: String
val updated: String
}

external fun ref(storage: FirebaseStorage, url: String?): StorageReference
external fun updateMetadata(ref: StorageReference, metadata: SettableMetadata): Promise<FullMetadata>

external fun deleteObject(ref: StorageReference): Promise<Unit>
external fun uploadBytes(
ref: StorageReference,
data: Blob,
metadata: UploadMetadata? = definedExternally,
)

external fun getDownloadURL(ref: StorageReference): Promise<String>
external fun uploadBytes(
ref: StorageReference,
data: Uint8Array,
metadata: UploadMetadata? = definedExternally,
)

external fun uploadBytes(
ref: StorageReference,
data: ArrayBuffer,
metadata: UploadMetadata? = definedExternally,
)

external interface UploadTask {
val snapshot: dynamic

fun cancel(): Boolean
fun catch(onRejected: (StorageError) -> dynamic): Promise<dynamic>
fun catch(onRejected: (StorageError) -> Any): Promise<Any>
}

external fun uploadBytes(ref: StorageReference, data: File)
19 changes: 19 additions & 0 deletions firebase-storage/src/jsMain/kotlin/StorageError.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
@file:JsModule("firebase/storage")
@file:JsNonModule
@file:Suppress("unused")

package wrapper.firebase.storage

external class StorageError(
code: StorageErrorCode,
message: String,
status: Number?,
) {
val customData: StorageErrorCustomData
val serverResponse: String?
val status: Number
}

external interface StorageErrorCustomData {
val serverResponse: String?
}
89 changes: 89 additions & 0 deletions firebase-storage/src/jsMain/kotlin/StorageErrorCode.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
@file:JsModule("firebase/storage")
@file:JsNonModule
@file:Suppress("unused")

package wrapper.firebase.storage

import seskar.js.JsValue
import seskar.js.JsVirtual

@Suppress("NESTED_CLASS_IN_EXTERNAL_INTERFACE")
@JsVirtual
sealed external interface StorageErrorCode {
companion object {
@JsValue("app-deleted")
val APP_DELETED: StorageErrorCode

@JsValue("bucket-not-found")
val BUCKET_NOT_FOUND: StorageErrorCode

@JsValue("canceled")
val CANCELED: StorageErrorCode

@JsValue("cannot-slice-blob")
val CANNOT_SLICE_BLOB: StorageErrorCode

@JsValue("internal-error")
val INTERNAL_ERROR: StorageErrorCode

@JsValue("invalid-argument")
val INVALID_ARGUMENT: StorageErrorCode

@JsValue("invalid-argument-count")
val INVALID_ARGUMENT_COUNT: StorageErrorCode

@JsValue("invalid-checksum")
val INVALID_CHECKSUM: StorageErrorCode

@JsValue("invalid-default-bucket")
val INVALID_DEFAULT_BUCKET: StorageErrorCode

@JsValue("invalid-event-name")
val INVALID_EVENT_NAME: StorageErrorCode

@JsValue("invalid-format")
val INVALID_FORMAT: StorageErrorCode

@JsValue("invalid-root-operation")
val INVALID_ROOT_OPERATION: StorageErrorCode

@JsValue("invalid-url")
val INVALID_URL: StorageErrorCode

@JsValue("no-default-bucket")
val NO_DEFAULT_BUCKET: StorageErrorCode

@JsValue("no-download-url")
val NO_DOWNLOAD_URL: StorageErrorCode

@JsValue("object-not-found")
val OBJECT_NOT_FOUND:StorageErrorCode

@JsValue("project-not-found")
val PROJECT_NOT_FOUND: StorageErrorCode

@JsValue("quota-exceeded")
val QUOTA_EXCEEDED: StorageErrorCode

@JsValue("retry-limit-exceeded")
val RETRY_LIMIT_EXCEEDED: StorageErrorCode

@JsValue("server-file-wrong-size")
val SERVER_FILE_WRONG_SIZE: StorageErrorCode

@JsValue("unauthenticated")
val UNAUTHENTICATED: StorageErrorCode

@JsValue("unauthorized")
val UNAUTHORIZED: StorageErrorCode

@JsValue("unauthorized-app")
val UNAUTHORIZED_APP: StorageErrorCode

@JsValue("unknown")
val UNKNOWN: StorageErrorCode

@JsValue("unsupported-environment")
val UNSUPPORTED_ENVIRONMENT: StorageErrorCode
}
}
13 changes: 13 additions & 0 deletions firebase-storage/src/jsMain/kotlin/StorageObserver.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
@file:Suppress("unused")

package wrapper.firebase.storage

import CompleteFn
import ErrorFn
import NextFn

interface StorageObserver<T> {
val complete: CompleteFn?
val error: ErrorFn?
val next: NextFn<T>?
}

0 comments on commit 871c7bf

Please sign in to comment.