Skip to content

Commit 408e056

Browse files
authored
Refactor to remove __ APIs in Swift (#9678)
1 parent ede3dfd commit 408e056

File tree

2 files changed

+77
-74
lines changed

2 files changed

+77
-74
lines changed

FirebaseFunctions/Sources/Functions.swift

Lines changed: 56 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -64,20 +64,53 @@ internal enum FunctionsConstants {
6464
*/
6565
open private(set) var emulatorOrigin: String?
6666

67+
/**
68+
* Creates a Cloud Functions client or returns a pre-existing instance if it already exists.
69+
*/
70+
@objc(functions) open class func functions() -> Functions {
71+
return functions(
72+
app: FirebaseApp.app(),
73+
region: FunctionsConstants.defaultRegion,
74+
customDomain: nil
75+
)
76+
}
77+
78+
/**
79+
* Creates a Cloud Functions client with the given app, or returns a pre-existing
80+
* instance if one already exists.
81+
* @param app The app for the Firebase project.
82+
*/
83+
@objc(functionsForApp:) open class func functions(app: FirebaseApp) -> Functions {
84+
return functions(app: app, region: FunctionsConstants.defaultRegion, customDomain: nil)
85+
}
86+
87+
/**
88+
* Creates a Cloud Functions client with the default app and given region.
89+
* @param region The region for the http trigger, such as "us-central1".
90+
*/
91+
@objc(functionsForRegion:) open class func functions(region: String) -> Functions {
92+
return functions(app: FirebaseApp.app(), region: region, customDomain: nil)
93+
}
94+
95+
/**
96+
* Creates a Cloud Functions client with the given app and region, or returns a pre-existing
97+
* instance if one already exists.
98+
* @param customDomain A custom domain for the http trigger, such as "https://mydomain.com".
99+
*/
100+
@objc(functionsForCustomDomain:) open class func functions(customDomain: String) -> Functions {
101+
return functions(app: FirebaseApp.app(),
102+
region: FunctionsConstants.defaultRegion, customDomain: customDomain)
103+
}
104+
67105
/**
68106
* Creates a Cloud Functions client with the given app and region, or returns a pre-existing
69107
* instance if one already exists.
70108
* @param app The app for the Firebase project.
71109
* @param region The region for the http trigger, such as "us-central1".
72110
*/
73-
@objc(functionsForApp:region:) open class func functions(app: FirebaseApp = FirebaseApp.app()!,
111+
@objc(functionsForApp:region:) open class func functions(app: FirebaseApp,
74112
region: String) -> Functions {
75-
let provider = ComponentType<FunctionsProvider>.instance(for: FunctionsProvider.self,
76-
in: app.container)
77-
return provider.functions(for: app,
78-
region: region,
79-
customDomain: nil,
80-
type: self)
113+
return functions(app: app, region: region, customDomain: nil)
81114
}
82115

83116
/**
@@ -86,14 +119,10 @@ internal enum FunctionsConstants {
86119
* @param app The app for the Firebase project.
87120
* @param customDomain A custom domain for the http trigger, such as "https://mydomain.com".
88121
*/
89-
@objc(functionsForApp:customDomain:) open class func functions(app: FirebaseApp = FirebaseApp
90-
.app()!,
91-
customDomain: String? = nil) -> Functions {
92-
let provider = app.container.instance(for: FunctionsProvider.self) as? FunctionsProvider
93-
return provider!.functions(for: app,
94-
region: FunctionsConstants.defaultRegion,
95-
customDomain: customDomain,
96-
type: self)
122+
@objc(functionsForApp:customDomain:) open class func functions(app: FirebaseApp,
123+
customDomain: String)
124+
-> Functions {
125+
return functions(app: app, region: FunctionsConstants.defaultRegion, customDomain: customDomain)
97126
}
98127

99128
/**
@@ -158,44 +187,21 @@ internal enum FunctionsConstants {
158187
emulatorOrigin = origin
159188
}
160189

161-
// MARK: - Public APIs only for Objective C
162-
163-
/**
164-
* Creates a Cloud Functions client or returns a pre-existing instance if it already exists.
165-
*/
166-
@objc(functions) open class func __functions() -> Functions {
167-
return functions()
168-
}
169-
170-
/**
171-
* Creates a Cloud Functions client with the given app, or returns a pre-existing
172-
* instance if one already exists.
173-
* @param app The app for the Firebase project.
174-
*/
175-
@objc(functionsForApp:) open class func __functionsForApp(app: FirebaseApp) -> Functions {
176-
return functions(app: app)
177-
}
178-
179-
/**
180-
* Creates a Cloud Functions client with the default app and given region.
181-
* @param region The region for the http trigger, such as "us-central1".
182-
*/
183-
@objc(functionsForRegion:) open class func __functionsForRegion(region: String) -> Functions {
184-
return functions(region: region)
185-
}
190+
// MARK: - Private Funcs (or Internal for tests)
186191

187-
/**
188-
* Creates a Cloud Functions client with the given app and region, or returns a pre-existing
189-
* instance if one already exists.
190-
* @param customDomain A custom domain for the http trigger, such as "https://mydomain.com".
191-
*/
192-
@objc(functionsForCustomDomain:) open class func __functionsForCustomDomain(customDomain: String?)
193-
-> Functions {
194-
return functions(customDomain: customDomain)
192+
/// Solely used to have one precondition and one location where we fetch from the container. This
193+
/// previously was avoided due to default arguments but that doesn't work well with Obj-C compatibility.
194+
private class func functions(app: FirebaseApp?, region: String,
195+
customDomain: String?) -> Functions {
196+
precondition(app != nil,
197+
"`FirebaseApp.configure()` needs to be called before using Functions.")
198+
let provider = app!.container.instance(for: FunctionsProvider.self) as? FunctionsProvider
199+
return provider!.functions(for: app!,
200+
region: region,
201+
customDomain: customDomain,
202+
type: self)
195203
}
196204

197-
// MARK: - Private Funcs (or Internal for tests)
198-
199205
@objc internal init(projectID: String,
200206
region: String,
201207
customDomain: String?,

FirebaseStorage/Sources/Storage.swift

Lines changed: 21 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,29 @@ import FirebaseAuthInterop
3434
@objc(FIRStorage) open class Storage: NSObject {
3535
// MARK: - Public APIs
3636

37+
/**
38+
* An instance of FirebaseStorage, configured with the default FirebaseApp.
39+
* @return the FirebaseStorage instance, configured with the default FirebaseApp.
40+
*/
41+
@objc(storage) open class func storage() -> Storage {
42+
return storage(app: FirebaseApp.app()!)
43+
}
44+
45+
/**
46+
* An instance of FirebaseStorage, configured with a custom storage bucket @a url.
47+
* @param url The gs:// url to your Firebase Storage Bucket.
48+
* @return the FirebaseStorage instance, configured with the custom FirebaseApp.
49+
*/
50+
@objc(storageWithURL:) open class func storage(url: String) -> Storage {
51+
return storage(app: FirebaseApp.app()!, url: url)
52+
}
53+
3754
/**
3855
* Creates an instance of FirebaseStorage, configured with the custom FirebaseApp @a app.
3956
* @param app The custom FirebaseApp used for initialization.
40-
* @return the FirebaseStorage instance, initialized with the custom FirebaseApp.
57+
* @return the FirebaseStorage instance, configured with the custom FirebaseApp.
4158
*/
42-
@objc(storageForApp:) open class func storage(app: FirebaseApp = FirebaseApp.app()!) -> Storage {
59+
@objc(storageForApp:) open class func storage(app: FirebaseApp) -> Storage {
4360
let provider = ComponentType<StorageProvider>.instance(for: StorageProvider.self,
4461
in: app.container)
4562
return provider.storage(for: FIRIMPLStorage.bucket(for: app))
@@ -50,11 +67,10 @@ import FirebaseAuthInterop
5067
* bucket @a url.
5168
* @param app The custom FirebaseApp used for initialization.
5269
* @param url The gs:// url to your Firebase Storage Bucket.
53-
* @return the FirebaseStorage instance, initialized with the custom FirebaseApp.
70+
* @return the FirebaseStorage instance, configured with the custom FirebaseApp.
5471
*/
5572
@objc(storageForApp:URL:)
56-
open class func storage(app: FirebaseApp = FirebaseApp.app()!,
57-
url: String) -> Storage {
73+
open class func storage(app: FirebaseApp, url: String) -> Storage {
5874
let provider = ComponentType<StorageProvider>.instance(for: StorageProvider.self,
5975
in: app.container)
6076
return provider.storage(for: FIRIMPLStorage.bucket(for: app, url: url))
@@ -155,25 +171,6 @@ import FirebaseAuthInterop
155171
impl.useEmulator(withHost: host, port: port)
156172
}
157173

158-
// MARK: - Public APIs only for Objective C
159-
160-
/**
161-
* Creates an instance of FirebaseStorage, configured with the default FirebaseApp.
162-
* @return the FirebaseStorage instance, initialized with the default FirebaseApp.
163-
*/
164-
@objc(storage) open class func __storage() -> Storage {
165-
return storage(app: FirebaseApp.app()!)
166-
}
167-
168-
/**
169-
* Creates an instance of FirebaseStorage, configured with a custom storage bucket @a url.
170-
* @param url The gs:// url to your Firebase Storage Bucket.
171-
* @return the FirebaseStorage instance, initialized with the custom FirebaseApp.
172-
*/
173-
@objc(storageWithURL:) open class func __storage(url: String) -> Storage {
174-
return storage(app: FirebaseApp.app()!, url: url)
175-
}
176-
177174
// MARK: - NSObject overrides
178175

179176
@objc override open func copy() -> Any {

0 commit comments

Comments
 (0)