Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

autorest.typescript - codegen file extensions #2919

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
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
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ export function generateClient(clientDetails: ClientDetails, project: Project) {
});
clientFile.addImportDeclaration({
namedImports: ["createLroSpec"],
moduleSpecifier: `./lroImpl`
moduleSpecifier: `./lroImpl.js`
});
}

Expand All @@ -156,7 +156,7 @@ export function generateClient(clientDetails: ClientDetails, project: Project) {
true /* shouldGuard */
)}Impl`
),
moduleSpecifier: "./operations"
moduleSpecifier: "./operations/index.js"
});

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

if (hasInlineOperations && shouldImportParameters(clientDetails)) {
addTracingOperationImports(clientFile, ".");
clientFile.addImportDeclaration({
namespaceImport: "Parameters",
moduleSpecifier: "./models/parameters"
moduleSpecifier: "./models/parameters.js"
});
}

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

Expand All @@ -193,8 +193,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 +241,7 @@ export function generateClient(clientDetails: ClientDetails, project: Project) {
if (importedModels.size) {
clientFile.addImportDeclaration({
namedImports: [...importedModels],
moduleSpecifier: "./models"
moduleSpecifier: "./models/index.js"
});
}

Expand Down Expand Up @@ -476,12 +476,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 +666,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 +766,11 @@ function writeDefaultOptions(
return !useCoreV2
? getTrack1DefaultContent(addScopes, hasCredentials)
: getTrack2DefaultContent(
addScopes,
defaults,
packageDetails,
clientDetails
);
addScopes,
defaults,
packageDetails,
clientDetails
);
}

function isAddScopes(
Expand All @@ -789,15 +787,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
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,17 @@ function generateHLCIndex(clientDetails: ClientDetails, file: SourceFile) {
if (clientDetails.options.hasPaging && !disablePagingAsyncIterators) {
file.addStatements([`/// <reference lib="esnext.asynciterable" />`]);
file.addExportDeclaration({
moduleSpecifier: "./pagingHelper",
moduleSpecifier: "./pagingHelper.js",
namedExports: ["getContinuationToken"]
});
}

