Skip to content
This repository has been archived by the owner on Oct 9, 2022. It is now read-only.
/ api-querier Public archive

A simple wrapper around axios to work with remote API resources as though they were local

License

Notifications You must be signed in to change notification settings

sopherapps/api-querier

Repository files navigation

api-querier Build Status Coverage Status

THIS PACKAGE IS NO LONGER BEING MAINTAINED.

A simple wrapper around axios to work with remote API resources as though they were local

Installation

This is an NPM package. It can be used with any of your favourite JavaScript/Typescript frameworks, anywhere axios runs

npm install --save api-querier

Usage

This package provides two entities that you could use in your code. It exports, by default, a function that can make an ApiQuerier object.

        import { makeApiQuerier, IApiQuerier, IResourceQuerier} from 'api-querier';

        const resourceNames = ['books', 'tables', 'pens'];
        const baseURL = 'https://your_api_base_url.com';
        // Put the baseUrl of your api without trailing slash
        /*
        * In this example, the endpoints exposed by the remote API are
        *   - https://your_api_base_url.com/books
        *   - https://your_api_base_url.com/tables
        *   - https://your_api_base_url.com/pens
        */

        const apiQuerier: IApiQuerier = makeApiQuerier(resourceNames, baseURL,
        /*{...headers}, customResourceProcessorFunction // if you wish to have a different one
                                                        // but there is surely no need
         */);
        // apiQuerier is object whose keys are the resource names while
        // the values are IResourceQuerier objects

        const booksQuerier = apiQuerier['books'];
        const tablesQuerier = apiQuerier['tables'];
        const pensQuerier = apiQuerier['pens'];
        // signature of each individual querier /
        /*
        *interface IResourceQuerier {
            list: (queryObj?: any, headers?: any) => Promise<AxiosPromise<any>>;
            getOne: (id: string | number, headers?: any) => Promise<AxiosPromise<any>>;
            update: ((id: string | number, payload: any, headers?: any) =>
                     Promise<AxiosPromise<any>>);
            create: (payload: any, headers?: any) => Promise<AxiosPromise<any>>;
            destroy: (id: string | number, headers?: any) => Promise<AxiosPromise<any>>;
          }
        *
        */
       // each ResourceQuerier can list, getOne, update, destroy, create its items
       // each returning the same data an axios call would return; whose data is on
       // response.data

       const someAsyncFunction = () => {

         // list and filter books
         const booksQueryParams = {
           author: 'Some Name'
         };
         const someHeadersIfYouWant = {
           Authorization: 'Bearer some_token',
           'Content-Type': 'application/json',
         };
         const allBooksAuthoredBySomeName = await booksQuerier.list(booksQueryParams,
               someHeadersIfYouWant);

         // getOne table
         const tableOfIdRandomString = await tablesQuerier.getOne('RandomString',
               /*{...extraHeaders}*/);

         // update table
         await tablesQuerier.update('RandomString', {legs: 2}, /*, {...extraHeaders}*/);
         const updatedTableOfIdRandomString = await tablesQuerier.getOne('RandomString',
               /*{...extraHeaders}*/);
         updatedTableOfIdRandomString.data.legs == 2; // true

         // delete table
         const response = await tablesQuerier.delete(
              'RandomString', /*, {...extraHeaders}*/);
         const responseAfterDelete = await tablesQuerier.getOne('RandomString',
               /*{...extraHeaders}*/);
        responseAfterDelete.status === 404; //true

       }

Advanced Docs

For more details of each class, interface, enum etc, visit the TypeDoc generated docs site.

Acknowledgements

Axios is real nice!!

License

Copyright (c) 2019 Martin Ahindura Licensed under the MIT License

About

A simple wrapper around axios to work with remote API resources as though they were local

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published