Skip to content

Commit

Permalink
fix(typescript): choose the correct snippet for readme (#5250)
Browse files Browse the repository at this point in the history
  • Loading branch information
dsinghvi authored Nov 21, 2024
1 parent cf894f0 commit 8d985a5
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 20 deletions.
4 changes: 4 additions & 0 deletions generators/typescript/sdk/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ 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.42.3] - 2024-11-22

- Fix: Fixed issue with snippets used for pagination endpoints.

## [0.42.2] - 2024-11-21

- Improvement: Added documentation for pagination in the README. The snippet below will
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.42.2
0.42.3
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { getTextOfTsNode } from "@fern-typescript/commons";
import { SdkContext } from "@fern-typescript/contexts";
import { code, Code } from "ts-poet";
import { AbstractReadmeSnippetBuilder } from "@fern-api/generator-commons";
import { isNonNullish } from "@fern-api/core-utils";

interface EndpointWithFilepath {
endpoint: HttpEndpoint;
Expand Down Expand Up @@ -84,24 +85,50 @@ export class ReadmeSnippetBuilder extends AbstractReadmeSnippetBuilder {
return snippets;
}

private buildSnippetsForFeature(featureId: FernGeneratorCli.feature.FeatureId): string[] {
const usageEndpointIds = this.getEndpointIdsForFeature(featureId);
if (usageEndpointIds != null) {
return usageEndpointIds.map((endpointId) => this.getSnippetForEndpointId(endpointId));
private getExplicitlyConfiguredSnippets(featureId: FeatureId): string[] | undefined {
const endpointIds = this.getEndpointIdsForFeature(featureId);
if (endpointIds != null) {
return endpointIds.map((endpointId) => this.getSnippetForEndpointId(endpointId)).filter(isNonNullish);
}
return [this.getSnippetForEndpointId(this.defaultEndpointId)];
return undefined;
}

private buildStreamingSnippets(): string[] {
return this.buildSnippetsForFeature(FernGeneratorCli.StructuredFeatureId.Streaming);
const explicitlyConfigured = this.getExplicitlyConfiguredSnippets(
FernGeneratorCli.StructuredFeatureId.Streaming
);
if (explicitlyConfigured != null) {
return explicitlyConfigured;
}
const streamingEndpoint = this.getEndpointWithStreaming();
if (streamingEndpoint != null) {
const snippet = this.getSnippetForEndpointId(streamingEndpoint.endpoint.id);
return snippet != null ? [snippet] : [];
}
return [];
}

private buildPaginationSnippets(): string[] {
return this.buildSnippetsForFeature(FernGeneratorCli.StructuredFeatureId.Pagination);
const explicitlyConfigured = this.getExplicitlyConfiguredSnippets(
FernGeneratorCli.StructuredFeatureId.Pagination
);
if (explicitlyConfigured != null) {
return explicitlyConfigured;
}
const paginationEndpoint = this.getEndpointWithPagination();
if (paginationEndpoint != null) {
const snippet = this.getSnippetForEndpointId(paginationEndpoint.endpoint.id);
return snippet != null ? [snippet] : [];
}
return [];
}

private buildUsageSnippets(): string[] {
return this.buildSnippetsForFeature(FernGeneratorCli.StructuredFeatureId.Usage);
const explicitlyConfigured = this.getExplicitlyConfiguredSnippets(FernGeneratorCli.StructuredFeatureId.Usage);
if (explicitlyConfigured != null) {
return explicitlyConfigured;
}
return [this.getSnippetForEndpointIdOrThrow(this.defaultEndpointId)];
}

private buildExceptionHandlingSnippets(): string[] {
Expand Down Expand Up @@ -275,22 +302,54 @@ const ${this.clientVariableName} = new ${this.rootClientConstructorName}({
return snippets;
}

private getSnippetForEndpointId(endpointId: EndpointId): string {
const snippet = this.snippets[endpointId];
private getSnippetForEndpointIdOrThrow(endpointId: EndpointId): string {
const snippet = this.getSnippetForEndpointId(endpointId);
if (snippet == null) {
throw new Error(`Internal error; missing snippet for endpoint ${endpointId}`);
}
return snippet;
}

private getSnippetForEndpointId(endpointId: EndpointId): string | undefined {
return this.snippets[endpointId];
}

private getEndpointWithPagination(): EndpointWithFilepath | undefined {
return this.filterEndpoint((endpointWithFilepath) => {
if (endpointWithFilepath.endpoint.pagination != null) {
return endpointWithFilepath;
}
return undefined;
});
}

private getEndpointWithStreaming(): EndpointWithFilepath | undefined {
return this.filterEndpoint((endpointWithFilepath) => {
if (endpointWithFilepath.endpoint.response?.body?.type === "streaming") {
return endpointWithFilepath;
}
return undefined;
});
}

private getEndpointWithRequest(): EndpointWithRequest | undefined {
for (const endpointWithFilepath of Object.values(this.endpoints)) {
return this.filterEndpoint((endpointWithFilepath) => {
if (endpointWithFilepath.endpoint.sdkRequest?.shape?.type === "wrapper") {
return {
endpoint: endpointWithFilepath.endpoint,
requestWrapper: endpointWithFilepath.endpoint.sdkRequest.shape
};
}
return undefined;
});
}

private filterEndpoint<T>(transform: (endpoint: EndpointWithFilepath) => T | undefined): T | undefined {
for (const endpointWithFilepath of Object.values(this.endpoints)) {
const result = transform(endpointWithFilepath);
if (result !== undefined) {
return result;
}
}
return undefined;
}
Expand Down
18 changes: 10 additions & 8 deletions seed/ts-sdk/pagination/README.md

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

0 comments on commit 8d985a5

Please sign in to comment.