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

improvement: add example id to snippet and to search #904

Merged
merged 9 commits into from
May 23, 2024
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
14 changes: 9 additions & 5 deletions fern/apis/fdr/definition/snippets-factory.yml
Original file line number Diff line number Diff line change
Expand Up @@ -69,30 +69,34 @@ types:
snippets: list<SingleJavaSnippetCreate>

# Single creates
BaseSnippetCreate:
properties:
endpoint: commons.EndpointIdentifier
exampleIdentifier: optional<string>

SingleTypescriptSnippetCreate:
extends: BaseSnippetCreate
properties:
endpoint: commons.EndpointIdentifier
snippet: TypeScriptSnippetCode

SinglePythonSnippetCreate:
extends: BaseSnippetCreate
properties:
endpoint: commons.EndpointIdentifier
snippet: PythonSnippetCode

SingleGoSnippetCreate:
extends: BaseSnippetCreate
properties:
endpoint: commons.EndpointIdentifier
snippet: GoSnippetCode

SingleRubySnippetCreate:
extends: BaseSnippetCreate
properties:
endpoint: commons.EndpointIdentifier
snippet: RubySnippetCode

SingleJavaSnippetCreate:
extends: BaseSnippetCreate
properties:
endpoint: commons.EndpointIdentifier
snippet: JavaSnippetCode

# Snippet Code
Expand Down
5 changes: 5 additions & 0 deletions fern/apis/fdr/definition/snippets.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ service:
The SDKs for which to load snippets. If unspecified,
snippets for the latest published SDKs will be returned.
endpoint: commons.EndpointIdentifier
exampleIdentifier:
type: optional<string>
docs: The identifier of the example to fetch the snippet for, this is ignored if a payload is passed in.
# TODO: Nice to have, allow client variable name to be configurable
# clientName:
# type: optional<string>
Expand Down Expand Up @@ -311,6 +314,8 @@ types:
type: map<commons.EndpointMethod, list<Snippet>>

Snippet:
base-properties:
exampleIdentifier: optional<string>
union:
typescript: TypeScriptSnippet
python: PythonSnippet
Expand Down
2 changes: 1 addition & 1 deletion packages/fdr-sdk/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,6 @@
"format": "prettier --write --ignore-unknown --ignore-path ../../shared/.prettierignore \"**\"",
"format:check": "prettier --check --ignore-unknown --ignore-path ../../shared/.prettierignore \"**\"",
"lint": "eslint --max-warnings 0 src --ext .ts --resolve-plugins-relative-to ../../",
"test": "vitest --passWithNoTests"
"test": "vitest"
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { SDKSnippetHolder } from "@fern-api/fdr-sdk";
import { SDKSnippetHolder } from "../converters";

