{"id":18896126,"url":"https://github.com/udivankin/pico-ajax","last_synced_at":"2025-04-15T01:34:22.732Z","repository":{"id":47804621,"uuid":"90836500","full_name":"udivankin/pico-ajax","owner":"udivankin","description":"Very tiny (~ 1kb uncompressed) yet fully functional AJAX library with zero dependencies","archived":false,"fork":false,"pushed_at":"2023-01-06T16:20:52.000Z","size":380,"stargazers_count":5,"open_issues_count":8,"forks_count":0,"subscribers_count":4,"default_branch":"master","last_synced_at":"2024-10-06T06:53:36.322Z","etag":null,"topics":["ajax","axios","fetch","superagent","xmlhttprequest"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/udivankin.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2017-05-10T07:54:21.000Z","updated_at":"2023-10-13T23:16:50.000Z","dependencies_parsed_at":"2023-02-06T06:32:38.011Z","dependency_job_id":null,"html_url":"https://github.com/udivankin/pico-ajax","commit_stats":null,"previous_names":[],"tags_count":8,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/udivankin%2Fpico-ajax","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/udivankin%2Fpico-ajax/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/udivankin%2Fpico-ajax/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/udivankin%2Fpico-ajax/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/udivankin","download_url":"https://codeload.github.com/udivankin/pico-ajax/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":223655159,"owners_count":17180663,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["ajax","axios","fetch","superagent","xmlhttprequest"],"created_at":"2024-11-08T08:32:35.000Z","updated_at":"2024-11-08T08:32:35.101Z","avatar_url":"https://github.com/udivankin.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# PicoAjax\nUniversal, very tiny (browser version is ~1kb uncompressed) yet fully functional AJAX library with zero dependencies. It implements browser XMLHttpRequest and Node.js http/https module returning Promise.\n\n## Motivation\nWhat makes Pico-Ajax different is that it's unaware on how data is passed. That requires a few more bytes of code to make a request, but gives much more control and (more important) better understanding of HTTP requests in exchange. This also makes it perfect for building your own DIY API module.\n\n## Limitations\nSince server implementation is mostly synchronous it's not recommended to use PicoAjax in loaded projects.\n\n## Install\nVia npm:\n\n```\nnpm install --save pico-ajax\n```\n\nthen import pico-ajax module\n```javascript\nimport PicoAjax from 'pico-ajax';\n// or if you use CommonJS imports:\nconst PicoAjax = require('pico-ajax');\n```\n\nOr use as a legacy module (will be available as PicoAjax in a global scope):\n```html\n\u003cscript src=\"/scripts/picoajax.min.js\"\u003e\u003c/script\u003e\n```\n\n## API\n\nPicoAjax exposes all known http methods (connect, delete, get, head, options, patch, post and put) with two arguments: 'url' and 'options'.\n\nFirst argument is url of type string. Note that you should compose GET parameters by yourself:\n```javascript\n  const params = new URLSearchParams({ foo: \"bar\", page: 2 }).toString();\n  const url = `https://example.com?${params}`;\n\n  PicoAjax\n    .get(url)\n    .then(response =\u003e console.log('response received'));\n```\n\nSecond argument (options) is a plain object, whose keys override defaults below:\n```javascript\noptions: {\n  body: undefined,        // Request body, see details below\n  headers: {},            // Request headers, see details below\n  password: undefined,    // HTTP auth password\n  user: undefined,        // HTTP auth user\n  timeout: undefined,     // Request timeout\n  responseType: '',       // [Browser-only] Could be 'json|arraybuffer|blob|document|text',\n  async: true,            // [Browser-only] Could be helpful since e.g. workers lack async support\n  onProgress: undefined,  // [Browser-only] XMLHttpRequest onprogress callback\n  withCredentials: false, // [Browser-only] Whether should send cookies with cross-origin requests\n}\n```\n\nPicoAjax http methods return Promises which are resolved with Response object:\n```javascript\nresponse: {\n  body: any,                  // Response body, PicoAjax always tries to JSON.parse response body\n  headers: Object,            // Response headers\n  statusCode: number,         // Response status code, e.g. 200\n  statusMessage: string,      // Response status message, e.g. OK\n}\n```\nIn case http didn't succeed (response code other than 2xx, or another error), Promise is rejected with an Error instance with reponse fields added:\n```javascript\nerror: {\n  name: string,               // Error name, e.g. NetworkError\n  message: string,            // Error message, e.g. 500 Server Error\n  body: any,                  // Response body, PicoAjax always tries to JSON.parse response body\n  headers: Object,            // Response headers\n  statusCode: number,         // Response status code, e.g. 200\n  statusMessage: string,      // Response status message, e.g. OK\n}\n```\n\n## Usage\n\nYou may start right now with a simple GET request:\n```javascript\nPicoAjax\n  .get('/some/api/?foo=bar\u0026baz=qux')\n  .then(({ headers, body }) =\u003e {\n    console.log(headers, body);\n  })\n  .catch((error) =\u003e {\n    console.error(error.message, error.statusCode);\n  });\n\n// or if you prefer async/await\ntry {\n  const { headers, body } = await PicoAjax.get('/some/api/?foo=bar\u0026baz=qux');\n  console.log(headers, body);\n} catch (e) {\n  console.error(e.message, e.statusCode);\n}\n```\n**Multipart/form-data**\n\n```javascript\n// Prepare form data using DOM form (Browser only)\nconst formData = new FormData(document.querySelector('form'));\n\n// Or with a plain object \nconst foo = { bar: 'baz' };\nconst formData = new FormData();\n\nObject.keys(foo).forEach(key =\u003e {\n  formData.append(key, foo[key]);\n});\n\n// Perform POST request\nPicoAjax\n  .post('/some/api/', { body: formData })\n  .then(({ headers, body, statusCode }) =\u003e {\n    console.log(statusCode, headers, body);\n  })\n  .catch((error) =\u003e {\n    console.error(error.message, error.statusCode);\n  });\n```\n\n**JSON**\n\n```javascript\nconst body = JSON.stringify({ foo: 'bar', baz: 'qux' });\nconst headers = { 'Content-Type': 'application/json' };\n\nPicoAjax\n  .post('/some/api/', { body, headers })\n  .then(({ headers, body, statusCode }) =\u003e {\n    console.log(statusCode, headers, body);\n  })\n  .catch((error) =\u003e {\n    console.error(error.message, error.statusCode);\n  });\n```\n\n**File upload**\n\n```javascript\nconst formData = new FormData(); \nformData.append('userfile', fileInputElement.files[0]);\n\nPicoAjax\n  .post('/some/api/', { body: formData })\n  .then(({ headers, body, statusCode }) =\u003e {\n    console.log(statusCode, headers, body);\n  })\n  .catch((error) =\u003e {\n    console.error(error.message, error.statusCode);\n  });\n```\n\n## Advanced use\n\nIf you are going to make quite a few similar requests in your project, you probably\nmay want to make one more layer of abstraction over Pico-Ajax. Please refer to api-example.js\nmodule in examples directory.\n\n## License\n\nMIT found in `LICENSE` file.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fudivankin%2Fpico-ajax","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fudivankin%2Fpico-ajax","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fudivankin%2Fpico-ajax/lists"}