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

resourcy

v0.1.0

Published

A zero dependency JSON/object mapper to resource objects

Downloads

6

Readme

Resourcy

A zero dependency JSON/object mapper to resource objects

pipeline status coverage report NPM Version Node Version Downloads Stats

Resourcy is a zero dependency object mapper. It enables easy mapping to actual resource classes by using decorators to describe the resource.

This enables easy mapping from API JSON responses to your own resources.

Installation

NPM:

npm install resourcy --save

Yarn:

yarn add resourcy

Usage example

Ever had to call an API and use the response resource? Tired of having unstructured helper methods to parse the data laying around? Resourcy can help structure your resources by mapping the data from the response into an actual resource class.

Assume you would retrieve some user data by calling //example.com/user/[id].

// response from //example.com/user/[id]
{
	"id": 10,
	"name": "John Doe",
	"created_at": 1530345678
}

Now define a user resource

// User.ts
import {Resource, Property} from 'resourcy';

export class User extends Resource
{
	@Property(Number)
	public id: number;
	
	@Property(String)
	public name: string;
	
	@Property(Date)
	public created_at: Date;
	
	// Create resource specific helper methods if needed
}

Now there is only the simple mapping left.

// index.ts
import {User} from './User.ts'

api.get('/user/[id]')
	.then((response) => {
		const user = User.factory(response.json());
		
		expect(user).toBeInstanceOf(User); // true
		expect(user.id).toBeInstanceOf(Number); // true
		expect(user.name).toBeInstanceOf(String); // true
		expect(user.created_at).toBeInstanceOf(Date); // true
	});

Resourcy API

Use by:

import {Resource, Property} from 'resourcy';

resourcy.Resource (abstract class)

Abstract class to define a Resource class. Has one static method Resource.factory to map the Resource against a given dataset.

resourcy.Resource.factory(data: {[name: string]: any}): T

Returns a mapped Resource class. The argument is an object of the raw data which should be mapped.

resourcy.Entity (Deprecated)

Alias for resourcy.Resource. Deprecated - will be removed in a later version.

resourcy.Property(type, [optional = false], [options])

Decorator used to specify information on a given Resource property. Define which type the property is some other optional options.

resourcy.Property.optional

As default the properties is required. This means they will throw an ResourcyError if they are not given.

resourcy.Property.options

Additional options can be given as an object. The options are shown below with their default values.

interface IPropertyOptions<T>
{
	// Defines if the property on the resource should be optional.
	// If set to false, they will throw an ResourcyError if they are not given.
	// Default: false
	optional?: boolean;
	
	// Function to handle/manipulate the data before mapping.
	// The funtion retrieves one argument of the raw type and should return raw data.
	before?: (data: any) => any;
	
	// Function to handle/manipulate the data after is has been mapped.
	// The function retrieves one argument of the type specified and should return something of the same type.
	after?: (data: T) => T;
}

Development setup

After cloning/forking run the following commands to setup the project and run tests.

yarn install
yarn test

Maintainer

@sunnhas

Distributed under the MIT license. See LICENSE for more information.

Contributing

  1. Fork it
  2. Create your feature branch (git checkout -b feature/fooBar)
  3. Commit your changes (git commit -am 'Add some fooBar')
  4. Push to the branch (git push origin feature/fooBar)
  5. Create a new Pull Request