describe("SDK Snippet Holder", () => {
it("Test SDK Snippet Retrieval", () => {
Expand Down Expand Up @@ -45,8 +45,10 @@ describe("SDK Snippet Holder", () => {
const snippet = sdkSnippetHolder.getPythonCodeSnippetForEndpoint({
endpointMethod: "GET",
endpointPath: "/users/v1",
identifierOverride: "endpoint_connectors.list",
endpointId: "endpoint_connectors.list",
exampleId: undefined,
});
console.log(snippet);
expect(snippet?.async_client).toEqual("client = AsyncAcme(api_key='YOUR_API_KEY')");
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ export interface GetSnippetRequest {
*/
sdks?: FernRegistry.SdkRequest[];
endpoint: FernRegistry.EndpointIdentifier;
/** The identifier of the example to fetch the snippet for, this is ignored if a payload is passed in. */
exampleIdentifier?: string;
/**
* The JSON payload to be used as the input for the code snippet. This should just be thought of as the
* request body you'd be sending to the endpoint as a cURL. If not specified then the default payload will be used.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,27 @@ export type Snippet =
| FernRegistry.Snippet.Ruby;

export declare namespace Snippet {
interface Typescript extends FernRegistry.TypeScriptSnippet {
interface Typescript extends FernRegistry.TypeScriptSnippet, _Base {
type: "typescript";
}

interface Python extends FernRegistry.PythonSnippet {
interface Python extends FernRegistry.PythonSnippet, _Base {
type: "python";
}

interface Java extends FernRegistry.JavaSnippet {
interface Java extends FernRegistry.JavaSnippet, _Base {
type: "java";
}

interface Go extends FernRegistry.GoSnippet {
interface Go extends FernRegistry.GoSnippet, _Base {
type: "go";
}

interface Ruby extends FernRegistry.RubySnippet {
interface Ruby extends FernRegistry.RubySnippet, _Base {
type: "ruby";
}

interface _Base {
exampleIdentifier?: string;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/**
* This file was auto-generated by Fern from our API Definition.
*/

import * as FernRegistry from "../../..";

export interface BaseSnippetCreate {
endpoint: FernRegistry.EndpointIdentifier;
exampleIdentifier?: string;
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

import * as FernRegistry from "../../..";

export interface SingleGoSnippetCreate {
endpoint: FernRegistry.EndpointIdentifier;
export interface SingleGoSnippetCreate extends FernRegistry.BaseSnippetCreate {
snippet: FernRegistry.GoSnippetCode;
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

import * as FernRegistry from "../../..";

export interface SingleJavaSnippetCreate {
endpoint: FernRegistry.EndpointIdentifier;
export interface SingleJavaSnippetCreate extends FernRegistry.BaseSnippetCreate {
snippet: FernRegistry.JavaSnippetCode;
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

import * as FernRegistry from "../../..";

export interface SinglePythonSnippetCreate {
endpoint: FernRegistry.EndpointIdentifier;
export interface SinglePythonSnippetCreate extends FernRegistry.BaseSnippetCreate {
snippet: FernRegistry.PythonSnippetCode;
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

import * as FernRegistry from "../../..";

export interface SingleRubySnippetCreate {
endpoint: FernRegistry.EndpointIdentifier;
export interface SingleRubySnippetCreate extends FernRegistry.BaseSnippetCreate {
snippet: FernRegistry.RubySnippetCode;
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

import * as FernRegistry from "../../..";

export interface SingleTypescriptSnippetCreate {
endpoint: FernRegistry.EndpointIdentifier;
export interface SingleTypescriptSnippetCreate extends FernRegistry.BaseSnippetCreate {
snippet: FernRegistry.TypeScriptSnippetCode;
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ export * from "./PythonSdkSnippetCreate";
export * from "./GoSdkSnippetsCreate";
export * from "./RubySdkSnippetsCreate";
export * from "./JavaSdkSnippetsCreate";
export * from "./BaseSnippetCreate";
export * from "./SingleTypescriptSnippetCreate";
export * from "./SinglePythonSnippetCreate";
export * from "./SingleGoSnippetCreate";
Expand Down
17 changes: 12 additions & 5 deletions packages/fdr-sdk/src/converters/db/convertAPIDefinitionToDb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ function transformEndpoint({
snippetTemplates: snippets.getSnippetTemplateForEndpoint({
endpointPath: getEndpointPathAsString(writeShape),
endpointMethod: writeShape.method,
identifierOverride: writeShape.originalEndpointId,
endpointId: writeShape.originalEndpointId,
}),
};
}
Expand Down Expand Up @@ -473,6 +473,7 @@ export function transformExampleEndpointCall({
codeExamples: transformCodeExamples({
endpointDefinition,
snippets,
exampleId: writeShape.name,
}),
requestBodyV3:
writeShape.requestBodyV3 ??
Expand All @@ -495,31 +496,37 @@ export function transformExampleEndpointCall({
}

function transformCodeExamples({
exampleId,
endpointDefinition,
snippets,
}: {
endpointDefinition: APIV1Write.EndpointDefinition;
snippets: SDKSnippetHolder;
exampleId: string | undefined;
}): FdrAPI.api.v1.read.CodeExamples {
const maybePythonSnippet = snippets.getPythonCodeSnippetForEndpoint({
endpointMethod: endpointDefinition.method,
endpointPath: getEndpointPathAsString(endpointDefinition),
identifierOverride: endpointDefinition.originalEndpointId,
endpointId: endpointDefinition.originalEndpointId,
exampleId,
});
const maybeTypescriptSnippet = snippets.getTypeScriptCodeSnippetForEndpoint({
endpointMethod: endpointDefinition.method,
endpointPath: getEndpointPathAsString(endpointDefinition),
identifierOverride: endpointDefinition.originalEndpointId,
endpointId: endpointDefinition.originalEndpointId,
exampleId,
});
const maybeGoSnippet = snippets.getGoCodeSnippetForEndpoint({
endpointMethod: endpointDefinition.method,
endpointPath: getEndpointPathAsString(endpointDefinition),
identifierOverride: endpointDefinition.originalEndpointId,
endpointId: endpointDefinition.originalEndpointId,
exampleId,
});
const maybeRubySnippet = snippets.getRubyCodeSnippetForEndpoint({
endpointMethod: endpointDefinition.method,
endpointPath: getEndpointPathAsString(endpointDefinition),
identifierOverride: endpointDefinition.originalEndpointId,
endpointId: endpointDefinition.originalEndpointId,
exampleId,
});
return {
nodeAxios: "",
Expand Down
Loading
Loading