Coder Social home page Coder Social logo

haxe-axios's Introduction

#Haxe externs for axios

Axios is a promise based HTTP client for the browser and node.js

haxelib install axios

Then put -lib axios into your hxml.

Examples

Performing a GET request

var axios = axios.Axios.create();
var axios:axios.Axios.AxiosInstance = untyped __js__('axios.create()');

axios.get('/user?ID=12345')
    .then(function(response: axios.AxiosResponse<Dynamic>) {
        trace(response);
    })
    .catchError(function(error) {
        trace(error);
    });
var axios = axios.Axios.create();

var config:axios.AxiosRequestConfig = {
    params: {
        ID: 12345
    }
};

axios.get('/user', config)
    .then(function(response: axios.AxiosResponse<Dynamic>) {
        trace(response);
    })
    .catchError(function(error) {
        trace(error);
    });
new axios.Axios('/user/12345');

Performing a POST request

var axios = axios.Axios.create();

var data = {
    firstName: 'Fred',
    lastName: 'Flintstone'
};

axios.post('/user', data)
    .then(function(response: axios.AxiosResponse<Dynamic>) {
        trace(response);
    })
    .catchError(function(error) {
        trace(error);
    });
new axios.Axios({
    method: 'post',
    url: '/user/12345',
    data: {
        firstName: 'Fred',
        lastName: 'Flintstone'
    }
});

###Defaults

var instance = axios.Axios.create({
    timeout: 1000,
    headers: {'X-Custom-Header': 'foobar'}
});

instance.get('/user?ID=12345');
var instance = axios.Axios.create({
    baseURL: 'https://api.example.com'
});

// Alter defaults after instance has been created

instance.defaults.headers.common.Authorization = 'some secret token';

instance.get("/");

###Interceptors

var instance = axios.Axios.create({
    baseURL: 'https://httpbin.org/'
});

// Add a request interceptor
instance.interceptors.request.use(function (config:axios.AxiosRequestConfig) {
    trace(config);

    return config;
}, function (error) {
    trace(error);
    
    return js.Promise.reject(error);
});

// Add a response interceptor
instance.interceptors.response.use(function (response:axios.AxiosResponse<Dynamic>) {
    trace(response);

    return response;
}, function (error) {
    trace(error);

    return js.Promise.reject(error);
});

instance.get("/get");

If you may need to remove an interceptor later you can.

var instance = axios.Axios.create({
    baseURL: 'https://httpbin.org/'
});

var interceptorId = instance.interceptors.request.use(function (config:axios.AxiosRequestConfig) {
    trace(config);
    return config;
});

instance.interceptors.request.eject(interceptorId);

instance.get("/get");

Axios JS dependency

There are 2 ways to link the Axios JS library:

Require method (default)

By default the library uses require('axios') to reference Axios.

This means you are expected to use npm to install this dependency:

npm install axios

Global JS

The other common method is to download or reference the CDN files of Axios in your HTML page:

<script src="https://cdnjs.cloudflare.com/ajax/libs/axios/0.15.3/axios.js"></script>

and don't forget to add the following Haxe define to your build command:

-D axios_global

haxe-axios's People

Contributors

hlizard avatar

Watchers

 avatar

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.