Coder Social home page Coder Social logo

radi-fetch's Introduction

radi-fetch

radi-fetch is the official HTTP client for Radi.js. It deeply integrates with Radi for seamless application building.

npm version npm downloads gzip bundle size discord

Installation

To install the stable version:

npm install --save radi-fetch

This assumes you are using npm as your package manager.

If you're not, you can access these files on unpkg, download them, or point your package manager to them.

Browser Compatibility

radi-fetch currently is compatible with browsers that support ES5.

Getting started

Here is how it works. We start by initiating plugin for Radi Radi.plugin(/* radi-fetch */, /* config */). Config here is optional, but we can define crucial parts of http client.

import RadiFetch from 'radi-fetch'

Radi.plugin(RadiFetch)

or

import RadiFetch from 'radi-fetch'

Radi.plugin(RadiFetch, {
  /* We can define base url */
  baseUrl: 'https://example.com',

  /* We can define headers */
  headers: {
    'Authorization': 'key',
  },

  /* We can define dummy data that will be returned for requests made with radi-fetch */
  dummy: true,           // Should return dummy data or use real requests
  dummyTimeout: 1000,    // Dummy data latency simulation
  dummyData: {           // Actual dummy data
    get: {
      '/api': {
        hello: 'world',
      },
    },
  },
});

That's it, we're ready to go. We can start fetching our api or whatever we want by using fetch by $fetch[type](url, params, headers) handle. Response can be automatically parsed as JSON by using .json() method, or .text() for plain text response.

class MyComponent extends Radi.Component {
  on() {
    return {
      mount() {
        this.$fetch.get('/test')
          .then(response => {
            console.log(response.json())
          })
          .catch(error => {
            console.log('Got some', error)
          })
      }
    }
  }
}

Fetch automatically sets loading state for any ongoing request. We can listen to them in radi using $loading handle.

/* @jsx Radi.r */

class MyComponent extends Radi.Component {
  view() {
    return (
      <ul>
        <li>/api is { this.$loading.state['/api'] ? 'loading' : 'ready' }</li>
        <li>Number of active requests: { this.$loading.state.$count }</li>
        <li>Anything loading: { this.$loading.state.$any }</li>
      </ul>
    )
  }
}

Stay In Touch

License

MIT

Copyright (c) 2018-present, Marcis (Marcisbee) Bergmanis

radi-fetch's People

Contributors

marcisbee avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

Forkers

the-cc-dev

radi-fetch's Issues

Use a class for HTTP

It's currently using prototype syntax. The class syntax would be much cleaner.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.