Skip to content

Commit

Permalink
feat(typescript): Support retainOriginalName/noSerdeLayer in inline p…
Browse files Browse the repository at this point in the history
…ath parameters (#5427)

Support retainOriginalName/noSerdeLayer in inline path parameters in TS SDK
  • Loading branch information
Swimburger authored Dec 17, 2024
1 parent 00476af commit 2ae45d4
Show file tree
Hide file tree
Showing 554 changed files with 39,843 additions and 698 deletions.
14 changes: 11 additions & 3 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,21 @@
"**/.yarn": true,
},
"eslint.nodePath": ".yarn/sdks",
"eslint.execArgv": ["--max_old_space_size=16384"],
"eslint.workingDirectories": [ "./packages", "./generators" ],
"eslint.execArgv": [
"--max_old_space_size=16384"
],
"eslint.workingDirectories": [
"./packages",
"./generators"
],
"prettier.prettierPath": "node_modules/prettier/index.js",
"typescript.tsdk": "node_modules/typescript/lib",
"typescript.enablePromptUseWorkspaceTsdk": true,
"editor.formatOnSave": true,
"editor.codeActionsOnSave": ["source.fixAll", "source.sortMembers"],
"editor.codeActionsOnSave": [
"source.fixAll",
"source.sortMembers"
],
"editor.defaultFormatter": null,
"[typescript]": {
"editor.defaultFormatter": "esbenp.prettier-vscode"
Expand Down
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.44.2] - 2024-12-17

- Fix: Inline path parameters will use their original name when `retainOriginalName` or `noSerdeLayer` is enabled.

## [0.44.1] - 2024-12-16

