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

rc-transforms

v1.0.2

Published

A transformation layer that sits between orm and json responses

Downloads

20

Readme

npm version Coverage Status Build Status

rc-transforms

A transformation layer that sits between orm and json responses

Inspired by Laravels Eloquent API Resources.

Installation

npm i rc-transforms

or

pnpm add rc-transforms

Programmatic Usage

Create a resource file, PostResource.ts.

import { Resource } from "rc-transforms";

export class PostResource extends Resource {
    topic: string;

    toArray() {
        return {
            topic: this.topic
        }
    }
}

Create a resource file, UserResource.ts.

import { Resource } from "rc-transforms";
import { PostResource } from "./PostResource"

export class UserResource extends Resource {
    id: number;
    fname: string;
    created_at: string;
    updated_at: string;
    posts: Posts[];
    lname: string;

    toArray() {
        return {
            id: this.id,
            fname: this.fname,
            lname: this.lname,
            posts: PostResource.collection(this.posts),
            created_at: this.created_at,
            updated_at: this.updated_at,
        }
    }
}

Create some data and interface, maybe in data.ts

export interface Posts {
    topic: string;
}

export interface User {
    id: number;
    fname: string;
    lname: string;
    created_at: string;
    updated_at: string;
    posts?: Posts[];
}

export const data: User[] = [{
    id: 1,
    fname: 'lalalan',
    lname: 'nana',
    created_at: '1990-02-01',
    updated_at: '2000-02-01',
    posts: [{
        topic: 'im cool'
    },
    {
        topic: 'so cool cool'
    }
    ]
}, {
    id: 2,
    fname: 'bababan',
    lname: 'nana',
    created_at: '1990-02-01',
    updated_at: '2000-02-01'
}]

you can transform a single item:

const resource = new UserResource(data[0]);
console.log(resource);
/**
            {
                "id": 1,
                "fname": "lalalan",
                "lname": "nana",
                "posts": [
                    {
                        "topic": "im cool"
                    },
                    {
                        "topic": "so cool cool"
                    }
                ],
                "created_at": "1990-02-01",
                "updated_at": "2000-02-01"
            }
 */

you can transform a all of them:

const collection = UserResource.collection(data);
console.log(collection);
/**
            [
                {
                    "id": 1,
                    "fname": "lalalan",
                    "lname": "nana",
                    "posts": [
                        {
                            "topic": "im cool"
                        },
                        {
                            "topic": "so cool cool"
                        }
                    ],
                    "created_at": "1990-02-01",
                    "updated_at": "2000-02-01"
                },
                {
                    "id": 2,
                    "fname": "bababan",
                    "lname": "nana",
                    "posts": [

                    ],
                    "created_at": "1990-02-01",
                    "updated_at": "2000-02-01"
                }
            ]
 */

Create a collection file, UserCollection.ts.

export class UserCollection {
    constructor(data: object) {
        return {
            api: "v1",
            meta: {
                next: "---",
                prev: "---"
            },
            data: data
        }
    }
}

you can transform a collection wrapper:

const collection = UserResource.collection(new UserCollection(data));
console.log(collection);
/**
            {
                "api": "v1",
                "meta": {
                    "next": "---",
                    "prev": "---"
                },
                "data": [
                    {
                        "id": 1,
                        "fname": "lalalan",
                        "lname": "nana",
                        "posts": [
                            {
                                "topic": "im cool"
                            },
                            {
                                "topic": "so cool cool"
                            }
                        ],
                        "created_at": "1990-02-01",
                        "updated_at": "2000-02-01"
                    },
                    {
                        "id": 2,
                        "fname": "bababan",
                        "lname": "nana",
                        "posts": [

                        ],
                        "created_at": "1990-02-01",
                        "updated_at": "2000-02-01"
                    }
                ]
            }
 */

you can transform a single item data key with collection wrapper:

const collection = UserResource.collection(new UserCollection([data[1]]));
console.log(collection);
/**
            {
                "api": "v1",
                "meta": {
                    "next": "---",
                    "prev": "---"
                },
                "data": [
                    {
                        "id": 2,
                        "fname": "bababan",
                        "lname": "nana",
                        "posts": [

                        ],
                        "created_at": "1990-02-01",
                        "updated_at": "2000-02-01"
                    }
                ]
            }
 */