Skip to content
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions Functions/Example/Tests/FIRFunctionsTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,43 @@ - (void)tearDown {
- (void)testURLWithName {
FIRFunctions *functions = [[FIRFunctions alloc] initWithProjectID:@"my-project"
region:@"my-region"
customDomain:nil
auth:nil
messaging:nil];
NSString *url = [functions URLWithName:@"my-endpoint"];
XCTAssertEqualObjects(@"https://my-region-my-project.cloudfunctions.net/my-endpoint", url);
}

- (void)testRegionWithEmulator {
FIRFunctions *functions = [[FIRFunctions alloc] initWithProjectID:@"my-project"
region:@"my-region"
customDomain:nil
auth:nil
messaging:nil];
[functions useFunctionsEmulatorOrigin:@"http://localhost:5005"];
NSString *url = [functions URLWithName:@"my-endpoint"];
XCTAssertEqualObjects(@"http://localhost:5005/my-project/my-region/my-endpoint", url);
}

- (void)testCustomDomain {
FIRFunctions *functions = [[FIRFunctions alloc] initWithProjectID:@"my-project"
region:@"my-region"
customDomain:@"https://mydomain.com"
auth:nil
messaging:nil];
NSString *url = [functions URLWithName:@"my-endpoint"];
XCTAssertEqualObjects(@"https://mydomain.com/my-endpoint", url);
}

- (void)testCustomDomainWithEmulator {
FIRFunctions *functions = [[FIRFunctions alloc] initWithProjectID:@"my-project"
region:@"my-region"
customDomain:@"https://mydomain.com"
auth:nil
messaging:nil];
[functions useFunctionsEmulatorOrigin:@"http://localhost:5005"];
NSString *url = [functions URLWithName:@"my-endpoint"];
XCTAssertEqualObjects(@"http://localhost:5005/my-project/us-central1/my-endpoint", url);
}

@end
31 changes: 25 additions & 6 deletions Functions/FirebaseFunctions/FIRFunctions.m
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ @interface FIRFunctions () <FIRLibrary, FIRFunctionsInstanceProvider> {
NSString *_projectID;
// The region to use for all function references.
NSString *_region;
// The custom domain to use for all functions references (optional).
NSString *_customDomain;
// A serializer to encode/decode data and return values.
FUNSerializer *_serializer;
// A factory for getting the metadata to include with function calls.
Expand Down Expand Up @@ -97,33 +99,47 @@ + (instancetype)functionsForApp:(FIRApp *)app {
}

+ (instancetype)functionsForRegion:(NSString *)region {
return [[self alloc] initWithApp:[FIRApp defaultApp] region:region];
return [[self alloc] initWithApp:[FIRApp defaultApp] region:region customDomain:nil];
}

+ (instancetype)functionsForCustomDomain:(NSString *)customDomain {
return [[self alloc] initWithApp:[FIRApp defaultApp]
region:kFUNDefaultRegion
customDomain:customDomain];
}

+ (instancetype)functionsForApp:(FIRApp *)app region:(NSString *)region {
return [[self alloc] initWithApp:app region:region];
return [[self alloc] initWithApp:app region:region customDomain:nil];
}

- (instancetype)initWithApp:(FIRApp *)app region:(NSString *)region {
+ (instancetype)functionsForApp:(FIRApp *)app customDomain:(NSString *)customDomain {
return [[self alloc] initWithApp:app region:kFUNDefaultRegion customDomain:customDomain];
}

- (instancetype)initWithApp:(FIRApp *)app
region:(NSString *)region
customDomain:(NSString *)customDomain {
return [self initWithProjectID:app.options.projectID
region:region
customDomain:customDomain
auth:FIR_COMPONENT(FIRAuthInterop, app.container)
messaging:FIR_COMPONENT(FIRMessagingInterop, app.container)];
}

- (instancetype)initWithProjectID:(NSString *)projectID
region:(NSString *)region
customDomain:(NSString *)customDomain
auth:(nullable id<FIRAuthInterop>)auth
messaging:(nullable id<FIRMessagingInterop>)messaging {
self = [super init];
if (self) {
if (!region) {
FUNThrowInvalidArgument(@"FIRFunctions region cannot be nil.");
FUNThrowInvalidArgument(@"FIRFunctions region cannot nil.");
}
_fetcherService = [[GTMSessionFetcherService alloc] init];
_projectID = [projectID copy];
_region = [region copy];
_serializer = [[FUNSerializer alloc] init];
_region = [region copy] _customDomain = [customDomain copy] _serializer =
[[FUNSerializer alloc] init];
_contextProvider = [[FUNContextProvider alloc] initWithAuth:auth messaging:messaging];
_emulatorOrigin = nil;
}
Expand All @@ -148,6 +164,9 @@ - (NSString *)URLWithName:(NSString *)name {
if (_emulatorOrigin) {
return [NSString stringWithFormat:@"%@/%@/%@/%@", _emulatorOrigin, _projectID, _region, name];
}
if (_customDomain) {
return [NSString stringWithFormat:@"%@/%@/", _customDomain, name];
}
return
[NSString stringWithFormat:@"https://%@-%@.cloudfunctions.net/%@", _region, _projectID, name];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,32 @@ NS_SWIFT_NAME(Functions)
*/
+ (instancetype)functionsForRegion:(NSString *)region NS_SWIFT_NAME(functions(region:));

/**
* Creates a Cloud Functions client with the default app and given custom domain.
* @param customDomain A custom domain for the http trigger, such as "https://mydomain.com"
*/
+ (instancetype)functionsForCustomDomain:(NSString *)customDomain
NS_SWIFT_NAME(functions(customDomain:));

/**
* Creates a Cloud Functions client with the given app and region.
* @param app The app for the Firebase project.
* @param region The region for the http trigger, such as "us-central1".
* @param region The region for the http trigger, such as "us-central1"
*/
// clang-format off
// because it incorrectly breaks this NS_SWIFT_NAME.
+ (instancetype)functionsForApp:(FIRApp *)app
region:(NSString *)region;

/**
* Creates a Cloud Functions client with the given app and region.
* @param app The app for the Firebase project.
* @param customDomain A custom domain for the http trigger, such as "https://mydomain.com".
*/
// clang-format off
// because it incorrectly breaks this NS_SWIFT_NAME.
+ (instancetype)functionsForApp:(FIRApp *)app
region:(NSString *)region NS_SWIFT_NAME(functions(app:region:));
customDomain:(NSString *)customDomain NS_SWIFT_NAME(functions(app:customDomain:));
// clang-format on

/**
Expand Down
2 changes: 1 addition & 1 deletion Functions/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ integration test FirebaseFunctions:

### To Develop

- Run `pod gen FirebaseFunctions.podspec`
- Run `pod gen FirebaseFunctions.podspec --local-sources=./`
- `open gen/FirebaseFunctions/FirebaseFunctions.xcworkspace`

OR these two commands can be combined with
Expand Down