lesca-fetcher
TypeScript icon, indicating that this package has built-in type declarations

1.1.7 • Public • Published

NPM React React React React React NPM

Integrate fetch for easier use.

Installation

npm install lesca-fetcher --save

Usage

import Fetcher, { contentType, formatType } from 'lesca-fetcher';

Fetcher.install({
  hostUrl: 'https://yourhost.com/api/',
  contentType: contentType.JSON,
  formatType: formatType.JSON,
});

Fetch.setJWT('Fsr.956b6.67ktJGr'); // if necessary


<button
  onClick={() => {
    const path = '/save';
    const data = { name: 'myName', age: '18' };
    Fetcher.post(path, data).then((respond) => {
      console.log(respond);
    });
  }}
>post</div>;

<button
  onClick={() => {
    const path = '/get';
    Fetcher.get(path).then((respond) => {
      console.log(respond);
    });
  }}
>get</div>;

Development

Methods

method description default
.install(config:object) install first
.post(api:string, data:object) POST
.get(api:string) GET
.setJWT(token):string set JWT Token
.postStringify(api:string, data:object):string post and stringify
.mergePath(api:string):string get full path

config

const config = {
  hostUrl: 'https://www.yourHost.com/api/', // string
  contentType: contentType.JSON, // enum contentType.JSON || contentType.URL_ENCODED
  formatType: formatType.JSON, // enum formatType.JSON || formatType.string
};

Features

  • maintain if necessary

/lesca-fetcher/

    Package Sidebar

    Install

    npm i lesca-fetcher

    Weekly Downloads

    35

    Version

    1.1.7

    License

    MIT

    Unpacked Size

    312 kB

    Total Files

    30

    Last publish

    Collaborators

    • jameshsu1125