UNPKG

facebook-nodejs-business-sdk

Version:
83 lines (78 loc) 1.75 kB
/** * Copyright (c) 2017-present, Facebook, Inc. * All rights reserved. * * This source code is licensed under the license found in the * LICENSE file in the root directory of this source tree. * * Abstract Object * Manages object data fields and provides matching properties * * @flow * @format */ export default class AbstractObject { _data: any; _fields: Array<string>; static get Fields () { return Object.freeze({}); } constructor () { this._data = {}; if (this.constructor.Fields === undefined) { throw new Error( 'A "Fields" frozen object must be defined in the object class' ); } let fields: any = this.constructor.Fields; this._fields = Object.keys(fields); this._fields.forEach(field => { this._defineProperty(field); }); } /** * Define data getter and setter field * @param {String} field */ _defineProperty (field: string) { Object.defineProperty(this, field, { get: () => this._data[field], set: value => { this._data[field] = value; }, enumerable: true }); } /** * Set data field * @param {String} field * @param {Mixed} value * @return this */ set (field: string, value: mixed) { if (this._fields.indexOf(field) < 0) { this._defineProperty(field); } var that: {[key: string]: any} = this; that[field] = value; return this; } /** * Set multiple data fields * @param {Object} data * @return this */ setData (data: Object) { Object.keys(data).forEach(key => { this.set(key, data[key]); }); return this; } /** * Export object data * @return {Object} */ exportData (): Object { return this._data; } }