UNPKG

vike

Version:

The Framework *You* Control - Next.js & Nuxt alternative for unprecedented flexibility and dependability.

28 lines (27 loc) 1.53 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.getPublicProxy = getPublicProxy; // We use a proxy instead of property getters. // - The issue with property getters is that they can't be `writable: true` but we do want the user to be able to modify the value of internal properties. // ```console // TypeError: Invalid property descriptor. Cannot both specify accessors and a value or writable attribute, #<Object> // ``` // - Previous implementation using property getters: https://github.com/vikejs/vike/blob/main/vike/utils/makePublicCopy.ts const assert_js_1 = require("./assert.js"); // Show warning when user is accessing internal `_` properties. function getPublicProxy(obj, objName, propsPublic, expectCustomUserLandProps) { if (!expectCustomUserLandProps) { Object.keys(obj).forEach((key) => (0, assert_js_1.assert)(key.startsWith('_') || propsPublic.includes(key))); propsPublic.forEach((prop) => prop in obj); } return new Proxy(obj, { get(_, prop) { const propStr = String(prop); if (propStr.startsWith('_')) { (0, assert_js_1.assertWarning)(false, `Using internal ${objName}.${propStr} which may break in any minor version update. Reach out on GitHub and elaborate your use case so that the Vike team can add official support for your use case.`, { onlyOnce: true }); } // @ts-ignore Seems to be TypeScript bug return Reflect.get(...arguments); } }); }