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

structured-resources

v0.1.3

Published

A structured way to define resources. Built to bring structure into stacks with many resources, like in AWS CDK or a smart home setup.

Downloads

1

Readme

Structured-resources

A structured way to define resources. Built to bring structure into stacks with many resources, like in aws-cdk or a smart home setup.

Structured-resources is a small (dependency free) tool that helps to structure your resources. It provides an additional layer to your resources so you can easily call methods for multiple constructs at once, surrounded with intelligent typing.

It is designed to help create a clear stack definition, so that you can easily see what resources are used in your stack, and how they interact with each-other.

Installation

npm install structured-resources

Usage

It provides five methods to access your constructs;

  • for: selects one or more resources and returns a new object with inherited methods and attributes from the resources.
  • all: selects all resources and returns a new object with inherited methods and attributes from the resources.
  • select: select single resource
  • selectSome: select one or more resources and returns an array of the selected resources
  • selectAll: returns an array of all resources
class Bar {
  public testA = () => 123;
}
class FooOne extends Bar {
  public testB = () => 456;
}
class FooTwo extends Bar {
  public testC = () => 789;
}
const structuredResources = new StructuredResources({
  fooOne: new FooOne(),
  fooTwo: new FooTwo(),
});

structuredResources.for("fooOne", "fooTwo").testA().data; // returns { fooOne: 123, fooTwo: 123 }, both fooOne and fooTwo have method testA
structuredResources.for("fooTwo").testC().data; // returns { fooTwo: 789 }
structuredResources.for("fooOne", "fooTwo").testB(); // Type ERROR: Property 'testB' does not exist on type, fooTwo does not have method testB
structuredResources.all().testA().data; // returns { fooOne: 123, fooTwo: 123 }
structuredResources.select("fooOne"); // returns FooOne instance

See /examples/cdk-stack for an example with aws-cdk.

Advanced examples

Chaining methods

On for and all methods a new function chain is returned. This will make the resource methods chainable.

Example:

class Foo {
  public methodA = () => 123;
  public methodB = () => 456;
}
const structuredResources = new StructuredResources({
  foo: new Foo(),
});
structuredResources.all().chain().methodA().methodB().methodA(); // calls methodA twice anc methodB once
structuredResources.all().chain().methodA().methodB().methodA().data; // calls methodA twice anc methodB once and returns { foo: [123, 456, 123] }

Asynchronous functions

By calling promise() at the end of a resolution chain, all called methods will be promised.

Example:

class LightBulb {
  private on: boolean;

  private brightness: number;

  constructor(public readonly name: string) {
    this.on = false;
    this.brightness = 50;
  }

  async toggle(): Promise<boolean> {
    this.on = !this.on;
    return someAsyncActionThatReturns(this.on);
  }

  async setBrightness(brightness: number): Promise<number> {
    this.brightness = brightness;
    return someAsyncActionThatReturns(this.brightness);
  }
}

const livingRoomLights = new StructuredResources({
  lampCouch: new LightBulb("lamp-couch"),
  ceilingLampOne: new LightBulb("ceiling-lamp-a"),
  ceilingLampTwo: new LightBulb("ceiling-lamp-b"),
});

await livingRoomLights.all().setBrightness(30).promise(); // calls and awaits setBrightness() for every light and returns { lampCouch: 30, ceilingLampOne: 30, ceilingLampTwo: 30 }
await livingRoomLights.all().chain().setBrightness(30).toggle().promise(); // calls and awaits setBrightness() and toggle() for every light and returns { lampCouch: [30, true], ceilingLampOne: [30, true], ceilingLampTwo: [30, true] }

Testing

Tests can be ran using the following command:

npm run test

Contributing

Pull requests are welcome. Please make sure to update tests as appropriate.

License

MIT