Skip to content
This repository was archived by the owner on Aug 15, 2025. It is now read-only.

Commit adacc29

Browse files
committed
Implemented mechanism to write yaml on updates to API
1 parent ff51309 commit adacc29

File tree

2 files changed

+75
-8
lines changed

2 files changed

+75
-8
lines changed

desktop-app/src/service/client.ts

Lines changed: 61 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,9 @@ import { join } from "@tauri-apps/api/path";
1818
import {
1919
GolemApplicationManifest,
2020
HttpApiDefinition,
21+
serializeHttpApiDefinition,
2122
} from "@/types/golemManifest.ts";
22-
import { parse, stringify } from "yaml";
23+
import { parse, parseDocument, Document, stringify } from "yaml";
2324

2425
export class Service {
2526
public baseUrl: string;
@@ -158,32 +159,29 @@ export class Service {
158159
public async saveComponentManifest(
159160
appId: string,
160161
componentId: string,
161-
manifest: GolemApplicationManifest,
162+
manifest: string,
162163
): Promise<boolean> {
163164
const component = await this.getComponentById(appId, componentId);
164165
let componentYamlPath = await this.getComponentYamlPath(
165166
appId,
166167
component.componentName!,
167168
);
168-
let rawYaml = stringify(manifest);
169169
// Write the YAML string to the file
170-
await writeTextFile(componentYamlPath, rawYaml);
170+
await writeTextFile(componentYamlPath, manifest);
171171

172172
return true;
173173
}
174174

175175
public async saveAppManifest(
176176
appId: string,
177-
manifest: GolemApplicationManifest,
177+
manifest: string,
178178
): Promise<boolean> {
179179
const app = await settingsService.getAppById(appId);
180180
if (!app) {
181181
throw new Error("App not found");
182182
}
183183
let appManifestPath = await join(app.folderLocation, "golem.yaml");
184-
let rawYaml = stringify(manifest);
185-
// Write the YAML string to the file
186-
await writeTextFile(appManifestPath, rawYaml);
184+
await writeTextFile(appManifestPath, manifest);
187185

188186
return true;
189187
}
@@ -315,13 +313,15 @@ export class Service {
315313
for (const apiListKey in APIList.definitions) {
316314
let data = APIList.definitions[apiListKey];
317315
data.id = apiListKey;
316+
data.componentId = component.componentId;
318317
result.push(data);
319318
}
320319
}
321320
} catch (e) {
322321
console.error(e, component.componentName);
323322
}
324323
}
324+
// find in app's golem.yaml
325325

326326
return result;
327327
};
@@ -603,4 +603,57 @@ export class Service {
603603
// });
604604
// return resp;
605605
// };
606+
607+
public async createApiVersion(appId: string, payload: HttpApiDefinition) {
608+
// We need to know if the definition came from a component and store it there
609+
const app = await settingsService.getAppById(appId);
610+
let yamlToUpdate = app!.golemYamlLocation;
611+
612+
if (payload.componentId) {
613+
const component = await this.getComponentById(appId, payload.componentId);
614+
yamlToUpdate = await this.getComponentYamlPath(
615+
appId,
616+
component.componentName!,
617+
);
618+
}
619+
620+
// Now load the YAML into memory, update and save
621+
const rawYaml = await readTextFile(yamlToUpdate);
622+
623+
// Parse as Document to preserve comments and formatting
624+
const manifest: Document = parseDocument(rawYaml);
625+
626+
// Type-safe access to the parsed content
627+
// const manifestData = manifest.toJS() as GolemApplicationManifest;
628+
629+
// Get or create httpApi section
630+
let httpApi = manifest.get("httpApi") as YAMLMap | undefined;
631+
if (!httpApi) {
632+
// Create new httpApi section if it doesn't exist
633+
manifest.set("httpApi", {});
634+
httpApi = manifest.get("httpApi") as YAMLMap;
635+
}
636+
637+
// Get or create definitions section
638+
let definitions = httpApi.get("definitions") as YAMLMap | undefined;
639+
if (!definitions) {
640+
// Create new definitions section if it doesn't exist
641+
httpApi.set("definitions", {});
642+
definitions = httpApi.get("definitions") as YAMLMap;
643+
}
644+
645+
// Add or update the API definition
646+
definitions.set(payload.id!, serializeHttpApiDefinition(payload));
647+
648+
// Save config back
649+
if (payload.componentId) {
650+
await this.saveComponentManifest(
651+
appId,
652+
payload.componentId,
653+
manifest.toString(),
654+
);
655+
} else {
656+
await this.saveAppManifest(appId, manifest.toString());
657+
}
658+
}
606659
}

desktop-app/src/types/golemManifest.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,21 @@ export interface HttpApiDefinition {
8888
version: string;
8989
project?: string;
9090
routes?: HttpApiDefinitionRoute[];
91+
92+
// id and componentId not part of YAML
9193
id?: string;
94+
componentId?: string;
95+
}
96+
97+
export function serializeHttpApiDefinition(
98+
definition: HttpApiDefinition,
99+
): HttpApiDefinition {
100+
// remove added attributes
101+
return {
102+
version: definition.version,
103+
project: definition.project,
104+
routes: definition.routes,
105+
};
92106
}
93107

94108
export interface HttpApiDefinitionRoute {

0 commit comments

Comments
 (0)