Skip to content
Merged
4 changes: 4 additions & 0 deletions packages/autorest.typescript/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 6.0.32 (Unreleased)

- [Feature] Add a flag to generate ESM compatible source code. Please refer to [#2925](https://github.com/Azure/autorest.typescript/pull/2925)

## 6.0.31 (2024-12-27)

- [Feature] Sdk package methods adoption. Please refer to [#2943](https://github.com/Azure/autorest.typescript/pull/2943)
Expand Down
1 change: 1 addition & 0 deletions packages/autorest.typescript/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ In addition to the [list of Autorest flags](https://github.com/Azure/autorest/bl
| `--tracing-info` | Controls specification of meta info attached to requests for tracing purposes |
| `--disable-async-iterators` | Does not generate async iterators needed for paging operations |
| `--allow-insecure-connection` | Allow generated clients to make requests to HTTP endpoints |
| `--module-kind` | Allows controlling between CommonJS and ESM style imports and exports. Accepted values: `esm`, `cjs`. Defaults to `cjs` for backwards compatibility. ESM style imports and exports will include the file extension and avoid directory imports. |

## Contributing

Expand Down
1 change: 1 addition & 0 deletions packages/autorest.typescript/src/autorestSession.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ export interface AutorestOptions {
lenientModelDeduplication?: boolean;
useLegacyLro?: boolean;
flavor?: PackageFlavor;
moduleKind?: "cjs" | "esm";
Comment thread
maorleger marked this conversation as resolved.
}

let host: AutorestExtensionHost;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import { EndpointDetails } from "../transforms/urlTransforms";
import { PackageDetails } from "../models/packageDetails";
import { getSecurityInfoFromModel } from "../utils/schemaHelpers";
import { createLroImports } from "../utils/lroHelpers";
import { getImportModuleName } from "../utils/nameConstructors";

type OperationDeclarationDetails = { name: string; typeName: string };

Expand All @@ -43,7 +44,8 @@ export function generateClient(clientDetails: ClientDetails, project: Project) {
srcPath,
packageDetails,
coreHttpCompatMode,
useLegacyLro
useLegacyLro,
moduleKind
} = getAutorestOptions();
const { addCredentials } = getSecurityInfoFromModel(clientDetails.security);
const hasMappers = !!clientDetails.mappers.length;
Expand Down Expand Up @@ -142,7 +144,7 @@ export function generateClient(clientDetails: ClientDetails, project: Project) {
});
clientFile.addImportDeclaration({
namedImports: ["createLroSpec"],
moduleSpecifier: `./lroImpl`
moduleSpecifier: getImportModuleName(`./lroImpl`, moduleKind)
Comment thread
maorleger marked this conversation as resolved.
});
}

Expand All @@ -156,7 +158,7 @@ export function generateClient(clientDetails: ClientDetails, project: Project) {
true /* shouldGuard */
)}Impl`
),
moduleSpecifier: "./operations"
moduleSpecifier: getImportModuleName({ cjsName: "./operations", esModulesName: "./operations/index.js" }, moduleKind)
Comment thread
maorleger marked this conversation as resolved.
});

clientFile.addImportDeclaration({
Expand All @@ -168,23 +170,23 @@ export function generateClient(clientDetails: ClientDetails, project: Project) {
true /* shouldGuard */
)}`
),
moduleSpecifier: "./operationsInterfaces"
moduleSpecifier: getImportModuleName({ cjsName: "./operationsInterfaces", esModulesName: "./operationsInterfaces/index.js" }, moduleKind)
});
}

if (hasInlineOperations && shouldImportParameters(clientDetails)) {
addTracingOperationImports(clientFile, ".");
clientFile.addImportDeclaration({
namespaceImport: "Parameters",
moduleSpecifier: "./models/parameters"
moduleSpecifier: getImportModuleName("./models/parameters", moduleKind)
Comment thread
maorleger marked this conversation as resolved.
});
}

// Only import mappers if there are any
if (hasInlineOperations && hasMappers) {
clientFile.addImportDeclaration({
namespaceImport: "Mappers",
moduleSpecifier: "./models/mappers"
moduleSpecifier: getImportModuleName("./models/mappers", moduleKind)
});
}

Expand All @@ -193,8 +195,8 @@ export function generateClient(clientDetails: ClientDetails, project: Project) {
extends: !useCoreV2
? "coreHttp.ServiceClient"
: coreHttpCompatMode
? "coreHttpCompat.ExtendedServiceClient"
: "coreClient.ServiceClient",
? "coreHttpCompat.ExtendedServiceClient"
: "coreClient.ServiceClient",
isExported: true
});

