Skip to content

Commit

Permalink
fix(cli): generate fdr definition id (#4754)
Browse files Browse the repository at this point in the history
  • Loading branch information
dsinghvi committed Sep 26, 2024
1 parent c0dd19b commit 1a1754c
Show file tree
Hide file tree
Showing 8 changed files with 158 additions and 2 deletions.
13 changes: 11 additions & 2 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,15 @@ module.exports = {
"@typescript-eslint/strict-boolean-expressions": "off",
"@typescript-eslint/no-empty-interface": "off",
"@typescript-eslint/no-unnecessary-condition": "off",
"eslint-comments/no-unused-disable": "off"
}
"eslint-comments/no-unused-disable": "off",
"jest/expect-expect": "off"
},
overrides: [
{
files: ['**/*.test.ts', '**/*.spec.ts'],
rules: {
'no-console': 'off'
}
}
]
};
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
sdks/
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
name: api
error-discrimination:
strategy: status-code
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
# yaml-language-server: $schema=https://raw.githubusercontent.com/fern-api/fern/main/fern.schema.json

service:
auth: false
base-path: /movies
endpoints:
createMovie:
docs: Add a movie to the database
method: POST
path: /create-movie
request: CreateMovieRequest
response: MovieId

getMovie:
docs: Retrieve a movie from the database based on the ID
method: GET
path: /{id}
path-parameters:
id: MovieId
response: Movie
errors:
- MovieDoesNotExistError
examples:
# Success response
- path-parameters:
id: tt0111161
response:
body:
id: tt0111161
title: The Shawshank Redemption
rating: 9.3
# Error response
- path-parameters:
id: tt1234
response:
error: MovieDoesNotExistError
body: tt1234

types:
MovieId:
type: string
docs: The unique identifier for a Movie in the database

Movie:
properties:
id: MovieId
title: string
rating:
type: double
docs: The rating scale out of ten stars

CreateMovieRequest:
properties:
title: string
rating: double

errors:
MovieDoesNotExistError:
status-code: 404
type: MovieId
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"organization": "dsinghvi",
"version": "*"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
default-group: local
groups:
local:
generators:
- name: fernapi/fern-typescript-node-sdk
version: 0.39.3
output:
location: local-file-system
path: ../sdks/typescript
44 changes: 44 additions & 0 deletions packages/cli/ete-tests/src/tests/generate/generate.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import { AbsoluteFilePath, doesPathExist, join, RelativeFilePath } from "@fern-a
import stripAnsi from "strip-ansi";
import { runFernCli } from "../../utils/runFernCli";
import { init } from "../init/init";
import { exec } from "child_process";
import { CONSOLE_LOGGER } from "../../../../task-context/node_modules/@fern-api/logger/src";

const fixturesDir = join(AbsoluteFilePath.of(__dirname), RelativeFilePath.of("fixtures"));
// const FIXTURES = ["docs"];
Expand All @@ -17,6 +19,37 @@ describe("fern generate", () => {
expect(await doesPathExist(join(pathOfDirectory, RelativeFilePath.of("sdks/typescript")))).toBe(true);
}, 180_000);

it("ir contains fdr definitionid", async () => {
const { stdout, stderr } = await runFernCli(["generate", "--log-level", "debug"], {
cwd: join(fixturesDir, RelativeFilePath.of("basic")),
reject: false
});

console.log("stdout", stdout);
console.log("stderr", stderr);

const filepath = extractFilepath(stdout);
if (filepath == null) {
throw new Error(`Failed to get path to IR:\n${stdout}`);
}

const process = exec(
`./ir-contains-fdr-definition-id.sh ${filepath}`,
{ cwd: __dirname },
(error, stdout, stderr) => {
if (error) {
console.error(`Error: ${error.message}`);
return;
}
if (stderr) {
console.error(`stderr: ${stderr}`);
return;
}
console.log(`stdout: ${stdout}`);
}
);
}, 180_000);

it("missing docs page", async () => {
const { stdout } = await runFernCli(["generate", "--docs"], {
cwd: join(fixturesDir, RelativeFilePath.of("docs-missing-page")),
Expand All @@ -30,3 +63,14 @@ describe("fern generate", () => {
).toMatchSnapshot();
}, 180_000);
});

function extractFilepath(logLine: string): string | null {
const prefix = "Wrote IR to disk:";
const index = logLine.indexOf(prefix);

if (index !== -1) {
return logLine.slice(index + prefix.length).trim();
}

return null;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#!/bin/bash

# Check if a file name is provided
if [ $# -eq 0 ]; then
echo "Error: No file provided"
echo "Usage: $0 <json_file>"
exit 1
fi

# Store the file name
file="$1"

# Check if the file exists
if [ ! -f "$file" ]; then
echo "Error: File '$file' not found"
exit 1
fi

# Use jq to check if "fdrApiDefinitionId" key exists
if cat "$file" | jq 'has("fdrApiDefinitionId")' | grep -q true; then
echo "Success: 'fdrApiDefinitionId' key found in $file"
exit 0
else
echo "Failure: 'fdrApiDefinitionId' key not found in $file"
exit 1
fi

0 comments on commit 1a1754c

Please sign in to comment.