Created
November 28, 2018 15:28
-
-
Save bentstamnes/e4efbab9d9ba5fe0adc8d48197e3d203 to your computer and use it in GitHub Desktop.
This file contains 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
function NFetch(url, request, callBack) {}; // Makes async HTTP request and returns the response in the following update frame. | |
class Headers { | |
// This is essentially a list of keys that have a bunch of functions to act on them. The JS functions below act upon a list of headers. | |
// When you are ready to make the call with CURL, you're going to iterate over this JS list and append them with: | |
// headers = curl_slist_append(headers, "Content-Type: text/xml"); <-- Inserting data as below | |
// | |
// i.e. | |
// struct curl_slist *headers=NULL; | |
// LOOP THROUGH HEADER KEYS FROM JS | |
// If value is not null do: headers = curl_slist_append(headers, "<key>: <value>"); | |
// If value is null do: headers = curl_slist_append(headers, "<key;>"); | |
// END LOOP | |
// res = curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers); | |
append(key, value) {}; // Appends a new value onto an existing header inside a Headers object, or adds the header if it does not already exist. | |
set(key, value) {}; // Sets a new value for an existing header inside a Headers object, or adds the header if it does not already exist. | |
delete(key); // Deletes a header from a Headers object. | |
get(key) {}; // Returns a ByteString sequence of all the values of a header within a Headers object with a given name. | |
has(key) {}; | |
// Functions that we won't support | |
// entries() | |
// forEach() | |
// keys() | |
// values() | |
} | |
class Request { | |
constructor() { | |
this.method = "GET"; // "GET", "POST", "PUT", "DELETE" -- curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "PUT"); | |
this.headers = new Headers(); // Key pair values for the header (these get turned into a string) See Headers class | |
this.body = none; // Payload when using POST or PUT -- curl_easy_setopt(curl, CURLOPT_POSTFIELDS, body); | |
this.redirect = "follow"; // "follow" or "error" or "manual". If "follow" set the following: curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L); | |
this.saveToPath = ""; // If populated with a path string saves the return response to a file specified by the path. | |
this.referrer = "no-referrer"; // If not "no-referrer" or "client" , then: curl_easy_setopt(curl, CURLOPT_REFERER, "http://example.com/aboutme.html"); | |
// Ignored values | |
// mode | |
// credentials | |
// integrity | |
// cache | |
} | |
} | |
class Response { | |
constructor() { | |
this.ok = false; // The if curlError = 0 and the response code is between 200-299 then this will be true. curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &response_code); | |
this.headers = new Headers(); // Response headers. Populate a Headers object using code similar to this: http://permalink.gmane.org/gmane.comp.web.curl.library/28803 | |
this.status = "200"; // Integer number of the status code. curl_easy_getinfo (session, CURLINFO_RESPONSE_CODE, &http_code); | |
this.saveSuccess = false; // If a save to path was requested, then return whether the file was succesfully written. | |
this.curlErrorCode = 0; // The resulting number from the CURL call | |
this.curlErrorText = ""; // char errbuf[CURL_ERROR_SIZE]; | |
// curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, errbuf); | |
// errbuf[0] = 0; | |
// Then read back errbuf | |
this.url = ""; // Gets the final response URL. curl_easy_getinfo(curl, CURLINFO_REDIRECT_URL, &location); | |
} | |
text() {} // Return the body as a text string | |
json() {} // Return the body as a JSON | |
// Not implementing | |
// Body | |
// blob() | |
// formData() | |
// arrayBuffer() | |
// statusText | |
// type | |
// useFinalURL | |
// clone() | |
// error() | |
// redirect() | |
} | |
// ------- Example --------- | |
req = { method: 'GET', headers: { 'Content-Type': 'application/json' } }; | |
NFetch("https://example.com/endpoint", req, weatherResponse); | |
function weatherResponse(response) { | |
if (response.ok && response.status == 200) { | |
console.log(response.text()); | |
} | |
} | |
// Things we're deliberately NOT implementing right now: | |
// - Form submission | |
// - File uploads to end points (but we will support downloads | |
// - CORS - libcurl should just ignore limitations |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment