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-mc-full-ts

v1.0.8

Published

TypeScript version of Vue Model Collections library

Downloads

532

Readme

vue-mc-full-ts

A TypeScript port of vue-mc, bringing type safety to Models and Collections for Vue.js.

Overview

This library is a TypeScript implementation of the popular vue-mc library, providing strongly-typed models and collections for Vue.js applications. It maintains compatibility with the original library's API while adding full TypeScript support and Vue 3 compatibility.

Features

  • Full TypeScript support with proper type definitions
  • Vue 3 compatibility with Composition API support
  • Models with validation, computed properties, and API integration
  • Collections for managing groups of models
  • HTTP request/response handling with axios
  • Maintains API compatibility with the original vue-mc library

Installation

npm install vue-mc-full-ts

Requirements

  • Vue.js 3.x
  • TypeScript 4.x or higher

Basic Usage

There are two ways to add type safety to your models:

Approach 1: Using a getter

import { Model, Collection } from 'vue-mc-full-ts';

interface UserAttributes {
    name: string;
    email: string;
    active: boolean;
}

class User extends Model {
    // Type the model's attributes using a getter
    get attributes(): UserAttributes {
        return this._attributes.value as UserAttributes;
    }

    defaults(): Partial<UserAttributes> {
        return {
            name: '',
            email: '',
            active: false,
        };
    }

    validation(): Record<string, any> {
        return {
            name: (value: string) => Boolean(value) || 'Name is required',
            email: (value: string) => /\S+@\S+\.\S+/.test(value) || 'Invalid email'
        };
    }

    // Route configuration for API requests
    routes(): Record<string, string> {
        return {
            fetch: 'users/{id}',
            save: 'users'
        };
    }
}

// Define a collection with TypeScript types
class UserCollection extends Collection<User> {
    model(): typeof User {
        return User;
    }

    // Route configuration for the collection
    routes(): Record<string, string> {
        return {
            fetch: 'users'
        };
    }
}

// Usage in Vue component
const user = new User({ name: 'John' });
const users = new UserCollection();

// Validate
await user.validate(); // Returns Promise<boolean>

// Save to API
await user.save();

// Fetch from API
await users.fetch();

Approach 2: Using declare

import { Model, Collection } from 'vue-mc-full-ts';
import { Ref } from 'vue';

interface UserAttributes {
    name: string;
    email: string;
    active: boolean;
}

class User extends Model {
    // Type the model's attributes using declare
    declare attributes: Ref<UserAttributes>;

    defaults(): Partial<UserAttributes> {
        return {
            name: '',
            email: '',
            active: false,
        };
    }

    validation(): Record<string, any> {
        return {
            name: (value: string) => Boolean(value) || 'Name is required',
            email: (value: string) => /\S+@\S+\.\S+/.test(value) || 'Invalid email'
        };
    }

    // Route configuration for API requests
    routes(): Record<string, string> {
        return {
            fetch: 'users/{id}',
            save: 'users'
        };
    }
}

// Define a collection with TypeScript types
class UserCollection extends Collection<User> {
    model(): typeof User {
        return User;
    }

    // Route configuration for the collection
    routes(): Record<string, string> {
        return {
            fetch: 'users'
        };
    }
}

// Usage in Vue component
const user = new User({ name: 'John' });
const users = new UserCollection();

// Validate
await user.validate(); // Returns Promise<boolean>

// Save to API
await user.save();

// Fetch from API
await users.fetch();

Both approaches provide full type safety for your model attributes. Choose the one that best fits your coding style.

Differences from vue-mc

  1. TypeScript Support:

    • Full type definitions for all classes and methods
    • Improved type safety for validation rules and API responses
    • Better IDE support with TypeScript intellisense
  2. Vue 3 Integration:

    • Uses Vue 3's reactivity system
    • Compatible with Composition API
    • Modern Vue.js best practices
  3. Enhanced Features:

    • Improved error handling with TypeScript types
    • Better type inference for model attributes
    • Type-safe validation rules

Documentation

For detailed documentation about the original library features, visit:

For TypeScript-specific features and improvements, check the following sections:

Type-Safe Models

interface UserAttributes {
    id: number;
    name: string;
    email: string;
}

class User extends Model {
    // Type the model's attributes using declare
    declare attributes: Ref<UserAttributes>;

    defaults(): Partial<UserAttributes> {
        return {
            name: '',
            email: ''
        };
    }
}

Type-Safe Collections

class UserCollection extends Collection<User> {
    model(): typeof Model {
        return User;
    }
}

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

License

MIT License - see LICENSE file for details

Credits

This library is a TypeScript implementation of vue-mc by Figured Limited.