Skip to content

akgunfu/react-native-basic-api

Repository files navigation

Introduction

This library is a basic api implementation I used in several hobby projects. Featurewise it is limited as of now, but I will implement extra features when needed. It can be used in many different javascript projects, it is not only limited to React Native as the name could imply.

Installation

npm i react-native-basic-api

Usage

import _api from 'react-native-basic-api';

/* Define your endpoints with available methods */
const yourEndpoints = ({get, post, put}) => ({
    getSomething1: (foo) => get('/endpoint1?foo=${foo}'),
    getSomething2: (data) => get('/endpoint2', data),
    postSomething1: (data) => post('/endpoint3', data),
    putSomething1: (data) => put('/endpoint4', data)
});

/* configure baseUrl, globalHeaders and endpoints */
const apiConfig = {
    baseUrl: 'http://someurl.com',
    globalHeaders: {},
    endpoints: yourEndpoints
}

const api = _api(apiConfig);

/* Either use with promise.then() or async/await */
/* Promise then() */
api.getSomething1(foo)
    .then(response => /* do anything you want with response */)
    .catch(e => /* error */)

/* async/await */
try {
    const response = await api.postSomething1(data);
    /* do anything you want with response */
} catch(e){
    /* handle error */
}

About

Basic api I used mostly in personal hobby projects

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published