- Fix: When there is an environment variable set, you do not need to pass in any parameters
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.44.1
0.44.2
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
VariableId
} from "@fern-fern/ir-sdk/api";
import {
getParameterNameForRootPathParameter,
getTextOfTsNode,
ImportsManager,
JavaScriptRuntime,
Expand All @@ -31,7 +32,6 @@ import {
MethodDeclarationStructure,
ModuleDeclarationStructure,
OptionalKind,
PropertyDeclarationStructure,
PropertySignatureStructure,
Scope,
StructureKind,
Expand All @@ -47,7 +47,6 @@ import { GeneratedDefaultEndpointImplementation } from "./endpoints/default/Gene
import { GeneratedFileDownloadEndpointImplementation } from "./endpoints/GeneratedFileDownloadEndpointImplementation";
import { GeneratedStreamingEndpointImplementation } from "./endpoints/GeneratedStreamingEndpointImplementation";
import { getNonVariablePathParameters } from "./endpoints/utils/getNonVariablePathParameters";
import { getParameterNameForPathParameter } from "./endpoints/utils/getParameterNameForPathParameter";
import { getLiteralValueForHeader, isLiteralHeader } from "./endpoints/utils/isLiteralHeader";
import { REQUEST_OPTIONS_PARAMETER_NAME } from "./endpoints/utils/requestOptionsParameter";
import { GeneratedHeader } from "./GeneratedHeader";
Expand Down Expand Up @@ -1190,7 +1189,7 @@ export class GeneratedSdkClientClassImpl implements GeneratedSdkClientClass {

for (const pathParameter of getNonVariablePathParameters(this.intermediateRepresentation.pathParameters)) {
properties.push({
name: getParameterNameForPathParameter({
name: getParameterNameForRootPathParameter({
pathParameter,
retainOriginalCasing: this.retainOriginalCasing
}),
Expand Down Expand Up @@ -1895,7 +1894,7 @@ export class GeneratedSdkClientClassImpl implements GeneratedSdkClientClass {

public getReferenceToRootPathParameter(pathParameter: PathParameter): ts.Expression {
return this.getReferenceToOption(
getParameterNameForPathParameter({
getParameterNameForRootPathParameter({
pathParameter,
retainOriginalCasing: this.retainOriginalCasing
})
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import {
ExampleEndpointCall,
FileProperty,
HttpEndpoint,
HttpRequestBody,
HttpService,
IntermediateRepresentation
} from "@fern-fern/ir-sdk/api";
import {
Fetcher,
getParameterNameForPositionalPathParameter,
GetReferenceOpts,
getTextOfTsNode,
ImportsManager,
Expand All @@ -22,7 +22,6 @@ import { generateHeaders } from "../endpoints/utils/generateHeaders";
import { GeneratedSdkClientClassImpl } from "../GeneratedSdkClientClassImpl";
import { FileUploadRequestParameter } from "../request-parameter/FileUploadRequestParameter";
import { GeneratedEndpointRequest } from "./GeneratedEndpointRequest";
import { getParameterNameForPathParameter } from "../endpoints/utils/getParameterNameForPathParameter";
import { getPathParametersForEndpointSignature } from "../endpoints/utils/getPathParametersForEndpointSignature";

export declare namespace GeneratedBytesEndpointRequest {
Expand Down Expand Up @@ -152,7 +151,7 @@ export class GeneratedBytesEndpointRequest implements GeneratedEndpointRequest {
context
})) {
parameters.push({
name: getParameterNameForPathParameter({
name: getParameterNameForPositionalPathParameter({
pathParameter,
retainOriginalCasing: this.retainOriginalCasing
}),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,17 @@ import {
SdkRequest,
SdkRequestShape
} from "@fern-fern/ir-sdk/api";
import { Fetcher, GetReferenceOpts, getTextOfTsNode, PackageId } from "@fern-typescript/commons";
import {
Fetcher,
getParameterNameForPositionalPathParameter,
GetReferenceOpts,
getTextOfTsNode,
PackageId
} from "@fern-typescript/commons";
import { SdkContext } from "@fern-typescript/contexts";
import { OptionalKind, ParameterDeclarationStructure, ts } from "ts-morph";
import { GeneratedQueryParams } from "../endpoints/utils/GeneratedQueryParams";
import { generateHeaders } from "../endpoints/utils/generateHeaders";
import { getParameterNameForPathParameter } from "../endpoints/utils/getParameterNameForPathParameter";
import { getPathParametersForEndpointSignature } from "../endpoints/utils/getPathParametersForEndpointSignature";
import { GeneratedSdkClientClassImpl } from "../GeneratedSdkClientClassImpl";
import { RequestBodyParameter } from "../request-parameter/RequestBodyParameter";
Expand Down Expand Up @@ -109,7 +114,7 @@ export class GeneratedDefaultEndpointRequest implements GeneratedEndpointRequest
context
})) {
parameters.push({
name: getParameterNameForPathParameter({
name: getParameterNameForPositionalPathParameter({
pathParameter,
retainOriginalCasing: this.retainOriginalCasing
}),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
} from "@fern-fern/ir-sdk/api";
import {
Fetcher,
getParameterNameForPositionalPathParameter,
GetReferenceOpts,
getTextOfTsNode,
ImportsManager,
Expand All @@ -21,7 +22,6 @@ import { appendPropertyToFormData } from "../endpoints/utils/appendPropertyToFor
import { GeneratedQueryParams } from "../endpoints/utils/GeneratedQueryParams";
import { generateHeaders } from "../endpoints/utils/generateHeaders";
import { getParameterNameForFile } from "../endpoints/utils/getParameterNameForFile";
import { getParameterNameForPathParameter } from "../endpoints/utils/getParameterNameForPathParameter";
import { getPathParametersForEndpointSignature } from "../endpoints/utils/getPathParametersForEndpointSignature";
import { GeneratedSdkClientClassImpl } from "../GeneratedSdkClientClassImpl";
import { FileUploadRequestParameter } from "../request-parameter/FileUploadRequestParameter";
Expand Down Expand Up @@ -188,7 +188,7 @@ export class GeneratedFileUploadEndpointRequest implements GeneratedEndpointRequ
context
})) {
parameters.push({
name: getParameterNameForPathParameter({
name: getParameterNameForPositionalPathParameter({
pathParameter,
retainOriginalCasing: this.retainOriginalCasing
}),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ export class GeneratedFileDownloadEndpointImplementation implements GeneratedEnd
retainOriginalCasing: this.retainOriginalCasing,
omitUndefined: this.omitUndefined
});

if (url != null) {
return context.externalDependencies.urlJoin.invoke([referenceToEnvironment, url]);
} else {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import { assertNever } from "@fern-api/core-utils";
import { HttpEndpoint, PathParameter, PathParameterLocation } from "@fern-fern/ir-sdk/api";
import {
getParameterNameForPositionalPathParameter,
getParameterNameForPropertyPathParameter
} from "@fern-typescript/commons";
import { SdkContext } from "@fern-typescript/contexts";
import { ts } from "ts-morph";
import { GeneratedSdkClientClassImpl } from "../../GeneratedSdkClientClassImpl";
import { getParameterNameForPathParameter } from "./getParameterNameForPathParameter";

export function buildUrl({
endpoint,
Expand Down Expand Up @@ -41,6 +44,7 @@ export function buildUrl({
pathParameter,
generatedClientClass,
retainOriginalCasing,
includeSerdeLayer,
requestVariableName: endpoint.sdkRequest?.requestParameterName.camelCase.safeName ?? "request",
shouldInlinePathParameters: context.requestWrapper.shouldInlinePathParameters(endpoint.sdkRequest)
});
Expand Down Expand Up @@ -75,12 +79,14 @@ export function buildUrl({
function getReferenceToPathParameter({
pathParameter,
generatedClientClass,
includeSerdeLayer,
retainOriginalCasing,
requestVariableName,
shouldInlinePathParameters
}: {
pathParameter: PathParameter;
generatedClientClass: GeneratedSdkClientClassImpl;
includeSerdeLayer: boolean;
retainOriginalCasing: boolean;
requestVariableName: string;
shouldInlinePathParameters: boolean;
Expand All @@ -91,15 +97,20 @@ function getReferenceToPathParameter({
switch (pathParameter.location) {
case PathParameterLocation.Service:
case PathParameterLocation.Endpoint: {
const pathParamName = getParameterNameForPathParameter({
pathParameter,
retainOriginalCasing
});
if (shouldInlinePathParameters) {
const pathParamName = getParameterNameForPropertyPathParameter({
pathParameter,
retainOriginalCasing,
includeSerdeLayer
});
return ts.factory.createIdentifier(`${requestVariableName}.${pathParamName}`);
} else {
const pathParamName = getParameterNameForPositionalPathParameter({
pathParameter,
retainOriginalCasing
});
return ts.factory.createIdentifier(pathParamName);
}

return ts.factory.createIdentifier(pathParamName);
}
case PathParameterLocation.Root:
return generatedClientClass.getReferenceToRootPathParameter(pathParameter);
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ import {
import {
generateInlinePropertiesModule,
getExampleEndpointCalls,
getParameterNameForPropertyPathParameter,
getParameterNameForPropertyPathParameterName,
getTextOfTsNode,
maybeAddDocsNode,
PackageId,
Expand Down Expand Up @@ -481,7 +483,11 @@ export class GeneratedRequestWrapperImpl implements GeneratedRequestWrapper {
public getPropertyNameOfPathParameterFromName(name: Name): RequestWrapperNonBodyProperty {
return {
safeName: name.camelCase.safeName,
propertyName: name.camelCase.unsafeName
propertyName: getParameterNameForPropertyPathParameterName({
includeSerdeLayer: this.includeSerdeLayer,
retainOriginalCasing: this.retainOriginalCasing,
pathParameterName: name
})
};
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
import { Name, PathParameter } from "@fern-fern/ir-sdk/api";

/**
* Determines the casing of the path parameter when used as a positional function parameter
*/
export function getParameterNameForPositionalPathParameter({
pathParameter,
retainOriginalCasing
}: {
pathParameter: PathParameter;
retainOriginalCasing: boolean;
}): string {
return getParameterNameForPathParameterInternalName({
pathParameterName: pathParameter.name,
retainOriginalCasing
});
}

/**
* Determines the casing of the root path parameter which is put into the client class options class
*/
export function getParameterNameForRootPathParameter({
pathParameter,
retainOriginalCasing
}: {
pathParameter: PathParameter;
retainOriginalCasing: boolean;
}): string {
if (pathParameter.location !== "ROOT") {
throw new Error("pathParameter.location must be ROOT");
}
return getParameterNameForPathParameterInternalName({
pathParameterName: pathParameter.name,
retainOriginalCasing
});
}

/**
* Determines the casing of the path parameter when used as a parameter inside an object/interface
*/
export function getParameterNameForPropertyPathParameter({
pathParameter,
retainOriginalCasing,
includeSerdeLayer
}: {
pathParameter: PathParameter;
retainOriginalCasing: boolean;
includeSerdeLayer: boolean;
}): string {
return getParameterNameForPropertyPathParameterName({
pathParameterName: pathParameter.name,
retainOriginalCasing,
includeSerdeLayer
});
}

/**
* Determines the casing of the path parameter when used as a parameter inside an object/interface
*/
export function getParameterNameForPropertyPathParameterName({
pathParameterName,
retainOriginalCasing,
includeSerdeLayer
}: {
pathParameterName: Name;
retainOriginalCasing: boolean;
includeSerdeLayer: boolean;
}): string {
return getParameterNameForPathParameterInternalName({
pathParameterName,
retainOriginalCasing: retainOriginalCasing || !includeSerdeLayer
});
}

function getParameterNameForPathParameterInternalName({
pathParameterName,
retainOriginalCasing
}: {
pathParameterName: Name;
retainOriginalCasing: boolean;
}): string {
if (retainOriginalCasing) {
return pathParameterName.originalName;
}
return pathParameterName.camelCase.safeName;
}
6 changes: 6 additions & 0 deletions generators/typescript/utils/commons/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ export { getTextOfTsNode } from "./codegen-utils/getTextOfTsNode";
export { maybeAddDocsNode, maybeAddDocsStructure } from "./codegen-utils/maybeAddDocs";
export { writerToString } from "./codegen-utils/writerToString";
export { generateInlineAliasModule, generateInlinePropertiesModule } from "./codegen-utils/generateInlineModule";
export {
getParameterNameForPositionalPathParameter,
getParameterNameForPropertyPathParameter,
getParameterNameForPropertyPathParameterName,
getParameterNameForRootPathParameter
} from "./codegen-utils/getParameterNameForPathParameter";
export * from "./core-utilities";
export { type Zurg } from "./core-utilities/zurg/Zurg";
export { DependencyManager, DependencyType, type PackageDependencies } from "./dependency-manager/DependencyManager";
Expand Down
Loading

0 comments on commit 2ae45d4

Please sign in to comment.