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

nuxt-resource-based-api

v0.4.33

Published

Provide extremely efficient way to handle API on Nuxt.js.

Downloads

97

Readme

nuxt-resource-based-api

npm version MIT License CircleCI

This library provide extremely efficient way to handle API which has resource based routing (like Ruby on Rails) on Nuxt.js. You can implement vuex stores and vue components with simple and DRY code.

Overview

Let's assume you want to develop a task management service, and you've already developed API server.

With this library, you can implement state/mutations/actions by following simple code.

store/index.js

import Napi from 'nuxt-resource-based-api'
import axios from 'axios'

const axiosInstance = axios.create({
  baseURL: 'https://api.awesome-task-manager.com'
})

Napi.setConfig({
  axios: axiosInstance
})

store/task.js

import Napi from 'nuxt-resource-based-api'

export const { state, mutations, actions } = Napi.createStore(
  'task',
  ['index', 'show', 'new', 'edit', 'destroy'],
)

That's it! And in addition to creating vuex store, you can create vue component.

lib/create_component.js

import Vue from 'vue'
import Napi from 'nuxt-resource-based-api'

export default function createComponent(resources) {
  return Vue.extend({
    fetch: Napi.generateFetch(resources),
    computed: Napi.generateComputed(resources)
    methods: Napi.generateMethods(resources)
  })
}

pages/index.vue

<script>
import createComponent from '@/lib/create_component'

export default createComponent([
  { resource: 'task', action: 'index' },
]).extend({ // you can define other option by using Vue.extend method
  methods: {
    foo() { return 1 }
  }
})
</script>

<template>
<div>
  <div class="task" v-for="task in tasks">
    {{ task.name }}
  </div>
</div>
</template>

By using createComponent function, you can omit boring fetch and mapState implementation and keep your source code simple. This is syntax sugar of following.

// you can implement these callbakcks in configuration step
const createHeaders = (context) => { return {} }
const errorHandler = (e, context) => {}

export default Vue.extend({
  fetch(context) {
    const headers = createHeaders(context)
    const { store } = context
    try {
      await store.dispatch('task/fetchTasks', { headers }) // send a request (GET https://api.awesome-task-manager.com/tasks) and store the response
    } catch (e) {
      errorHandler(e, context)
    }
  },
  computed: {
    tasks() {
      return this.$store.state.task.tasks
    }
  },
  methods: {
    fetchTasks(force = false) {
      const headers = createHeaders(this)
      try {
        await store.dispatch('task/fetchTasks', { headers, force })
      } catch(e) {
        errorHandler(e, this)
      }
    },
    foo() { return 1 }
  }
})

If you want to implement a page to create task, write following code.

pages/tasks/new.vue

<script>
import createComponent from '@/lib/create_component'

export default createComponent([
  { resource: 'task', action: 'create' },
]).extend({
  data() {
    return {
      title: '',
      body: '',
    }
  },
  methods: {
    async create() {
      await this.createTask({
        title: this.title,
        body: this.body
      })
    }
  }
})
</script>

<template>
<!-- ... -->
</template>

See other examples in Wiki.

Installation

npm install nuxt-resource-based-api

As minimum configuration, all you need to do just add following code.

// store/index.js
import Napi from 'nuxt-resource-based-api'
import axios from 'axios'

const axiosInstance = axios.create({
  baseURL: 'https://api.awesome-task-manager.com'
})

Napi.setConfig({
  axios: axiosInstance
})
// lib/create_component.js
import Vue from 'vue'
import Napi from 'nuxt-resource-based-api'

export default function createComponent(resources) {
  return Vue.extend({
    fetch: Napi.generateFetch(resources),
    computed: Napi.generateComputed(resources)
    methods: Napi.generateMethods(resources)
  })
}

You can also customize request handler, error handler and so on. Please see Wiki

Typescript Support

Unfortunately Typescript is incompatible to dynamic generating codes like this library.

But you can use typescript with some trick and we recommend to use typescript. See Wiki for details.