1.2.8 • Published 1 year ago

wp-request v1.2.8

Weekly downloads
-
License
MIT
Repository
-
Last release
1 year ago

Quick Start

/**
** main.ts
**/
import { createApp } from 'vue'
const app = createApp(App)

import request from 'wp-request'
import useStore from '@/store' // require a pinia store

app.use(request, {
  useStore
})

import api from '@/api'
app.use(api)

import { createPinia } from 'pinia'
app.use(createPinia())
app.mount('#app')

/**
** api/main.ts
**/
import v1 from './v1'

const api = {
    v1
}

import { App } from 'vue'

declare global {
    interface Window {
        api: typeof api
    }
}

declare module '@vue/runtime-core'  {
    export interface ComponentCustomProperties {
        readonly api: typeof api;
    }
}

export default {
    install(app: App<Element>) {
        app.config.globalProperties.api = api
        window.api = api
    }
}

How to Use in Vue SFC

Way 1: Import directly, fit to options api and composition api.

import request, { requestAll, download } from '@/api'
request({
	url: 'http://exp.com',
  method: 'GET'
})

Way 2: window.common, fit to options api and composition api.

window.common.axios({
	url: 'http://exp.com',
  method: 'GET'
})

Way 3: this.axios, fit to options api only.

import { defineComponent } from 'vue'
export default defineComponent({
  created(){
    this.axios({
      url: 'http://exp.com',
      method: 'GET'
    })
  }
})

How to Use in Vue SFC by API

Way 1: window.api, fit to options api and composition api.

window.api.v1.sth()

Way 2: this.api, fit to options api only.

this.api.v1.sth()

How to Use with Typescript

window.common.axios<{
  name: string
}>({
  url: 'http://exp.com'
})
	.then(res => {
  	// res.data is as { name: string }
		const data = res.data
  })

Type

declare type request = <T = any>(config: AxiosRequestConfig, setLoading: boolean) => Promise<ResponseType<T>>
// Store 要求三项内容
interface Store {
    token: string;
    loading: boolean;
    requests: Set<Promise<any>>;
}
// Options 选项,初始化时均为可选
interface Options {
    baseURL: string;
    useStore: null | (() => Store);
    successCode: number[];
}
// ResponseType 用于定义类型
interface ResponseType<T = any> {
    code: number;
    errorMsg?: string;
    data: T;
    success?: boolean;
}
1.2.8

1 year ago

1.2.7

1 year ago

1.2.5

2 years ago

1.2.0

2 years ago

1.2.3

2 years ago

1.2.2

2 years ago

1.2.1

2 years ago

1.1.16

2 years ago

1.1.15

2 years ago

1.1.14

2 years ago

1.1.13

2 years ago

1.1.18

2 years ago

1.1.9

2 years ago

1.1.8

2 years ago

1.1.7

2 years ago

1.1.6

2 years ago

1.1.5

2 years ago

1.1.4

2 years ago

1.1.12

2 years ago

1.1.11

2 years ago

1.1.10

2 years ago

1.1.1

2 years ago

1.1.0

2 years ago

1.1.3

2 years ago

1.1.2

2 years ago

1.0.33

2 years ago

1.0.34

2 years ago

1.0.32

2 years ago

1.0.31

2 years ago

1.0.30

2 years ago

1.0.29

2 years ago

1.0.28

2 years ago

1.0.27

2 years ago

1.0.26

2 years ago

1.0.25

2 years ago

1.0.24

2 years ago

1.0.23

2 years ago

1.0.22

2 years ago

1.0.21

2 years ago

1.0.20

2 years ago

1.0.19

2 years ago

1.0.18

2 years ago

1.0.17

2 years ago

1.0.16

2 years ago

1.0.15

2 years ago

1.0.14

2 years ago

1.0.13

2 years ago

1.0.12

2 years ago