file.addExportDeclarations([
{
moduleSpecifier: "./models"
moduleSpecifier: "./models/index.js"
},
{
moduleSpecifier: `./${clientDetails.sourceFileName}`,
moduleSpecifier: `./${clientDetails.sourceFileName}.js`,
namedExports: [clientDetails.className]
}
]);
Expand All @@ -47,7 +47,7 @@ function generateHLCIndex(clientDetails: ClientDetails, file: SourceFile) {
if (operationGroups.length) {
file.addExportDeclarations([
{
moduleSpecifier: "./operationsInterfaces"
moduleSpecifier: "./operationsInterfaces/index.js"
}
]);
}
Expand Down
60 changes: 28 additions & 32 deletions packages/autorest.typescript/src/generators/operationGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ export function generateOperations(
operationIndexFile.addExportDeclarations(
fileNames.map(fileName => {
return {
moduleSpecifier: `./${fileName}`
moduleSpecifier: `./${fileName}.js`
} as ExportDeclarationStructure;
})
);
Expand Down Expand Up @@ -328,8 +328,8 @@ function buildRequestBody({
required = rb.required
? rb.required
: rb.parameter.groupedBy && rb.parameter.groupedBy.required
? rb.parameter.groupedBy.required
: required;
? rb.parameter.groupedBy.required
: required;

if (required) break;
}
Expand Down Expand Up @@ -526,7 +526,7 @@ function addClass(

operationGroupFile.addImportDeclaration({
namedImports,
moduleSpecifier: "../models"
moduleSpecifier: "../models/index.js"
});
}
}
Expand Down Expand Up @@ -750,14 +750,13 @@ function compileOperationOptionsToRequestOptionsBase(
// In Lro we have a couple extra properties to add that's why we use
// the private getOperationOptions function instead of the one in core-http
return isLro
? `this.getOperationOptions(${options}${
lroResourceLocationConfig === undefined
? ""
: `, "${lroResourceLocationConfig}"`
})`
? `this.getOperationOptions(${options}${lroResourceLocationConfig === undefined
? ""
: `, "${lroResourceLocationConfig}"`
})`
: !useCoreV2
? `coreHttp.operationOptionsToRequestOptionsBase(options || {})`
: `options || {}`;
? `coreHttp.operationOptionsToRequestOptionsBase(options || {})`
: `options || {}`;
}

function writeNoOverloadsOperationBody(
Expand Down Expand Up @@ -910,11 +909,11 @@ function writeLroOperationBody(
const sendOperationStatement = !useCoreV2
? `const directSendOperation = async (args: coreHttp.OperationArguments, spec: coreHttp.OperationSpec): Promise<${responseName}> => {
${getTracingClientWithSpanStatement(
sendRequestStatement,
responseName,
isTracingEnabled,
spanName
)}
sendRequestStatement,
responseName,
isTracingEnabled,
spanName
)}
};
const sendOperation = async (args: coreHttp.OperationArguments, spec: coreHttp.OperationSpec) => {
const response = await directSendOperation(args, spec);
Expand Down Expand Up @@ -956,21 +955,18 @@ function writeLroOperationBody(
}};
}`;

const commonOptions = `intervalInMs: options?.updateIntervalInMs${
lroResourceLocationConfig
? `, ${
useLegacyLro ? "lroResourceLocationConfig" : "resourceLocationConfig"
}: "${lroResourceLocationConfig.toLowerCase()}"`
: ""
}`;
const commonOptions = `intervalInMs: options?.updateIntervalInMs${lroResourceLocationConfig
? `, ${useLegacyLro ? "lroResourceLocationConfig" : "resourceLocationConfig"
}: "${lroResourceLocationConfig.toLowerCase()}"`
: ""
}`;
methodDeclaration.addStatements([
sendOperationStatement,
`const lro = createLroSpec({sendOperationFn, args: ${operationParamsName},
spec: ${operationSpecName}})`,
`const poller = ${
useLegacyLro
? `new LroEngine(lro, { resumeFrom: options?.resumeFrom, ${commonOptions} })`
: `await createHttpPoller<${responseName}, OperationState<${responseName}>>(lro, { restoreFrom: options?.resumeFrom, ${commonOptions} })`
`const poller = ${useLegacyLro
? `new LroEngine(lro, { resumeFrom: options?.resumeFrom, ${commonOptions} })`
: `await createHttpPoller<${responseName}, OperationState<${responseName}>>(lro, { restoreFrom: options?.resumeFrom, ${commonOptions} })`
};`,
"await poller.poll();",
"return poller;"
Expand Down Expand Up @@ -1254,7 +1250,7 @@ function addImports(

operationGroupFile.addImportDeclaration({
namedImports: [`${operationGroupInterfaceName}`],
moduleSpecifier: "../operationsInterfaces"
moduleSpecifier: "../operationsInterfaces/index.js"
});

if (!useCoreV2) {
Expand Down Expand Up @@ -1284,14 +1280,14 @@ function addImports(
if (mappers.length) {
operationGroupFile.addImportDeclaration({
namespaceImport: "Mappers",
moduleSpecifier: "../models/mappers"
moduleSpecifier: "../models/mappers.js"
});
}

if (shouldImportParameters(clientDetails)) {
operationGroupFile.addImportDeclaration({
namespaceImport: "Parameters",
moduleSpecifier: "../models/parameters"
moduleSpecifier: "../models/parameters.js"
});
}

Expand All @@ -1301,7 +1297,7 @@ function addImports(

operationGroupFile.addImportDeclaration({
namedImports: [`${clientClassName}`],
moduleSpecifier: `../${clientFileName}`
moduleSpecifier: `../${clientFileName}.js`
});

if (hasLroOperation(operationGroupDetails)) {
Expand All @@ -1311,7 +1307,7 @@ function addImports(
});
operationGroupFile.addImportDeclaration({
namedImports: ["createLroSpec"],
moduleSpecifier: `../lroImpl`
moduleSpecifier: `../lroImpl.js`
});
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ export function generateOperationsInterfaces(
operationIndexFile.addExportDeclarations(
fileNames.map(fileName => {
return {
moduleSpecifier: `./${fileName}`
moduleSpecifier: `./${fileName}.js`
} as ExportDeclarationStructure;
})
);
Expand Down Expand Up @@ -164,7 +164,7 @@ function addInterface(

operationGroupFile.addImportDeclaration({
namedImports,
moduleSpecifier: "../models"
moduleSpecifier: "../models/index.js"
});
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export function generateParameters(
if (importedMappers.length) {
parametersFile.addImportDeclaration({
namedImports: importedMappers,
moduleSpecifier: "../models/mappers"
moduleSpecifier: "../models/mappers.js"
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ export function addPagingImports(
},
{
namedImports: ["setContinuationToken"],
moduleSpecifier: isClient ? "./pagingHelper" : "../pagingHelper"
moduleSpecifier: isClient ? "./pagingHelper.js" : "../pagingHelper.js"
}
]);
}
Expand Down Expand Up @@ -227,9 +227,9 @@ export function writeAsyncIterators(
},
nextMethod: nextMethodParameters
? {
name: `${operation.namePrefix}${nextOperationName}`,
parameters: nextMethodParameters
}
name: `${operation.namePrefix}${nextOperationName}`,
parameters: nextMethodParameters
}
: undefined,
publicMethod: {
name: getPublicMethodName(operation),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
*/

import * as coreClient from "@azure/core-client";
import { PetsImpl } from "./operations";
import { Pets } from "./operationsInterfaces";
import { AdditionalPropertiesClientOptionalParams } from "./models";
import { PetsImpl } from "./operations/index.js";
import { Pets } from "./operationsInterfaces/index.js";
import { AdditionalPropertiesClientOptionalParams } from "./models/index.js";

export class AdditionalPropertiesClient extends coreClient.ServiceClient {
$host: string;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@
* Changes may cause incorrect behavior and will be lost if the code is regenerated.
*/

export * from "./models";
export { AdditionalPropertiesClient } from "./additionalPropertiesClient";
export * from "./operationsInterfaces";
export * from "./models/index.js";
export { AdditionalPropertiesClient } from "./additionalPropertiesClient.js";
export * from "./operationsInterfaces/index.js";
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {
PetAPString as PetAPStringMapper,
PetAPInProperties as PetAPInPropertiesMapper,
PetAPInPropertiesWithAPString as PetAPInPropertiesWithAPStringMapper,
} from "../models/mappers";
} from "../models/mappers.js";

export const contentType: OperationParameter = {
parameterPath: ["options", "contentType"],
Expand Down
Loading
Loading