Skip to content

Instantly share code, notes, and snippets.

@jpoutrin
Created May 15, 2017 15:00
Show Gist options
  • Save jpoutrin/20d6236d9dc1b4cb52cff6960d834078 to your computer and use it in GitHub Desktop.
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
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