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

retonio

v0.0.36

Published

A simple Vue store setup. A Wrapper for Pinia, including response states for loading.

Downloads

273

Readme

Retonio

A simple store setup for Vue. (If you use fetch, you need to return data as response. Works directly with axios). Probably the code, you would have written with Pinia, anyway.

  • 📦 Wrapps a Pinia Store in one line
  • 🔧 Customizeable
  • ⏳ Loading States

Pinia is a magical awesome Tool, to create Vue Stores simpler (expecially with TypeScript) and lighter then Vuex.

Retonio is a Tool on top, which return with just 2 arguments a whole store unit, to avoid repetition for standard cases (and slightly above).

In addition, it adds loading states, highly inspired by another amazing tool VueUse. By that you have can render the status of you API call in a vue component.

Possbile coming Feature

  • [] Add Types
  • [] Rewrite in TypeScript
  • [] Single File Store creation
  • [] Simpler way to import
  • [] Vue-Component for Loading

Installation

npm install retonio
# or with yarn
yarn add retonio

Presets

If you have not setup Pinia already, include it in the main.ts file See official docs:

import { createPinia } from 'pinia'
app.use(createPinia())

Usage

Create default a Store with Retonio

As with Pinia, create preferably each store in a single file (i. e. in src/store/moudles). (apiAllRecepies is an imported axios function). You need to pass Pinia via config and Id and api call as params

// src/store/modules/allRecepies.ts
// LIBS
import { defineStore } from 'pinia';
import { retonio } from 'retonio';

// API
import { apiAllRecepies } from '@/api/allRecepies.api';

// ----------
// RETONIO
// ----------
export const useAllRecepies = retonio({
  id: 'AllRecepies',
  api: useAllRecepies,
  init: defineStore,
});

Even easier with make command

With make command

  1. Create a Makefile (exactly that name, no extension) or add to your existing Makefile and add following:
retonio:
	node node_modules/retonio/make-retonio.mjs ts false	$(filter-out $@,$(MAKECMDGOALS))

Just once and you are done.

  1. From you terminal in you root dir type i. e.: make retonio AllRecepies recepies This will create src/store/modules/recepies/AllRecepies.js with same content of the file above.

Just make retonio AllRecepies will create src/store/modules/AllRecepies.js.

With make npm script

Add to the package.json:

"scripts": {
  "retonio": "node node_modules/retonio/make-retonio.mjs js true"
},

Run npm run retonio or click in vscode in npm scripts at retonio to achive the same as with make

If you prefer TypeScript, change in the Makefile js to ts at node node_modules/retonio/make-retonio.mjs ts false and in the package.json "node src/script/make-retonio.mjs ts true". There might be later a better way to configure it.

This is highly inspired by Laravel Artisan.

Add custom getter path, getter, action, error

In order to so, pass a config object (all items optional). path as string and rest as export function.

// src/store/modules/allRecepies.ts
// LIBS
import { retonio } from 'retonio';

// API
import { apiAllRecepies } from '@/api/allRecepies.api';
etonio

// Example for custom action
export function testAction(response) {
  try {
    const recepies = apiCall(response);

    return recepies;
  } catch (error) {
    console.error(error);
    // alertStore.error({ error: error });
    // ALERT.error({ error: error, messageType });
    return null;
  }

  return response;
}

// ----------
// RETONIO
// ----------
export const useAllRecepies = retonio({
  id: 'AllRecepies',
  api: useAllRecepies,
  init: defineStore,
  action: testAction,
});

This will create such a Pinia store

import { defineStore } from 'pinia'
// main is the name of the store. It is unique across your application
// and will appear in devtools
export const useAllRecepies = defineStore('main', {
  // a function that returns a fresh state
  state: () => ({
    isFinished: true,
    isLoading: false, // Is ture, as long as it is loading
    error: false,
    response: { /* Response object */ },
  }),
  // optional getters
  getters: {
    getData(state) {
    // ...
    // RETURNS
    // Gets raw response (from state.response)
    // OR
    // Nested path if proviced by objet depth (i.e.: state.response.data.recepies)
    // OR
    // Return from optional getterHelper
    // ...
    }
  },
  // optional actions
  actions: {
    async fetchData(params = undefined): {
      // ...
      // Fetches response from API call

      // IF RESOLVED
      // Sets raw response (to state.response)
      // OR
      // Sets from optional actionHelper

      // IF REJECTED
      // Logs erroro with Pinia Id to console
      // OR
      // Executes optonal errorHelper
    },
  },
})

Import store in Vue 3 component

Using setup script (LoadingComponent might be added in the future)

<!-- src/components/RecepieComponent.vue -->
<template>
  <!-- Component which hadles loadingState.isLoading to render a spinner -->
  <LoadingComponent :loadingState="loadingState" />

  <!-- Renders a List if it is not loading an has no errror -->
  <RecepieComponent v-if="!loadingState.isLoading && !loadingState.error && recepies" :itemsList="recepies" />
</template>

<script lang="ts" setup>
  // VUE
  import { computed } from 'vue';

  // STORE
  import { useAllRecepies } from '@/store/modules/allRecepies';
  const allRecepies = useAllRecepies();

  // --------------------------------------
  // GET DATA FROM STORE
  // --------------------------------------
  allRecepies.fetchData(/* Optional params*/);
  // Get loading state
  const loadingState = computed(() => {
    if (allRecepies.$state) return allRecepies.$state;
    return false;
  });

  // Get data from getter
  const recepies = computed(() => {
    if (allRecepies.getData) return allRecepies.getData;
    return false;
  });
</script>

Documentation

To learn more about Retonio, check cooming soon.

License

MIT