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

mithril-utilities

v1.4.0

Published

A collection of utilities for Mithril

Downloads

2

Readme

Mithril utilities

A collection of utilities for Mithril.

Installation

pnpm add mithril-utilities
# or
# npm install mithril-utilities
# or
# yarn add mithril-utilities

Usage

Mithril utilities are available as named exports from the package.

Features/Available utilities

Component class

The Component class is a base class for Mithril components. It provides a few useful features:

  • A base class for Mithril class components
  • Type-safe props and methods
  • An attributes collection wrapper

Example:

import { Component } from 'mithril-utilities';

class MyComponent extends Component {
  view() {
    return m('div', this.attrs);
  }
}

Reactive form

The Form component is an implementation of reactive forms (term from other JS frameworks), where form elements data is costantly updated into reactive data structures. In Mithril, this is achieved by using Streams and input event handlers.

It provides a few useful features:

  • A reactive form component
  • Easy form state management

Example:

import {Form, Component} from 'mithril-utilities';
import Stream from 'mithril/stream';

class MyComponent extends Component {
  formState = {
    name: Stream(),
    email: Stream(),
  };

  view() {
    return (
      <Form onsubmit={this.onSubmit.bind(this)} state={this.formState}>
        <input type="text" name="name"/>
        <input type="email" name="email"/>
        <input type="submit" value="Submit"/>
      </Form>
    )
  }
  
  onSubmit(e: Event) {
    e.preventDefault();
    console.log(this.formState);
  }
}

Request class

The Request class is a wrapper around the m.request API, providing a few useful features:

  • A wrapper around m.request with a more convenient API
  • Type-safe request and response data
  • Interceptors for request and response (before and after, like Axios interceptors)
  • XSRF header support
  • Workaround for PHP issue with PUT/PATCH/DELETE requests and FormData (see this issue)

Examples:

import { Request } from 'mithril-utilities';

// Static methods
Request.get('/api/users');
Request.post('/api/users', {body: {name: 'John'}});
Request.put('/api/users/1', {body: {name: 'John'}});
Request.patch('/api/users/1', {body: {name: 'John'}});
Request.delete('/api/users/1');

// Instance methods
const request = new Request({
  url: '/api/users',
  method: 'GET',
  // ... (other options, check your IDE for more info)
});

You can pass options to the constructor, or to the static methods.

Interceptors

You can add interceptors to the Request class, to be executed before and after the request:

import {Request, RequestOptionsWithUrl} from 'mithril-utilities';

// Static
Request.get('/api/users', {
  beforeRequest: (options: RequestOptionsWithUrl) => {
    // Do something with the options
  },
  after: (response: Promise, options: RequestOptionsWithUrl) => {
    // Do something with the response (Promise)
  },
});

// Instance
const request = new Request({
  url: '/api/users',
  method: 'GET',
  beforeRequest: (options: RequestOptionsWithUrl) => {
    // Do something with the options
  },
  after: (response: Promise, options: RequestOptionsWithUrl) => {
    // Do something with the response (Promise)
  }
});

JSX IDE support

Prerequisites

JSX is supported by default in Mithril, but IDEs like Webstorm or VSCode don't know how to handle it. To enable JSX support in VSCode, add the following to your tsconfig.json:

{
  "compilerOptions": {
    "jsx": "react",
    "jsxFactory": "m",
    "jsxFragmentFactory": "m.Fragment"
  }
}
Further support

You can now use JSX in your Mithril components, but IDEs won't be able to provide you with much help. To enable full support, you can install the @types/mithril package:

pnpm add -D @types/mithril
# or
# npm install -D @types/mithril
# or
# yarn add -D @types/mithril
Advanced typings

You can then add the advanced typings provided by this package in your d.ts file:

import 'mithril-utilities/typings';

or in your tsconfig.json:

{
  "compilerOptions": {
    "types": [
      "mithril-utilities/typings"
    ]
  }
}

Development

Setup

pnpm install

Build

pnpm build