Integrate fetch for easier use.
Installation
npm install lesca-fetcher --save
Usage
import Fetch, { contentType, formatType } from 'lesca-fetcher';
Fetch.install({
hostUrl: 'https://yourhost.com/api/',
contentType: contentType.JSON,
formatType: formatType.JSON,
});
Fetch.setJWT('Fsr.956b6.67ktJGr');
<button
onClick={() => {
const path = '/save';
const data = { name: 'myName', age: '18' };
Fetch.post(path, data).then((respone) => {
// ...script
console.log(respone);
});
}}
>post</div>;
<button
onClick={() => {
const path = '/get';
Fetch.get(path).then((respone) => {
// ...script
console.log(respone);
});
}}
>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 | |
config
const config = {
hostUrl: 'https://www.yourHost.com/api/',
contentType: contentType.JSON,
formatType: formatType.JSON,
};
Features