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

[Document Translator] Update package metadata #14899

Merged
merged 12 commits into from
Apr 16, 2021
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
Original file line number Diff line number Diff line change
Expand Up @@ -36,41 +36,65 @@ export = {
"ExpressionStatement > ObjectExpression > Property[key.value='name']": (
node: Property
): void => {
const nodeValue = node.value as Literal;
const name = nodeValue.value as string;

if (!name.startsWith("@azure/")) {
const { nodeValue, packageName, subScope } = getPackageMetadata(node);
// Check for a valid scope
if (!/^@azure(-[a-z]+)?\//.test(packageName)) {
context.report({
node: nodeValue,
message: "name is not set to @azure/<service>"
message: "name is not set to @azure[-<subscope>]/<service>"
});
return;
}

const packageBaseName = stripPath(packageName);
const packageDirectory = stripPath(stripFileName(fileName));
const packageBaseName = stripPath(name);
if (!/^@azure\/([a-z]+-)*[a-z]+$/.test(name)) {

if (!/^@azure(-[a-z]+)?\/([a-z]+-)*[a-z]+$/.test(packageName)) {
context.report({
node: nodeValue,
message: "service name is not in kebab-case (lowercase and separated by hyphens)"
});

// Give a good error report if the non-kebab-case name does match the directory and suggest renaming it as well
if (name === `@azure/${packageDirectory}`) {
if (packageName === `@azure/${packageDirectory}`) {
context.report({
node: nodeValue,
message:
"service name matches directory name, but the directory is not kebab case (lowercase and separated by hyphens)"
});
}
} else if (name !== `@azure/${packageDirectory}`) {
} else if (!isValidFolder(packageName, packageDirectory, subScope)) {
const subScopeSuffix = subScope ?? "";
context.report({
node: nodeValue,
message: `service should be named '@azure/${packageDirectory}' or should be moved to a directory called '${packageBaseName}'`
message: `service should be named '@azure${subScopeSuffix}/${packageDirectory}' or should be moved to a directory called '${packageBaseName}${subScopeSuffix}'`
});
}
}
} as Rule.RuleListener)
: {};
}
};

function isValidFolder(packageName: string, folderName: string, subScope?: string): boolean {
if (!subScope) {
return RegExp(`^@azure(-[a-z]+)?\/${folderName}`).test(packageName);
}

// If there is a subScope, allow the folder name to have it appended at the end i.e folder-name-subscope
return RegExp(`^@azure(-[a-z]+)?\/${folderName}`).test(`${packageName}${subScope}`);
}

