Skip to content

Commit

Permalink
Remove custom Result type [SDK-2901] (#542)
Browse files Browse the repository at this point in the history
* Remove the custom Result type

* Document changes
  • Loading branch information
Widcket authored Nov 10, 2021
1 parent 0706663 commit 5be2582
Show file tree
Hide file tree
Showing 16 changed files with 59 additions and 103 deletions.
2 changes: 1 addition & 1 deletion App/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import Auth0

class ViewController: UIViewController {

var onAuth: ((Result<Credentials>) -> ())!
var onAuth: ((Auth0Result<Credentials>) -> ())!

override func viewDidLoad() {
super.viewDidLoad()
Expand Down
10 changes: 0 additions & 10 deletions Auth0.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,6 @@
5F23E6E51D4ACD8500C3F2D9 /* Request.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5FE2F8B71CD0E910003628F4 /* Request.swift */; };
5F23E6E61D4ACD8500C3F2D9 /* Requestable.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5FE2F8B11CCEAED8003628F4 /* Requestable.swift */; };
5F23E6E71D4ACD8500C3F2D9 /* Response.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5FE2F8BA1CD0EAAD003628F4 /* Response.swift */; };
5F23E6E81D4ACD8500C3F2D9 /* Result.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5FE2F8A81CCE54F1003628F4 /* Result.swift */; };
5F23E6E91D4ACD9200C3F2D9 /* Auth0.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5F06DDC81CC66B710011842B /* Auth0.swift */; };
5F23E6EA1D4ACD9600C3F2D9 /* Auth0Error.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5FD255B61D14F00900387ECB /* Auth0Error.swift */; };
5F23E6FE1D4B88C500C3F2D9 /* Auth0.h in Headers */ = {isa = PBXBuildFile; fileRef = 5F049B6C1CB42C29006F6C05 /* Auth0.h */; settings = {ATTRIBUTES = (Public, ); }; };
Expand All @@ -199,7 +198,6 @@
5F23E70E1D4B88FC00C3F2D9 /* Request.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5FE2F8B71CD0E910003628F4 /* Request.swift */; };
5F23E70F1D4B88FC00C3F2D9 /* Requestable.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5FE2F8B11CCEAED8003628F4 /* Requestable.swift */; };
5F23E7101D4B88FC00C3F2D9 /* Response.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5FE2F8BA1CD0EAAD003628F4 /* Response.swift */; };
5F23E7111D4B88FC00C3F2D9 /* Result.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5FE2F8A81CCE54F1003628F4 /* Result.swift */; };
5F23E71A1D4B891E00C3F2D9 /* Auth0.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5F06DDC81CC66B710011842B /* Auth0.swift */; };
5F23E71B1D4B891E00C3F2D9 /* Auth0Error.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5FD255B61D14F00900387ECB /* Auth0Error.swift */; };
5F28B4611D8216180000EB23 /* Loggable.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5F28B4601D8216180000EB23 /* Loggable.swift */; };
Expand Down Expand Up @@ -311,8 +309,6 @@
5FE1182B1D8A4A2B00A374BF /* Telemetry.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5FDE87461D8A422300EA27DC /* Telemetry.swift */; };
5FE2F8A61CCA9C17003628F4 /* CredentialsSpec.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5FE2F8A51CCA9C17003628F4 /* CredentialsSpec.swift */; };
5FE2F8A71CCA9C17003628F4 /* CredentialsSpec.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5FE2F8A51CCA9C17003628F4 /* CredentialsSpec.swift */; };
5FE2F8A91CCE54F1003628F4 /* Result.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5FE2F8A81CCE54F1003628F4 /* Result.swift */; };
5FE2F8AA1CCE54F1003628F4 /* Result.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5FE2F8A81CCE54F1003628F4 /* Result.swift */; };
5FE2F8B21CCEAED8003628F4 /* Requestable.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5FE2F8B11CCEAED8003628F4 /* Requestable.swift */; };
5FE2F8B31CCEAED8003628F4 /* Requestable.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5FE2F8B11CCEAED8003628F4 /* Requestable.swift */; };
5FE2F8B81CD0E910003628F4 /* Request.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5FE2F8B71CD0E910003628F4 /* Request.swift */; };
Expand Down Expand Up @@ -594,7 +590,6 @@
5FDE874E1D8A424700EA27DC /* Credentials.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Credentials.swift; sourceTree = "<group>"; };
5FDE874F1D8A424700EA27DC /* Handlers.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Handlers.swift; sourceTree = "<group>"; };
5FE2F8A51CCA9C17003628F4 /* CredentialsSpec.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = CredentialsSpec.swift; path = Auth0Tests/CredentialsSpec.swift; sourceTree = SOURCE_ROOT; };
5FE2F8A81CCE54F1003628F4 /* Result.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = Result.swift; path = Auth0/Result.swift; sourceTree = SOURCE_ROOT; };
5FE2F8B11CCEAED8003628F4 /* Requestable.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = Requestable.swift; path = Auth0/Requestable.swift; sourceTree = SOURCE_ROOT; };
5FE2F8B71CD0E910003628F4 /* Request.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = Request.swift; path = Auth0/Request.swift; sourceTree = SOURCE_ROOT; };
5FE2F8BA1CD0EAAD003628F4 /* Response.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = Response.swift; path = Auth0/Response.swift; sourceTree = SOURCE_ROOT; };
Expand Down Expand Up @@ -1053,7 +1048,6 @@
5FE2F8B71CD0E910003628F4 /* Request.swift */,
5FE2F8B11CCEAED8003628F4 /* Requestable.swift */,
5FE2F8BA1CD0EAAD003628F4 /* Response.swift */,
5FE2F8A81CCE54F1003628F4 /* Result.swift */,
);
name = Networking;
sourceTree = "<group>";
Expand Down Expand Up @@ -1664,7 +1658,6 @@
5FD255B41D14DD2600387ECB /* ManagementError.swift in Sources */,
5FE2F8B81CD0E910003628F4 /* Request.swift in Sources */,
5C41F6BB244E1DC100252548 /* UIApplication+Shared.swift in Sources */,
5FE2F8A91CCE54F1003628F4 /* Result.swift in Sources */,
5C41F6A4244DC94E00252548 /* BaseTransaction.swift in Sources */,
5F3965C21CF67CF000CDE7C0 /* WebAuth.swift in Sources */,
5FCAB1761D0900CF00331C84 /* TransactionStore.swift in Sources */,
Expand Down Expand Up @@ -1732,7 +1725,6 @@
5FDE875A1D8A424700EA27DC /* Authentication.swift in Sources */,
5C41F6C1244F964000252548 /* A0SHA.m in Sources */,
5C41F6D8244F976200252548 /* WebAuth.swift in Sources */,
5FE2F8AA1CCE54F1003628F4 /* Result.swift in Sources */,
5C41F6CC244F96F200252548 /* ClaimValidators.swift in Sources */,
5C41F6D9244F977900252548 /* WebAuthError.swift in Sources */,
5FCAB1741D09009600331C84 /* NSData+URLSafe.swift in Sources */,
Expand Down Expand Up @@ -1838,7 +1830,6 @@
5FDE87571D8A424700EA27DC /* Auth0Authentication.swift in Sources */,
5C4F552523C8FBA100C89615 /* JWKS.swift in Sources */,
5F23E6E51D4ACD8500C3F2D9 /* Request.swift in Sources */,
5F23E6E81D4ACD8500C3F2D9 /* Result.swift in Sources */,
5F23E6DD1D4ACD6100C3F2D9 /* NSURL+Auth0.swift in Sources */,
5F23E6E71D4ACD8500C3F2D9 /* Response.swift in Sources */,
5B2860D11EEAC30A00C75D54 /* UserInfo.swift in Sources */,
Expand Down Expand Up @@ -1869,7 +1860,6 @@
buildActionMask = 2147483647;
files = (
5F23E70E1D4B88FC00C3F2D9 /* Request.swift in Sources */,
5F23E7111D4B88FC00C3F2D9 /* Result.swift in Sources */,
5FDE87701D8A424700EA27DC /* Handlers.swift in Sources */,
5C4F551D23C8FB8E00C89615 /* String+URLSafe.swift in Sources */,
970BC36E25C27095007A7745 /* Challenge.swift in Sources */,
Expand Down
7 changes: 6 additions & 1 deletion Auth0/Auth0.swift
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
import Foundation

/**
Default scope value used across Auth0.swift
Auth0.swift wrapper for the Swift `Result` type
*/
public typealias Auth0Result<T> = Result<T, Error>

/**
Default scope value used across Auth0.swift
*/
public let defaultScope = "openid profile email"

Expand Down
4 changes: 2 additions & 2 deletions Auth0/Auth0WebAuth.swift
Original file line number Diff line number Diff line change
Expand Up @@ -123,9 +123,9 @@ final class Auth0WebAuth: WebAuth {
return self
}

func start(_ callback: @escaping (Result<Credentials>) -> Void) {
func start(_ callback: @escaping (Auth0Result<Credentials>) -> Void) {
guard let redirectURL = self.redirectURL else {
return callback(Result.failure(WebAuthError.noBundleIdentifierFound))
return callback(.failure(WebAuthError.noBundleIdentifierFound))
}
let handler = self.handler(redirectURL)
let state = self.parameters["state"] ?? generateDefaultState()
Expand Down
4 changes: 2 additions & 2 deletions Auth0/BaseTransaction.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import Foundation

class BaseTransaction: NSObject, AuthTransaction {

typealias FinishTransaction = (Result<Credentials>) -> Void
typealias FinishTransaction = (Auth0Result<Credentials>) -> Void

var authSession: AuthSession?
let state: String?
Expand All @@ -26,7 +26,7 @@ class BaseTransaction: NSObject, AuthTransaction {
}

func cancel() {
self.callback(Result.failure(WebAuthError.userCancelled))
self.callback(.failure(WebAuthError.userCancelled))
authSession?.cancel()
authSession = nil
}
Expand Down
6 changes: 3 additions & 3 deletions Auth0/NativeAuth.swift
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public protocol NativeAuthTransaction: AuthTransaction {
var authentication: Authentication { get }

/// Callback where the result of the native authentication is sent
typealias Callback = (Result<NativeAuthCredentials>) -> Void
typealias Callback = (Auth0Result<NativeAuthCredentials>) -> Void

/**
Starts the native Auth flow using the IdP SDK and once completed it will notify the result using the callback.
Expand All @@ -67,7 +67,7 @@ public protocol NativeAuthTransaction: AuthTransaction {
- parameter callback: closure that will notify with the result of the Auth transaction. On success it will yield the Auth0 credentilas of the user otherwise it will yield the cause of the failure.
- important: Only one `AuthTransaction` can be active at a given time, if there is a pending one (OAuth or Native) it will be cancelled and replaced by the new one.
*/
func start(callback: @escaping (Result<Credentials>) -> Void)
func start(callback: @escaping (Auth0Result<Credentials>) -> Void)
}

/**
Expand All @@ -89,7 +89,7 @@ public extension NativeAuthTransaction {
- parameter callback: closure that will notify with the result of the Auth transaction. On success it will yield the Auth0 credentilas of the user otherwise it will yield the cause of the failure.
- important: Only one `AuthTransaction` can be active at a given time, if there is a pending one (OAuth or Native) it will be cancelled and replaced by the new one.
*/
func start(callback: @escaping (Result<Credentials>) -> Void) {
func start(callback: @escaping (Auth0Result<Credentials>) -> Void) {
TransactionStore.shared.store(self)
self.auth { result in
switch result {
Expand Down
4 changes: 2 additions & 2 deletions Auth0/OAuth2Grant.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import Auth0ObjectiveC

protocol OAuth2Grant {
var defaults: [String: String] { get }
func credentials(from values: [String: String], callback: @escaping (Result<Credentials>) -> Void)
func credentials(from values: [String: String], callback: @escaping (Auth0Result<Credentials>) -> Void)
func values(fromComponents components: URLComponents) -> [String: String]
}

Expand Down Expand Up @@ -69,7 +69,7 @@ struct PKCE: OAuth2Grant {
self.defaults = newDefaults
}

func credentials(from values: [String: String], callback: @escaping (Result<Credentials>) -> Void) {
func credentials(from values: [String: String], callback: @escaping (Auth0Result<Credentials>) -> Void) {
guard let code = values["code"] else {
let string = "No code found in parameters \(values)"
return callback(.failure(AuthenticationError(string: string)))
Expand Down
4 changes: 2 additions & 2 deletions Auth0/Request.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import Foundation
```
*/
public struct Request<T, E: Auth0Error>: Requestable {
public typealias Callback = (Result<T>) -> Void
public typealias Callback = (Auth0Result<T>) -> Void

let session: URLSession
let url: URL
Expand Down Expand Up @@ -100,7 +100,7 @@ public struct ConcatRequest<F, S, E: Auth0Error>: Requestable {

- parameter callback: called when the request finishes and yield it's result
*/
public func start(_ callback: @escaping (Result<ResultType>) -> Void) {
public func start(_ callback: @escaping (Auth0Result<ResultType>) -> Void) {
let second = self.second
first.start { result in
switch result {
Expand Down
2 changes: 1 addition & 1 deletion Auth0/Requestable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ import Foundation
protocol Requestable {
associatedtype ResultType

func start(_ callback: @escaping (Result<ResultType>) -> Void)
func start(_ callback: @escaping (Auth0Result<ResultType>) -> Void)
}
42 changes: 0 additions & 42 deletions Auth0/Result.swift

This file was deleted.

2 changes: 1 addition & 1 deletion Auth0/WebAuth.swift
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ public protocol WebAuth: Trackable, Loggable {

- parameter callback: callback called with the result of the WebAuth flow
*/
func start(_ callback: @escaping (Result<Credentials>) -> Void)
func start(_ callback: @escaping (Auth0Result<Credentials>) -> Void)

/**
Removes Auth0 session and optionally remove the Identity Provider session.
Expand Down
4 changes: 2 additions & 2 deletions Auth0Tests/BaseTransactionSpec.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ class BaseTransactionSpec: QuickSpec {

override func spec() {
var transaction: BaseTransaction!
var result: Result<Credentials>? = nil
let callback: (Result<Credentials>) -> () = { result = $0 }
var result: Auth0Result<Credentials>? = nil
let callback: (Auth0Result<Credentials>) -> () = { result = $0 }
let authentication = Auth0Authentication(clientId: ClientId, url: Domain)
let generator = A0SHA256ChallengeGenerator()
let handler = PKCE(authentication: authentication,
Expand Down
14 changes: 7 additions & 7 deletions Auth0Tests/ManagementSpec.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class ManagementSpec: QuickSpec {

it("should yield success with payload") {
let response = Response<ManagementError>(data: data, response: http, error: nil)
var actual: Result<ManagementObject>? = nil
var actual: Auth0Result<ManagementObject>? = nil
management.managementObject(response: response) { actual = $0 }
expect(actual).toEventually(haveObjectWithAttributes(["key"]))
}
Expand All @@ -50,7 +50,7 @@ class ManagementSpec: QuickSpec {
let data = "NOT JSON".data(using: .utf8)!
let http = HTTPURLResponse(url: DomainURL, statusCode: 200, httpVersion: nil, headerFields: nil)
let response = Response<ManagementError>(data: data, response: http, error: nil)
var actual: Result<ManagementObject>? = nil
var actual: Auth0Result<ManagementObject>? = nil
management.managementObject(response: response) { actual = $0 }
expect(actual).toEventually(beFailure())
}
Expand All @@ -59,7 +59,7 @@ class ManagementSpec: QuickSpec {
let data = "[{\"key\": \"value\"}]".data(using: .utf8)!
let http = HTTPURLResponse(url: DomainURL, statusCode: 200, httpVersion: nil, headerFields: nil)
let response = Response<ManagementError>(data: data, response: http, error: nil)
var actual: Result<ManagementObject>? = nil
var actual: Auth0Result<ManagementObject>? = nil
management.managementObject(response: response) { actual = $0 }
expect(actual).toEventually(beFailure())
}
Expand All @@ -68,7 +68,7 @@ class ManagementSpec: QuickSpec {
let data = "[{\"key\": \"value\"}]".data(using: .utf8)!
let http = HTTPURLResponse(url: DomainURL, statusCode: 400, httpVersion: nil, headerFields: nil)
let response = Response<ManagementError>(data: data, response: http, error: nil)
var actual: Result<ManagementObject>? = nil
var actual: Auth0Result<ManagementObject>? = nil
management.managementObject(response: response) { actual = $0 }
expect(actual).toEventually(beFailure())
}
Expand All @@ -78,22 +78,22 @@ class ManagementSpec: QuickSpec {
let data = try? JSONSerialization.data(withJSONObject: error, options: [])
let http = HTTPURLResponse(url: DomainURL, statusCode: 400, httpVersion: nil, headerFields: nil)
let response = Response<ManagementError>(data: data, response: http, error: nil)
var actual: Result<ManagementObject>? = nil
var actual: Auth0Result<ManagementObject>? = nil
management.managementObject(response: response) { actual = $0 }
expect(actual).toEventually(haveManagementError("error", description: "description", code: "code", statusCode: 400))
}

it("should yield generic failure when no payload") {
let http = HTTPURLResponse(url: DomainURL, statusCode: 400, httpVersion: nil, headerFields: nil)
let response = Response<ManagementError>(data: nil, response: http, error: nil)
var actual: Result<ManagementObject>? = nil
var actual: Auth0Result<ManagementObject>? = nil
management.managementObject(response: response) { actual = $0 }
expect(actual).toEventually(beFailure())
}

it("should yield generic failure") {
let response = Response<ManagementError>(data: nil, response: nil, error: NSError(domain: "com.auth0", code: -99999, userInfo: nil))
var actual: Result<ManagementObject>? = nil
var actual: Auth0Result<ManagementObject>? = nil
management.managementObject(response: response) { actual = $0 }
expect(actual).toEventually(beFailure())
}
Expand Down
Loading

0 comments on commit 5be2582

Please sign in to comment.