lesca-fetcher
Version:
integrate fetch for easier use
79 lines (61 loc) • 2.9 kB
Markdown
[](https://www.npmjs.com/)
[](https://nodejs.org/en/)
[](https://zh-hant.reactjs.org/)
[](https://lesscss.org/)
[](https://www.w3schools.com/html/)
[](https://www.w3schools.com/css/)
[](https://www.npmjs.com/~jameshsu1125)
Integrate fetch for easier use.
```sh
npm install lesca-fetcher --save
```
```JSX
import Fetcher, { contentType, formatType } from 'lesca-fetcher';
Fetcher.install({
hostUrl: 'https://yourhost.com/api/',
contentType: contentType.JSON,
formatType: formatType.JSON,
});
Fetcher.setJWT('Fsr.956b6.67ktJGr'); // if necessary
Fetcher.setHeader({ user:'user', password:'123456' }) // 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>;
```
| method | description | default |
| :--------------------------------------------------------------- | :----------------: | ------: |
| .**install**(**[config](
| .**post**(**api**:_string_, **data**:_object_) | POST | |
| .**get**(**api**:_string_) | GET | |
| .**setJWT**(**token**):_string_ | set JWT Token | |
| .**setHeader**(**Objects**):_string_ | set Header | |
| .**postStringify**(**api**:_string_, **data**:_object_):_string_ | post and stringify | |
| .**mergePath**(**api**:_string_):_string_ | get full path | |
```js
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
};
```
- maintain if necessary