Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wrap fusion instance in proxy #302

Open
Gustav-Eikaas opened this issue Sep 30, 2022 · 2 comments
Open

Wrap fusion instance in proxy #302

Gustav-Eikaas opened this issue Sep 30, 2022 · 2 comments
Labels
✨ improvement 🤷 question Further information is requested

Comments

@Gustav-Eikaas
Copy link
Collaborator

Gustav-Eikaas commented Sep 30, 2022

Is your feature request related to a problem? Please describe.
To prevent any consumers from altering the fusion instance, wrapping it in a proxy could be a feasible solution.

Describe the solution you'd like
A clear and concise description of what you want to happen.

/**
 *  Wraps an object and all its children objects in a proxy 
 *  Handles arrays and objects
 */
function deepProxy<T extends Record<PropertyKey,any>>(obj: T){
    Object.keys(obj).filter(key => typeof obj[key] === "object").filter(key => obj[key] !== null).forEach(key => {
        obj[key as keyof T] = deepProxy(obj[key])
    })
    return new Proxy(obj, {set(){return false}})
}

Describe alternatives you've considered
A clear and concise description of any alternative solutions or features you've considered.

Additional context
Add any other context or screenshots about the feature request here.

@Gustav-Eikaas Gustav-Eikaas added ✨ improvement 🤷 question Further information is requested labels Sep 30, 2022
@odinr
Copy link
Collaborator

odinr commented Sep 30, 2022

yes that could work 🤙🏻
but there might be sub object which the user want to set properties... maybe.

@Gustav-Eikaas
Copy link
Collaborator Author

Gustav-Eikaas commented Sep 30, 2022

Yeah, I made a solution that handles properties you want to exclude from the proxy trap

type NestedKeyOf<ObjectType extends object> = {[Key in keyof ObjectType & (string | number)]: ObjectType[Key] extends object ? ObjectType[Key] extends Array<any> ? `${Key}` : `${Key}` | `${Key}.${NestedKeyOf<ObjectType[Key]>}`  : `${Key}`}[keyof ObjectType & (string | number)]

/**
 *  Wraps an object and all its children objects in a proxy 
 *  Handles arrays and objects
 */
function deepProxy<T extends Record<PropertyKey,any>>(obj: T, ignores?: NestedKeyOf<T>[]){
    Object.keys(obj).filter(key => typeof obj[key] === "object").filter(key => obj[key] !== null).forEach(key => {
        obj[key as keyof T] = deepProxy(obj[key],ignores?.filter((s: string) => s.includes(key) && s.includes(".")).map((s: string) => s.slice((s.indexOf(".")+1 ))))
    })
    return new Proxy(obj, {set(target,property, val){
        if(!ignores?.includes(property as NestedKeyOf<T>)) return false;
            target[property as keyof T] = val;
            return true;
    }})
}

.eg

interface Person{
    name: string;
    age: number;
    children: [];
}

const test = deepProxy<Person>({age:18, children: [],name: "Tor"},["children"])

Adds an array of properties to ignore in the proxy trap, works recursively and gives you autocomplete on it. Code got kinda messy tho

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
✨ improvement 🤷 question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants