0.1.13 • Published 3 years ago

vue-api-services v0.1.13

Weekly downloads
-
License
-
Repository
github
Last release
3 years ago

Installation

npm i -D vue-api-services

Usage

<script>
import VueService from 'vue-api-services'
import Vue from 'vue'

// Local registration
Vue.use(VueService)

// This part would obviously suit much nicer in a separate file
const BookService = VueService.create({
  name: 'books', // this.$services[name] in component
  endpoints: {
    list: {
      // If you are using Nuxt, it will use your global axios instance by default
      url: 'books.json',
      method: 'get',
      params: {
        bibkeys: 'ISBN:0201558025,LCCN:93005405',
      },
      // You can optionally transform response data this way
      handleResponse(data) {
        return { example: data }
      },
    },
    // It can be a method as well, obviously
    get: (id) => ({
      url: 'book/' + id,
      method: 'get',
      params: { archived: false },
    }),
  },
})

export default {
  // Services registered here will be available using this.$services
  services: {
    BookService,
  },
  async asyncData(ctx) {
    // You can use it in asyncData this way
    const bookService = BookService.use(ctx)
    return {
      list: await bookService.list
    }
  },
  async mounted() {
    // Or this way in a component
    const list = await this.$services.books.list
    
    // In case of a method
    const book = await this.$services.books.get(666)
  },
}
</script>

Nuxt

Place is in modules like this for global registration.

export default {
  modules: [
    'vue-api-services/nuxt',
  ],
}
0.1.12

3 years ago

0.1.13

3 years ago

0.1.10

3 years ago

0.1.11

3 years ago

0.1.9

3 years ago

0.1.8

3 years ago

0.1.7

3 years ago

0.1.6

3 years ago

0.1.5

3 years ago

0.1.4

3 years ago

0.1.3

3 years ago

0.1.2

3 years ago

0.1.1

3 years ago

0.1.0

3 years ago