Skip to content

Commit

Permalink
Generate API types using code generation (#325)
Browse files Browse the repository at this point in the history
  • Loading branch information
jdtzmn committed Apr 6, 2021
1 parent fad545c commit dde1044
Show file tree
Hide file tree
Showing 11 changed files with 1,629 additions and 347 deletions.
2 changes: 1 addition & 1 deletion src/client/v2/algod/algod.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import ServiceClient from '../serviceClient';
import { AlgodTokenHeader, CustomTokenHeader } from '../../client';
import modelsv2 from './models/types';
import * as modelsv2 from './models/types';
import AccountInformation from './accountInformation';
import Block from './block';
import Compile from './compile';
Expand Down
2 changes: 1 addition & 1 deletion src/client/v2/algod/dryrun.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import JSONRequest from '../jsonrequest';
import HTTPClient from '../../client';
import modelsv2 from './models/types';
import * as modelsv2 from './models/types';
import * as encoding from '../../../encoding/encoding';
import { setHeaders } from './compile';

Expand Down
Original file line number Diff line number Diff line change
@@ -1,23 +1,32 @@
import { Address } from '../../../../types/address';

/**
* Base class for models
*/

/* eslint-disable no-underscore-dangle,camelcase,class-methods-use-this */
class BaseModel {
_is_primitive(val) {
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')
);
}

_is_address(val) {
_is_address(val: any): val is Address {
return val.publicKey !== undefined && val.checksum !== undefined;
}

_get_obj_for_encoding(val) {
let targetPropValue;
/* 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)) {
Expand All @@ -38,9 +47,10 @@ class BaseModel {
}
return targetPropValue;
}
/* eslint-disable no-dupe-class-members */

get_obj_for_encoding() {
const obj = {};
const obj: Record<string, any> = {};
for (const prop of Object.keys(this)) {
const val = this[prop];
if (prop !== 'attribute_map' && typeof val !== 'undefined') {
Expand All @@ -51,5 +61,3 @@ class BaseModel {
return obj;
}
}

module.exports = { BaseModel };
Loading

0 comments on commit dde1044

Please sign in to comment.