JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 89
  • Score
    100M100P100Q89321F
  • License MIT

Prop-Proxy allows you to intercept getters and setters of class attributes through decorators

Package Exports

  • prop-proxy
  • prop-proxy/lib/index.js

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (prop-proxy) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

Prop-Proxy

Proxy for class properties

npm version unpkg install size

Prop-Proxy allows you to intercept getters and setters of class attributes through decorators

Installation

This package has to be used with Typescript

npm install prop-proxy --save
yarn add prop-proxy

Configuration

You need to enable in tsconfig.js:

{
    "experimentalDecorators": true, 
    "emitDecoratorMetadata": true
}

Usage

import { usePropertyProxy } from 'prop-proxy'

type PartialPick<T, K extends keyof T> = Partial<T> & Pick<T, K>

interface Struct {
    title: string;
    isActive: boolean;
}

type Params = PartialPick<Struct, 'title'> //isActive is optional param in constructor

const { Property, proxy } = usePropertyProxy<Struct>();

class Category implements Struct {
    @Property()
    title!: string;

    @Property()
    isActive!: boolean;

    constructor({title, isActive}: Params){
        Object.assign(this, {title, isActive})
    }
}

//getter intercept
proxy.getTitle((value) => `other_${value}`)

//setter intercept
proxy.setIsActive(({setValue, value}) => value ? setValue(value) : setValue(true));

const category = new Category({title: 'title'})

console.log(category.title) // output: other_title

console.log(category.isActive) // output: true

License

MIT Free Software, Yeah!