1.2.8 • Published 3 years ago

wp-request v1.2.8

Weekly downloads
-
License
MIT
Repository
-
Last release
3 years 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

3 years ago

1.2.7

3 years ago

1.2.5

3 years ago

1.2.0

3 years ago

1.2.3

3 years ago

1.2.2

3 years ago

1.2.1

3 years ago

1.1.16

3 years ago

1.1.15

3 years ago

1.1.14

3 years ago

1.1.13

3 years ago

1.1.18

3 years ago

1.1.9

3 years ago

1.1.8

3 years ago

1.1.7

3 years ago

1.1.6

3 years ago

1.1.5

3 years ago

1.1.4

3 years ago

1.1.12

3 years ago

1.1.11

3 years ago

1.1.10

3 years ago

1.1.1

3 years ago

1.1.0

3 years ago

1.1.3

3 years ago

1.1.2

3 years ago

1.0.33

3 years ago

1.0.34

3 years ago

1.0.32

3 years ago

1.0.31

3 years ago

1.0.30

3 years ago

1.0.29

3 years ago

1.0.28

3 years ago

1.0.27

3 years ago

1.0.26

3 years ago

1.0.25

3 years ago

1.0.24

3 years ago

1.0.23

3 years ago

1.0.22

3 years ago

1.0.21

3 years ago

1.0.20

3 years ago

1.0.19

3 years ago

1.0.18

4 years ago

1.0.17

4 years ago

1.0.16

4 years ago

1.0.15

4 years ago

1.0.14

4 years ago

1.0.13

4 years ago

1.0.12

4 years ago