Skip to content

Commit

Permalink
(fix, ts): Add environment property to snippets (#3850)
Browse files Browse the repository at this point in the history
  • Loading branch information
amckinney authored Jun 14, 2024
1 parent f7d3a57 commit 5eb4012
Show file tree
Hide file tree
Showing 180 changed files with 1,126 additions and 999 deletions.
7 changes: 7 additions & 0 deletions generators/typescript/sdk/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [0.23.2] - 2024-06-14

- Fix: Client constructor snippets now include an `environment` property whenever it's required.
- Fix: The import paths included in the `README.md` exclusively use double quotes.
- Fix: When an NPM package name is not specified, the generated `README.md` will default to using
the namespace export.

## [0.23.1] - 2024-06-13

- Fix: Undiscriminated unions used as map keys examples no longer return an error.
Expand Down
2 changes: 1 addition & 1 deletion generators/typescript/sdk/VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.23.1
0.23.2
Original file line number Diff line number Diff line change
Expand Up @@ -810,6 +810,23 @@ export class GeneratedSdkClientClassImpl implements GeneratedSdkClientClass {
public getOptionsPropertiesForSnippet(context: SdkContext): ts.ObjectLiteralElementLike[] {
const properties: ts.ObjectLiteralElementLike[] = [];

const defaultEnvironment = context.environments
.getGeneratedEnvironments()
.getReferenceToDefaultEnvironment(context);
if (!this.requireDefaultEnvironment && defaultEnvironment == null) {
const firstEnvironment = context.environments.getReferenceToFirstEnvironmentEnum();
const environment =
firstEnvironment != null
? firstEnvironment.getExpression()
: ts.factory.createStringLiteral("YOUR_BASE_URL");
properties.push(
ts.factory.createPropertyAssignment(
GeneratedSdkClientClassImpl.ENVIRONMENT_OPTION_PROPERTY_NAME,
environment
)
);
}

if (this.oauthAuthScheme != null && context.generateOAuthClients) {
properties.push(
ts.factory.createPropertyAssignment(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ export class SdkContextImpl implements SdkContext {
public readonly npmPackage: NpmPackage | undefined;
public readonly type: TypeContextImpl;
public readonly typeSchema: TypeSchemaContextImpl;
public readonly namespaceExport: string | undefined;
public readonly namespaceExport: string;
public readonly rootClientVariableName: string;
public readonly sdkInstanceReferenceForSnippet: ts.Identifier;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,13 @@ export class EnvironmentsContextImpl implements EnvironmentsContext {
});
}

public getReferenceToFirstEnvironmentEnum(): Reference | undefined {
return this.environmentsDeclarationReferencer.getReferenceToFirstEnvironmentEnum({
importsManager: this.importsManager,
sourceFile: this.sourceFile
});
}

public getReferenceToEnvironmentUrls(): Reference {
return this.environmentsDeclarationReferencer.getReferenceToEnvironmentUrls({
importsManager: this.importsManager,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { EnvironmentsConfig } from "@fern-fern/ir-sdk/api";
import { EnvironmentsConfig, MultipleBaseUrlsEnvironments, SingleBaseUrlEnvironments } from "@fern-fern/ir-sdk/api";
import {
ExportedFilePath,
getReferenceToExportViaNamespaceImport,
Expand Down Expand Up @@ -51,6 +51,17 @@ export class EnvironmentsDeclarationReferencer extends AbstractDeclarationRefere
return `${this.namespaceExport}EnvironmentUrls`;
}

public getExportedNameOfFirstEnvironmentEnum(): string | undefined {
if (this.environmentsConfig == null) {
return undefined;
}
const maybeFirstEnvironmentName = this.getFirstEnvironmentName(this.environmentsConfig);
if (maybeFirstEnvironmentName == null) {
return undefined;
}
return `${this.getExportedNameOfEnvironmentsEnum()}.${maybeFirstEnvironmentName}`;
}

public getReferenceToEnvironmentsEnum({
importsManager,
sourceFile
Expand All @@ -65,6 +76,24 @@ export class EnvironmentsDeclarationReferencer extends AbstractDeclarationRefere
});
}

public getReferenceToFirstEnvironmentEnum({
importsManager,
sourceFile
}: {
importsManager: ImportsManager;
sourceFile: SourceFile;
}): Reference | undefined {
const exportedName = this.getExportedNameOfFirstEnvironmentEnum();
if (exportedName == null) {
return undefined;
}
return this.getReferenceToExport({
importsManager,
sourceFile,
exportedName
});
}

public getReferenceToEnvironmentUrls({
importsManager,
sourceFile
Expand Down Expand Up @@ -97,4 +126,31 @@ export class EnvironmentsDeclarationReferencer extends AbstractDeclarationRefere
referencedIn: sourceFile
});
}

private getFirstEnvironmentName(environmentsConfig: EnvironmentsConfig): string | undefined {
switch (environmentsConfig.environments.type) {
case "singleBaseUrl":
return this.getFirstEnvironmentNameFromSingleEnvironment(environmentsConfig.environments);
case "multipleBaseUrls":
return this.getFirstEnvironmentNameFromMultiEnvironment(environmentsConfig.environments);
}
}

private getFirstEnvironmentNameFromSingleEnvironment(
singleBaseUrlEnvironments: SingleBaseUrlEnvironments
): string | undefined {
for (const environment of singleBaseUrlEnvironments.environments) {
return environment.name.pascalCase.unsafeName;
}
return undefined;
}

private getFirstEnvironmentNameFromMultiEnvironment(
multiBaseUrlsEnvironments: MultipleBaseUrlsEnvironments
): string | undefined {
for (const environment of multiBaseUrlsEnvironments.environments) {
return environment.name.pascalCase.unsafeName;
}
return undefined;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ import { SdkContext } from "@fern-typescript/contexts";
import { camelCase } from "lodash-es";
import { code, Code } from "ts-poet";

interface EndpointWithRequest {
endpoint: HttpEndpoint;
requestWrapper: SdkRequestWrapper;
}

export class ReadmeSnippetBuilder {
private static ABORTING_REQUESTS_FEATURE_ID: FernGeneratorCli.FeatureId = "ABORTING_REQUESTS";
private static EXCEPTION_HANDLING_FEATURE_ID: FernGeneratorCli.FeatureId = "EXCEPTION_HANDLING";
Expand Down Expand Up @@ -86,7 +91,7 @@ export class ReadmeSnippetBuilder {
return exceptionHandlingEndpoints.map((exceptionHandlingEndpoint) =>
this.writeCode(
code`
import { ${this.genericAPISdkErrorName} } from '${this.rootPackageName}';
import { ${this.genericAPISdkErrorName} } from "${this.rootPackageName}";
try {
await ${this.getMethodCall(exceptionHandlingEndpoint)}(...);
Expand Down Expand Up @@ -170,7 +175,7 @@ controller.abort(); // aborts the request
private buildRuntimeCompatibilitySnippets(): string[] {
const snippet = this.writeCode(
code`
import { ${this.rootClientConstructorName} } from '${this.rootPackageName}';
import { ${this.rootClientConstructorName} } from "${this.rootPackageName}";
const ${this.clientVariableName} = new ${this.rootClientConstructorName}({
...
Expand Down Expand Up @@ -275,7 +280,11 @@ const ${this.clientVariableName} = new ${this.rootClientConstructorName}({
}

private getRootPackageName(npmPackage: NpmPackage | undefined): string {
return npmPackage?.packageName ?? "src";
const packageName = npmPackage?.packageName;
if (packageName == null || packageName.length === 0) {
return this.context.namespaceExport;
}
return packageName;
}

private getRootClientConstructorName(context: SdkContext): string {
Expand Down Expand Up @@ -310,8 +319,3 @@ const ${this.clientVariableName} = new ${this.rootClientConstructorName}({
return code.toString({ dprintOptions: { indentWidth: 4 } }).trim() + "\n";
}
}

interface EndpointWithRequest {
endpoint: HttpEndpoint;
requestWrapper: SdkRequestWrapper;
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import { TimeoutSdkErrorContext } from "./timeout-sdk-error";

export interface SdkContext extends ModelContext {
sdkInstanceReferenceForSnippet: ts.Identifier;
namespaceExport: string | undefined;
namespaceExport: string;
endpointErrorUnion: EndpointErrorUnionContext;
environments: EnvironmentsContext;
genericAPISdkError: GenericAPISdkErrorContext;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@ import { GeneratedEnvironments } from "./GeneratedEnvironments";
export interface EnvironmentsContext {
getGeneratedEnvironments: () => GeneratedEnvironments;
getReferenceToEnvironmentsEnum: () => Reference;
getReferenceToFirstEnvironmentEnum: () => Reference | undefined;
getReferenceToEnvironmentUrls: () => Reference;
}
6 changes: 3 additions & 3 deletions seed/ts-sdk/api-wide-base-path/README.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion seed/ts-sdk/api-wide-base-path/snippet-templates.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion seed/ts-sdk/api-wide-base-path/snippet.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions seed/ts-sdk/audiences/README.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions seed/ts-sdk/audiences/snippet-templates.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions seed/ts-sdk/audiences/snippet.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions seed/ts-sdk/auth-environment-variables/README.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions seed/ts-sdk/auth-environment-variables/snippet-templates.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions seed/ts-sdk/auth-environment-variables/snippet.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 5eb4012

Please sign in to comment.