项目作者: whatsaaaa

项目描述 :
VueDataFetch NPM package for fetching data from any backend.
高级语言: TypeScript
项目地址: git://github.com/whatsaaaa/VueDataFetch.git
创建时间: 2020-11-15T14:16:05Z
项目社区:https://github.com/whatsaaaa/VueDataFetch

开源协议:

下载


VueDataFetch - A module that is used to fetch data from any backend


Made by @whatsaaaa

Installation

  1. npm install @whatsaaaaa/vue-data-fetch --save

Usage

With vueFetch method you can always fetch the data from any backend and get a nice structured response that can be used to enhance your application UX. This is achieved by using loading, data and error properties, so you can show the loading, error state and finally your data.

Vue with JavaScript

  1. import vueFetch from "@whatsaaaaa/vue-data-fetch/dist";
  2. ...
  3. setup() {
  4. const url = computed(() => "https://jsonplaceholder.typicode.com/posts");
  5. const response = vueFetch(url);
  6. console.log(response);
  7. }

When the request is sent, initial values for response variable are:

  1. {
  2. "loading": true,
  3. "data": null,
  4. "error": null
  5. }

If the request was successful values for response variable are:

  1. {
  2. "loading": false,
  3. "data": [
  4. {
  5. "userId": 1,
  6. "id": 1,
  7. "title": "qui est esse",
  8. "body": "lorem ipsum dolor sit amet"
  9. }
  10. ...
  11. ],
  12. "error": null
  13. }

If the request failed, values for response variable are:

  1. {
  2. "loading": false,
  3. "data": null,
  4. "error": {
  5. "status": 404,
  6. "statusText": "Not Found",
  7. "response": "Requested resource not found"
  8. }
  9. }

Vue with TypeScript

  1. import vueFetch from "@whatsaaaaa/vue-data-fetch/dist";
  2. ...
  3. interface Posts {
  4. userId: number;
  5. id: number;
  6. title: string;
  7. body: string;
  8. }
  9. ...
  10. const url = computed(() => "https://jsonplaceholder.typicode.com/posts");
  11. const response = vueFetch<Posts[]>(url);
  12. console.log(response);

If you are using TypeScript in your Vue project you can set vueFetch< T > type.

Objects

  • response:
  1. {
  2. loading: boolean;
  3. error: Error | null;
  4. data: T | null;
  5. }
  • Error:
  1. {
  2. status: number;
  3. statusText: string;
  4. response: object | null;
  5. }

Dependencies

  • Vue 3: ^3.0.2
  • Axios: ^0.21.0