Skip to content

Commit ea0c837

Browse files
committed
sdks/ts: add MetadataJSON types to metadata pipeline
1 parent 4ecb6f9 commit ea0c837

File tree

4 files changed

+30
-16
lines changed

4 files changed

+30
-16
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: 5 additions & 3 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()) {
@@ -739,7 +741,7 @@ export function saveAndClearInMemoryMetadata() {
739741
const tsFilePath = path.join(METADATA_DIR, METADATA_TS_FILE);
740742
const jsonFilePath = path.join(METADATA_DIR, METADATA_JSON_FILE);
741743

742-
const tsContent = `export const Metadata = ${JSON.stringify(json, null, 2)};`;
744+
const tsContent = `import type { MetadataJSON } from '@golemcloud/golem-ts-types-core';\n\nexport const Metadata = ${JSON.stringify(json, null, 2)} as const satisfies MetadataJSON;`;
743745
const jsonContent = JSON.stringify(json, null, 2);
744746

745747
fs.writeFileSync(tsFilePath, tsContent, 'utf-8');

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

sdks/ts/packages/golem-ts-types-core/src/type-json.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ export type LiteTypeJSON =
3333
types: LiteTypeJSON[];
3434
typeParams: LiteTypeJSON[];
3535
optional: boolean;
36-
originalTypeName: string | undefined;
36+
originalTypeName?: string | undefined;
3737
}
3838
| { kind: 'literal'; name?: string; literalValue?: string; optional: boolean }
3939
| {

0 commit comments

Comments
 (0)