Skip to content

Commit

Permalink
(fix, typescript): sdk code snippets dont render empty dicts for para…
Browse files Browse the repository at this point in the history
…meters with default values (#3074)

* (fix, typescript): sdk code snippets dont render empty dicts for parameters with default values

* update changelog
  • Loading branch information
dsinghvi authored Feb 28, 2024
1 parent 3f888df commit 83e7138
Show file tree
Hide file tree
Showing 21 changed files with 223 additions and 3 deletions.
15 changes: 15 additions & 0 deletions generators/typescript/sdk/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,21 @@ 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.12.2] - 2024-02-27

- Fix: Previously SDK code snippets would not take into account default parameter values
and would always include a `{}`. This was odd and didn't represent how a developer
would use the SDK. Now, the snippets check for default parameter values and omit
if there are no fields specified.

```ts
// Before
client.users.list({})

// After
client.users.list()
```

## [0.12.1] - 2024-02-27

- Fix: Optional objects in deep query parameters were previously being incorrectly
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.12.1
0.12.2
Original file line number Diff line number Diff line change
Expand Up @@ -132,10 +132,17 @@ export class GeneratedDefaultEndpointRequest implements GeneratedEndpointRequest
}
if (this.requestParameter != null) {
const requestParameterExample = this.requestParameter.generateExample({ context, example, opts });
if (requestParameterExample == null) {
if (
requestParameterExample != null &&
getTextOfTsNode(requestParameterExample) === "{}" &&
this.requestParameter.isOptional({ context })
) {
// pass
} else if (requestParameterExample != null) {
result.push(requestParameterExample);
} else if (!this.requestParameter.isOptional({ context })) {
return undefined;
}
result.push(requestParameterExample);
}
return result;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,4 +73,5 @@ export abstract class AbstractRequestParameter implements RequestParameter {
hasQuestionToken: boolean;
initializer?: ts.Expression;
};
public abstract isOptional({ context }: { context: SdkContext }): boolean;
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ export class FileUploadRequestParameter extends AbstractRequestParameter {
return this.getGeneratedRequestWrapper(context).getAllQueryParameters();
}

public isOptional({ context }: { context: SdkContext }): boolean {
return false;
}

public withQueryParameter(
queryParameter: QueryParameter,
context: SdkContext,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ export class RequestBodyParameter extends AbstractRequestParameter {
throw new Error("Cannot reference query parameter because request is not wrapped");
}

public isOptional({ context }: { context: SdkContext }): boolean {
const type = context.type.getReferenceToType(this.requestBodyReference.requestBodyType);
return type.isOptional;
}

public generateExample({
context,
example,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,5 @@ export interface RequestParameter {
example: ExampleEndpointCall;
opts: GetReferenceOpts;
}): ts.Expression | undefined;
isOptional({ context }: { context: SdkContext }): boolean;
}
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,10 @@ export class RequestWrapperParameter extends AbstractRequestParameter {
});
}

public isOptional({ context }: { context: SdkContext }): boolean {
return this.getGeneratedRequestWrapper(context).areAllPropertiesOptional(context);
}

public getReferenceToQueryParameter(queryParameterKey: string, context: SdkContext): ts.Expression {
const queryParameter = this.endpoint.queryParameters.find(
(queryParam) => queryParam.name.wireValue === queryParameterKey
Expand Down
48 changes: 48 additions & 0 deletions seed/ts-sdk/exhaustive/with-audiences/.github/workflows/ci.yml

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

6 changes: 6 additions & 0 deletions seed/ts-sdk/exhaustive/with-audiences/.gitignore

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

8 changes: 8 additions & 0 deletions seed/ts-sdk/exhaustive/with-audiences/.npmignore

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

2 changes: 2 additions & 0 deletions seed/ts-sdk/exhaustive/with-audiences/.prettierrc.yml

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

5 changes: 5 additions & 0 deletions seed/ts-sdk/exhaustive/with-audiences/jest.config.js

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

22 changes: 22 additions & 0 deletions seed/ts-sdk/exhaustive/with-audiences/package.json

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

4 changes: 4 additions & 0 deletions seed/ts-sdk/exhaustive/with-audiences/snippet.json

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

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

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

2 changes: 2 additions & 0 deletions seed/ts-sdk/exhaustive/with-audiences/src/errors/index.ts

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

1 change: 1 addition & 0 deletions seed/ts-sdk/exhaustive/with-audiences/src/index.ts

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

11 changes: 11 additions & 0 deletions seed/ts-sdk/exhaustive/with-audiences/tests/client.test.ts

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

19 changes: 19 additions & 0 deletions seed/ts-sdk/exhaustive/with-audiences/tsconfig.json

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

0 comments on commit 83e7138

Please sign in to comment.