Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: better example generation, add parameter definitions #2018

Merged
merged 13 commits into from
Jan 15, 2025
2 changes: 1 addition & 1 deletion packages/parsers/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@fern-api/docs-parsers",
"version": "0.0.37",
"version": "0.0.38",
"repository": {
"type": "git",
"url": "https://github.com/fern-api/fern-platform.git",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -184,14 +184,12 @@ export class XFernEndpointExampleConverterNode extends BaseOpenApiV3_1ConverterN
this.requestBodyByContentType ?? {}
)[0];

if (requestBodyContentTypeKey == null) {
return undefined;
}

return this.examples.flatMap((example) => {
return (this.responseBodies ?? []).map((responseBodyNode) => {
const requestBodyShape =
this.requestBodyByContentType?.[requestBodyContentTypeKey];
requestBodyContentTypeKey != null
? this.requestBodyByContentType?.[requestBodyContentTypeKey]
: undefined;
let requestBody:
| FernRegistry.api.latest.ExampleEndpointRequest
| undefined;
Expand Down Expand Up @@ -305,27 +303,35 @@ export class XFernEndpointExampleConverterNode extends BaseOpenApiV3_1ConverterN
}
});

const pathParameters = Object.fromEntries(
Object.entries(example["path-parameters"] ?? {}).map(
([key, value]) => [FernRegistry.PropertyKey(key), value]
)
);
const queryParameters = Object.fromEntries(
Object.entries(example["query-parameters"] ?? {}).map(
([key, value]) => [FernRegistry.PropertyKey(key), value]
)
);
const headers = Object.fromEntries(
Object.entries(example.headers ?? {}).map(([key, value]) => [
FernRegistry.PropertyKey(key),
value,
])
);

return {
path: this.path,
responseStatusCode: this.successResponseStatusCode,
name: example.name,
description: example.docs,
pathParameters: Object.fromEntries(
Object.entries(example["path-parameters"] ?? {}).map(
([key, value]) => [FernRegistry.PropertyKey(key), value]
)
),
queryParameters: Object.fromEntries(
Object.entries(example["query-parameters"] ?? {}).map(
([key, value]) => [FernRegistry.PropertyKey(key), value]
)
),
headers: Object.fromEntries(
Object.entries(example.headers ?? {}).map(([key, value]) => [
FernRegistry.PropertyKey(key),
value,
])
),
pathParameters:
Object.keys(pathParameters).length > 0 ? pathParameters : undefined,
queryParameters:
Object.keys(queryParameters).length > 0
? queryParameters
: undefined,
headers: Object.keys(headers).length > 0 ? headers : undefined,
requestBody,
responseBody,
snippets,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,6 @@ describe("XFernEndpointExampleConverterNode", () => {
responseStatusCode: 200,
name: "Create user",
description: "Example of creating a user",
pathParameters: {},
queryParameters: {},
headers: {},
requestBody: {
type: "json",
value: {
Expand Down Expand Up @@ -136,9 +133,6 @@ describe("XFernEndpointExampleConverterNode", () => {
path: "/upload",
responseStatusCode: 200,
name: "Upload file",
pathParameters: {},
queryParameters: {},
headers: {},
requestBody: {
type: "form",
value: {
Expand Down Expand Up @@ -206,9 +200,6 @@ describe("XFernEndpointExampleConverterNode", () => {
path: "/stream",
responseStatusCode: 200,
name: "Stream events",
pathParameters: {},
queryParameters: {},
headers: {},
requestBody: {
type: "json",
value: {},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,31 +107,33 @@ describe("RedocExampleConverterNode", () => {
const result = node.convert();

expect(result).toEqual({
typescript: [
{
name: "TS Example",
language: "TypeScript",
code: "console.log('hello')",
install: undefined,
generated: false,
description: undefined,
},
{
name: undefined,
language: "TypeScript",
code: "console.log('world')",
install: undefined,
generated: false,
description: undefined,
},
],
snippets: {
typescript: [
{
name: "TS Example",
language: "TypeScript",
code: "console.log('hello')",
install: undefined,
generated: false,
description: undefined,
},
{
name: undefined,
language: "TypeScript",
code: "console.log('world')",
install: undefined,
generated: false,
description: undefined,
},
],
},
});
});

test("returns undefined when no code samples", () => {
const node = createNode({});
const result = node.convert();
expect(result).toEqual({});
expect(result).toEqual(undefined);
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,16 @@ export declare namespace RedocExampleConverterNode {

export class RedocExampleConverterNode extends BaseOpenApiV3_1ConverterNode<
unknown,
Record<string, FernRegistry.api.latest.CodeSnippet[]>
FernRegistry.api.latest.ExampleEndpointCall
> {
codeSamples: RedocExampleConverterNode.RedocCodeSample[] | undefined;

constructor(args: BaseOpenApiV3_1ConverterNodeConstructorArgs<unknown>) {
constructor(
args: BaseOpenApiV3_1ConverterNodeConstructorArgs<unknown>,
protected path: string,
protected responseStatusCode: number,
protected name: string | undefined
) {
super(args);
this.safeParse();
}
Expand Down Expand Up @@ -57,7 +62,7 @@ export class RedocExampleConverterNode extends BaseOpenApiV3_1ConverterNode<
});
}

convert(): Record<string, FernRegistry.api.latest.CodeSnippet[]> | undefined {
convert(): FernRegistry.api.latest.ExampleEndpointCall | undefined {
const convertedCodeSamples: Record<
string,
FernRegistry.api.latest.CodeSnippet[]
Expand All @@ -73,6 +78,20 @@ export class RedocExampleConverterNode extends BaseOpenApiV3_1ConverterNode<
description: undefined,
});
});
return convertedCodeSamples;
if (Object.keys(convertedCodeSamples).length === 0) {
return undefined;
}
return {
path: this.path,
responseStatusCode: this.responseStatusCode,
name: this.name,
description: undefined,
pathParameters: undefined,
queryParameters: undefined,
headers: undefined,
requestBody: undefined,
responseBody: undefined,
snippets: convertedCodeSamples,
};
}
}
Loading
Loading