@cosmonauts/ast-gen
TypeScript icon, indicating that this package has built-in type declarations

0.1.1 • Public • Published

@cosmonauts/ast-gen

Cosmos Typescript ASTs

messages

import generate from '@babel/generator';
import { messages } from '@cosmonauts/ast-gen';

export const mutations: Mutation[] = [
    { methodName: 'joinPool', typeUrl: '/cosmos.pools.transfer.v1.MsgJoinPool', TypeName: 'MsgJoinPool' },
    { methodName: 'exitPool', typeUrl: '/cosmos.pools.transfer.v1.MsgExitPool', TypeName: 'MsgExitPool' }
];

const ast = messages(mutations)
console.log(generate(myModule).code)

produces:

export const messages = {
  joinPool(value: MsgJoinPool) {
    return {
      typeUrl: "/cosmos.pools.transfer.v1.MsgJoinPool",
      value: MsgJoinPool.fromPartial(value)
    };
  },
  exitPool(value: MsgExitPool) {
    return {
      typeUrl: "/cosmos.pools.transfer.v1.MsgExitPool",
      value: MsgExitPool.fromPartial(value)
    };
  }
};

encoded

import generate from '@babel/generator';
import { encoded } from '@cosmonauts/ast-gen';

export const mutations: Mutation[] = [
    { methodName: 'joinPool', typeUrl: '/cosmos.pools.transfer.v1.MsgJoinPool', TypeName: 'MsgJoinPool' },
    { methodName: 'exitPool', typeUrl: '/cosmos.pools.transfer.v1.MsgExitPool', TypeName: 'MsgExitPool' }
];

const ast = encoded(mutations)
console.log(generate(myModule).code)

produces:

export const encoded = {
  joinPool(value: MsgJoinPool) {
    return {
      type_url: "/cosmos.pools.transfer.v1.MsgJoinPool",
      value: MsgJoinPool.encode(value).finish()
    };
  },

  exitPool(value: MsgExitPool) {
    return {
      type_url: "/cosmos.pools.transfer.v1.MsgExitPool",
      value: MsgExitPool.encode(value).finish()
    };
  }
};

namespaces

import generate from '@babel/generator';
import * as t from '@babel/types';
import { recursiveNamespace } from '@cosmonauts/ast-gen';

const myModule = recursiveNamespace(
    [
        'osmosis',
        'gamm',
        'v1beta',
        'pools'
    ].reverse(), [astBody]);

console.log(generate(t.program(myModule)).code)

produces:

export namespace osmosis {
  export namespace gamm {
    export namespace v1beta {
      export namespace pools {

          // astBody here

      }
    }
  }
}

Readme

Keywords

none

Package Sidebar

Install

npm i @cosmonauts/ast-gen

Weekly Downloads

0

Version

0.1.1

License

SEE LICENSE IN LICENSE

Unpacked Size

95.5 kB

Total Files

37

Last publish

Collaborators

  • ljun
  • pyramation