function getPackageMetadata(
node: Property
): { nodeValue: Literal; packageName: string; subScope: string | undefined } {
const nodeValue = node.value as Literal;
const packageName = nodeValue.value as string;
// Check if there is a sub scope i.e @azure-rest
const [_, subScope] = packageName.match(/^@azure(-[a-z]+)?\//) ?? [];

return {
nodeValue,
packageName,
subScope
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,11 @@ ruleTester.run("ts-package-json-name", rule, {
code: '{"name": "@azure/service-bus"}',
filename: "service-bus/package.json"
},
{
// subscope
code: '{"name": "@azure-rest/service-bus"}',
filename: "service-bus-rest/package.json"
},
{
// a full example package.json (taken from https://github.com/Azure/azure-sdk-for-js/blob/master/sdk/eventhub/event-hubs/package.json with "scripts" removed for testing purposes)
code: examplePackageGood,
Expand Down Expand Up @@ -295,7 +300,7 @@ ruleTester.run("ts-package-json-name", rule, {
filename: "service-bus/package.json",
errors: [
{
message: "name is not set to @azure/<service>"
message: "name is not set to @azure[-<subscope>]/<service>"
}
]
},
Expand Down Expand Up @@ -329,13 +334,24 @@ ruleTester.run("ts-package-json-name", rule, {
}
]
},
{
// not kebab-case
code: '{"name": "@azure-rest/service-bus"}',
filename: "not-service-bus/package.json",
errors: [
{
message:
"service should be named '@azure-rest/not-service-bus' or should be moved to a directory called 'service-bus-rest'"
}
]
},
{
// example file with name set to service-bus
code: examplePackageBad,
filename: "service-bus/package.json",
errors: [
{
message: "name is not set to @azure/<service>"
message: "name is not set to @azure[-<subscope>]/<service>"
}
]
},
Expand Down
2 changes: 2 additions & 0 deletions sdk/core-rest/core-client/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# Release History

## 1.0.0-beta.1 (Unreleased)

- First release of package, see README.md for details.
8 changes: 8 additions & 0 deletions sdk/core-rest/core-client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,14 @@
"./dist-esm/src/url.js": "./dist-esm/src/url.browser.js"
},
"types": "types/src/latest/core-client.d.ts",
"//metadata": {
"constantPaths": [
{
"path": "src/constants.ts",
"prefix": "SDK_VERSION"
}
]
},
"scripts": {
"audit": "node ../../../common/scripts/rush-audit.js && rimraf node_modules package-lock.json && npm i --package-lock-only 2>&1 && npm audit",
"build:browser": "npm run build:ts && cross-env ONLY_BROWSER=true rollup -c 2>&1",
Expand Down
3 changes: 3 additions & 0 deletions sdk/core-rest/core-client/src/clientHelpers.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.

import {
createPipelineFromOptions,
bearerTokenAuthenticationPolicy,
Expand Down
3 changes: 3 additions & 0 deletions sdk/core-rest/core-client/src/common.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.

import { PipelineOptions, RawHttpHeaders, PipelineRequest } from "@azure/core-rest-pipeline";

/**
Expand Down
7 changes: 7 additions & 0 deletions sdk/core-rest/core-client/src/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.

/**
* @internal
*/
export const SDK_VERSION: string = "1.0.0-beta.1";
24 changes: 13 additions & 11 deletions sdk/core-rest/core-client/src/getClient.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.

import { isTokenCredential, KeyCredential, TokenCredential } from "@azure/core-auth";
import { HttpMethods, Pipeline, PipelineOptions } from "@azure/core-rest-pipeline";
import { createDefaultPipeline } from "./clientHelpers";
Expand All @@ -19,6 +22,7 @@ export interface Client {
* This method will be used to send request that would check the path to provide
* strong types
*/
// eslint-disable-next-line @typescript-eslint/ban-types
path: Function;
/**
* This method allows arbitrary paths and doesn't provide strong types
Expand All @@ -40,15 +44,15 @@ export interface Client {

/**
* Creates a client with a default pipeline
* @param baseUrl Base endpoint for the client
* @param options Client options
* @param baseUrl - Base endpoint for the client
* @param options - Client options
*/
export function getClient(baseUrl: string, options?: PipelineOptions): Client;
/**
* Creates a client with a default pipeline
* @param baseUrl Base endpoint for the client
* @param credentials Credentials to authenticate the requests
* @param options Client options
* @param baseUrl - Base endpoint for the client
* @param credentials - Credentials to authenticate the requests
* @param options - Client options
*/
export function getClient(
baseUrl: string,
Expand All @@ -57,18 +61,16 @@ export function getClient(
): Client;
export function getClient(
baseUrl: string,
credentialsOrPipelineOptions?: (TokenCredential | KeyCredential) | PipelineOptions,
options: ClientOptions = {}
credentialsOrPipelineOptions?: (TokenCredential | KeyCredential) | ClientOptions,
clientOptions: ClientOptions = {}
): Client {
let credentials: TokenCredential | KeyCredential | undefined;
let clientOptions = options;

if (credentialsOrPipelineOptions) {
if (isCredential(credentialsOrPipelineOptions)) {
credentials = credentialsOrPipelineOptions;
clientOptions = options;
} else {
clientOptions = credentialsOrPipelineOptions || {};
clientOptions = credentialsOrPipelineOptions ?? {};
}
}

Expand Down Expand Up @@ -116,7 +118,7 @@ function buildSendRequest(
pipeline: Pipeline,
requestOptions: RequestParameters = {},
args: string[] = []
) {
): Promise<HttpResponse> {
// If the client has an api-version and the request doesn't specify one, inject the one in the client options
if (!requestOptions.queryParameters?.["api-version"] && clientOptions.apiVersion) {
if (!requestOptions.queryParameters) {
Expand Down
3 changes: 3 additions & 0 deletions sdk/core-rest/core-client/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.

/**
* Azure Rest Core Client library for JavaScript
* @packageDocumentation
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.

import { KeyCredential } from "@azure/core-auth";
import {
PipelinePolicy,
Expand Down
5 changes: 4 additions & 1 deletion sdk/core-rest/core-client/src/pathClientTypes.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.

import { RawHttpHeaders } from "@azure/core-rest-pipeline";

/**
Expand Down Expand Up @@ -32,7 +35,7 @@ export type RequestParameters = {

/**
* Helper type used to detect parameters in a path template
* keys surounded by {} will be considered a path parameter
* keys surounded by \{\} will be considered a path parameter
*/
export type RouteParams<TRoute extends string> = TRoute extends `{${infer _Param}}/${infer Tail}`
? [pathParam: string, ...pathParams: RouteParams<Tail>]
Expand Down
13 changes: 8 additions & 5 deletions sdk/core-rest/core-client/src/sendRequest.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.

import {
createHttpHeaders,
createPipelineRequest,
Expand All @@ -11,10 +14,10 @@ import { HttpResponse } from "./common";

/**
* Helper function to send request used by the client
* @param method method to use to send the request
* @param url url to send the request to
* @param pipeline pipeline with the policies to run when sending the request
* @param options request options
* @param method - method to use to send the request
* @param url - url to send the request to
* @param pipeline - pipeline with the policies to run when sending the request
* @param options - request options
* @returns returns and HttpResponse
*/
export async function sendRequest(
Expand Down Expand Up @@ -65,7 +68,7 @@ export async function sendRequest(
/**
* Function to determine the content-type of a body
* this is used if an explicit content-type is not provided
* @param body body in the request
* @param body - body in the request
* @returns returns the content-type
*/
function getContentType(body: any): string {
Expand Down
13 changes: 8 additions & 5 deletions sdk/core-rest/core-client/src/urlHelpers.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.

import { RequestParameters } from "./pathClientTypes";
import { URL } from "./url";

/**
* Builds the request url, filling in query and path parameters
* @param baseUrl base url which can be a template url
* @param routePath path to append to the baseUrl
* @param pathParameters values of the path parameters
* @param options request parameters including query parameters
* @param baseUrl - base url which can be a template url
* @param routePath - path to append to the baseUrl
* @param pathParameters - values of the path parameters
* @param options - request parameters including query parameters
* @returns a full url with path and query parameters
*/
export function buildRequestUrl(
Expand All @@ -22,7 +25,7 @@ export function buildRequestUrl(
}

for (const pathParam of pathParameters) {
path = path.replace(/{([^\/]+)}/, pathParam);
path = path.replace(/{([^/]+)}/, pathParam);
}

const url = new URL(`${baseUrl}/${path}`);
Expand Down
3 changes: 3 additions & 0 deletions sdk/core-rest/core-client/test/clientHelpers.spec.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.

import { createDefaultPipeline } from "../src/clientHelpers";
import { assert } from "chai";
import { bearerTokenAuthenticationPolicyName } from "@azure/core-rest-pipeline";
Expand Down
3 changes: 3 additions & 0 deletions sdk/core-rest/core-client/test/sendRequest.spec.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.

import { sendRequest } from "../src/sendRequest";
import { assert } from "chai";
import {
Expand Down
3 changes: 3 additions & 0 deletions sdk/core-rest/core-client/test/urlHelpers.spec.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.

import { buildRequestUrl } from "../src/urlHelpers";
import { assert } from "chai";
describe("urlHelpers", () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"plugins": ["@azure/azure-sdk"],
"extends": ["plugin:@azure/azure-sdk/azure-sdk-base"],
"rules": {
"@azure/azure-sdk/ts-modules-only-named": "warn"
}
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# Release History

## 1.0.0-beta.1 (Unreleased)

- First release of package, see README.md for details.
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@
{
"path": "src/constants.ts",
"prefix": "SDK_VERSION"
},
{
"path": "swagger/README.md",
"prefix": "package-version"
}
]
},
Expand All @@ -44,7 +48,7 @@
"productSlugs": [
"azure",
"azure-cognitive-services",
"azure-document-translator"
"azure-translator"
],
"requiredResources": {
"Azure Cognitive Services instance": "https://docs.microsoft.com/azure/cognitive-services/cognitive-services-apis-create-account"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ languages:
products:
- azure
- azure-cognitive-services
- azure-document-translator
- azure-translator
urlFragment: ai-document-translator-javascript
disableDocsMs: true
---
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ languages:
products:
- azure
- azure-cognitive-services
- azure-document-translator
- azure-translator
urlFragment: ai-document-translator-typescript
disableDocsMs: true
---
Expand Down
Loading