npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

vue-apis

v0.4.1

Published

A vue plug-in integrated with axios. Build the API using chain programming and return the request instance as a Promise. A nice simplification of how apis are built, and how they are referenced.

Downloads

31

Readme

vue-apis

NPM version NPM download NPM download

  • A vue plug-in integrated with axios. Build the API using chain programming and return the request instance as a Promise. A nice simplification of how apis are built, and how they are referenced.
  • 一个集成了axios的vue插件。使用链式编程方式构建api,并以Promise返回请求实例。很好地简化了api的构建方式,和引用方式(通过this.$apis.apiName进行引用)。

Browser Support

Chrome | Firefox | Safari | Opera | Edge | IE | --- | --- | --- | --- | --- | --- | Latest ✔ | Latest ✔ | Latest ✔ | Latest ✔ | Latest ✔ | 11 ✔ |

Browser Matrix

Installing

npm install vue-apis
// or
yarn add vue-apis

Usage

Import

import Vue from 'vue'
import VueApis from 'vue-apis'

Vue.use(VueApis, options)

Api

| function | example | argument | description | | ---------- | ---- | ------------- | ----------- | | setUrl | setUrl('https://baidu.com') | (url: String) | set request url address | | setData | setData({ a: 1}) | (data: Object) | set post body object | | setParams | setParams({ t: Date.now() }) | (params: Object) | set request url query | | setHeaders | setHeaders({ 'content-type': 'application/json' }) | (headers: Object) | set request headers | | setTimeout | setTimeout(10000) | (timeout: Number) | set request timeout | | setCustomOptions | setCustomOptions({ responseType: 'stream' }) | (options: Object, clear: boolean) | set custom options |

Options

| option key | type | default value | description | | ---------- | ---- | ------------- | ----------- | | apis | object | {} | api set | | | showLoading | function | undefined | show loading layout function | | hideLoading | function | undefined | hide loading layout function | | interceptors | InterceptorObject | undefined | You can intercept requests or responses before they are handled by then or catch. |

InterceptorObject

| field | type | description | | --- | --- | --- | | request | RequestObject / Function | When the instance is 'Function', it is a 'then' callback to the interceptor | | response | ResponseObject / Function | When the instance is 'Function', it is a 'then' callback to the interceptor |

RequestObject

| Function | e.g. | | --- | --- | | then | (config) => { return config; } | | catch | (error) => { return Promise.reject(error); } |

ResponseObject

| Function | e.g. | | --- | --- | | then | (response) => { return response; } | | catch | (error) => { return Promise.reject(error); } |

Example

  • main.js
import Vue from 'vue'
import VueApis from 'vue-apis'
import Api from './api'

Vue.use(VueApis, {
  apis: Api,
  showLoading: () => {
    console.log('showLoading')
  },
  hideLoading: () => {
    console.log('hideLoading')
  },
  interceptors: {
    request: {
      then(config) {
        // Do something before request is sent
        return config;
      },
      catch(error) {
        // Do something with request error
        return Promise.reject(error);
      }
    },
    response: {
      then(response) {
        // Do something with response data
        return response;
      },
      catch(error) {
        // Do something with response error
        return Promise.reject(error);
      }
    }
  }
})
  • api.js
import { ApiOptions, ApiMethod } from 'vue-apis'

const $api = {
  readme () {
    return new ApiOptions()
    .setUrl(`https://raw.githubusercontent.com/Chans-Open-Source/vue-apis/master/README.md`)
    .setMethod(ApiMethod.GET)
    .setParams({
      timestamp: Date.now()
    })
    .setHeaders({
      Authorization: `Bearer ${Date.now()}`
    })
    .request()
  }
}

export default $api
  • home.vue
<template>
  <div v-html="readme"></div>
</template>
<script>
  export default {
    data () {
      return {
        readme: ''
      }
    },
    async created () {
      const res = await this.$apis.readme()
      this.readme = res
    }
  }
</script>

FormData

  • Api
const $api = {
  formDataRequest (formData) {
    return new ApiOptions()
    .setUrl(URL)
    .setMethod(ApiMethod.POST)
    .setData(formData)
    .request()
  }
}
  • Create FormData Instance
const formData = new window.FormData()
formData.append(key, value)

// Request
this.$apis.formDataRequest(formData)

Custom Options

const $api = {
  formDataRequest (formData) {
    return new ApiOptions()
    .setUrl(URL)
    .setMethod(ApiMethod.POST)
    .setData(formData)
    .setCustomOptions({
      url: `https://baidu.com`, // invalid
      data: {}, // invalid
      params: {}, // invalid
      headers: {}, // invalid
      method: {}, // invalid
      responseType: 'stream' // valid
    }, /* is clear all custom options at first */ false)
    .request()
  }
}

Source Code