Created
May 15, 2017 15:00
-
-
Save jpoutrin/20d6236d9dc1b4cb52cff6960d834078 to your computer and use it in GitHub Desktop.
This snippet can be used as a client parameter for swagger-client v2 on react native
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export const reactNativeHttpClient = { | |
// implment an execute function | |
execute: async function (obj) { | |
const httpMethod = obj.method; | |
const requestHeaders = obj.headers; | |
const body = obj.body; | |
const url = obj.url; | |
const response = await fetch(obj.url, { | |
method: httpMethod, | |
headers: requestHeaders, | |
body: body | |
}); | |
try { | |
const out = { | |
url: url, | |
method: httpMethod, | |
status: response.status, | |
statusText: response.statusText, | |
data: await response.text(), | |
headers: response.headers, | |
}; | |
out.obj = out.data ? JSON.parse(out.data) : null; | |
obj.on.response(out); | |
} | |
catch (error) { | |
obj.on.error(error); | |
} | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment