Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## Unreleased

- Add support for `@save` in `api.ts` ([#89](https://github.com/hasura/ndc-open-api-lambda/pull/89))

## [[1.5.2](https://github.com/hasura/ndc-open-api-lambda/releases/tag/v1.5.2)] 2025-03-25

- Update dependencies. Also update ndc-nodejs-lambda to `v1.12.0` ([#87](https://github.com/hasura/ndc-open-api-lambda/pull/87))
Expand Down
4 changes: 2 additions & 2 deletions docs/documentation.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,15 +66,15 @@ const api = new Api({

## Saving User Changes

When re-introspecting the connector, user changes in `functions.ts` can be preserved by adding an `@save` JS Doc Tag to the documentation comment of a statement. `@save` is currently supported for the following statements:
When re-introspecting the connector, user changes in `functions.ts` or `api.ts` can be preserved by adding an `@save` JS Doc Tag to the documentation comment of a statement. `@save` is only supported for root level statements. `@save` is currently supported for the following statements:

- functions
- variable/constant declarations
- types
- interfaces
- classes

This will ensure that the statements marked with `@save` are not overwritten and the saved statements will be added if missing in the newly generated `functions.ts`
This will ensure that the statements marked with `@save` are not overwritten and the saved statements will be added if missing in the newly generated `functions.ts` or the `apt.ts` file

Example

Expand Down
23 changes: 15 additions & 8 deletions src/app/writer/api-ts-writer.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import * as types from "../types";
import * as fs from "fs";
import * as logger from "../../util/logger";
import * as tsParser from "../parser/typescript";
import * as format from "../parser/typescript/fomat";

export class SimilarFileContentsError extends Error {
constructor(message: string) {
Expand All @@ -9,22 +11,27 @@ export class SimilarFileContentsError extends Error {
}
}

export function writeToFileSystem(codeToWrite: types.GeneratedCode) {
export async function writeToFileSystem(codeToWrite: types.GeneratedCode) {
if (!fs.existsSync(codeToWrite.filePath)) {
logger.info(`Creating ${codeToWrite.filePath}`);
fs.writeFileSync(codeToWrite.filePath, codeToWrite.fileContent);
return;
}

const staleFile = fs.readFileSync(codeToWrite.filePath).toString();
const shouldOverwrite = staleFile !== codeToWrite.fileContent;

if (!shouldOverwrite) {
throw new SimilarFileContentsError(
`'${codeToWrite.filePath}' already represents the latest OpenAPI Document`,
);
}

logger.info(`Overwriting ${codeToWrite.filePath}`);

codeToWrite.fileContent = tsParser.preserveUserChanges(
staleFile,
codeToWrite.fileContent,
);

await cleanupAndFormat(codeToWrite);

fs.writeFileSync(codeToWrite.filePath, codeToWrite.fileContent);
}

export async function cleanupAndFormat(apiTs: types.GeneratedCode) {
apiTs.fileContent = await format.format(apiTs.fileContent);
}
2 changes: 1 addition & 1 deletion src/app/writer/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export async function writeToFileSystem(codeToWrite: types.GeneratedCode[]) {
(element) => element.fileType === "functions-ts",
)[0]!;

apiWriter.writeToFileSystem(apiTsCode);
await apiWriter.writeToFileSystem(apiTsCode);
await functionsWriter.writeToFileSystem(functionsTsCode, apiTsCode);
await packageJsonWriter.writeToFileSystem();
tsConfigWriter.writeToFileSystem();
Expand Down