nativescript-community / https

Secure HTTP client with SSL pinning for Nativescript - iOS/Android
https://nativescript-community.github.io/https/
Other
50 stars 42 forks source link

[IOS] Keeping response standard between AFSuccess and AFFailure #64

Closed kefahB closed 4 years ago

kefahB commented 4 years ago

Hi @EddyVerbruggen

There is inconsistency between AFSuccess and AFFailure on the response .. of course I understand it is not the same .. but when dealing with API REST we expecting to get sometimes status response like 301 or 422... the problem it is the AFFailure return the content in a new object content.body .. when trying to do some check stuff, we need a lot of change in our code to change response.content to response.content.body

I added a new objet failure instead of content and keeping content he same as AFSuccess.

let failure: any = {
    body: content,
    description: error.description,
    reason: error.localizedDescription,
    url: error.userInfo.objectForKey('NSErrorFailingURLKey').description
  };

  resolve({task, content, reason, failure: failure});
kefahB commented 4 years ago

If you think that will affect the others .. we can add a new options :

{
    useLegacy: true // default (false) the AFFailure will return the "old" system, set to true to get the new retrun
}

// in AFFailure function add 
function AFFailure(resolve, reject, task: NSURLSessionDataTask, error: NSError) {
    // ... (code)
    if(this.opts.useLegacy) {
         let failure: any = {
            body: content,
            description: error.description,
            reason: error.localizedDescription,
            url: error.userInfo.objectForKey('NSErrorFailingURLKey').description
        };
        if (policies.secured === true) {
            failure.description = 'nativescript-https > Invalid SSL certificate! ' + error.description;
        }
        let reason = error.localizedDescription;
        resolve({task, content, reason, failure: failure});
    } else {
        let content: any = {
              body,
              description: error.description,
              reason: error.localizedDescription,
              url: error.userInfo.objectForKey('NSErrorFailingURLKey').description
        };

        if (policies.secured === true) {
           content.description = 'nativescript-https > Invalid SSL certificate! ' + content.description;
        }

        let reason = error.localizedDescription;
        resolve({task, content, reason});
   }
}
kefahB commented 4 years ago

Hi @EddyVerbruggen .. what do you think about this PR ?

kefahB commented 4 years ago

Hi @EddyVerbruggen,

I hope you have good health you and your family !

Any chance to view this please ?

EddyVerbruggen commented 4 years ago

Sorry for the delay my man! All is well over here, hope you and your loved ones are safe too!

I've merged and published your PR (v 2.1.0).

All the best, Eddy