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

resource.ts

v1.0.5

Published

The Resource Library is a TypeScript utility that helps developers easily manage the transformation of data objects. It provides a customizable and flexible `Resource` class, allowing developers to define how data should be formatted before being sent to

Downloads

366

Readme

Resource Library

The Resource Library is a TypeScript utility that helps developers easily manage the transformation of data objects. It provides a customizable and flexible Resource class, allowing developers to define how data should be formatted before being sent to the client. This is similar to Laravel’s resource handling.

Installation

To install the Resource Library, use npm:

npm install resource.ts

or with Yarn:

yarn add resource.ts

Usage

Overview

The Resource class is designed to be extended in order to define specific data formats for different types of resources. When you create a subclass of Resource, you override the format method to specify how each item should be transformed. You can also use powerful filtering options, such as include and exclude, to dynamically manage which fields appear in the final JSON output.

Creating a Custom Resource

Let's go through the steps to create a custom resource. Suppose you have a Post type with several fields:

interface Post {
  id: number;
  title: string;
  author: string;
  content: string;
  createdAt: string;
  updatedAt: string;
}

Define a PostResource class extending Resource:

import { Resource } from 'resource-library';

interface ReturnPost {
  id: number;
  title: string;
  author: string;
  content: string;
}

class PostResource extends Resource<Post, ReturnPost> {
  protected format(item: Post): ReturnPost {
    return {
      id: item.id,
      title: item.title,
      author: item.author,
      content: item.content,
    };
  }
}

Using toJSON with Filtering

You can call the toJSON method on the PostResource instance to output a JSON-friendly version of the data. You can use include or exclude to control which fields are included or excluded in the output.

const posts: Post[] = [
  {
    id: 1,
    title: 'First Post',
    author: 'John',
    content: 'This is the first post',
    createdAt: '2024-01-01',
    updatedAt: '2024-01-01',
  },
  {
    id: 2,
    title: 'Second Post',
    author: 'Jane',
    content: 'This is the second post',
    createdAt: '2024-01-02',
    updatedAt: '2024-01-02',
  },
];

const postResource = new PostResource(posts);
const output = postResource.toJSON(['id', 'title']);
console.log(output);

API Reference

new Resource(data)

Creates a new instance of the Resource class with the provided data. This data can be either a single object or an array of objects.

  • data: T | T[] — The data to be transformed by the resource.

toJSON(fields?: string[], exclude?: boolean)

Converts the resource's data to a JSON-compatible format, optionally including or excluding specified fields.

  • fields: string[] (optional) — An array of field names to include or exclude in the output.
  • exclude: boolean (optional) — If true, treat fields as fields to exclude instead.

Example Usage with Filtering

In the example below, we’ll use both include and exclude options to control which fields appear in the final output:

// Exclude specific fields
const outputExclude = postResource.toJSON(['createdAt', 'updatedAt'], true);
console.log(outputExclude);

// Include specific fields only
const outputInclude = postResource.toJSON(['id', 'author']);
console.log(outputInclude);

License

This project is licensed under the MIT License.