|
| 1 | +# handle with optional api-version parameter via custom VersionParameterTrait |
| 2 | + |
| 3 | +## TypeSpec |
| 4 | + |
| 5 | +```tsp |
| 6 | +import "@typespec/http"; |
| 7 | +import "@typespec/rest"; |
| 8 | +import "@typespec/versioning"; |
| 9 | +import "@azure-tools/typespec-azure-core"; |
| 10 | +
|
| 11 | +using TypeSpec.Http; |
| 12 | +using TypeSpec.Rest; |
| 13 | +using TypeSpec.Versioning; |
| 14 | +using Azure.Core; |
| 15 | +using Azure.Core.Traits; |
| 16 | +
|
| 17 | +@service(#{ |
| 18 | + title: "DataMapClient", |
| 19 | +}) |
| 20 | +@versioned(DataMapService.Versions) |
| 21 | +@server( |
| 22 | + "{endpoint}", |
| 23 | + "DataMap Service", |
| 24 | + { |
| 25 | + @doc("Service endpoint") |
| 26 | + endpoint: url, |
| 27 | + } |
| 28 | +) |
| 29 | +namespace DataMapService; |
| 30 | +
|
| 31 | +enum Versions { |
| 32 | + @doc("API Version 2023-09-01") |
| 33 | + `2023-09-01`, |
| 34 | +} |
| 35 | +
|
| 36 | +@route("/entities") |
| 37 | +@get |
| 38 | +op listEntities( |
| 39 | + @doc("The API version to use for this operation.") |
| 40 | + @query("api-version") |
| 41 | + @minLength(1) |
| 42 | + apiVersion?: string, |
| 43 | +): { |
| 44 | + @statusCode statusCode: 200; |
| 45 | + @body entities: string[]; |
| 46 | +}; |
| 47 | +``` |
| 48 | + |
| 49 | +The config would be like: |
| 50 | + |
| 51 | +```yaml |
| 52 | +withRawContent: true |
| 53 | +ignoreWeirdLine: false |
| 54 | +``` |
| 55 | +
|
| 56 | +## clientContext |
| 57 | +
|
| 58 | +```ts clientContext |
| 59 | +import { logger } from "../logger.js"; |
| 60 | +import { KnownVersions } from "../models/models.js"; |
| 61 | +import { Client, ClientOptions, getClient } from "@azure-rest/core-client"; |
| 62 | + |
| 63 | +export interface DataMapServiceContext extends Client { |
| 64 | + /** The API version to use for this operation. */ |
| 65 | + /** Known values of {@link KnownVersions} that the service accepts. */ |
| 66 | + apiVersion: string; |
| 67 | +} |
| 68 | + |
| 69 | +/** Optional parameters for the client. */ |
| 70 | +export interface DataMapServiceClientOptionalParams extends ClientOptions { |
| 71 | + /** The API version to use for this operation. */ |
| 72 | + /** Known values of {@link KnownVersions} that the service accepts. */ |
| 73 | + apiVersion?: string; |
| 74 | +} |
| 75 | + |
| 76 | +export function createDataMapService( |
| 77 | + endpointParam: string, |
| 78 | + options: DataMapServiceClientOptionalParams = {}, |
| 79 | +): DataMapServiceContext { |
| 80 | + const endpointUrl = options.endpoint ?? String(endpointParam); |
| 81 | + const prefixFromOptions = options?.userAgentOptions?.userAgentPrefix; |
| 82 | + const userAgentPrefix = prefixFromOptions |
| 83 | + ? `${prefixFromOptions} azsdk-js-api` |
| 84 | + : `azsdk-js-api`; |
| 85 | + const { apiVersion: _, ...updatedOptions } = { |
| 86 | + ...options, |
| 87 | + userAgentOptions: { userAgentPrefix }, |
| 88 | + loggingOptions: { logger: options.loggingOptions?.logger ?? logger.info }, |
| 89 | + }; |
| 90 | + const clientContext = getClient(endpointUrl, undefined, updatedOptions); |
| 91 | + clientContext.pipeline.removePolicy({ name: "ApiVersionPolicy" }); |
| 92 | + const apiVersion = options.apiVersion ?? "2023-09-01"; |
| 93 | + clientContext.pipeline.addPolicy({ |
| 94 | + name: "ClientApiVersionPolicy", |
| 95 | + sendRequest: (req, next) => { |
| 96 | + // Use the apiVersion defined in request url directly |
| 97 | + // Append one if there is no apiVersion and we have one at client options |
| 98 | + const url = new URL(req.url); |
| 99 | + if (!url.searchParams.get("api-version")) { |
| 100 | + req.url = `${req.url}${ |
| 101 | + Array.from(url.searchParams.keys()).length > 0 ? "&" : "?" |
| 102 | + }api-version=${apiVersion}`; |
| 103 | + } |
| 104 | + |
| 105 | + return next(req); |
| 106 | + }, |
| 107 | + }); |
| 108 | + return { ...clientContext, apiVersion } as DataMapServiceContext; |
| 109 | +} |
| 110 | +``` |
0 commit comments