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
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
## 6.0.0-beta.16 (UNRELEASED)
## 6.0.0-beta.16 (2022-03-01)

- [Feature] Bumped @autorest/extension-base version to 3.4.1 and fix breaking changes. [#1253](https://github.com/Azure/autorest.typescript/pull/1253)
- [Feature] Introduced new flag `core-http-compat-mode` and supported generation of SDKs with the new compatability package.

## 6.0.0-beta.15 (2021-11-10)

Expand Down
9 changes: 9 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
"@azure-tools/codegen": "^2.5.294",
"@azure/core-client": "^1.3.0",
"@azure/core-http": "^1.2.4",
"@azure/core-http-compat": "^1.1.0",
"@azure/core-lro": "^2.1.0",
"@azure/core-paging": "^1.2.0",
"@azure/core-rest-pipeline": "^1.3.0",
Expand Down
1 change: 1 addition & 0 deletions src/autorestSession.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ export interface AutorestOptions {
batch?: [string, any][];
multiClient?: boolean;
generateSample?: boolean;
coreHttpCompatMode?: boolean;
}

let host: AutorestExtensionHost;
Expand Down
31 changes: 25 additions & 6 deletions src/generators/clientFileGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ export function generateClient(clientDetails: ClientDetails, project: Project) {
hideClients,
srcPath,
addCredentials,
packageDetails
packageDetails,
coreHttpCompatMode
} = getAutorestOptions();
const hasMappers = !!clientDetails.mappers.length;

Expand Down Expand Up @@ -110,6 +111,10 @@ export function generateClient(clientDetails: ClientDetails, project: Project) {
moduleSpecifier: "@azure/core-auth"
});
}
clientFile.addImportDeclaration({
namespaceImport: "coreHttpCompat",
moduleSpecifier: "@azure/core-http-compat"
});
}

addPagingEsNextRef(flattenedInlineOperations, clientFile);
Expand Down Expand Up @@ -172,7 +177,11 @@ export function generateClient(clientDetails: ClientDetails, project: Project) {

const clientClass = clientFile.addClass({
name: clientDetails.className,
extends: !useCoreV2 ? "coreHttp.ServiceClient" : "coreClient.ServiceClient",
extends: !useCoreV2
? "coreHttp.ServiceClient"
: coreHttpCompatMode
? "coreHttpCompat.ExtendedServiceClient"
: "coreClient.ServiceClient",
isExported: true
});

Expand Down Expand Up @@ -330,7 +339,7 @@ function writeConstructor(
shouldAddBlankLine && writer.blankLine();
}
};

const writeStatements = (lines: string[], shouldAddBlankLine = false) => (
writer: CodeBlockWriter
) => {
Expand Down Expand Up @@ -477,7 +486,12 @@ function getTrack2DefaultContent(addScopes: string, hasCredentials: boolean) {
`;
}

function getTrack1DefaultContent(addScopes: string, defaults: string, packageDetails: PackageDetails, clientDetails: ClientDetails) {
function getTrack1DefaultContent(
addScopes: string,
defaults: string,
packageDetails: PackageDetails,
clientDetails: ClientDetails
) {
return `// Initializing default values for options
if (!options) {
options = {};
Expand Down Expand Up @@ -531,7 +545,12 @@ function writeDefaultOptions(

return !useCoreV2
? getTrack2DefaultContent(addScopes, hasCredentials)
: getTrack1DefaultContent(addScopes, defaults, packageDetails, clientDetails);
: getTrack1DefaultContent(
addScopes,
defaults,
packageDetails,
clientDetails
);
}

function getEndpointStatement({ endpoint }: EndpointDetails) {
Expand Down Expand Up @@ -571,4 +590,4 @@ function writePackageInfo(
`const packageName = "${packageDetails.name || ""}";`,
`const packageVersion = "${packageDetails.version || ""}";`
]);
}
}
26 changes: 18 additions & 8 deletions src/generators/modelsGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,10 @@ export function generateModels(clientDetails: ClientDetails, project: Project) {
namespaceImport: "coreRestPipeline",
moduleSpecifier: "@azure/core-rest-pipeline"
});
modelsIndexFile.addImportDeclaration({
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need to check for compatMode before importing this?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually nope. All unused imports will be removed at the end by the prettier.

namespaceImport: "coreHttpCompat",
moduleSpecifier: "@azure/core-http-compat"
});
}

