From d336f7718f451f9c23f5b13272994e23cf03a475 Mon Sep 17 00:00:00 2001 From: Trevor Blades Date: Tue, 2 Jul 2024 12:30:52 -0700 Subject: [PATCH] feat: keep sdk and http examples for syndicate (#1098) --- packages/ui/app/src/atoms/flags.ts | 4 +++ .../app/src/resolver/ApiDefinitionResolver.ts | 2 ++ .../app/src/resolver/resolveCodeSnippets.ts | 34 ++++++++++++------- .../src/pages/api/fern-docs/feature-flags.ts | 11 ++++++ 4 files changed, 38 insertions(+), 13 deletions(-) diff --git a/packages/ui/app/src/atoms/flags.ts b/packages/ui/app/src/atoms/flags.ts index fbfb78e4a8..fc07687a5f 100644 --- a/packages/ui/app/src/atoms/flags.ts +++ b/packages/ui/app/src/atoms/flags.ts @@ -13,6 +13,8 @@ export interface FeatureFlags { isDarkCodeEnabled: boolean; proxyShouldUseAppBuildwithfernCom: boolean; isImageZoomDisabled: boolean; + useJavaScriptAsTypeScript: boolean; + alwaysEnableJavaScriptFetch: boolean; } export const DEFAULT_FEATURE_FLAGS: FeatureFlags = { @@ -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); diff --git a/packages/ui/app/src/resolver/ApiDefinitionResolver.ts b/packages/ui/app/src/resolver/ApiDefinitionResolver.ts index 08b4d99ecd..2c98e680c9 100644 --- a/packages/ui/app/src/resolver/ApiDefinitionResolver.ts +++ b/packages/ui/app/src/resolver/ApiDefinitionResolver.ts @@ -354,6 +354,8 @@ export class ApiDefinitionResolver { example, requestBody, this.featureFlags.isHttpSnippetsEnabled, + this.featureFlags.useJavaScriptAsTypeScript, + this.featureFlags.alwaysEnableJavaScriptFetch, ), }; }), diff --git a/packages/ui/app/src/resolver/resolveCodeSnippets.ts b/packages/ui/app/src/resolver/resolveCodeSnippets.ts index 6f5c875ce4..7811f58163 100644 --- a/packages/ui/app/src/resolver/resolveCodeSnippets.ts +++ b/packages/ui/app/src/resolver/resolveCodeSnippets.ts @@ -27,6 +27,8 @@ export async function resolveCodeSnippets( example: APIV1Read.ExampleEndpointCall, requestBody: ResolvedExampleEndpointRequest | undefined, isHttpSnippetsEnabled: boolean, + useJavaScriptAsTypeScript: boolean, + alwaysEnableJavaScriptFetch: boolean, ): Promise { let toRet: ResolvedCodeSnippet[] = []; @@ -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"), @@ -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, @@ -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], diff --git a/packages/ui/docs-bundle/src/pages/api/fern-docs/feature-flags.ts b/packages/ui/docs-bundle/src/pages/api/fern-docs/feature-flags.ts index c56b7437ea..7bff8f53da 100644 --- a/packages/ui/docs-bundle/src/pages/api/fern-docs/feature-flags.ts +++ b/packages/ui/docs-bundle/src/pages/api/fern-docs/feature-flags.ts @@ -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]; @@ -46,6 +48,11 @@ export async function getFeatureFlags(domain: string): Promise { 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, @@ -59,6 +66,8 @@ export async function getFeatureFlags(domain: string): Promise { isDarkCodeEnabled, proxyShouldUseAppBuildwithfernCom, isImageZoomDisabled, + useJavaScriptAsTypeScript, + alwaysEnableJavaScriptFetch, }; } catch (e) { // eslint-disable-next-line no-console @@ -75,6 +84,8 @@ export async function getFeatureFlags(domain: string): Promise { isDarkCodeEnabled: false, proxyShouldUseAppBuildwithfernCom: false, isImageZoomDisabled: false, + useJavaScriptAsTypeScript: false, + alwaysEnableJavaScriptFetch: false, }; } }