npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

vivy-api

v3.3.1

Published

A Vivy plugin to handle apis in Vivy model to make api calling easier.

Downloads

141

Readme

vivy-api

NPM Version License

A Vivy plugin which extend Vivy model to request api more easily.

Docs

Installation

Using npm:

$ npm install vivy vivy-api

Examples

Examples in repository

$ cd ./examples/[EXAMPLE_NAME]
$ npm run start

Example names:

Complete and real project example

Documentation

Basic usage

index.js

import React from 'react';
import {render} from 'react-dom';
import {Provider} from 'react-vivy';

// Import Vivy
import Vivy, {registerModel} from 'vivy';

// Import Vivy api plugin
import VivyApi from 'vivy-api';

// Import sync component and model
import App from 'path_to_app_component';
import app from 'path_to_app_model';

// Create vivy
const vivy = Vivy();

// Apply api plugin
vivy.use(VivyApi({

    // Customized api status model name space ( default is "apiStatus" )
    // apiStatusModelNameSpace: 'customizedApiStatus',

    // Customized check response status callback
    // to tell whether response is successful
    checkResponseStatus: response => response?.data?.code === 2000,

    // A middleware like callback to handle the success response
    successResponseHandler: ({dispatch, getState}) => next => action => {
        // Do something when request successfully.
    },

    // A middleware like callback to handle the failure response
    failureResponseHandler: ({dispatch, getState}) => next => action => {
        // Do something when request failure.
    }

}));

// Create store after configuration
const store = vivy.createStore();

// Register model to store
registerModel(store, app);

render(
    <Provider store={store}>
        <App/>
    </Provider>,
    document.getElementById('app-container')
);

App.js

import React from 'react';
import {useModelActions} from 'react-vivy';

const App = ({
    getDataStatus, getData
}) => {

    // Get api from model using hook "useModelActions".
    const {getData} = useModelActions('app');

    // Get loading using "isRequest".
    const loading = getData.isRequest();

    // Or you can get "getData" api status using hook "useIsApiRequest".
    // import {useIsApiRequest} from 'vivy-api';
    // const loading = useIsApiRequest('app/getData');

    return (
        <button disabled={loading}
                onClick={getData}>
            {
                loading ?
                    'Loading'
                    :
                    'Get data'
            }
        </button>
    );

};

export default App;

app.js

export default {
    nameSpace: 'app',
    state: null,
    apis: {

        // Call api to get data
        getData: () => (dispatchApi, dispatch, getState) => {
            dispatchApi({
                api: YOUR_GET_DATA_API,
                params: {
                    // Api params
                },
                successCallback: () => {
                    // Do something when request successfully.
                },
                failureCallback: () => {
                    // Do something when request failure.
                }
            });
        }

    },
    reducers: {

        // These three reducers will be dispatched automatically after response.
        getDataRequest: (state, payload) => {
            return null;
        },
        getDataSuccess: (state, {responseData}) => {
            return responseData;
        },
        getDataFailure: (state, payload) => {
            return null;
        }

    }
};

Model api methods

getStatus

import {useModelActions} from 'react-vivy';

const {getData} = useModelActions('app');

// "REQUEST" / "SUCCESS" / "FAILURE"
const apiStatus = getData.getStatus();

isRequest

import {useModelActions} from 'react-vivy';

const {getData} = useModelActions('app');
const isGettingData = getData.isRequest();

isSuccess

import {useModelActions} from 'react-vivy';

const {getData} = useModelActions('app');
const isGetDataSuccess = getData.isSuccess();

isFailure

import {useModelActions} from 'react-vivy';

const {getData} = useModelActions('app');
const isGetDataFailure = getData.isFailure();

Hooks

useStatus

import {useModelActions} from 'react-vivy';
import {useStatus} from 'vivy-api';

const {getData} = useModelActions('app');
const status = useStatus(getData);

useIsRequest

import {useModelActions} from 'react-vivy';
import {useIsRequest} from 'vivy-api';

const {getData} = useModelActions('app');
const isRequest = useIsRequest(getData);

useIsSuccess

import {useModelActions} from 'react-vivy';
import {useIsSuccess} from 'vivy-api';

const {getData} = useModelActions('app');
const isSuccess = useIsSuccess(getData);

useIsFailure

import {useModelActions} from 'react-vivy';
import {useIsFailure} from 'vivy-api';

const {getData} = useModelActions('app');
const isFailure = useIsFailure(getData);

useApiStatus

  1. Get specific api status by model name sapce and api name.
import {useApiStatus} from 'vivy-api';

const apiStatus = useApiStatus('model_name_space/api_name');
  1. Get all apis status in a model by model name sapce and api name.
import {useApiStatus} from 'vivy-api';

const apiStatuses = useApiStatus('model_name_space');
  1. Get specific api status by callback function.
import {useApiStatus} from 'vivy-api';

const apiStatus = useApiStatus(state => state.model_name_space.api_name);
  1. Get all apis status in a model by callback function.
import {useApiStatus} from 'vivy-api';

const apiStatuses = useApiStatus(state => state.model_name_space);

useIsApiRequest

import {useIsApiRequest} from 'vivy-api';

const isApiRequest = useIsApiRequest('model_name_space/api_name');

useIsApiSuccess

import {useIsApiSuccess} from 'vivy-api';

const isApiSuccess = useIsApiSuccess('model_name_space/api_name');

useIsApiFailure

import {useIsApiFailure} from 'vivy-api';

const isApiFailure = useIsApiFailure('model_name_space/api_name');