Guest User

Untitled

a guest
Jun 25th, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.70 KB | None | 0 0
  1. function RestApi(endpoint, options) {
  2. this.endpoint = (endpoint || "/").replace(/\/$/, "");
  3. this.options = options || {};
  4. // Allow usage in queue, or in other places where the value of "this" is incorrect:
  5. for (let method in ["GET", "POST", "PUT", "DELETE", "HEAD", "PATCH"].reduce((p,c)=>p[c]=1,{})) {
  6. this[method] = RestApi.prototype[method].bind(this);
  7. }
  8. }
  9. RestApi.fetchResponseHandler = function fetchResponseHandler(hardFail, res) {
  10. return new Promise((resolve, reject) => {
  11. (res.headers.get("Content-Type").match(/^application\/json(?:$|;)/) ? res.json() : res.text()).then((body) => {
  12. res.content = body;
  13. if (!res.ok && hardFail) reject(res);
  14. else resolve(res);
  15. }, reject);
  16. })
  17. };
  18. RestApi.prototype.send = function send(method, path, headers, options) {
  19. return fetch(this.endpoint + path.replace(/^([^/])/, "/$1"), {
  20. ...this.options,
  21. ...options,
  22. method,
  23. headers: { ...this.options.headers, ...(options ? options.headers : {}), ...headers }
  24. }).then(RestApi.fetchResponseHandler.bind(null, this.options.hardFail ? (options || {}).hardFail !== false : (options || {}).hardFail));
  25. };
  26. RestApi.prototype.sendWithBody = function sendWithBody(method, path, body, headers, options) {
  27. let type = "text/plain";
  28. if (body instanceof FormData) type = "multipart/form-data";
  29. else if (body instanceof URLSearchParams) type = "application/x-www-form-urlencoded";
  30. else if (body instanceof Object) { type = "application/json"; body = JSON.stringify(body); }
  31.  
  32. return this.send(method, path, headers, {
  33. ...options,
  34. body,
  35. headers: { "Content-Type": type, ...(options ? options.headers : {}) }
  36. });
  37. }
  38. /**
  39. * Send a GET request to the API
  40. * @param {string} path Path of the REST endpoint to request
  41. * @param {object} headers Headers to set on the request
  42. * @param {object} options Other options to forward to fetch()
  43. * @returns {Promise<{body, response}>}
  44. */
  45. RestApi.prototype.GET = function GET(path, headers, options) {
  46. return this.send("GET", path, headers, options);
  47. };
  48.  
  49. /**
  50. * Send a POST request to the API
  51. * @param {string} path Path of the REST endpoint to request
  52. * @param {string|object|FormData|URLSearchParams} body Request body to send to the API; the content type will be determined automatically (text/plain, application/json, multipart/form-data, application/x-www-form-urlencoded)
  53. * @param {object} headers Headers to set on the request
  54. * @param {object} options Other options to forward to fetch()
  55. * @returns {Promise<{body, response}>}
  56. */
  57. RestApi.prototype.POST = function POST(path, body, headers, options) {
  58. return this.sendWithBody("POST", path, body, headers, options);
  59. };
  60.  
  61. /**
  62. * Send a PUT request to the API
  63. * @param {string} path Path of the REST endpoint to request
  64. * @param {string|object|FormData|URLSearchParams} body Request body to send to the API; the content type will be determined automatically (text/plain, application/json, multipart/form-data, application/x-www-form-urlencoded)
  65. * @param {object} headers Headers to set on the request
  66. * @param {object} options Other options to forward to fetch()
  67. * @returns {Promise<{body, response}>}
  68. */
  69. RestApi.prototype.PUT = function PUT(path, body, headers, options) {
  70. return this.sendWithBody("PUT", path, body, headers, options);
  71. };
  72.  
  73. /**
  74. * Send a DELETE request to the API
  75. * @param {string} path Path of the REST endpoint to request
  76. * @param {object} headers Headers to set on the request
  77. * @param {object} options Other options to forward to fetch()
  78. * @returns {Promise<{body, response}>}
  79. */
  80. RestApi.prototype.DELETE = function DELETE(path, headers, options) {
  81. return this.send("DELETE", path, headers, options);
  82. };
  83.  
  84. /**
  85. * Send a HEAD request to the API
  86. * @param {string} path Path of the REST endpoint to request
  87. * @param {object} headers Headers to set on the request
  88. * @param {object} options Other options to forward to fetch()
  89. * @returns {Promise<Response>}
  90. */
  91. RestApi.prototype.HEAD = function HEAD(path, headers, options) {
  92. return this.send("HEAD", path, headers, options);
  93. };
  94.  
  95. /**
  96. * Send a PATCH request to the API
  97. * @param {string} path Path of the REST endpoint to request
  98. * @param {string|object|FormData|URLSearchParams} body Request body to send to the API; the content type will be determined automatically (text/plain, application/json, multipart/form-data, application/x-www-form-urlencoded)
  99. * @param {object} headers Headers to set on the request
  100. * @param {object} options Other options to forward to fetch()
  101. * @returns {Promise<{body, response}>}
  102. */
  103. RestApi.prototype.PATCH = function PATCH(path, body, headers, options) {
  104. return this.sendWithBody("PATCH", path, body, headers, options);
  105. };
Add Comment
Please, Sign In to add comment