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-vroom

v0.9.5

Published

Vroom generates type safe stores and mock API with minimal config

Downloads

215

Readme

Vroom

Vroom is a store and api mock generator for Vue.js and pinia, that lets you build applications fast and even before backend is ready.

See the full documentation here.

Test Coverage Package

Getting started

To get started setup of a Vue 3 project with Pinia, e.g. using create-vue. In this guide we will

  1. Install Vroom package
  2. Setup config
  3. Setup global component types (for TS projects)
  4. Install Vroom in our app
  5. Fetch some data and show it
  6. Use store actions to manipulate data

Peer dependencies and browser support

Vroom is built to work with Pinia and Vue 3. Vroom is targeted at modern browsers and uses the Fetch API instead of XMLHttpRequest.

Peer dependencies:

  • Vue ^3.0.0
  • Pinia ^2.0.0

Note: Vroom is still in alpha, so breaking changes might be introduced between each minor release.

1. Install package

To install Vroom

npm i vue-vroom

2. Add config file

Create a vroom/index.ts file to store your configuration. This is a basic example that:

// src/vroom/index.ts
import { createVroom, defineModel } from "vue-vroom";

const vroom = createVroom({
  models: {
    book: defineModel({
      schema: {
        title: { type: String },
        isFavourite: { type: Boolean },
      },
      belongsTo: {
        author: () => "author",
      },
    }),
    author: defineModel({
      schema: {
        name: { type: String },
      },
      hasMany: {
        books: () => "book",
      },
    }),
  },
  server: {
    enable: true,
  },
});

vroom.db.author.createMany(
  {
    name: "George R.R. Martin",
    books: vroom.db.book.createMany(
      { title: "A Game of Thrones" },
      { title: "A Clash of Kings" },
    ),
  },
  {
    name: "JRR Tolkien",
    books: vroom.db.book.createMany(
      { title: "The Hobbit" },
      { title: "The Lord of the rings" },
    ),
  },
);

export type Models = typeof vroom.types;

export default vroom;

It is recommended to split up models, config and seeds for better organisation and bundle optimization. See notes on organisation here

3. Add global component declarations

Vroom adds some global components. To get proper type hints on these add a components.d.ts file to your /src folder.

// src/components.d.ts
import type {
  FetchListComponent,
  FetchSingleComponent,
  FetchSingletonComponent,
} from "vue-vroom";
import type vroom from "@/vroom";

declare module "@vue/runtime-core" {
  export interface GlobalComponents {
    FetchList: FetchListComponent<typeof vroom>;
    FetchSingle: FetchSingleComponent<typeof vroom>;
    FetchSingleton: FetchSingletonComponent<typeof vroom>;
  }
}

4. Install on Vue app

Then import your vroom instance in main.ts file along with your pinia installation and install it

import { createApp } from "vue";
import { createPinia } from "pinia";
import vroom from "./vroom";
import App from "./App.vue";

const pinia = createPinia();
const app = createApp(App);

app.use(pinia);
app.use(vroom);
app.mount("#app");

5. Fetch and show data

Find a place you want to show some data e.g. App.vue and add the following

<template>
    <FetchList 
        model="book" 
        :sort="[{ field: 'title', dir: 'ASC' }]"
        :include="['author']"
    >
        <template #loading>Loading books...</template>

        <template #default="{bookItems}">
            <p v-for="book in bookItems" :key="book.id">
                {{ book.title }} was written by {{ book.author.name }}
            </p>
        </template>
    </FetchList>
</template>

<script lang="ts" setup></script>
  • The FetchList component will in this example trigger a call to /books?sort=title,include=author
  • It will pass the results into the default slot, when it's done loading
  • It will keeps its results cached until it's unmounted (see more about cache)

There are three different components for fetching data

  • FetchList
  • FetchSingle
  • FetchSingleton

6. Manipulate data

Let's update the example above to be able to favourite a book

<template>
    <FetchList 
        model="book" 
        :sort="[{ field: 'title', dir: 'ASC' }]"
        :include="['author']"
    >
        <template #loading>Loading books...</template>

        <template #default="{bookItems}">
            <p v-for="book in bookItems" :key="book.id">
                {{ book.title }} was written by {{ book.author.name }}

                <button @click="toggleFavorite(book)">
                    {{ book.isFavourite ? 'Unfavourite' : 'Favourite' }}
                </button>
            </p>
        </template>
    </FetchList>
</template>

<script lang="ts" setup>
import vroom, { type Models } from '@/vroom';

const bookStore = vroom.stores.book();

function toggleFavourite(book: Models['book']) {
    bookStore.update(book.id, {
        isFavourite: !book.isFavourite
    })
}
</script>

This example uses the autogenerated store (all accessible on vroom.stores). Calling update will trigger a called to PATCH /books/:id along with data passed as the second argument

Contributing

Contributions are most welcome 🙌

To contribute:

  • Fork this repository
  • Create a branch from main with your feature (and try to keep it small)
  • Send a pull request from your branch to the main branch.

License

MIT