Skip to content

Commit 60226e4

Browse files
samtsternschmidt-sebastian
authored andcommitted
Add custom domain support to callable functions (#6787)
1 parent a7fde4a commit 60226e4

File tree

7 files changed

+84
-15
lines changed

7 files changed

+84
-15
lines changed

Functions/CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
# v7.1.0
2+
- [added] Added a constructor to set a custom domain. (#6787)
3+
14
# v2.9.0
25
- [changed] Weak dependency on Instance ID replaced by Firebase Messaging. (#6395)
36

Functions/Example/IntegrationTests/FIRIntegrationTests.m

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ - (void)setUp {
5454
_functions = [[FIRFunctions alloc]
5555
initWithProjectID:_projectID
5656
region:@"us-central1"
57+
customDomain:nil
5758
auth:[[FIRAuthInteropFake alloc] initWithToken:nil userID:nil error:nil]
5859
messaging:_messagingFake];
5960
if (_useLocalhost) {
@@ -105,6 +106,7 @@ - (void)testToken {
105106
FIRFunctions *functions = [[FIRFunctions alloc]
106107
initWithProjectID:_projectID
107108
region:@"us-central1"
109+
customDomain:nil
108110
auth:[[FIRAuthInteropFake alloc] initWithToken:@"token" userID:nil error:nil]
109111
messaging:_messagingFake];
110112
if (_useLocalhost) {

Functions/Example/Tests/FIRFunctionsTests.m

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,26 +17,54 @@
1717
#import "Functions/FirebaseFunctions/FIRFunctions+Internal.h"
1818
#import "Functions/FirebaseFunctions/Public/FirebaseFunctions/FIRFunctions.h"
1919

20-
@interface FIRFunctionsTests : XCTestCase
20+
@interface FIRFunctionsTests : XCTestCase {
21+
FIRFunctions *_functions;
22+
FIRFunctions *_functionsCustomDomain;
23+
}
2124
@end
2225

2326
@implementation FIRFunctionsTests
2427

2528
- (void)setUp {
2629
[super setUp];
30+
31+
_functions = [[FIRFunctions alloc] initWithProjectID:@"my-project"
32+
region:@"my-region"
33+
customDomain:nil
34+
auth:nil
35+
messaging:nil];
36+
37+
_functionsCustomDomain = [[FIRFunctions alloc] initWithProjectID:@"my-project"
38+
region:@"my-region"
39+
customDomain:@"https://mydomain.com"
40+
auth:nil
41+
messaging:nil];
2742
}
2843

2944
- (void)tearDown {
3045
[super tearDown];
3146
}
3247

3348
- (void)testURLWithName {
34-
FIRFunctions *functions = [[FIRFunctions alloc] initWithProjectID:@"my-project"
35-
region:@"my-region"
36-
auth:nil
37-
messaging:nil];
38-
NSString *url = [functions URLWithName:@"my-endpoint"];
49+
NSString *url = [_functions URLWithName:@"my-endpoint"];
3950
XCTAssertEqualObjects(@"https://my-region-my-project.cloudfunctions.net/my-endpoint", url);
4051
}
4152

53+
- (void)testRegionWithEmulator {
54+
[_functions useFunctionsEmulatorOrigin:@"http://localhost:5005"];
55+
NSString *url = [_functions URLWithName:@"my-endpoint"];
56+
XCTAssertEqualObjects(@"http://localhost:5005/my-project/my-region/my-endpoint", url);
57+
}
58+
59+
- (void)testCustomDomain {
60+
NSString *url = [_functionsCustomDomain URLWithName:@"my-endpoint"];
61+
XCTAssertEqualObjects(@"https://mydomain.com/my-endpoint", url);
62+
}
63+
64+
- (void)testCustomDomainWithEmulator {
65+
[_functionsCustomDomain useFunctionsEmulatorOrigin:@"http://localhost:5005"];
66+
NSString *url = [_functionsCustomDomain URLWithName:@"my-endpoint"];
67+
XCTAssertEqualObjects(@"http://localhost:5005/my-project/my-region/my-endpoint", url);
68+
}
69+
4270
@end

Functions/FirebaseFunctions/FIRFunctions+Internal.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,13 @@ NS_ASSUME_NONNULL_BEGIN
5252
* Internal initializer for the Cloud Functions client.
5353
* @param projectID The project ID for the Firebase project.
5454
* @param region The region for the http trigger, such as "us-central1".
55+
* @param customDomain A custom domain for the http trigger, such as "https://mydomain.com".
5556
* @param auth The auth provider to use (optional).
5657
* @param messaging The messaging interop to use (optional).
5758
*/
5859
- (id)initWithProjectID:(NSString *)projectID
5960
region:(NSString *)region
61+
customDomain:(nullable NSString *)customDomain
6062
auth:(nullable id<FIRAuthInterop>)auth
6163
messaging:(nullable id<FIRMessagingInterop>)messaging;
6264

Functions/FirebaseFunctions/FIRFunctions.m

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ @interface FIRFunctions () <FIRLibrary, FIRFunctionsInstanceProvider> {
4949
NSString *_projectID;
5050
// The region to use for all function references.
5151
NSString *_region;
52+
// The custom domain to use for all functions references (optional).
53+
NSString *_customDomain;
5254
// A serializer to encode/decode data and return values.
5355
FUNSerializer *_serializer;
5456
// A factory for getting the metadata to include with function calls.
@@ -60,6 +62,7 @@ @interface FIRFunctions () <FIRLibrary, FIRFunctionsInstanceProvider> {
6062
// Re-declare this initializer here in order to attribute it as the designated initializer.
6163
- (instancetype)initWithProjectID:(NSString *)projectID
6264
region:(NSString *)region
65+
customDomain:(nullable NSString *)customDomain
6366
auth:(nullable id<FIRAuthInterop>)auth
6467
messaging:(nullable id<FIRMessagingInterop>)messaging
6568
NS_DESIGNATED_INITIALIZER;
@@ -89,30 +92,44 @@ + (void)load {
8992
}
9093

9194
+ (instancetype)functions {
92-
return [[self alloc] initWithApp:[FIRApp defaultApp] region:kFUNDefaultRegion];
95+
return [[self alloc] initWithApp:[FIRApp defaultApp] region:kFUNDefaultRegion customDomain:nil];
9396
}
9497

9598
+ (instancetype)functionsForApp:(FIRApp *)app {
96-
return [[self alloc] initWithApp:app region:kFUNDefaultRegion];
99+
return [[self alloc] initWithApp:app region:kFUNDefaultRegion customDomain:nil];
97100
}
98101

99102
+ (instancetype)functionsForRegion:(NSString *)region {
100-
return [[self alloc] initWithApp:[FIRApp defaultApp] region:region];
103+
return [[self alloc] initWithApp:[FIRApp defaultApp] region:region customDomain:nil];
104+
}
105+
106+
+ (instancetype)functionsForCustomDomain:(NSString *)customDomain {
107+
return [[self alloc] initWithApp:[FIRApp defaultApp]
108+
region:kFUNDefaultRegion
109+
customDomain:customDomain];
101110
}
102111

103112
+ (instancetype)functionsForApp:(FIRApp *)app region:(NSString *)region {
104-
return [[self alloc] initWithApp:app region:region];
113+
return [[self alloc] initWithApp:app region:region customDomain:nil];
105114
}
106115

107-
- (instancetype)initWithApp:(FIRApp *)app region:(NSString *)region {
116+
+ (instancetype)functionsForApp:(FIRApp *)app customDomain:(NSString *)customDomain {
117+
return [[self alloc] initWithApp:app region:kFUNDefaultRegion customDomain:customDomain];
118+
}
119+
120+
- (instancetype)initWithApp:(FIRApp *)app
121+
region:(NSString *)region
122+
customDomain:(nullable NSString *)customDomain {
108123
return [self initWithProjectID:app.options.projectID
109124
region:region
125+
customDomain:customDomain
110126
auth:FIR_COMPONENT(FIRAuthInterop, app.container)
111127
messaging:FIR_COMPONENT(FIRMessagingInterop, app.container)];
112128
}
113129

114130
- (instancetype)initWithProjectID:(NSString *)projectID
115131
region:(NSString *)region
132+
customDomain:(nullable NSString *)customDomain
116133
auth:(nullable id<FIRAuthInterop>)auth
117134
messaging:(nullable id<FIRMessagingInterop>)messaging {
118135
self = [super init];
@@ -123,6 +140,7 @@ - (instancetype)initWithProjectID:(NSString *)projectID
123140
_fetcherService = [[GTMSessionFetcherService alloc] init];
124141
_projectID = [projectID copy];
125142
_region = [region copy];
143+
_customDomain = [customDomain copy];
126144
_serializer = [[FUNSerializer alloc] init];
127145
_contextProvider = [[FUNContextProvider alloc] initWithAuth:auth messaging:messaging];
128146
_emulatorOrigin = nil;
@@ -148,6 +166,9 @@ - (NSString *)URLWithName:(NSString *)name {
148166
if (_emulatorOrigin) {
149167
return [NSString stringWithFormat:@"%@/%@/%@/%@", _emulatorOrigin, _projectID, _region, name];
150168
}
169+
if (_customDomain) {
170+
return [NSString stringWithFormat:@"%@/%@", _customDomain, name];
171+
}
151172
return
152173
[NSString stringWithFormat:@"https://%@-%@.cloudfunctions.net/%@", _region, _projectID, name];
153174
}

Functions/FirebaseFunctions/Public/FirebaseFunctions/FIRFunctions.h

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,16 +44,29 @@ NS_SWIFT_NAME(Functions)
4444
*/
4545
+ (instancetype)functionsForRegion:(NSString *)region NS_SWIFT_NAME(functions(region:));
4646

47+
/**
48+
* Creates a Cloud Functions client with the default app and given custom domain.
49+
* @param customDomain A custom domain for the http trigger, such as "https://mydomain.com".
50+
*/
51+
+ (instancetype)functionsForCustomDomain:(NSString *)customDomain
52+
NS_SWIFT_NAME(functions(customDomain:));
53+
4754
/**
4855
* Creates a Cloud Functions client with the given app and region.
4956
* @param app The app for the Firebase project.
5057
* @param region The region for the http trigger, such as "us-central1".
5158
*/
52-
// clang-format off
53-
// because it incorrectly breaks this NS_SWIFT_NAME.
5459
+ (instancetype)functionsForApp:(FIRApp *)app
5560
region:(NSString *)region NS_SWIFT_NAME(functions(app:region:));
56-
// clang-format on
61+
62+
/**
63+
* Creates a Cloud Functions client with the given app and region.
64+
* @param app The app for the Firebase project.
65+
* @param customDomain A custom domain for the http trigger, such as "https://mydomain.com".
66+
*/
67+
+ (instancetype)functionsForApp:(FIRApp *)app
68+
customDomain:(NSString *)customDomain
69+
NS_SWIFT_NAME(functions(app:customDomain:));
5770

5871
/**
5972
* Creates a reference to the Callable HTTPS trigger with the given name.

Functions/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ integration test FirebaseFunctions:
1212

1313
### To Develop
1414

15-
- Run `pod gen FirebaseFunctions.podspec`
15+
- Run `pod gen FirebaseFunctions.podspec --local-sources=./`
1616
- `open gen/FirebaseFunctions/FirebaseFunctions.xcworkspace`
1717

1818
OR these two commands can be combined with

0 commit comments

Comments
 (0)