Skip to content

Commit

Permalink
[dev-tool] use package scope when generating sample apiref link (#32465)
Browse files Browse the repository at this point in the history
Currently the apiref link template has a hardcoded `@azure` scope so it doesn't
work for packages with `@azure-rest` scope. Those packages rely on the
customized `apiRefLink` fields in the sample's configuration section of
package.json. This PR adds package scope variable to the apiref link template as
well so the link generation works for both `@azure` and `@azure-rest` scopes.
These two scopes are the only ones that have SDK API reference pages currently.

With this change, customized `apiRefLink` for -rest packages are also removed.
  • Loading branch information
jeremymeng authored Jan 10, 2025
1 parent 57056dc commit e8acf69
Show file tree
Hide file tree
Showing 10 changed files with 51 additions and 19 deletions.
43 changes: 39 additions & 4 deletions common/tools/dev-tool/src/templates/sampleReadme.md.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ import path from "node:path";
import YAML from "yaml";
import { SampleReadmeConfiguration } from "../util/samples/info";
import { format } from "../util/prettier";
import { createPrinter } from "../util/printer";

const log = createPrinter("readme-template");

/**
* Renders the frontmatter of the sample README.
Expand Down Expand Up @@ -143,7 +146,7 @@ function exampleNodeInvocation(info: SampleReadmeConfiguration) {
}

/**
* Create a link to the package.
* Creates a link to the package.
* @param info - the README configuration
* @returns a link to the project
*/
Expand All @@ -154,16 +157,48 @@ function createReadmeLink(info: SampleReadmeConfiguration) {
return `https://github.com/Azure/azure-sdk-for-js/tree/main/${fragment}/README.md`;
}

/**
* Checks whether a url exists
* @param url -
* @returns true if the url is accessible; false otherwise
*/
async function urlExists(url: string): Promise<boolean> {
const res = await fetch(url);
await res.text();
return res.ok;
}

/**
* Creates link to the SDK package's Api reference page on learn.microsoft.com.
* @param info - the README configuration
* @returns a link to the api reference page.
This methods validates the generated api link first and will return an empty
string if the generated link is not valid.
No validation is done for customized api ref link that is passed in.
*/
async function createApiRef(info: SampleReadmeConfiguration): Promise<string> {
if (info.apiRefLink) {
return `[apiref]: ${info.apiRefLink}`;
} else if (info.scope.startsWith("@azure")) {
const link = `https://learn.microsoft.com/javascript/api/${info.scope}/${info.baseName}${info.isBeta ? "?view=azure-node-preview" : ""}`;
if (await urlExists(link)) {
return `[apiref]: ${link}`;
}
log.warn(`failed to reach api reference ${link} so not adding it.`);
}
return "";
}

/**
* Creates a README for a sample package from a SampleReadmeConfiguration.
*/
export default (info: SampleReadmeConfiguration): Promise<string> => {
export default async (info: SampleReadmeConfiguration): Promise<string> => {
let stepCount = 1;
const step = (content: string) => `${stepCount++}. ${content}`;

const language = info.useTypeScript ? "TypeScript" : "JavaScript";

return format(
return await format(
`${formatFrontmatter(info.frontmatter)}\
# ${info.productName} client library samples for ${language}${info.isBeta ? " (Beta)" : ""}
Expand Down Expand Up @@ -241,7 +276,7 @@ Take a look at our [API Documentation][apiref] for more information about the AP
${info.customSnippets?.footer ?? ""}
${fileLinks(info)}
[apiref]: ${info.apiRefLink ?? `https://learn.microsoft.com/javascript/api/@azure/${info.baseName}`}
${await createApiRef(info)}
[freesub]: https://azure.microsoft.com/free/
${resourceLinks(info)}
[package]: ${createReadmeLink(info)}
Expand Down
4 changes: 2 additions & 2 deletions common/tools/dev-tool/src/util/samples/generation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,7 @@ export async function makeSampleGenerationInfo(

const sampleConfiguration = getSampleConfiguration(projectInfo.packageJson);

const baseName = projectInfo.name.split("/").slice(-1)[0];

const [scope, baseName] = projectInfo.name.split("/");
log.debug("Determined project baseName:", baseName);

// A helper to handle configuration errors.
Expand Down Expand Up @@ -139,6 +138,7 @@ export async function makeSampleGenerationInfo(

return {
...sampleConfiguration,
scope,
baseName,
packageKeywords:
projectInfo.packageJson.keywords ??
Expand Down
4 changes: 4 additions & 0 deletions common/tools/dev-tool/src/util/samples/info.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ export const enum OutputKind {
* Information required for generating sample projects.
*/
export interface SampleGenerationInfo extends SampleConfiguration {
/**
* The scope part of the package name. For example, the base part of "@azure/template" is "@azure".
*/
scope: string;
/**
* The base part of the package name. For example, the base part of "@azure/template" is "template".
*/
Expand Down
3 changes: 1 addition & 2 deletions sdk/anomalydetector/ai-anomaly-detector-rest/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,7 @@
"productSlugs": [
"azure"
],
"disableDocsMs": true,
"apiRefLink": "https://learn.microsoft.com/javascript/api/@azure-rest/ai-anomaly-detector?view=azure-node-preview"
"disableDocsMs": true
},
"type": "module",
"tshy": {
Expand Down
3 changes: 1 addition & 2 deletions sdk/contentsafety/ai-content-safety-rest/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,7 @@
"typescript/src/example-data"
]
},
"disableDocsMs": true,
"apiRefLink": "https://learn.microsoft.com/javascript/api/@azure-rest/ai-content-safety?view=azure-node-preview"
"disableDocsMs": true
},
"type": "module",
"tshy": {
Expand Down
1 change: 0 additions & 1 deletion sdk/maps/maps-route-rest/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@
"requiredResources": {
"Azure Maps Resource": "https://learn.microsoft.com/azure/azure-maps/how-to-create-template"
},
"apiRefLink": "https://learn.microsoft.com/javascript/api/@azure-rest/maps-route",
"disableDocsMs": true
},
"//metadata": {
Expand Down
3 changes: 1 addition & 2 deletions sdk/network/arm-network-rest/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,7 @@
"productSlugs": [
"azure"
],
"disableDocsMs": true,
"apiRefLink": "https://learn.microsoft.com/javascript/api/@azure-rest/arm-network?view=azure-node-preview"
"disableDocsMs": true
},
"type": "module",
"tshy": {
Expand Down
3 changes: 1 addition & 2 deletions sdk/purview/purview-sharing-rest/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,7 @@
"productSlugs": [
"azure"
],
"disableDocsMs": true,
"apiRefLink": "https://learn.microsoft.com/javascript/api/@azure-rest/purview-sharing?view=azure-node-preview"
"disableDocsMs": true
},
"type": "module",
"tshy": {
Expand Down
3 changes: 1 addition & 2 deletions sdk/purview/purview-workflow-rest/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,7 @@
"productSlugs": [
"azure"
],
"disableDocsMs": true,
"apiRefLink": "https://learn.microsoft.com/javascript/api/@azure-rest/purview-workflow?view=azure-node-preview"
"disableDocsMs": true
},
"type": "module",
"tshy": {
Expand Down
3 changes: 1 addition & 2 deletions sdk/servicefabric/arm-servicefabric-rest/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,7 @@
"productSlugs": [
"azure"
],
"disableDocsMs": true,
"apiRefLink": "https://learn.microsoft.com/javascript/api/@azure-rest/arm-servicefabric?view=azure-node-preview"
"disableDocsMs": true
},
"type": "module",
"tshy": {
Expand Down

0 comments on commit e8acf69

Please sign in to comment.