Skip to content

Commit 246608f

Browse files
committed
sdks/ts: add MetadataJSON types to metadata pipeline
- Add MethodMetadataJSON, ClassMetadataJSON, MetadataJSON type definitions to types-core - Replace `any` with MetadataJSON in loadFromJson, removing all `as any` casts inside - Use ClassMetadataJSON and MethodMetadataJSON in typegen saveAndClearInMemoryMetadata - Use MetadataJSON in TypescriptTypeRegistry.register
1 parent 4ecb6f9 commit 246608f

File tree

3 files changed

+28
-14
lines changed

3 files changed

+28
-14
lines changed

sdks/ts/packages/golem-ts-sdk/src/typescriptTypeRegistry.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@
1515
// A wrapper over golem-ts-types-core/TypeMetadata to be used from user's code
1616
// for them to register its types with the SDk.
1717

18-
import { TypeMetadata } from '@golemcloud/golem-ts-types-core';
18+
import { TypeMetadata, MetadataJSON } from '@golemcloud/golem-ts-types-core';
1919

2020
export const TypescriptTypeRegistry = {
21-
register(typeMetadata: any): void {
21+
register(typeMetadata: MetadataJSON): void {
2222
TypeMetadata.loadFromJson(typeMetadata);
2323
},
2424
};

sdks/ts/packages/golem-ts-typegen/src/index.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,9 @@ import {
2727
} from 'ts-morph';
2828
import {
2929
buildJSONFromType,
30+
ClassMetadataJSON,
3031
LiteTypeJSON,
32+
MethodMetadataJSON,
3133
Node,
3234
Symbol,
3335
Type,
@@ -709,15 +711,15 @@ export function saveAndClearInMemoryMetadata() {
709711
fs.mkdirSync(METADATA_DIR);
710712
}
711713

712-
const json: Record<string, any> = {};
714+
const json: Record<string, ClassMetadataJSON> = {};
713715

714716
for (const [className, meta] of TypeMetadata.getAll().entries()) {
715717
const constructorArgsJSON = meta.constructorArgs.map((arg) => ({
716718
name: arg.name,
717719
type: buildJSONFromType(arg.type),
718720
}));
719721

720-
const methodsObj: Record<string, any> = {};
722+
const methodsObj: Record<string, MethodMetadataJSON> = {};
721723
for (const [methodName, { methodParams, returnType }] of meta.methods) {
722724
const paramsJSON: Record<string, LiteTypeJSON> = {};
723725
for (const [paramName, paramType] of methodParams.entries()) {

sdks/ts/packages/golem-ts-types-core/src/metadata.ts

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,21 @@ export type ClassMetadata = {
3232
methods: Map<MethodNameString, { methodParams: MethodParams; returnType: Type }>;
3333
};
3434

35+
export type MethodMetadataJSON = {
36+
methodParams: Record<string, LiteTypeJSON>;
37+
returnType: LiteTypeJSON;
38+
};
39+
40+
export type ClassMetadataJSON = {
41+
constructorArgs: Array<{
42+
name: string;
43+
type: LiteTypeJSON;
44+
}>;
45+
methods: Record<string, MethodMetadataJSON>;
46+
};
47+
48+
export type MetadataJSON = Record<ClassNameString, ClassMetadataJSON>;
49+
3550
const Metadata = new Map<ClassNameString, ClassMetadata>();
3651

3752
export const TypeMetadata = {
@@ -60,14 +75,11 @@ export const TypeMetadata = {
6075
Metadata.clear();
6176
},
6277

63-
// TODO: avoid any. Here any simply represents the json representation of Metadata
78+
// Represents the json representation of Metadata
6479
// such that every Type is represented as LiteTypeJSON
65-
loadFromJson(json: any) {
80+
loadFromJson(json: MetadataJSON) {
6681
for (const [className, meta] of Object.entries(json)) {
67-
const constructorArgsJSON = (meta as any).constructorArgs as Array<{
68-
name: string;
69-
type: LiteTypeJSON;
70-
}>;
82+
const constructorArgsJSON = meta.constructorArgs;
7183

7284
const constructorArgs = constructorArgsJSON.map((arg) => ({
7385
name: arg.name,
@@ -76,15 +88,15 @@ export const TypeMetadata = {
7688

7789
const methodsMap = new Map<string, { methodParams: Map<string, Type>; returnType: Type }>();
7890

79-
for (const [methodName, methodMeta] of Object.entries((meta as any).methods)) {
91+
for (const [methodName, methodMeta] of Object.entries(meta.methods)) {
8092
const methodParamsMap = new Map<string, Type>();
81-
for (const [paramName, paramJSON] of Object.entries((methodMeta as any).methodParams)) {
82-
methodParamsMap.set(paramName, buildTypeFromJSON(paramJSON as LiteTypeJSON));
93+
for (const [paramName, paramJSON] of Object.entries(methodMeta.methodParams)) {
94+
methodParamsMap.set(paramName, buildTypeFromJSON(paramJSON));
8395
}
8496

8597
methodsMap.set(methodName, {
8698
methodParams: methodParamsMap,
87-
returnType: buildTypeFromJSON((methodMeta as any).returnType as LiteTypeJSON),
99+
returnType: buildTypeFromJSON(methodMeta.returnType),
88100
});
89101
}
90102

0 commit comments

Comments
 (0)