Receiving Promise<Void> instead of NSMutableURLRequest

320 views Asked by At

I'm attempting to login to my app, however, all my APIRequests are showing as Void instead of APIRequest. This function was working back in Swift 2 and has since gone through some minor changes to attempt to migrate from PromiseKit 4 to PromiseKit 6.

The function does touch a lot of other things, please leave a comment if you need to see more. The function giving me the errors is:

class func recover403(error: Error) -> Promise<NSData> {
        if case let APIError.InvalidToken(request) = error {
            // Log in, then continue the promise chain
            let login = RootViewController.sharedInstance.showLoginController()
            return login.then { request -> Promise<NSData> in
                return API.transmit(request: request as! APIRequest)
            }
        } else if case CocoaError.userActivityConnectionUnavailable = error {

            let vc = ErrorViewController(nibName: "ErrorViewController", bundle: nil)
            let actions = ["Try Again"]

            vc.actions = actions
            vc.promise.then { index -> Promise<NSData> in
                RootViewController.sharedInstance.popAlertViewController()
                return API.transmit(request: request as! APIRequest)
            }.catch({ (Error) in
                // No catch
            })

            RootViewController.sharedInstance.pushAlertViewController(nvc: vc)
            vc.errorLabel.text = "Please check your network connection and try again!"
        }
        return Promise<NSData>(error: error)
    }

'return API.transmit(request: request as! APIRequest)' is throwing

Error: Value of tuple type 'Void' has no member 'value'
Warning: Cast from 'Void' to unrelated type 'APIRequest' always fails

the 2nd 'return API.transmit(request: request as! APIRequest)' is throwing

Error: Generic parameter 'T' could not be inferred
Warning: Cast from '(String, Any?) -> Promise<_>' to unrelated type 'APIRequest' always fails

Here is the APIRequest.swift

import Foundation

class APIRequest: NSMutableURLRequest {

    var errorConditions: [APIRequestErrorCondition] = []

    init(_ path: String, JSON: Any? = nil) {
        let URL = NSURL(string: API.baseURL + path)!
        super.init(url: URL as URL, cachePolicy: .useProtocolCachePolicy, timeoutInterval: 60)

        // Settings for both GET and POST
        setValue(API.agent, forHTTPHeaderField: "User-Agent")
        setValue("application/json", forHTTPHeaderField: "Accept")

        // Settings for POST, which is assumed when JSON != nil
        if let JSON = JSON {
            httpMethod = "POST"
            httpBody = try! JSONSerialization.data(withJSONObject: JSON, options: [])
            setValue("application/json", forHTTPHeaderField: "Content-Type")
        }
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

}

And finally the transmit function:

class func transmit(request: APIRequest) -> Promise<NSData> {
    request.auth()
    return Promise<NSData> { seal in
        let task = URLSession.shared.dataTask(with: request as URLRequest, completionHandler: { (_data, _response, _error) in
            if let data = _data {
                seal.fulfill(data as NSData)
            } else if let error = _error {
                if case URLError.badServerResponse = error, let rsp = _response as? HTTPURLResponse {
                    seal.reject(BadUrlError.badResponse(rsp.statusCode))
                } else {
                    seal.reject(error)
                }
            } else {
                seal.reject(PMKError.invalidCallingConvention)
            }
            seal.fulfill(_data! as NSData)
        })
        task.resume()
    }
}

This error touches a lot, so let me know in the comments if there's a section missing that you need to see!

Added: 'request' declaration

class func request<T>(path: String, JSON: Any? = nil) -> Promise<T> {
    let request = APIRequest(path, JSON: JSON)
    request.errorConditions = [API.InvalidTokenCondition, API.BadResponseCodeCondition]
    return API.promise(request: request)
}

Added: 'login' declaration

func showLoginController() -> Promise<Void> {
    if let vc = modalController.vcs.last as? LoginViewController {
        return vc.promise
    }
    let vc = LoginViewController(nibName: "LoginViewController", bundle: nil)
    pushModalViewController(nvc: vc, animated: true)
    return vc.promise
}
0

There are 0 answers