Expand Down Expand Up @@ -241,7 +243,7 @@ export function generateClient(clientDetails: ClientDetails, project: Project) {
if (importedModels.size) {
clientFile.addImportDeclaration({
namedImports: [...importedModels],
moduleSpecifier: "./models"
moduleSpecifier: getImportModuleName({ cjsName: "./models", esModulesName: "./models/index.js" }, moduleKind)
});
}

Expand Down Expand Up @@ -476,12 +478,11 @@ function writeConstructor(
]);
if (useCoreV2 && apiVersionParam) {
clientConstructor.addStatements(
`this.addCustomApiVersionPolicy(${
!apiVersionParam.required ||
`this.addCustomApiVersionPolicy(${!apiVersionParam.required ||
!!apiVersionParam.defaultValue ||
apiVersionParam.schemaType === SchemaType.Constant
? "options."
: ""
? "options."
: ""
}apiVersion);`
);
}
Expand Down Expand Up @@ -667,19 +668,18 @@ function getTrack2DefaultContent(
}
`;

const defaultContent = `${
clientDetails.hasTenantLevelOperation ? overloadDefaults : ""
}
const defaultContent = `${clientDetails.hasTenantLevelOperation ? overloadDefaults : ""
}
// Initializing default values for options
if (!options) {
options = {};
}
${defaults}

const packageDetails = \`azsdk-js-${packageDetails.name.replace(
/@.*\//,
""
)}/${packageDetails.version}\`;
/@.*\//,
""
)}/${packageDetails.version}\`;
const userAgentPrefix =
options.userAgentOptions && options.userAgentOptions.userAgentPrefix
? \`\${options.userAgentOptions.userAgentPrefix} \${packageDetails}\`
Expand Down Expand Up @@ -768,11 +768,11 @@ function writeDefaultOptions(
return !useCoreV2
? getTrack1DefaultContent(addScopes, hasCredentials)
: getTrack2DefaultContent(
addScopes,
defaults,
packageDetails,
clientDetails
);
addScopes,
defaults,
packageDetails,
clientDetails
);
}

function isAddScopes(
Expand All @@ -789,15 +789,13 @@ function isAddScopes(
}

function getEndpointStatement({ endpoint }: EndpointDetails) {
return `this.baseUri = options.endpoint ?? ${
endpoint ? `"${endpoint}"` : `""`
};`;
return `this.baseUri = options.endpoint ?? ${endpoint ? `"${endpoint}"` : `""`
};`;
}

function getEndpoint({ endpoint }: EndpointDetails) {
return `options.endpoint ?? options.baseUri ?? ${
endpoint ? `"${endpoint}"` : `""`
}`;
return `options.endpoint ?? options.baseUri ?? ${endpoint ? `"${endpoint}"` : `""`
}`;
}

function getRequiredParamAssignments(requiredParameters: ParameterDetails[]) {
Expand Down
11 changes: 6 additions & 5 deletions packages/autorest.typescript/src/generators/indexGenerator.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Project, SourceFile } from "ts-morph";
import { ClientDetails } from "../models/clientDetails";
import { getAutorestOptions } from "../autorestSession";
import { getImportModuleName } from "../utils/nameConstructors";
export function generateIndexFile(
project: Project,
clientDetails?: ClientDetails
Expand All @@ -21,21 +22,21 @@ export function generateIndexFile(
}

function generateHLCIndex(clientDetails: ClientDetails, file: SourceFile) {
const { disablePagingAsyncIterators } = getAutorestOptions();
const { disablePagingAsyncIterators, moduleKind } = getAutorestOptions();
if (clientDetails.options.hasPaging && !disablePagingAsyncIterators) {
file.addStatements([`/// <reference lib="esnext.asynciterable" />`]);
file.addExportDeclaration({
moduleSpecifier: "./pagingHelper",
moduleSpecifier: getImportModuleName("./pagingHelper", moduleKind),
namedExports: ["getContinuationToken"]
});
}

file.addExportDeclarations([
{
moduleSpecifier: "./models"
moduleSpecifier: getImportModuleName({ cjsName: "./models", esModulesName: "./models/index.js" }, moduleKind),
},
{
moduleSpecifier: `./${clientDetails.sourceFileName}`,
moduleSpecifier: getImportModuleName(`./${clientDetails.sourceFileName}`, moduleKind),
namedExports: [clientDetails.className]
}
]);
Expand All @@ -47,7 +48,7 @@ function generateHLCIndex(clientDetails: ClientDetails, file: SourceFile) {
if (operationGroups.length) {
file.addExportDeclarations([
{
moduleSpecifier: "./operationsInterfaces"
moduleSpecifier: getImportModuleName({ cjsName: "./operationsInterfaces", esModulesName: "./operationsInterfaces/index.js" }, moduleKind),
}
]);
}
Expand Down
Loading