Skip to content

Commit

Permalink
feat: keep sdk and http examples for syndicate (#1098)
Browse files Browse the repository at this point in the history
  • Loading branch information
trevorblades authored Jul 2, 2024
1 parent 49740e1 commit d336f77
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 13 deletions.
4 changes: 4 additions & 0 deletions packages/ui/app/src/atoms/flags.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ export interface FeatureFlags {
isDarkCodeEnabled: boolean;
proxyShouldUseAppBuildwithfernCom: boolean;
isImageZoomDisabled: boolean;
useJavaScriptAsTypeScript: boolean;
alwaysEnableJavaScriptFetch: boolean;
}

export const DEFAULT_FEATURE_FLAGS: FeatureFlags = {
Expand All @@ -27,6 +29,8 @@ export const DEFAULT_FEATURE_FLAGS: FeatureFlags = {
isDarkCodeEnabled: false,
proxyShouldUseAppBuildwithfernCom: false,
isImageZoomDisabled: false,
useJavaScriptAsTypeScript: false,
alwaysEnableJavaScriptFetch: false,
};

export const FEATURE_FLAGS_ATOM = atom(DEFAULT_FEATURE_FLAGS);
Expand Down
2 changes: 2 additions & 0 deletions packages/ui/app/src/resolver/ApiDefinitionResolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,8 @@ export class ApiDefinitionResolver {
example,
requestBody,
this.featureFlags.isHttpSnippetsEnabled,
this.featureFlags.useJavaScriptAsTypeScript,
this.featureFlags.alwaysEnableJavaScriptFetch,
),
};
}),
Expand Down
34 changes: 21 additions & 13 deletions packages/ui/app/src/resolver/resolveCodeSnippets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ export async function resolveCodeSnippets(
example: APIV1Read.ExampleEndpointCall,
requestBody: ResolvedExampleEndpointRequest | undefined,
isHttpSnippetsEnabled: boolean,
useJavaScriptAsTypeScript: boolean,
alwaysEnableJavaScriptFetch: boolean,
): Promise<ResolvedCodeSnippet[]> {
let toRet: ResolvedCodeSnippet[] = [];

Expand Down Expand Up @@ -55,8 +57,8 @@ export async function resolveCodeSnippets(

if (example.codeExamples.typescriptSdk != null) {
toRet.push({
name: undefined,
language: "typescript",
name: alwaysEnableJavaScriptFetch ? `${useJavaScriptAsTypeScript ? "Java" : "Type"}Script SDK` : undefined,
language: useJavaScriptAsTypeScript ? "javascript" : "typescript",
install: example.codeExamples.typescriptSdk.install,
code: example.codeExamples.typescriptSdk.client,
// hast: highlight(highlighter, code, "typescript"),
Expand Down Expand Up @@ -88,8 +90,12 @@ export async function resolveCodeSnippets(

example.codeSamples.forEach((codeSample) => {
const language = cleanLanguage(codeSample.language);
// Remove any generated code snippets with the same language
toRet = toRet.filter((snippet) => (snippet.generated ? snippet.language !== language : true));

if (!alwaysEnableJavaScriptFetch) {
// Remove any generated code snippets with the same language
toRet = toRet.filter((snippet) => (snippet.generated ? snippet.language !== language : true));
}

toRet.push({
name: codeSample.name,
language,
Expand All @@ -104,21 +110,23 @@ export async function resolveCodeSnippets(
if (isHttpSnippetsEnabled) {
const snippet = new HTTPSnippet(getHarRequest(endpoint, example, requestBody));
for (const { clientId, targetId } of CLIENTS) {
if (toRet.some((snippet) => cleanLanguage(snippet.language) === targetId)) {
continue;
}
if (!alwaysEnableJavaScriptFetch) {
if (toRet.some((snippet) => cleanLanguage(snippet.language) === targetId)) {
continue;
}

if (
targetId === "javascript" &&
toRet.some((snippet) => cleanLanguage(snippet.language) === "typescript")
) {
continue;
if (
targetId === "javascript" &&
toRet.some((snippet) => cleanLanguage(snippet.language) === "typescript")
) {
continue;
}
}

const code = await snippet.convert(targetId, clientId);
if (code != null) {
toRet.push({
name: undefined,
name: alwaysEnableJavaScriptFetch ? "HTTP Request" : undefined,
language: targetId,
install: undefined,
code: typeof code === "string" ? code : code[0],
Expand Down
11 changes: 11 additions & 0 deletions packages/ui/docs-bundle/src/pages/api/fern-docs/feature-flags.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ const FEATURE_FLAGS = [
"dark-code-enabled" as const,
"proxy-uses-app-buildwithfern" as const,
"image-zoom-disabled" as const,
"use-javascript-as-typescript" as const,
"always-enable-javascript-fetch" as const,
];

type FeatureFlag = (typeof FEATURE_FLAGS)[number];
Expand Down Expand Up @@ -46,6 +48,11 @@ export async function getFeatureFlags(domain: string): Promise<FeatureFlags> {
config["proxy-uses-app-buildwithfern"],
);
const isImageZoomDisabled = checkDomainMatchesCustomers(domain, config["image-zoom-disabled"]);
const useJavaScriptAsTypeScript = checkDomainMatchesCustomers(domain, config["use-javascript-as-typescript"]);
const alwaysEnableJavaScriptFetch = checkDomainMatchesCustomers(
domain,
config["always-enable-javascript-fetch"],
);

return {
isApiPlaygroundEnabled: isApiPlaygroundEnabledOverrides(domain) || isApiPlaygroundEnabled,
Expand All @@ -59,6 +66,8 @@ export async function getFeatureFlags(domain: string): Promise<FeatureFlags> {
isDarkCodeEnabled,
proxyShouldUseAppBuildwithfernCom,
isImageZoomDisabled,
useJavaScriptAsTypeScript,
alwaysEnableJavaScriptFetch,
};
} catch (e) {
// eslint-disable-next-line no-console
Expand All @@ -75,6 +84,8 @@ export async function getFeatureFlags(domain: string): Promise<FeatureFlags> {
isDarkCodeEnabled: false,
proxyShouldUseAppBuildwithfernCom: false,
isImageZoomDisabled: false,
useJavaScriptAsTypeScript: false,
alwaysEnableJavaScriptFetch: false,
};
}
}
Expand Down

0 comments on commit d336f77

Please sign in to comment.