Skip to content

Commit

Permalink
Merge pull request #32 from hasura/m-bilal/fix-import-issues--edc-38
Browse files Browse the repository at this point in the history
remove unused imports, organize imports and fix import issues
  • Loading branch information
gneeri authored Jun 5, 2024
2 parents 2f72327 + 449ad26 commit 49d834c
Show file tree
Hide file tree
Showing 27 changed files with 76,120 additions and 985 deletions.
4 changes: 3 additions & 1 deletion changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@

## Unreleased

- remove unused imports, organize imports and fix import issues ([#32](https://github.com/hasura/ndc-open-api-lambda/pull/32))

## [[0.1.0](https://github.com/hasura/ndc-open-api-lambda/releases/tag/v0.1.0)] 2024-06-03

- Update `ghcr.io/hasura/ndc-nodejs-lambda` to version `v1.4.0` and remove env var `NDC_LAMBDA_SDK_VERSION` ([30](https://github.com/hasura/ndc-open-api-lambda/pull/30)).
- Update `ghcr.io/hasura/ndc-nodejs-lambda` to version `v1.4.0` and remove env var `NDC_LAMBDA_SDK_VERSION` ([#30](https://github.com/hasura/ndc-open-api-lambda/pull/30)).
- API requests support forwarding headers that are sent to the data connector. Manual addition of headers via the `--headers` flag and `NDC_OAS_HEADERS` env var has been removed ([#28](https://github.com/hasura/ndc-open-api-lambda/pull/28)).
- Added support for adding secruity param as a query param in `api.ts` ([#27](https://github.com/hasura/ndc-open-api-lambda/pull/27))
- Added support for `@save` annotation to preserve user's changes ([#24](https://github.com/hasura/ndc-open-api-lambda/pull/24))
Expand Down
92 changes: 61 additions & 31 deletions src/app/generator/functions-ts-generator.test.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
import * as assert from "assert";
import * as context from "../context";
import * as legacyApiTsGenerator from "../parser/open-api/api-generator";
import * as apiTsGenerator from "./api-ts-generator";
import * as functionTsGenerator from "./functions-ts-generator";
import * as headerParser from "../parser/open-api/header-parser";
import * as prettier from "prettier";
import path from "path";
import { readFileSync, writeFileSync } from "fs";
import * as types from "./types";
import * as schemaParser from "../parser/open-api/schema-parser";
import * as generator from ".";
import { cleanupAndFormat } from "../writer/functions-ts-writer";

const CircularJSON = require("circular-json");

Expand All @@ -19,8 +14,6 @@ const tests: {
openApiUri: string; // for now, we only consider files on disks in test cases
goldenFile: string;
baseUrl?: string;
_legacyApiComponents?: legacyApiTsGenerator.ApiComponents;
_generatedApiTsComponents?: types.GeneratedApiTsCode;
_goldenFileContent?: string;
}[] = [
{
Expand Down Expand Up @@ -86,6 +79,42 @@ const tests: {
goldenFile: "./golden-files/kubernetes",
baseUrl: "http://localhost:9191",
},
{
name: "AtlassianJira",
openApiUri: "./open-api-docs/atlassian-jira.json",
goldenFile: "./golden-files/atlassian-jira",
baseUrl: "",
},
{
name: "1Password",
openApiUri: "./open-api-docs/1password.json",
goldenFile: "./golden-files/1password",
baseUrl: "",
},
{
name: "AwsCloudMap",
openApiUri: "./open-api-docs/aws-cloud-map.json",
goldenFile: "./golden-files/aws-cloud-map",
baseUrl: "",
},
{
name: "AmazonWorkspaces",
openApiUri: "./open-api-docs/amazon-workspaces.json",
goldenFile: "./golden-files/amazon-workspaces",
baseUrl: "",
},
{
name: "AckoInsurance",
openApiUri: "./open-api-docs/acko-insurance.json",
goldenFile: "./golden-files/acko-insurance",
baseUrl: "",
},
{
name: "MicrosoftWorkloadMonitor",
openApiUri: "./open-api-docs/microsoft-workload-monitor.json",
goldenFile: "./golden-files/microsoft-workload-monitor",
baseUrl: "",
},
];

describe("functions-ts-generator", async () => {
Expand All @@ -98,6 +127,16 @@ async function testGenerateFunctionsTsCode() {
before(async () => {
const relativeDirectorToTestFiles = "../../../tests/test-data/";

context
.getInstance()
.setOutputDirectory(
path.resolve(
__dirname,
relativeDirectorToTestFiles,
"golden-files",
),
);

testCase.openApiUri = path.resolve(
__dirname,
relativeDirectorToTestFiles,
Expand All @@ -111,35 +150,26 @@ async function testGenerateFunctionsTsCode() {
testCase._goldenFileContent = readFileSync(
testCase.goldenFile,
).toString();

testCase._generatedApiTsComponents =
await apiTsGenerator.generateApiTsCode(testCase.openApiUri);

const parsedSchemastore = schemaParser.getParsedSchemaStore(
testCase._generatedApiTsComponents.typeNames,
testCase._generatedApiTsComponents.schemaComponents,
);
testCase._generatedApiTsComponents.schemaStore = parsedSchemastore;

testCase._legacyApiComponents =
testCase._generatedApiTsComponents.legacyTypedApiComponents;
});

it(`should generate functions.ts file content for ${testCase.name}`, async () => {
let got = await functionTsGenerator.generateFunctionsTsCode(
testCase._legacyApiComponents!,
testCase._generatedApiTsComponents!,
testCase.baseUrl,
);

got.fileContent = await prettier.format(got.fileContent, {
parser: "typescript",
const got = await generator.generateCode({
openApiUri: testCase.openApiUri,
baseUrl: testCase.baseUrl,
});

assert.equal(got.fileContent, testCase._goldenFileContent);
const gotFunctionTs = got.filter(
(item) => item.fileType === "functions-ts",
)[0]!;

const gotApiTs = got.filter((item) => item.fileType === "api-ts")[0]!;

await cleanupAndFormat(gotFunctionTs, gotApiTs);

assert.equal(gotFunctionTs.fileContent, testCase._goldenFileContent);

// uncomment to update golden file
// writeFileSync(testCase.goldenFile, got.fileContent);
// writeFileSync(testCase.goldenFile, gotFunctionTs.fileContent);
});
}
});
Expand Down
63 changes: 63 additions & 0 deletions src/app/parser/typescript/cleanup.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import * as assert from "assert";
import path from "path";
import * as appTypes from "../../types";
import * as fs from "fs";
import * as cleanup from "./cleanup";

const tests: {
name: string;
_files?: appTypes.GeneratedCode[];
_goldenFileContent?: string;
_functionsTsFile?: appTypes.GeneratedCode;
_goldenFilePath?: string;
}[] = [
{
name: "atlassian-jira",
},
];

describe("cleanup::fixImports", async () => {
for (const testCase of tests) {
before(async () => {
const testFileDir = path.resolve(
__dirname,
"./test-data/cleanup-tests/",
testCase.name,
);

const apiFilePath = path.resolve(testFileDir, "api");
const functionsFilePath = path.resolve(testFileDir, "test");
testCase._goldenFilePath = path.resolve(testFileDir, "golden-file");
testCase._goldenFileContent = fs
.readFileSync(testCase._goldenFilePath)
.toString();

const apiTsFile: appTypes.GeneratedCode = {
fileContent: fs.readFileSync(apiFilePath).toString(),
filePath: `${apiFilePath}.ts`,
fileType: "api-ts",
};

const functionsTsFile: appTypes.GeneratedCode = {
fileContent: fs.readFileSync(functionsFilePath).toString(),
filePath: `${functionsFilePath}.ts`,
fileType: "functions-ts",
};

testCase._files = [apiTsFile, functionsTsFile];
testCase._functionsTsFile = functionsTsFile;
});

it(`${testCase.name}`, async () => {
cleanup.fixImports(testCase._files!);

assert.equal(
testCase._functionsTsFile!.fileContent,
testCase._goldenFileContent!,
);

// update golden file
// fs.writeFileSync(testCase._goldenFilePath!, testCase._functionsTsFile!.fileContent);
});
}
});
34 changes: 34 additions & 0 deletions src/app/parser/typescript/cleanup.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import * as ts from "ts-morph";
import * as types from "../../types";
import * as context from "../../context";
import * as fs from "fs";

export function fixImports(generatedCodeList: types.GeneratedCode[]) {
let project: ts.Project;
if (fs.existsSync(context.getInstance().getTsConfigFilePath())) {
project = new ts.Project({
tsConfigFilePath: context.getInstance().getTsConfigFilePath(),
});
} else {
project = new ts.Project();
}

for (const generatedCode of generatedCodeList) {
project.createSourceFile(
generatedCode.filePath,
generatedCode.fileContent,
{
overwrite: true,
},
);
}

for (const sourceFile of project.getSourceFiles()) {
sourceFile.fixMissingImports().organizeImports();

const code = generatedCodeList.filter(
(item) => item.filePath === sourceFile.getFilePath(),
)[0]!;
code.fileContent = sourceFile.getFullText();
}
}
16 changes: 16 additions & 0 deletions src/app/parser/typescript/fomat.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import * as prettier from "prettier";
import * as logger from "../../../util/logger";

export async function format(code: string): Promise<string> {
try {
code = await prettier.format(code, {
parser: "typescript",
});
} catch (e) {
logger.error(
"Error while formatting code. The resulting code will be unformatted and may contain syntax errors. Error: ",
e,
);
}
return code;
}
Loading

0 comments on commit 49d834c

Please sign in to comment.