writeUniontypes(clientDetails, modelsIndexFile);
Expand All @@ -78,18 +82,20 @@ export function generateModels(clientDetails: ClientDetails, project: Project) {
writeClientModels(clientDetails, modelsIndexFile);
modelsIndexFile.fixUnusedIdentifiers();
const allTypes = modelsIndexFile.getTypeAliases();
clientDetails.allTypes = allTypes.filter(item => {
return item.isExported()
}).map(item => {
return item.getName()
});
clientDetails.allTypes = allTypes
.filter(item => {
return item.isExported();
})
.map(item => {
return item.getName();
});
}

const writeClientModels = (
clientDetails: ClientDetails,
modelsIndexFile: SourceFile
) => {
const { useCoreV2 } = getAutorestOptions();
const { useCoreV2, coreHttpCompatMode } = getAutorestOptions();
let clientOptionalParams = clientDetails.parameters.filter(
p =>
(!p.required || p.defaultValue) &&
Expand All @@ -103,6 +109,8 @@ const writeClientModels = (
{
baseClass: !useCoreV2
? "coreHttp.ServiceClientOptions"
: coreHttpCompatMode
? "coreHttpCompat.ExtendedServiceClientOptions"
: "coreClient.ServiceClientOptions"
}
);
Expand Down Expand Up @@ -777,8 +785,10 @@ function getPropertyTypeName(
ignoreNullableOnOptional: boolean
) {
if (property.isConstant) {
if (property.type === SchemaType.Number
|| property.type === SchemaType.Boolean) {
if (
property.type === SchemaType.Number ||
property.type === SchemaType.Boolean
) {
return `${property.defaultValue}`;
}
return `"${getStringForValue(
Expand Down
64 changes: 40 additions & 24 deletions src/generators/static/packageFileGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ function restLevelPackage(packageDetails: PackageDetails) {
const { model } = getSession();
const hasPaging = hasPagingOperations(model);
const hasLRO = hasPollingOperations(model);
const packageInfo: Record<string, any> = {
const packageInfo: Record<string, any> = {
name: `${packageDetails.name}`,
"sdk-type": `${azureArm ? "mgmt" : "client"}`,
version: `${packageDetails.version}`,
Expand Down Expand Up @@ -144,20 +144,29 @@ function restLevelPackage(packageDetails: PackageDetails) {
packageInfo.devDependencies["nyc"] = "^14.0.0";
packageInfo.devDependencies["source-map-support"] = "^0.5.9";

packageInfo.scripts["test"] = "npm run clean && npm run build:test && npm run unit-test";
packageInfo.scripts["test:node"] = "npm run clean && npm run build:test && npm run unit-test:node";
packageInfo.scripts["test:browser"] = "tsc -p . && cross-env ONLY_BROWSER=true rollup -c 2>&1";
packageInfo.scripts["test"] =
"npm run clean && npm run build:test && npm run unit-test";
packageInfo.scripts["test:node"] =
"npm run clean && npm run build:test && npm run unit-test:node";
packageInfo.scripts["test:browser"] =
"tsc -p . && cross-env ONLY_BROWSER=true rollup -c 2>&1";
packageInfo.scripts["build:test"] = "tsc -p . && rollup -c 2>&1";
packageInfo.scripts["unit-test"] = "npm run unit-test:node && npm run unit-test:browser";
packageInfo.scripts["unit-test:node"] = "mocha -r esm --require ts-node/register --reporter ../../../common/tools/mocha-multi-reporter.js --timeout 1200000 --full-trace \"test/{,!(browser)/**/}*.spec.ts\"";
packageInfo.scripts["unit-test"] =
"npm run unit-test:node && npm run unit-test:browser";
packageInfo.scripts["unit-test:node"] =
'mocha -r esm --require ts-node/register --reporter ../../../common/tools/mocha-multi-reporter.js --timeout 1200000 --full-trace "test/{,!(browser)/**/}*.spec.ts"';
packageInfo.scripts["unit-test:browser"] = "karma start --single-run";
packageInfo.scripts["integration-test"] = "npm run integration-test:node && npm run integration-test:browser";
packageInfo.scripts["integration-test:browser"] = "karma start --single-run";
packageInfo.scripts["integration-test:node"] = "nyc mocha -r esm --require source-map-support/register --reporter ../../../common/tools/mocha-multi-reporter.js --timeout 5000000 --full-trace \"dist-esm/test/{,!(browser)/**/}*.spec.js\"";
packageInfo.scripts["integration-test"] =
"npm run integration-test:node && npm run integration-test:browser";
packageInfo.scripts["integration-test:browser"] =
"karma start --single-run";
packageInfo.scripts["integration-test:node"] =
'nyc mocha -r esm --require source-map-support/register --reporter ../../../common/tools/mocha-multi-reporter.js --timeout 5000000 --full-trace "dist-esm/test/{,!(browser)/**/}*.spec.js"';

packageInfo["browser"] = {
"./dist-esm/test/public/utils/env.js": "./dist-esm/test/public/utils/env.browser.js"
}
"./dist-esm/test/public/utils/env.js":
"./dist-esm/test/public/utils/env.browser.js"
};
}

return packageInfo;
Expand All @@ -180,7 +189,8 @@ function regularAutorestPackage(
addCredentials,
azureOutputDirectory,
generateTest,
generateSample
generateSample,
coreHttpCompatMode
} = getAutorestOptions();
const { model } = getSession();
const hasLro = hasPollingOperations(model);
Expand Down Expand Up @@ -211,6 +221,8 @@ function regularAutorestPackage(
...(!useCoreV2 && { "@azure/core-http": "^2.0.0" }),
...(useCoreV2 && { "@azure/core-client": "^1.0.0" }),
...(useCoreV2 && addCredentials && { "@azure/core-auth": "^1.3.0" }),
...(useCoreV2 &&
coreHttpCompatMode && { "@azure/core-http-compat": "^1.0.0" }),
...(useCoreV2 && {
"@azure/core-rest-pipeline": "^1.1.0"
}),
Expand Down Expand Up @@ -295,11 +307,13 @@ function regularAutorestPackage(
docs: "echo skipped"
},
sideEffects: false,
"//metadata":{
constantPaths: [{
path: `src/${normalizeName(clientDetails.name, NameType.File)}.ts`,
prefix: "packageDetails",
}],
"//metadata": {
constantPaths: [
{
path: `src/${normalizeName(clientDetails.name, NameType.File)}.ts`,
prefix: "packageDetails"
}
]
},
autoPublish: true
};
Expand All @@ -319,14 +333,16 @@ function regularAutorestPackage(
packageInfo.scripts["integration-test:node"] =
"mocha -r esm --require ts-node/register --timeout 1200000 --full-trace test/*.ts --reporter ../../../common/tools/mocha-multi-reporter.js";
}
if (generateSample && clientDetails.samples && clientDetails.samples.length > 0) {
if (
generateSample &&
clientDetails.samples &&
clientDetails.samples.length > 0
) {
packageInfo["//sampleConfiguration"] = {
"productName": description,
"productSlugs": [
"azure"
],
"disableDocsMs": true,
"apiRefLink": `https://docs.microsoft.com/javascript/api/${clientPackageName}${apiRefUrlQueryParameter}`
productName: description,
productSlugs: ["azure"],
disableDocsMs: true,
apiRefLink: `https://docs.microsoft.com/javascript/api/${clientPackageName}${apiRefUrlQueryParameter}`
};
}
return packageInfo;
Expand Down
22 changes: 17 additions & 5 deletions src/utils/autorestOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ export async function extractAutorestOptions(): Promise<AutorestOptions> {
const batch = await getBatch(host);
const multiClient = await getMultiClient(host);
const generateSample = await getGenerateSample(host);
const coreHttpCompatMode = await getCoreHttpCompatMode(host);

return {
azureArm,
Expand Down Expand Up @@ -63,7 +64,8 @@ export async function extractAutorestOptions(): Promise<AutorestOptions> {
generateTest,
batch,
multiClient,
generateSample
generateSample,
coreHttpCompatMode
};
}

Expand Down Expand Up @@ -91,7 +93,9 @@ async function getGenerateTest(host: AutorestExtensionHost): Promise<boolean> {
return generateTest === null ? false : Boolean(generateTest);
}

async function getGenerateSample(host: AutorestExtensionHost): Promise<boolean> {
async function getGenerateSample(
host: AutorestExtensionHost
): Promise<boolean> {
const generateSample = await host.getValue("generate-sample");
return generateSample === null ? false : Boolean(generateSample);
}
Expand Down Expand Up @@ -289,12 +293,20 @@ async function getAzureOutputDirectoryPath(
: undefined;
}

async function getBatch(host: AutorestExtensionHost): Promise<[string, any][] | undefined> {
const batch = await host.getValue<[string, any][]>('batch');
async function getBatch(
host: AutorestExtensionHost
): Promise<[string, any][] | undefined> {
const batch = await host.getValue<[string, any][]>("batch");
return batch;
}

async function getMultiClient(host: AutorestExtensionHost): Promise<boolean> {
const multiClient = await host.getValue("multi-client") || undefined;
const multiClient = (await host.getValue("multi-client")) || undefined;
return !!multiClient;
}

async function getCoreHttpCompatMode(
host: AutorestExtensionHost
): Promise<boolean> {
return (await host.getValue("core-http-compat-mode")) || false;
}
18 changes: 18 additions & 0 deletions test/integration/corecampat.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { PetStore as PetStoreClient } from "./generated/corecompattest/src";
import { assert } from "chai";

describe("Integration tests for Core Comapability", () => {
let client: PetStoreClient;

it("should create a client successfully", async () => {
client = new PetStoreClient({
keepAliveOptions: {
enable: true
},
redirectOptions: {
handleRedirects: true
}
});
assert.notEqual(client, null);
});
});
31 changes: 31 additions & 0 deletions test/integration/generated/corecompattest/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# Service client library for JavaScript

This package contains an isomorphic SDK (runs both in Node.js and in browsers) for Service client.

This is a sample server Petstore server. You can find out more about Swagger at <a href="http://swagger.io">http://swagger.io</a> or on irc.freenode.net, #swagger. For this sample, you can use the api key "special-key" to test the authorization filters

[Package (NPM)](https://www.npmjs.com/package/@msinternal/petstore) |
[Samples](https://github.com/Azure-Samples/azure-samples-js-management)

## Getting started

### Currently supported environments

- [LTS versions of Node.js](https://nodejs.org/about/releases/)
- Latest versions of Safari, Chrome, Edge and Firefox.

See our [support policy](https://github.com/Azure/azure-sdk-for-js/blob/main/SUPPORT.md) for more details.





### JavaScript Bundle
To use this client library in the browser, first you need to use a bundler. For details on how to do this, please refer to our [bundling documentation](https://aka.ms/AzureSDKBundling).

## Key concepts

### PetStore

`PetStore` is the primary interface for developers using the Service client library. Explore the methods on this client object to understand the different features of the Service service that you can access.

Loading