Skip to content

Commit

Permalink
update definition files of ast package
Browse files Browse the repository at this point in the history
add helpers for mobx
  • Loading branch information
Zetazzz committed Feb 22, 2023
1 parent a41501d commit 73f3da4
Show file tree
Hide file tree
Showing 13 changed files with 229 additions and 1 deletion.
3 changes: 3 additions & 0 deletions packages/ast/src/state/mobx/mobx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,9 @@ const buildStore = (
//add util for MobxResponse
context.addUtil('MobxResponse');

//add util for override
context.addUtil('override');

const requestType = serviceMethod.requestType;
const responseType = serviceMethod.responseType;
const fieldNames = Object.keys(serviceMethod.fields ?? {});
Expand Down
1 change: 1 addition & 0 deletions packages/ast/types/src/state/index.d.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from './react-query';
export * from './mobx';
1 change: 1 addition & 0 deletions packages/ast/types/src/state/mobx/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './mobx';
46 changes: 46 additions & 0 deletions packages/ast/types/src/state/mobx/mobx.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import * as t from '@babel/types';
import { ProtoService } from '@osmonauts/types';
import { GenericParseContext } from '../../encoding';
/**
* Entry for building stores.
* @param {Object=} context - context of generating the file
* @param {Object=} service - method details
*/
export declare const createMobxQueryStores: (context: GenericParseContext, service: ProtoService) => void;
/**
* Create an AST to generate creating store functions.
* eg:
* export const createRpcStores = (rpc: ProtobufRpcClient | undefined) => {
* const queryService = getQueryService(rpc);
*
* class BalanceStoreInherited extends QueryStore<
* QueryBalanceRequest,
* QueryBalanceResponse
* > {
* constructor() {
* super(queryService?.balance);
* makeObservable(this, {
* state: override,
* request: override,
* response: override,
* isLoading: override,
* isSuccess: override,
* refetch: override,
* getData: override
* });
* }
*
* balance(request: QueryBalanceRequest): MobxResponse<QueryBalanceResponse> {
* return this.getData(request);
* }
* }
*
* return {
* BalanceStoreInherited
* };
* };
* @param {Object=} context - context of generating the file
* @param {Object=} service - method details
* @returns {ParseResult} created AST
*/
export declare const buildRpcStores: (context: GenericParseContext, service: ProtoService) => t.ExportNamedDeclaration;
1 change: 1 addition & 0 deletions packages/ast/types/src/state/mobx/mobx.scoped.test.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export {};
1 change: 1 addition & 0 deletions packages/ast/types/src/state/mobx/mobx.test.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export {};
16 changes: 16 additions & 0 deletions packages/ast/types/src/state/mobx/scoped-bundle.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { GenericParseContext } from '../../encoding';
export declare const createMobxQueryFactory: (context: GenericParseContext, obj: object) => (import("@babel/types").ExportNamedDeclaration | {
type: string;
importKind: string;
specifiers: {
type: string;
local: {
type: string;
name: string;
};
}[];
source: {
type: string;
value: string;
};
})[];
1 change: 1 addition & 0 deletions packages/ast/types/src/utils/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export * from './babel';
export * from './utils';
export * from './scoped-bundle-builder';
53 changes: 53 additions & 0 deletions packages/ast/types/src/utils/scoped-bundle-builder.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import * as t from '@babel/types';
import { GenericParseContext } from '../encoding';
/**
* Create an AST for a certain key and method.
* eg: __fixtures__/output1/hooks.ts
* v1beta2: _AkashAuditV1beta2Queryrpc.createRpcQueryHooks(rpc)
* @param {Object=} imports - imports array reference for generating imports.
* @param {Object=} path - filename of a package.
* @param {string} methodName - hook method name of packages
* @returns {ParseResult} created AST
*/
export declare const buildSingleCreator: (imports: HookImport[], path: string, methodName: string) => t.CallExpression;
/**
* Create an ASTs for method creators of packages recursively, and get imports of packages.
* eg: __fixtures__/output1/hooks.ts
* export const createRpcQueryHooks = ...
* @param {Object=} imports - imports array reference for generating imports.
* @param {Object=} obj - mapping of packages and rpc query filenames
* @param {string} methodName - hook method name of packages
* @returns {ParseResult} created AST
*/
export declare const buildNestedCreator: (imports: HookImport[], obj: object, methodName: string) => any;
interface HookImport {
as: string;
path: string;
}
/**
* Create an ASTs for export creators.
* Generating files like:
* __fixtures__/output1/hooks.ts
* @param {Object=} context - context of generating the file
* @param {Object=} obj - mapping of packages and rpc query filenames
* @param {string} identifier - name of function creating hooks. eg: createRpcQueryHooks
* @param {string[]} utils - name of imported utils.
* @param {string} methodName - name of a certain method that creates a store or hook. eg: createRpcQueryHooks
* @returns {ParseResult} created AST
*/
export declare const buildExportCreators: (context: GenericParseContext, obj: object, identifier: string, utils: string[], methodName?: string) => (t.ExportNamedDeclaration | {
type: string;
importKind: string;
specifiers: {
type: string;
local: {
type: string;
name: string;
};
}[];
source: {
type: string;
value: string;
};
})[];
export {};
3 changes: 2 additions & 1 deletion packages/telescope/src/helpers/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export * from './external';
export * from './internal';
export * from './react-query';
export * from './mobx';
export * from './grpc-gateway';
export * from './grpc-web';
export * from './grpc-web';
88 changes: 88 additions & 0 deletions packages/telescope/src/helpers/mobx.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
export const mobx = `
import {
action,
computed,
makeObservable,
observable,
runInAction
} from 'mobx';
import { QueryStatus } from '@tanstack/react-query';
interface MobxResponse<T> {
data: T | undefined;
isSuccess: boolean;
isLoading: boolean;
refetch: () => Promise<void>;
}
class QueryStore<Request, Response> {
state?: QueryStatus;
request?: Request;
response?: Response;
fetchFunc?: (request: Request) => Promise<Response>;
constructor(fetchFunc?: (request: Request) => Promise<Response>) {
this.fetchFunc = fetchFunc;
makeObservable(this, {
state: observable,
request: observable.ref,
response: observable.ref,
isLoading: computed,
isSuccess: computed,
refetch: action.bound,
getData: action.bound,
});
}
get isLoading() {
return this.state === 'loading';
}
get isSuccess() {
return this.state === 'success';
}
async refetch(): Promise<void> {
runInAction(() => {
this.response = void 0;
this.state = 'loading';
});
try {
if (!this.fetchFunc)
throw new Error(
'Query Service not initialized or request function not implemented'
);
if (!this.request) throw new Error('Request not provided');
const response = await this.fetchFunc(this.request);
runInAction(() => {
this.response = response;
this.state = 'success';
});
console.log(
'%cquery.rpc.Query.ts line:572 this.state',
'color: #007acc;',
this.state,
this.response
);
} catch (e) {
console.error(e);
runInAction(() => {
this.state = 'error';
});
}
}
getData(request: Request): MobxResponse<Response> {
runInAction(() => {
this.request = request;
});
return {
data: this.response,
isSuccess: this.isSuccess,
isLoading: this.isLoading,
refetch: this.refetch,
};
}
}
`;
4 changes: 4 additions & 0 deletions packages/telescope/src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,16 @@ export const UTILS = {
useTendermintClient: '__react-query__',
ReactQueryParams: '__react-query__',
UseQueryOptions: '@tanstack/react-query',
QueryStore: '__mobx__',
MobxResponse: '__mobx__',
override: 'mobx',
};

export const UTIL_HELPERS = [
'__helpers__',
'__extern__',
'__react-query__',
'__mobx__',
'__grpc-gateway__',
];

Expand Down
12 changes: 12 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3344,6 +3344,18 @@
dependencies:
"@octokit/openapi-types" "^12.7.0"

"@osmonauts/ast@./packages/ast":
version "0.74.0"
dependencies:
"@babel/parser" "^7.19.3"
"@babel/runtime" "^7.19.0"
"@babel/types" "7.19.3"
"@osmonauts/proto-parser" "^0.37.1"
"@osmonauts/types" "^0.29.1"
"@osmonauts/utils" "^0.10.0"
case "1.6.3"
dotty "0.1.2"

"@osmonauts/lcd@^0.8.0":
version "0.8.0"
resolved "https://registry.npmjs.org/@osmonauts/lcd/-/lcd-0.8.0.tgz#fcabba93edadd23f73b2046a5cad897b420a9c84"
Expand Down

0 comments on commit 73f3da4

Please sign in to comment.