-
Notifications
You must be signed in to change notification settings - Fork 208
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
21 changed files
with
867 additions
and
448 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,63 +1,79 @@ | ||
import { Address } from '../../../../types/address'; | ||
|
||
/** | ||
* Base class for models | ||
*/ | ||
|
||
/* eslint-disable no-underscore-dangle,camelcase,class-methods-use-this */ | ||
export default class BaseModel { | ||
attribute_map: Record<string, string>; | ||
|
||
_is_primitive(val: any): val is string | boolean | number | bigint { | ||
return ( | ||
val === undefined || | ||
val == null || | ||
(typeof val !== 'object' && typeof val !== 'function') | ||
); | ||
} | ||
/* eslint-disable no-underscore-dangle,camelcase */ | ||
function _is_primitive(val: any): val is string | boolean | number | bigint { | ||
/* eslint-enable no-underscore-dangle,camelcase */ | ||
return ( | ||
val === undefined || | ||
val == null || | ||
(typeof val !== 'object' && typeof val !== 'function') | ||
); | ||
} | ||
|
||
_is_address(val: any): val is Address { | ||
return val.publicKey !== undefined && val.checksum !== undefined; | ||
} | ||
/* eslint-disable no-underscore-dangle,camelcase,no-redeclare,no-unused-vars */ | ||
function _get_obj_for_encoding( | ||
val: Function, | ||
binary: boolean | ||
): Record<string, any>; | ||
function _get_obj_for_encoding(val: any[], binary: boolean): any[]; | ||
function _get_obj_for_encoding( | ||
val: Record<string, any>, | ||
binary: boolean | ||
): Record<string, any>; | ||
function _get_obj_for_encoding(val: any, binary: boolean): any { | ||
/* eslint-enable no-underscore-dangle,camelcase,no-redeclare,no-unused-vars */ | ||
let targetPropValue: any; | ||
|
||
/* eslint-disable no-dupe-class-members,no-unused-vars */ | ||
_get_obj_for_encoding(val: Function): Record<string, any>; | ||
_get_obj_for_encoding(val: any[]): any[]; | ||
_get_obj_for_encoding(val: Record<string, any>): Record<string, any>; | ||
_get_obj_for_encoding(val: any): any { | ||
/* eslint-disable no-unused-vars */ | ||
let targetPropValue: any; | ||
if (typeof val.get_obj_for_encoding === 'function') { | ||
targetPropValue = val.get_obj_for_encoding(); | ||
} else if (Array.isArray(val)) { | ||
targetPropValue = []; | ||
for (const elem of val) { | ||
targetPropValue.push(this._get_obj_for_encoding(elem)); | ||
} | ||
} else if (typeof val === 'object') { | ||
const obj = {}; | ||
for (const prop of Object.keys(val)) { | ||
obj[prop] = this._get_obj_for_encoding(val[prop]); | ||
} | ||
targetPropValue = obj; | ||
} else if (this._is_primitive(val)) { | ||
targetPropValue = val; | ||
} else { | ||
throw new Error(`Unsupported value: ${String(val)}`); | ||
if (val instanceof Uint8Array) { | ||
targetPropValue = binary ? val : Buffer.from(val).toString('base64'); | ||
} else if (typeof val.get_obj_for_encoding === 'function') { | ||
targetPropValue = val.get_obj_for_encoding(binary); | ||
} else if (Array.isArray(val)) { | ||
targetPropValue = []; | ||
for (const elem of val) { | ||
targetPropValue.push(_get_obj_for_encoding(elem, binary)); | ||
} | ||
return targetPropValue; | ||
} else if (typeof val === 'object') { | ||
const obj = {}; | ||
for (const prop of Object.keys(val)) { | ||
obj[prop] = _get_obj_for_encoding(val[prop], binary); | ||
} | ||
targetPropValue = obj; | ||
} else if (_is_primitive(val)) { | ||
targetPropValue = val; | ||
} else { | ||
throw new Error(`Unsupported value: ${String(val)}`); | ||
} | ||
/* eslint-disable no-dupe-class-members */ | ||
return targetPropValue; | ||
} | ||
|
||
export default class BaseModel { | ||
/* eslint-disable no-underscore-dangle,camelcase */ | ||
attribute_map: Record<string, string>; | ||
|
||
get_obj_for_encoding() { | ||
/** | ||
* Get an object ready for encoding to either JSON or msgpack. | ||
* @param binary - Use true to indicate that the encoding can handle raw binary objects | ||
* (Uint8Arrays). Use false to indicate that raw binary objects should be converted to base64 | ||
* strings. True should be used for objects that will be encoded with msgpack, and false should | ||
* be used for objects that will be encoded with JSON. | ||
*/ | ||
get_obj_for_encoding(binary = false) { | ||
/* eslint-enable no-underscore-dangle,camelcase */ | ||
const obj: Record<string, any> = {}; | ||
for (const prop of Object.keys(this)) { | ||
const val = this[prop]; | ||
if (prop !== 'attribute_map' && typeof val !== 'undefined') { | ||
const name = this.attribute_map[prop]; | ||
obj[name] = val === null ? null : this._get_obj_for_encoding(val); | ||
|
||
for (const prop of Object.keys(this.attribute_map)) { | ||
const name = this.attribute_map[prop]; | ||
const value = this[prop]; | ||
|
||
if (typeof value !== 'undefined') { | ||
obj[name] = | ||
value === null ? null : _get_obj_for_encoding(value, binary); | ||
} | ||
} | ||
|
||
return obj; | ||
} | ||
} |
Oops, something went wrong.