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

lazy-provide-inject

v2.5.0

Published

A utility library to simplify and enhance the usage of Vue 3's Composition API `provide` and `inject` features with type safety.

Downloads

13

Readme

Lazy Provide Inject

lazy-provide-inject is a utility library designed to enhance and simplify the use of Vue 3's Composition API provide and inject features, with an emphasis on type safety and ease of use.

Features

  • Type-Safe Providers: Create strongly-typed providers that encapsulate state and methods, ensuring type consistency throughout your application.
  • Lazy Injection: Inject dependencies only when they're not already provided, preventing multiple registrations and ensuring singletons.
  • Flexible & Reusable: Easily provide and inject multiple dependencies with a single function call, simplifying your setup code.
  • Simplified API: Reduce boilerplate and improve readability of your composition functions with minimal and intuitive function calls.

Installation

To install the lazy-provide-inject library, run the following command:

npm install -D lazy-provide-inject

or if you're using yarn:

yarn add -D lazy-provide-inject

Usage as npm Package

Creating a Provider

Use the createProvider function to create a new provider.

import { ref, readonly } from 'vue';

import { createProvider } from './index';

const context = () => {
  const count = ref(0);
  const increment = () => {
    count.value++;
  };

  return {
    count: readonly(count),
    increment,
  };
};

export const counterProvider = createProvider('counter', context);

Providing Dependencies

Provide dependencies in your setup function using lazyProvide.

<script setup lang="ts">
import TheChild from './components/TheChild.vue';
import { lazyProvide } from './providers';
import { counterProvider } from './providers/counterProvider';

lazyProvide(counterProvider, usersProvider);
</script>

<template>
  <div class="app-component">
    <h1>App</h1>
    <p>provided counterProvider</p>
    <TheChild />
  </div>
</template>

<style scoped>
.app-component {
  padding: 3rem;
  border: 1px solid green;
}
</style>

Injecting Dependencies

Inject provided dependencies into any component with lazyInject.

<script setup lang="ts">
import { lazyInject } from '../providers';
import { counterProvider } from '../providers/counterProvider';

const { count } = lazyInject(counterProvider);
</script>

<template>
  <div class="child-component">
    <h1>Child</h1>
    <p>injected 'count' from counterProvider</p>
    <p>count is: {{ count }}</p>
  </div>
</template>

<style scoped>
.child-component {
  padding: 3rem;
  border: 1px solid red;
}
</style>

API Reference

  • createProvider(name, setup): Creates a new provider with a unique key based on the provided name.
  • lazyProvide(...providers): Provides dependencies using Vue's provide function without duplication.
  • lazyInject(provider): Injects a provided dependency and throws an error if it's not available.

For detailed API documentation, please refer to the JSDoc comments in the source code.

Contributing

I welcome contributions to lazy-provide-inject! If you have suggestions or improvements, please submit a pull request or create an issue on my GitHub repository.

License

lazy-provide-inject is MIT licensed.