New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

irajs

Package Overview
Dependencies
Maintainers
1
Versions
9
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

irajs

Ira Fetch - Vanilla JS Fetch API wrapper with goodies 🍒

  • 0.0.4
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
1
Maintainers
1
Weekly downloads
 
Created
Source

Ira Fetch Wrapper

Ira Fetch: Vanilla JS Fetch API wrapper with goodies 🍒

Ira is a window.fetch API wrapper with some extra stuff, debug logs, persistent settings and custom currying for requesting functions with a set of settings.

This intends to be writtend just using current JS engine features, no babel or typescript used. It's plain vanilla Javascript.

NPM Install

npm install --save irajs

Yarn Install

yarn add irajs

CDN

<script src="https://d3portillo.github.io/ira/src/index.js"></script>

Long live to Github Pages : )

Examples

GET

ira.get(`https://postman-echo.com/get?foo1=bar1&foo2=bar2`).then(({ data }) => {
  console.log(data.json, data.text, data.blob)
  // * Automatic response parsing
  /*
    You can do it also parsing params as
    ira.get(URL, { params: { foo1: "bar1", foo2: "bar2" } })
  */
})

POST

ira
  .post(`https://postman-echo.com/post`, {
    body: "This body will be returned",
  })
  .then(({ data }) => {
    console.log(data.json, data.text, data.blob)
    // Automatic response parsing
  })

Set global headers

Usefull if doing different request with auth stuff

  • Set all request to application/json Content-Type
ira.config({
  headers: {
    "Content-type": "application/json",
  },
})
  • Now, all your requests will include those headers
ira.get("https://something").then(console.info)

Extend a Ira fork

A custom settings fork of main Ira function that's gapped to provided - config

  • Set debug mode
const nfetch = ira.extend({
  headers: {
    "Content-type": "application/json",
  },
  debug: true,
  parseBlob: false /* Do not include .blob body response */,
})
  • You can now fetch data with those settings
nfetch.get("https://something").then(({ blob }) => console.info(null == blob))
  • Fetching with keys and session that contain same config
const request = ira.extend({
  headers: {
    "x-api-key": "somsaltedencryptedawesomekey",
  },
  debug: true /* Show Ira stuff on console */,
  baseURL: "https://someendpoint"
  parseBlob: false /* Do not include .blob body response */
})

request.get("/stuff").then(({ data })=> console.log({ data }))
request.get("/post", { headers: { "a-header": "a-value" } }).then(({ data })=>{
  console.log({data})
})

Fetching with params

URL/?yourparams=avalue

ira.get(`anendpoint`, {
  params: {
    token: 222,
    "another-token": 354,
  },
})
// http://anendpoint/?token=222&another-token=354

Ira Object instances

IRA_RESPONSE = {
  data: { json: Object, text: String, blob: ?Blob }
  ok: Boolean,
  status: Number,
  statusText: String,
  statusCode: status<Number>,
  error:?Error
}
IRA_REQUEST_PROPS = {
  headers: {},
  body: ?String,
  ...({
    Request:`https://developer.mozilla.org/en-US/docs/Web/API/Request`
  })
}
IRA_SETTINGS = {
  headers: {},
  debug: Boolean,
  parseBlob: Boolean,
  baseURL: ?String,
}
IRA_HTTP_METHODS = {
  get: Function,
  put: Function,
  post: Function,
  head: Function,
  delete: Function,
  connect: Function,
  options: Function,
  trace: Function,
}

// Exported object  {Main}
ira = {
  ...IRA_HTTP_METHODS,
  default(): IRA_HTTP_METHODS.get,
  _settings: Object,
  reset: Function,
  config: Function,
  extend: IRA_HTTP_METHODS
}
Name/InstanceParams ?ReturnsComments
IRA_HTTP_METHODS(URL,IRA_REQUEST_PROPS)Promise<IRA_RESPONSE>Fetch API HTTP Methods
default()(URL,IRA_REQUEST_PROPS)Promise<IRA_RESPONSE>When you do Ira("URL")
_settingsNONEVoidAcces current Ira global settings
reset()NONEVoidResets persistence settings to default
config()IRA_SETTINGSVoidSet ira settings (This affects all requests)
extend()IRA_SETTINGSIRA_HTTP_METHODSReturns a fork of Ira with just HTTP Methods and provided Ira Settings

Ira will return a void response if an error ocurred and status of 500, I'm currently working on a way of returning status on error


Ira stands for: Go to, rage or anger. That's all the feelings you have while handling HTTP stuff : )

  • Source: ./src/index.js
  • Changelog: ./CHANGELOG.md

TODO

  • Unit tests
  • Some custom examples
  • Live examples on Codepen or similar

Keywords

FAQs

Package last updated on 13 Aug 2020

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc