Files
Mary GaoandGitHub a1c81a4e20 Refactor the AI document intelligence code structure to apply our customization v2 (#35896)
Refactor the AI document intelligence code structure to apply our
customization v2.

/cc @joheredi
2025-09-12 14:57:11 -07:00

74 lines
2.7 KiB
TypeScript
Generated

// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
import type { ClientOptions } from "@azure-rest/core-client";
import { getClient } from "@azure-rest/core-client";
import { logger } from "./logger.js";
import type { TokenCredential, KeyCredential } from "@azure/core-auth";
import type { DocumentIntelligenceClient } from "./clientDefinitions.js";
import { KnownDocumentIntelligenceAudience } from "./audience.js";
/** The optional parameters for the client */
export interface DocumentIntelligenceClientOptions extends ClientOptions {
/** The api version option of the client */
apiVersion?: string;
}
/**
* Initialize a new instance of `DocumentIntelligenceClient`
* @param endpointParam - The Document Intelligence service endpoint.
* @param credentials - uniquely identify client credential
* @param options - the parameter for all optional parameters
*/
export default function createClient(
endpointParam: string,
credentials: TokenCredential | KeyCredential,
{ apiVersion = "2024-11-30", ...options }: DocumentIntelligenceClientOptions = {},
): DocumentIntelligenceClient {
const endpointUrl =
options.endpoint ?? options.baseUrl ?? `${endpointParam}/documentintelligence`;
const userAgentInfo = `azsdk-js-ai-document-intelligence-rest/1.1.0`;
const userAgentPrefix =
options.userAgentOptions && options.userAgentOptions.userAgentPrefix
? `${options.userAgentOptions.userAgentPrefix} ${userAgentInfo}`
: `${userAgentInfo}`;
options = {
...options,
userAgentOptions: {
userAgentPrefix,
},
loggingOptions: {
logger: options.loggingOptions?.logger ?? logger.info,
},
credentials: {
scopes: (
options.credentials?.scopes ?? [KnownDocumentIntelligenceAudience.AzurePublicCloud]
).map((scope) => {
if (scope.endsWith("/.default")) return scope;
return `${scope}/.default`;
}),
apiKeyHeaderName: options.credentials?.apiKeyHeaderName ?? "Ocp-Apim-Subscription-Key",
},
};
const client = getClient(endpointUrl, credentials, options) as DocumentIntelligenceClient;
client.pipeline.removePolicy({ name: "ApiVersionPolicy" });
client.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") && apiVersion) {
req.url = `${req.url}${
Array.from(url.searchParams.keys()).length > 0 ? "&" : "?"
}api-version=${apiVersion}`;
}
return next(req);
},
});
return client;
}