Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions packages/typespec-ts/src/modular/helpers/clientHelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,14 +73,15 @@ export function getClientParameters(
const hasDefaultValue = (p: SdkParameter) =>
p.clientDefaultValue || p.__raw?.defaultValue || p.type.kind === "constant";
const isRequired = (p: SdkParameter) =>
!p.optional &&
((!hasDefaultValue(p) &&
// Special case: when apiVersionAsRequired is true, apiVersion should always be considered required
(options.apiVersionAsRequired && p.isApiVersionParam) ||
(!p.optional &&
!hasDefaultValue(p) &&
!(
p.type.kind === "endpoint" &&
p.type.templateArguments[0] &&
hasDefaultValue(p.type.templateArguments[0])
)) ||
(options.apiVersionAsRequired && p.isApiVersionParam));
));
const isOptional = (p: SdkParameter) => p.optional || hasDefaultValue(p);
const skipCredentials = (p: SdkParameter) => p.kind !== "credential";
const skipMethodParam = (p: SdkParameter) => p.kind !== "method";
Expand All @@ -98,7 +99,6 @@ export function getClientParameters(
const params = clientParams.filter((p) =>
filters.every((filter) => !filter || filter(p))
);

return params;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
# handle with optional api-version parameter via custom VersionParameterTrait

## TypeSpec

```tsp
import "@typespec/http";
import "@typespec/rest";
import "@typespec/versioning";
import "@azure-tools/typespec-azure-core";

using TypeSpec.Http;
using TypeSpec.Rest;
using TypeSpec.Versioning;
using Azure.Core;
using Azure.Core.Traits;

@service(#{
title: "DataMapClient",
})
@versioned(DataMapService.Versions)
@server(
"{endpoint}",
"DataMap Service",
{
@doc("Service endpoint")
endpoint: url,
}
)
namespace DataMapService;

enum Versions {
@doc("API Version 2023-09-01")
`2023-09-01`,
}

@route("/entities")
@get
op listEntities(
@doc("The API version to use for this operation.")
@query("api-version")
@minLength(1)
apiVersion?: string,
): {
@statusCode statusCode: 200;
@body entities: string[];
};
```

The config would be like:

```yaml
withRawContent: true
ignoreWeirdLine: false
```

## clientContext

```ts clientContext
import { logger } from "../logger.js";
import { KnownVersions } from "../models/models.js";
import { Client, ClientOptions, getClient } from "@azure-rest/core-client";

export interface DataMapServiceContext extends Client {
/** The API version to use for this operation. */
/** Known values of {@link KnownVersions} that the service accepts. */
apiVersion: string;
}

/** Optional parameters for the client. */
export interface DataMapServiceClientOptionalParams extends ClientOptions {
/** The API version to use for this operation. */
/** Known values of {@link KnownVersions} that the service accepts. */
apiVersion?: string;
}

export function createDataMapService(
endpointParam: string,
options: DataMapServiceClientOptionalParams = {},
): DataMapServiceContext {
const endpointUrl = options.endpoint ?? String(endpointParam);
const prefixFromOptions = options?.userAgentOptions?.userAgentPrefix;
const userAgentPrefix = prefixFromOptions
? `${prefixFromOptions} azsdk-js-api`
: `azsdk-js-api`;
const { apiVersion: _, ...updatedOptions } = {
...options,
userAgentOptions: { userAgentPrefix },
loggingOptions: { logger: options.loggingOptions?.logger ?? logger.info },
};
const clientContext = getClient(endpointUrl, undefined, updatedOptions);
clientContext.pipeline.removePolicy({ name: "ApiVersionPolicy" });
const apiVersion = options.apiVersion ?? "2023-09-01";
clientContext.pipeline.addPolicy({
name: "ClientApiVersionPolicy",
sendRequest: (req, next) => {
// Use the apiVersion defined in request url directly
// Append one if there is no apiVersion and we have one at client options
const url = new URL(req.url);
if (!url.searchParams.get("api-version")) {
req.url = `${req.url}${
Array.from(url.searchParams.keys()).length > 0 ? "&" : "?"
}api-version=${apiVersion}`;
}

return next(req);
},
});
return { ...clientContext, apiVersion } as DataMapServiceContext;
}
```
Loading