Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@azure-tools/rlc-common

Package Overview
Dependencies
Maintainers
0
Versions
392
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@azure-tools/rlc-common - npm Package Compare versions

Comparing version 0.33.0-alpha.20240917.1 to 0.33.0-alpha.20240918.1

8

.rush/temp/package-deps_build.json

@@ -43,3 +43,3 @@ {

"packages/rlc-common/src/metadata/buildLicenseFile.ts": "93aede17cfd25fd7793180cab3a4398af90edc9e",
"packages/rlc-common/src/metadata/buildPackageFile.ts": "643863a38cfd773ff7476b101b2c330485ef73e4",
"packages/rlc-common/src/metadata/buildPackageFile.ts": "a09636228174ab49652ca592f8db89ddcaeea0ed",
"packages/rlc-common/src/metadata/buildReadmeFile.ts": "8366c61e781cd4589b1765708d538ecad9dacc32",

@@ -49,3 +49,3 @@ "packages/rlc-common/src/metadata/buildRollupConfig.ts": "6d05e8cdab182f7529b725cdc70d7adfdeb108f1",

"packages/rlc-common/src/metadata/buildVitestConfig.ts": "0e8fed4600c6727da03767e38adab03753e65840",
"packages/rlc-common/src/metadata/packageJson/azurePackageCommon.ts": "b3195294ecfa5a67a3a32c6e2127c624bd3594c1",
"packages/rlc-common/src/metadata/packageJson/azurePackageCommon.ts": "d0d7d2bc1d754e0c31108d6d349a0298b7d6ba6d",
"packages/rlc-common/src/metadata/packageJson/buildAzureMonorepoPackage.ts": "9db097e6dfbae5d7f47ac69985d100ec557f3443",

@@ -55,3 +55,3 @@ "packages/rlc-common/src/metadata/packageJson/buildAzureStandalonePackage.ts": "0c1842ed2dd176b813c6455a0f7e1ba24e117ed3",

"packages/rlc-common/src/metadata/packageJson/packageCommon.ts": "e6acbfb431579f89232752dbf5e610314600ed5e",
"packages/rlc-common/src/static/paginateContent.ts": "db871d05c9ef53a1d0b498f2a24e8a2349e2c44b",
"packages/rlc-common/src/static/paginateContent.ts": "7662e30b3d45a8c812a20ea9e56bd57846b50ddc",
"packages/rlc-common/src/static/pollingContent.ts": "e258455ebf140e9e11bf567771ac373de9ec8f76",

@@ -69,3 +69,3 @@ "packages/rlc-common/src/static/sampleTemplate.ts": "e27b992af09b5c6b5f7e30a16af60eb3f90f42b0",

"packages/rlc-common/test/integration/mockHelper.ts": "5c7f57aaaea34fb206bd37571491fb56ac80613b",
"packages/rlc-common/test/integration/packageJson.spec.ts": "b4e0976cbc3f884acfabb7b42ca8fcf29ff5e4c0",
"packages/rlc-common/test/integration/packageJson.spec.ts": "5d67e3f6b3249679048f0423ff772a7b9f65b0fe",
"packages/rlc-common/test/integration/static/package.json": "09d24ae57d9e1d9ff74e057c0caa90a22ffe147a",

@@ -72,0 +72,0 @@ "packages/rlc-common/test/integration/vitestConfig.spec.ts": "ec157c5cdbcd50f8e3864c069c9447ab85034e1e",

// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
import { NameType, normalizeName } from "../helpers/nameUtils.js";
import { hasPagingOperations, hasPollingOperations } from "../helpers/operationHelpers.js";
import { hasPollingOperations } from "../helpers/operationHelpers.js";
import { isAzureMonorepoPackage, isAzurePackage, isAzureStandalonePackage } from "../helpers/packageUtil.js";

@@ -29,3 +29,2 @@ import { Project } from "ts-morph";

hasLro: hasPollingOperations(model),
hasPaging: hasPagingOperations(model),
monorepoPackageDirectory: model.options?.azureOutputDirectory,

@@ -58,4 +57,4 @@ specSource: model.options?.sourceFrom ?? "TypeSpec",

export function updatePackageFile(model, existingFilePathOrContent) {
const hasPaging = hasPagingOperations(model), hasLro = hasPollingOperations(model);
if (!isAzurePackage(model) || (!hasPaging && !hasLro)) {
const hasLro = hasPollingOperations(model);
if (!isAzurePackage(model) || !hasLro) {
return;

@@ -79,9 +78,2 @@ }

}
// no need to update paging dependency for modular libraries
if (hasPaging && model.options?.isModularLibrary !== true) {
packageInfo.dependencies = {
...packageInfo.dependencies,
"@azure/core-paging": "^1.5.0"
};
}
if (hasLro) {

@@ -88,0 +80,0 @@ packageInfo.dependencies = {

@@ -19,3 +19,3 @@ // Copyright (c) Microsoft Corporation.

*/
export function getAzurePackageDependencies({ hasLro, hasPaging, specSource, dependencies, isModularLibrary }) {
export function getAzurePackageDependencies({ hasLro, specSource, dependencies }) {
let azureDependencies = {

@@ -36,8 +36,2 @@ ...dependencies,

}
if (hasPaging && isModularLibrary !== true) {
azureDependencies = {
...azureDependencies,
"@azure/core-paging": "^1.5.0"
};
}
return azureDependencies;

@@ -44,0 +38,0 @@ }

@@ -5,7 +5,2 @@ // Copyright (c) Microsoft Corporation.

import {
getPagedAsyncIterator,
PagedAsyncIterableIterator,
PagedResult
} from "@azure/core-paging";
import {
Client,

@@ -17,2 +12,143 @@ createRestError,

/**
* returns an async iterator that iterates over results. It also has a \`byPage\`
* method that returns pages of items at once.
*
* @param pagedResult - an object that specifies how to get pages.
* @returns a paged async iterator that iterates over results.
*/
function getPagedAsyncIterator<
TElement,
TPage = TElement[],
TPageSettings = PageSettings,
TLink = string,
>(
pagedResult: PagedResult<TPage, TPageSettings, TLink>,
): PagedAsyncIterableIterator<TElement, TPage, TPageSettings> {
const iter = getItemAsyncIterator<TElement, TPage, TLink, TPageSettings>(pagedResult);
return {
next() {
return iter.next();
},
[Symbol.asyncIterator]() {
return this;
},
byPage:
pagedResult?.byPage ??
(((settings?: PageSettings) => {
const { continuationToken } = settings ?? {};
return getPageAsyncIterator(pagedResult, {
pageLink: continuationToken as unknown as TLink | undefined,
});
}) as unknown as (settings?: TPageSettings) => AsyncIterableIterator<TPage>),
};
}
async function* getItemAsyncIterator<TElement, TPage, TLink, TPageSettings>(
pagedResult: PagedResult<TPage, TPageSettings, TLink>,
): AsyncIterableIterator<TElement> {
const pages = getPageAsyncIterator(pagedResult);
const firstVal = await pages.next();
// if the result does not have an array shape, i.e. TPage = TElement, then we return it as is
if (!Array.isArray(firstVal.value)) {
// can extract elements from this page
const { toElements } = pagedResult;
if (toElements) {
yield* toElements(firstVal.value) as TElement[];
for await (const page of pages) {
yield* toElements(page) as TElement[];
}
} else {
yield firstVal.value;
// \`pages\` is of type \`AsyncIterableIterator<TPage>\` but TPage = TElement in this case
yield* pages as unknown as AsyncIterableIterator<TElement>;
}
} else {
yield* firstVal.value;
for await (const page of pages) {
// pages is of type \`AsyncIterableIterator<TPage>\` so \`page\` is of type \`TPage\`. In this branch,
// it must be the case that \`TPage = TElement[]\`
yield* page as unknown as TElement[];
}
}
}
async function* getPageAsyncIterator<TPage, TLink, TPageSettings>(
pagedResult: PagedResult<TPage, TPageSettings, TLink>,
options: {
pageLink?: TLink;
} = {},
): AsyncIterableIterator<TPage> {
const { pageLink } = options;
let response = await pagedResult.getPage(pageLink ?? pagedResult.firstPageLink);
if (!response) {
return;
}
yield response.page;
while (response.nextPageLink) {
response = await pagedResult.getPage(response.nextPageLink);
if (!response) {
return;
}
yield response.page;
}
}
/**
* An interface that tracks the settings for paged iteration
*/
export interface PageSettings {
/**
* The token that keeps track of where to continue the iterator
*/
continuationToken?: string;
}
/**
* An interface that allows async iterable iteration both to completion and by page.
*/
export interface PagedAsyncIterableIterator<
TElement,
TPage = TElement[],
TPageSettings = PageSettings,
> {
/**
* The next method, part of the iteration protocol
*/
next(): Promise<IteratorResult<TElement>>;
/**
* The connection to the async iterator, part of the iteration protocol
*/
[Symbol.asyncIterator](): PagedAsyncIterableIterator<TElement, TPage, TPageSettings>;
/**
* Return an AsyncIterableIterator that works a page at a time
*/
byPage: (settings?: TPageSettings) => AsyncIterableIterator<TPage>;
}
/**
* An interface that describes how to communicate with the service.
*/
interface PagedResult<TPage, TPageSettings = PageSettings, TLink = string> {
/**
* Link to the first page of results.
*/
firstPageLink: TLink;
/**
* A method that returns a page of results.
*/
getPage: (
pageLink: TLink,
) => Promise<{ page: TPage; nextPageLink?: TLink } | undefined>;
/**
* a function to implement the \`byPage\` method on the paged async iterator.
*/
byPage?: (settings?: TPageSettings) => AsyncIterableIterator<TPage>;
/**
* A function to extract elements from a page.
*/
toElements?: (page: TPage) => unknown[];
}
/**
* Helper type to extract the type of an array

@@ -27,3 +163,2 @@ */

pageLink: string,
maxPageSize?: number
) => Promise<{

@@ -30,0 +165,0 @@ page: TPage;

@@ -34,3 +34,2 @@ "use strict";

hasLro: (0, operationHelpers_js_1.hasPollingOperations)(model),
hasPaging: (0, operationHelpers_js_1.hasPagingOperations)(model),
monorepoPackageDirectory: (_k = model.options) === null || _k === void 0 ? void 0 : _k.azureOutputDirectory,

@@ -63,5 +62,4 @@ specSource: (_m = (_l = model.options) === null || _l === void 0 ? void 0 : _l.sourceFrom) !== null && _m !== void 0 ? _m : "TypeSpec",

function updatePackageFile(model, existingFilePathOrContent) {
var _a;
const hasPaging = (0, operationHelpers_js_1.hasPagingOperations)(model), hasLro = (0, operationHelpers_js_1.hasPollingOperations)(model);
if (!(0, packageUtil_js_1.isAzurePackage)(model) || (!hasPaging && !hasLro)) {
const hasLro = (0, operationHelpers_js_1.hasPollingOperations)(model);
if (!(0, packageUtil_js_1.isAzurePackage)(model) || !hasLro) {
return;

@@ -85,9 +83,2 @@ }

}
// no need to update paging dependency for modular libraries
if (hasPaging && ((_a = model.options) === null || _a === void 0 ? void 0 : _a.isModularLibrary) !== true) {
packageInfo.dependencies = {
...packageInfo.dependencies,
"@azure/core-paging": "^1.5.0"
};
}
if (hasLro) {

@@ -94,0 +85,0 @@ packageInfo.dependencies = {

@@ -24,3 +24,3 @@ "use strict";

*/
function getAzurePackageDependencies({ hasLro, hasPaging, specSource, dependencies, isModularLibrary }) {
function getAzurePackageDependencies({ hasLro, specSource, dependencies }) {
let azureDependencies = {

@@ -41,8 +41,2 @@ ...dependencies,

}
if (hasPaging && isModularLibrary !== true) {
azureDependencies = {
...azureDependencies,
"@azure/core-paging": "^1.5.0"
};
}
return azureDependencies;

@@ -49,0 +43,0 @@ }

@@ -8,7 +8,2 @@ "use strict";

import {
getPagedAsyncIterator,
PagedAsyncIterableIterator,
PagedResult
} from "@azure/core-paging";
import {
Client,

@@ -20,2 +15,143 @@ createRestError,

/**
* returns an async iterator that iterates over results. It also has a \`byPage\`
* method that returns pages of items at once.
*
* @param pagedResult - an object that specifies how to get pages.
* @returns a paged async iterator that iterates over results.
*/
function getPagedAsyncIterator<
TElement,
TPage = TElement[],
TPageSettings = PageSettings,
TLink = string,
>(
pagedResult: PagedResult<TPage, TPageSettings, TLink>,
): PagedAsyncIterableIterator<TElement, TPage, TPageSettings> {
const iter = getItemAsyncIterator<TElement, TPage, TLink, TPageSettings>(pagedResult);
return {
next() {
return iter.next();
},
[Symbol.asyncIterator]() {
return this;
},
byPage:
pagedResult?.byPage ??
(((settings?: PageSettings) => {
const { continuationToken } = settings ?? {};
return getPageAsyncIterator(pagedResult, {
pageLink: continuationToken as unknown as TLink | undefined,
});
}) as unknown as (settings?: TPageSettings) => AsyncIterableIterator<TPage>),
};
}
async function* getItemAsyncIterator<TElement, TPage, TLink, TPageSettings>(
pagedResult: PagedResult<TPage, TPageSettings, TLink>,
): AsyncIterableIterator<TElement> {
const pages = getPageAsyncIterator(pagedResult);
const firstVal = await pages.next();
// if the result does not have an array shape, i.e. TPage = TElement, then we return it as is
if (!Array.isArray(firstVal.value)) {
// can extract elements from this page
const { toElements } = pagedResult;
if (toElements) {
yield* toElements(firstVal.value) as TElement[];
for await (const page of pages) {
yield* toElements(page) as TElement[];
}
} else {
yield firstVal.value;
// \`pages\` is of type \`AsyncIterableIterator<TPage>\` but TPage = TElement in this case
yield* pages as unknown as AsyncIterableIterator<TElement>;
}
} else {
yield* firstVal.value;
for await (const page of pages) {
// pages is of type \`AsyncIterableIterator<TPage>\` so \`page\` is of type \`TPage\`. In this branch,
// it must be the case that \`TPage = TElement[]\`
yield* page as unknown as TElement[];
}
}
}
async function* getPageAsyncIterator<TPage, TLink, TPageSettings>(
pagedResult: PagedResult<TPage, TPageSettings, TLink>,
options: {
pageLink?: TLink;
} = {},
): AsyncIterableIterator<TPage> {
const { pageLink } = options;
let response = await pagedResult.getPage(pageLink ?? pagedResult.firstPageLink);
if (!response) {
return;
}
yield response.page;
while (response.nextPageLink) {
response = await pagedResult.getPage(response.nextPageLink);
if (!response) {
return;
}
yield response.page;
}
}
/**
* An interface that tracks the settings for paged iteration
*/
export interface PageSettings {
/**
* The token that keeps track of where to continue the iterator
*/
continuationToken?: string;
}
/**
* An interface that allows async iterable iteration both to completion and by page.
*/
export interface PagedAsyncIterableIterator<
TElement,
TPage = TElement[],
TPageSettings = PageSettings,
> {
/**
* The next method, part of the iteration protocol
*/
next(): Promise<IteratorResult<TElement>>;
/**
* The connection to the async iterator, part of the iteration protocol
*/
[Symbol.asyncIterator](): PagedAsyncIterableIterator<TElement, TPage, TPageSettings>;
/**
* Return an AsyncIterableIterator that works a page at a time
*/
byPage: (settings?: TPageSettings) => AsyncIterableIterator<TPage>;
}
/**
* An interface that describes how to communicate with the service.
*/
interface PagedResult<TPage, TPageSettings = PageSettings, TLink = string> {
/**
* Link to the first page of results.
*/
firstPageLink: TLink;
/**
* A method that returns a page of results.
*/
getPage: (
pageLink: TLink,
) => Promise<{ page: TPage; nextPageLink?: TLink } | undefined>;
/**
* a function to implement the \`byPage\` method on the paged async iterator.
*/
byPage?: (settings?: TPageSettings) => AsyncIterableIterator<TPage>;
/**
* A function to extract elements from a page.
*/
toElements?: (page: TPage) => unknown[];
}
/**
* Helper type to extract the type of an array

@@ -30,3 +166,2 @@ */

pageLink: string,
maxPageSize?: number
) => Promise<{

@@ -33,0 +168,0 @@ page: TPage;

{
"name": "@azure-tools/rlc-common",
"version": "0.33.0-alpha.20240917.1",
"version": "0.33.0-alpha.20240918.1",
"description": "",

@@ -5,0 +5,0 @@ "type": "module",

@@ -5,7 +5,4 @@ // Copyright (c) Microsoft Corporation.

import { NameType, normalizeName } from "../helpers/nameUtils.js";
import { hasPollingOperations } from "../helpers/operationHelpers.js";
import {
hasPagingOperations,
hasPollingOperations
} from "../helpers/operationHelpers.js";
import {
isAzureMonorepoPackage,

@@ -51,3 +48,2 @@ isAzurePackage,

hasLro: hasPollingOperations(model),
hasPaging: hasPagingOperations(model),
monorepoPackageDirectory: model.options?.azureOutputDirectory,

@@ -93,5 +89,4 @@ specSource: model.options?.sourceFrom ?? "TypeSpec",

) {
const hasPaging = hasPagingOperations(model),
hasLro = hasPollingOperations(model);
if (!isAzurePackage(model) || (!hasPaging && !hasLro)) {
const hasLro = hasPollingOperations(model);
if (!isAzurePackage(model) || !hasLro) {
return;

@@ -114,10 +109,2 @@ }

// no need to update paging dependency for modular libraries
if (hasPaging && model.options?.isModularLibrary !== true) {
packageInfo.dependencies = {
...packageInfo.dependencies,
"@azure/core-paging": "^1.5.0"
};
}
if (hasLro) {

@@ -124,0 +111,0 @@ packageInfo.dependencies = {

@@ -11,3 +11,2 @@ // Copyright (c) Microsoft Corporation.

hasLro: boolean;
hasPaging: boolean;
specSource: "Swagger" | "TypeSpec";

@@ -34,6 +33,4 @@ }

hasLro,
hasPaging,
specSource,
dependencies,
isModularLibrary
dependencies
}: AzurePackageInfoConfig) {

@@ -57,9 +54,2 @@ let azureDependencies: Record<string, string> = {

if (hasPaging && isModularLibrary !== true) {
azureDependencies = {
...azureDependencies,
"@azure/core-paging": "^1.5.0"
};
}
return azureDependencies;

@@ -66,0 +56,0 @@ }

@@ -6,7 +6,2 @@ // Copyright (c) Microsoft Corporation.

import {
getPagedAsyncIterator,
PagedAsyncIterableIterator,
PagedResult
} from "@azure/core-paging";
import {
Client,

@@ -18,2 +13,143 @@ createRestError,

/**
* returns an async iterator that iterates over results. It also has a \`byPage\`
* method that returns pages of items at once.
*
* @param pagedResult - an object that specifies how to get pages.
* @returns a paged async iterator that iterates over results.
*/
function getPagedAsyncIterator<
TElement,
TPage = TElement[],
TPageSettings = PageSettings,
TLink = string,
>(
pagedResult: PagedResult<TPage, TPageSettings, TLink>,
): PagedAsyncIterableIterator<TElement, TPage, TPageSettings> {
const iter = getItemAsyncIterator<TElement, TPage, TLink, TPageSettings>(pagedResult);
return {
next() {
return iter.next();
},
[Symbol.asyncIterator]() {
return this;
},
byPage:
pagedResult?.byPage ??
(((settings?: PageSettings) => {
const { continuationToken } = settings ?? {};
return getPageAsyncIterator(pagedResult, {
pageLink: continuationToken as unknown as TLink | undefined,
});
}) as unknown as (settings?: TPageSettings) => AsyncIterableIterator<TPage>),
};
}
async function* getItemAsyncIterator<TElement, TPage, TLink, TPageSettings>(
pagedResult: PagedResult<TPage, TPageSettings, TLink>,
): AsyncIterableIterator<TElement> {
const pages = getPageAsyncIterator(pagedResult);
const firstVal = await pages.next();
// if the result does not have an array shape, i.e. TPage = TElement, then we return it as is
if (!Array.isArray(firstVal.value)) {
// can extract elements from this page
const { toElements } = pagedResult;
if (toElements) {
yield* toElements(firstVal.value) as TElement[];
for await (const page of pages) {
yield* toElements(page) as TElement[];
}
} else {
yield firstVal.value;
// \`pages\` is of type \`AsyncIterableIterator<TPage>\` but TPage = TElement in this case
yield* pages as unknown as AsyncIterableIterator<TElement>;
}
} else {
yield* firstVal.value;
for await (const page of pages) {
// pages is of type \`AsyncIterableIterator<TPage>\` so \`page\` is of type \`TPage\`. In this branch,
// it must be the case that \`TPage = TElement[]\`
yield* page as unknown as TElement[];
}
}
}
async function* getPageAsyncIterator<TPage, TLink, TPageSettings>(
pagedResult: PagedResult<TPage, TPageSettings, TLink>,
options: {
pageLink?: TLink;
} = {},
): AsyncIterableIterator<TPage> {
const { pageLink } = options;
let response = await pagedResult.getPage(pageLink ?? pagedResult.firstPageLink);
if (!response) {
return;
}
yield response.page;
while (response.nextPageLink) {
response = await pagedResult.getPage(response.nextPageLink);
if (!response) {
return;
}
yield response.page;
}
}
/**
* An interface that tracks the settings for paged iteration
*/
export interface PageSettings {
/**
* The token that keeps track of where to continue the iterator
*/
continuationToken?: string;
}
/**
* An interface that allows async iterable iteration both to completion and by page.
*/
export interface PagedAsyncIterableIterator<
TElement,
TPage = TElement[],
TPageSettings = PageSettings,
> {
/**
* The next method, part of the iteration protocol
*/
next(): Promise<IteratorResult<TElement>>;
/**
* The connection to the async iterator, part of the iteration protocol
*/
[Symbol.asyncIterator](): PagedAsyncIterableIterator<TElement, TPage, TPageSettings>;
/**
* Return an AsyncIterableIterator that works a page at a time
*/
byPage: (settings?: TPageSettings) => AsyncIterableIterator<TPage>;
}
/**
* An interface that describes how to communicate with the service.
*/
interface PagedResult<TPage, TPageSettings = PageSettings, TLink = string> {
/**
* Link to the first page of results.
*/
firstPageLink: TLink;
/**
* A method that returns a page of results.
*/
getPage: (
pageLink: TLink,
) => Promise<{ page: TPage; nextPageLink?: TLink } | undefined>;
/**
* a function to implement the \`byPage\` method on the paged async iterator.
*/
byPage?: (settings?: TPageSettings) => AsyncIterableIterator<TPage>;
/**
* A function to extract elements from a page.
*/
toElements?: (page: TPage) => unknown[];
}
/**
* Helper type to extract the type of an array

@@ -28,3 +164,2 @@ */

pageLink: string,
maxPageSize?: number
) => Promise<{

@@ -31,0 +166,0 @@ page: TPage;

@@ -706,22 +706,3 @@ // Copyright (c) Microsoft Corporation.

it("[cjs] should update to correct paging dependencies if there are paging operations", () => {
const model = createMockModel({
moduleKind: "cjs",
flavor: "azure",
isMonorepo: false,
withTests: true,
hasPaging: true
});
const packageFileContent = updatePackageFile(
model,
"./test/integration/static/package.json"
);
const packageFile = JSON.parse(packageFileContent?.content ?? "{}");
expect(packageFile.dependencies).to.have.property(
"@azure/core-paging",
"^1.5.0"
);
});
it("[cjs] should return directly if package.json is non-existing or no paging/lro operations", () => {
it("[cjs] should return directly if package.json is non-existing or no lro operations", () => {
let model = createMockModel({

@@ -732,3 +713,2 @@ moduleKind: "cjs",

withTests: true,
hasPaging: false,
hasLro: false

@@ -746,3 +726,2 @@ });

withTests: true,
hasPaging: true,
hasLro: true

@@ -756,36 +735,2 @@ });

});
it("[esm] should not update paging dependency if there exists paging operations for Modular", () => {
let model = createMockModel({
moduleKind: "esm",
flavor: "azure",
isMonorepo: false,
withTests: true,
hasPaging: true,
isModularLibrary: true
});
const packageFileContent = updatePackageFile(
model,
"./test/integration/static/package.json"
);
const packageFile = JSON.parse(packageFileContent?.content ?? "{}");
expect(packageFile.dependencies).to.not.have.property(
"@azure/core-paging"
);
});
it("[esm] should not include paging dependency if there exists paging operations for Modular", () => {
let model = createMockModel({
moduleKind: "esm",
flavor: "azure",
isMonorepo: false,
withTests: true,
hasPaging: true,
isModularLibrary: true
});
const packageFileContent = buildPackageFile(model);
const packageFile = JSON.parse(packageFileContent?.content ?? "{}");
expect(packageFile.dependencies).to.not.have.property(
"@azure/core-paging"
);
});
});

@@ -792,0 +737,0 @@

import { PackageCommonInfoConfig } from "./packageCommon.js";
export interface AzurePackageInfoConfig extends PackageCommonInfoConfig {
hasLro: boolean;
hasPaging: boolean;
specSource: "Swagger" | "TypeSpec";

@@ -34,3 +33,3 @@ }

*/
export declare function getAzurePackageDependencies({ hasLro, hasPaging, specSource, dependencies, isModularLibrary }: AzurePackageInfoConfig): Record<string, string>;
export declare function getAzurePackageDependencies({ hasLro, specSource, dependencies }: AzurePackageInfoConfig): Record<string, string>;
export declare function getAzurePackageDevDependencies(config: AzurePackageInfoConfig): {

@@ -37,0 +36,0 @@ autorest?: string | undefined;

@@ -1,1 +0,1 @@

export declare const paginateContent = "\nimport {\n getPagedAsyncIterator,\n PagedAsyncIterableIterator,\n PagedResult\n} from \"@azure/core-paging\";\nimport {\n Client,\n createRestError,\n PathUncheckedResponse\n} from \"@azure-rest/core-client\";\n\n/**\n * Helper type to extract the type of an array\n */\nexport type GetArrayType<T> = T extends Array<infer TData> ? TData : never;\n\n/**\n * The type of a custom function that defines how to get a page and a link to the next one if any.\n */\nexport type GetPage<TPage> = (\n pageLink: string,\n maxPageSize?: number\n) => Promise<{\n page: TPage;\n nextPageLink?: string;\n}>;\n\n/**\n * Options for the paging helper\n */\nexport interface PagingOptions<TResponse> {\n /**\n * Custom function to extract pagination details for crating the PagedAsyncIterableIterator\n */\n customGetPage?: GetPage<PaginateReturn<TResponse>[]>\n}\n\n/**\n * Helper type to infer the Type of the paged elements from the response type\n * This type is generated based on the swagger information for x-ms-pageable\n * specifically on the itemName property which indicates the property of the response\n * where the page items are found. The default value is `value`.\n * This type will allow us to provide strongly typed Iterator based on the response we get as second parameter\n */\n export type PaginateReturn<TResult> = TResult extends {{#each itemNames}}\n {\n \n body: { {{this}}?: infer TPage } \n \n} {{#if @last }}{{else}} | {{/if}}\n{{/each}}\n ? GetArrayType<TPage>\n : Array<unknown>;\n \n /**\n * Helper to paginate results from an initial response that follows the specification of Autorest `x-ms-pageable` extension\n * @param client - Client to use for sending the next page requests\n * @param initialResponse - Initial response containing the nextLink and current page of elements\n * @param customGetPage - Optional - Function to define how to extract the page and next link to be used to paginate the results\n * @returns - PagedAsyncIterableIterator to iterate the elements\n */\n export function paginate<TResponse extends PathUncheckedResponse>(\n client: Client,\n initialResponse: TResponse,\n options: PagingOptions<TResponse> = {}\n ): PagedAsyncIterableIterator<PaginateReturn<TResponse>> {\n // Extract element type from initial response\n type TElement = PaginateReturn<TResponse>;\n let firstRun = true;\n {{#if isComplexPaging}}\n // We need to check the response for success before trying to inspect it looking for\n // the properties to use for nextLink and itemName\n checkPagingRequest(initialResponse);\n const { itemName, nextLinkName } = getPaginationProperties(initialResponse);\n {{else}}\n const itemName = {{ quoteWrap itemNames }};\n const nextLinkName = {{quoteWrap nextLinkNames}};\n {{/if}}\n const { customGetPage } = options;\n const pagedResult: PagedResult<TElement[]> = {\n firstPageLink: \"\",\n getPage:\n typeof customGetPage === \"function\"\n ? customGetPage\n : async (pageLink: string) => {\n const result = firstRun\n ? initialResponse\n : await client.pathUnchecked(pageLink).get();\n firstRun = false;\n checkPagingRequest(result);\n const nextLink = getNextLink(result.body, nextLinkName);\n const values = getElements<TElement>(result.body, itemName);\n return {\n page: values,\n nextPageLink: nextLink\n };\n }\n };\n\n return getPagedAsyncIterator(pagedResult);\n}\n\n/**\n * Gets for the value of nextLink in the body\n */\nfunction getNextLink(body: unknown, nextLinkName?: string): string | undefined {\n if (!nextLinkName) {\n return undefined;\n }\n\n const nextLink = (body as Record<string, unknown>)[nextLinkName];\n\n if (typeof nextLink !== \"string\" && typeof nextLink !== \"undefined\") {\n throw new Error(\n `Body Property ${nextLinkName} should be a string or undefined`\n );\n }\n\n return nextLink;\n}\n\n/**\n * Gets the elements of the current request in the body.\n */\nfunction getElements<T = unknown>(body: unknown, itemName: string): T[] {\n const value = (body as Record<string, unknown>)[itemName] as T[];\n\n // value has to be an array according to the x-ms-pageable extension.\n // The fact that this must be an array is used above to calculate the\n // type of elements in the page in PaginateReturn\n if (!Array.isArray(value)) {\n throw new Error(\n `Couldn't paginate response\\n Body doesn't contain an array property with name: ${itemName}`\n );\n }\n\n return value ?? [];\n}\n\n/**\n * Checks if a request failed\n */\nfunction checkPagingRequest(response: PathUncheckedResponse): void {\n const Http2xxStatusCodes = [\n \"200\",\n \"201\",\n \"202\",\n \"203\",\n \"204\",\n \"205\",\n \"206\",\n \"207\",\n \"208\",\n \"226\"\n ];\n if (!Http2xxStatusCodes.includes(response.status)) {\n throw createRestError(\n `Pagination failed with unexpected statusCode ${response.status}`,\n response\n );\n }\n}\n\n{{#if isComplexPaging}}\n/**\n * Extracts the itemName and nextLinkName from the initial response to use them for pagination\n */\nfunction getPaginationProperties(initialResponse: PathUncheckedResponse) {\n // Build a set with the passed custom nextLinkNames\n const nextLinkNames = new Set([{{ quoteWrap nextLinkNames }}]);\n\n // Build a set with the passed custom set of itemNames\n const itemNames = new Set([{{ quoteWrap itemNames }}]);\n\n let nextLinkName: string | undefined;\n let itemName: string | undefined;\n\n for (const name of nextLinkNames) {\n const nextLink = (initialResponse.body as Record<string, unknown>)[\n name\n ] as string;\n if (nextLink) {\n nextLinkName = name;\n break;\n }\n }\n\n for (const name of itemNames) {\n const item = (initialResponse.body as Record<string, unknown>)[\n name\n ] as string;\n if (item) {\n itemName = name;\n break;\n }\n }\n\n if (!itemName) {\n throw new Error(\n `Couldn't paginate response\\n Body doesn't contain an array property with name: ${[\n ...itemNames\n ].join(\" OR \")}`\n );\n }\n\n return { itemName, nextLinkName };\n}\n{{/if}}\n";
export declare const paginateContent = "\nimport {\n Client,\n createRestError,\n PathUncheckedResponse\n} from \"@azure-rest/core-client\";\n\n/**\n * returns an async iterator that iterates over results. It also has a `byPage`\n * method that returns pages of items at once.\n *\n * @param pagedResult - an object that specifies how to get pages.\n * @returns a paged async iterator that iterates over results.\n */\nfunction getPagedAsyncIterator<\n TElement,\n TPage = TElement[],\n TPageSettings = PageSettings,\n TLink = string,\n>(\n pagedResult: PagedResult<TPage, TPageSettings, TLink>,\n): PagedAsyncIterableIterator<TElement, TPage, TPageSettings> {\n const iter = getItemAsyncIterator<TElement, TPage, TLink, TPageSettings>(pagedResult);\n return {\n next() {\n return iter.next();\n },\n [Symbol.asyncIterator]() {\n return this;\n },\n byPage:\n pagedResult?.byPage ??\n (((settings?: PageSettings) => {\n const { continuationToken } = settings ?? {};\n return getPageAsyncIterator(pagedResult, {\n pageLink: continuationToken as unknown as TLink | undefined,\n });\n }) as unknown as (settings?: TPageSettings) => AsyncIterableIterator<TPage>),\n };\n}\n\nasync function* getItemAsyncIterator<TElement, TPage, TLink, TPageSettings>(\n pagedResult: PagedResult<TPage, TPageSettings, TLink>,\n): AsyncIterableIterator<TElement> {\n const pages = getPageAsyncIterator(pagedResult);\n const firstVal = await pages.next();\n // if the result does not have an array shape, i.e. TPage = TElement, then we return it as is\n if (!Array.isArray(firstVal.value)) {\n // can extract elements from this page\n const { toElements } = pagedResult;\n if (toElements) {\n yield* toElements(firstVal.value) as TElement[];\n for await (const page of pages) {\n yield* toElements(page) as TElement[];\n }\n } else {\n yield firstVal.value;\n // `pages` is of type `AsyncIterableIterator<TPage>` but TPage = TElement in this case\n yield* pages as unknown as AsyncIterableIterator<TElement>;\n }\n } else {\n yield* firstVal.value;\n for await (const page of pages) {\n // pages is of type `AsyncIterableIterator<TPage>` so `page` is of type `TPage`. In this branch,\n // it must be the case that `TPage = TElement[]`\n yield* page as unknown as TElement[];\n }\n }\n}\n\nasync function* getPageAsyncIterator<TPage, TLink, TPageSettings>(\n pagedResult: PagedResult<TPage, TPageSettings, TLink>,\n options: {\n pageLink?: TLink;\n } = {},\n): AsyncIterableIterator<TPage> {\n const { pageLink } = options;\n let response = await pagedResult.getPage(pageLink ?? pagedResult.firstPageLink);\n if (!response) {\n return;\n }\n yield response.page;\n while (response.nextPageLink) {\n response = await pagedResult.getPage(response.nextPageLink);\n if (!response) {\n return;\n }\n yield response.page;\n }\n}\n\n/**\n * An interface that tracks the settings for paged iteration\n */\nexport interface PageSettings {\n /**\n * The token that keeps track of where to continue the iterator\n */\n continuationToken?: string;\n}\n\n/**\n * An interface that allows async iterable iteration both to completion and by page.\n */\nexport interface PagedAsyncIterableIterator<\n TElement,\n TPage = TElement[],\n TPageSettings = PageSettings,\n> {\n /**\n * The next method, part of the iteration protocol\n */\n next(): Promise<IteratorResult<TElement>>;\n /**\n * The connection to the async iterator, part of the iteration protocol\n */\n [Symbol.asyncIterator](): PagedAsyncIterableIterator<TElement, TPage, TPageSettings>;\n /**\n * Return an AsyncIterableIterator that works a page at a time\n */\n byPage: (settings?: TPageSettings) => AsyncIterableIterator<TPage>;\n}\n\n/**\n * An interface that describes how to communicate with the service.\n */\ninterface PagedResult<TPage, TPageSettings = PageSettings, TLink = string> {\n /**\n * Link to the first page of results.\n */\n firstPageLink: TLink;\n /**\n * A method that returns a page of results.\n */\n getPage: (\n pageLink: TLink,\n ) => Promise<{ page: TPage; nextPageLink?: TLink } | undefined>;\n /**\n * a function to implement the `byPage` method on the paged async iterator.\n */\n byPage?: (settings?: TPageSettings) => AsyncIterableIterator<TPage>;\n\n /**\n * A function to extract elements from a page.\n */\n toElements?: (page: TPage) => unknown[];\n}\n\n/**\n * Helper type to extract the type of an array\n */\nexport type GetArrayType<T> = T extends Array<infer TData> ? TData : never;\n\n/**\n * The type of a custom function that defines how to get a page and a link to the next one if any.\n */\nexport type GetPage<TPage> = (\n pageLink: string,\n) => Promise<{\n page: TPage;\n nextPageLink?: string;\n}>;\n\n/**\n * Options for the paging helper\n */\nexport interface PagingOptions<TResponse> {\n /**\n * Custom function to extract pagination details for crating the PagedAsyncIterableIterator\n */\n customGetPage?: GetPage<PaginateReturn<TResponse>[]>\n}\n\n/**\n * Helper type to infer the Type of the paged elements from the response type\n * This type is generated based on the swagger information for x-ms-pageable\n * specifically on the itemName property which indicates the property of the response\n * where the page items are found. The default value is `value`.\n * This type will allow us to provide strongly typed Iterator based on the response we get as second parameter\n */\n export type PaginateReturn<TResult> = TResult extends {{#each itemNames}}\n {\n \n body: { {{this}}?: infer TPage } \n \n} {{#if @last }}{{else}} | {{/if}}\n{{/each}}\n ? GetArrayType<TPage>\n : Array<unknown>;\n \n /**\n * Helper to paginate results from an initial response that follows the specification of Autorest `x-ms-pageable` extension\n * @param client - Client to use for sending the next page requests\n * @param initialResponse - Initial response containing the nextLink and current page of elements\n * @param customGetPage - Optional - Function to define how to extract the page and next link to be used to paginate the results\n * @returns - PagedAsyncIterableIterator to iterate the elements\n */\n export function paginate<TResponse extends PathUncheckedResponse>(\n client: Client,\n initialResponse: TResponse,\n options: PagingOptions<TResponse> = {}\n ): PagedAsyncIterableIterator<PaginateReturn<TResponse>> {\n // Extract element type from initial response\n type TElement = PaginateReturn<TResponse>;\n let firstRun = true;\n {{#if isComplexPaging}}\n // We need to check the response for success before trying to inspect it looking for\n // the properties to use for nextLink and itemName\n checkPagingRequest(initialResponse);\n const { itemName, nextLinkName } = getPaginationProperties(initialResponse);\n {{else}}\n const itemName = {{ quoteWrap itemNames }};\n const nextLinkName = {{quoteWrap nextLinkNames}};\n {{/if}}\n const { customGetPage } = options;\n const pagedResult: PagedResult<TElement[]> = {\n firstPageLink: \"\",\n getPage:\n typeof customGetPage === \"function\"\n ? customGetPage\n : async (pageLink: string) => {\n const result = firstRun\n ? initialResponse\n : await client.pathUnchecked(pageLink).get();\n firstRun = false;\n checkPagingRequest(result);\n const nextLink = getNextLink(result.body, nextLinkName);\n const values = getElements<TElement>(result.body, itemName);\n return {\n page: values,\n nextPageLink: nextLink\n };\n }\n };\n\n return getPagedAsyncIterator(pagedResult);\n}\n\n/**\n * Gets for the value of nextLink in the body\n */\nfunction getNextLink(body: unknown, nextLinkName?: string): string | undefined {\n if (!nextLinkName) {\n return undefined;\n }\n\n const nextLink = (body as Record<string, unknown>)[nextLinkName];\n\n if (typeof nextLink !== \"string\" && typeof nextLink !== \"undefined\") {\n throw new Error(\n `Body Property ${nextLinkName} should be a string or undefined`\n );\n }\n\n return nextLink;\n}\n\n/**\n * Gets the elements of the current request in the body.\n */\nfunction getElements<T = unknown>(body: unknown, itemName: string): T[] {\n const value = (body as Record<string, unknown>)[itemName] as T[];\n\n // value has to be an array according to the x-ms-pageable extension.\n // The fact that this must be an array is used above to calculate the\n // type of elements in the page in PaginateReturn\n if (!Array.isArray(value)) {\n throw new Error(\n `Couldn't paginate response\\n Body doesn't contain an array property with name: ${itemName}`\n );\n }\n\n return value ?? [];\n}\n\n/**\n * Checks if a request failed\n */\nfunction checkPagingRequest(response: PathUncheckedResponse): void {\n const Http2xxStatusCodes = [\n \"200\",\n \"201\",\n \"202\",\n \"203\",\n \"204\",\n \"205\",\n \"206\",\n \"207\",\n \"208\",\n \"226\"\n ];\n if (!Http2xxStatusCodes.includes(response.status)) {\n throw createRestError(\n `Pagination failed with unexpected statusCode ${response.status}`,\n response\n );\n }\n}\n\n{{#if isComplexPaging}}\n/**\n * Extracts the itemName and nextLinkName from the initial response to use them for pagination\n */\nfunction getPaginationProperties(initialResponse: PathUncheckedResponse) {\n // Build a set with the passed custom nextLinkNames\n const nextLinkNames = new Set([{{ quoteWrap nextLinkNames }}]);\n\n // Build a set with the passed custom set of itemNames\n const itemNames = new Set([{{ quoteWrap itemNames }}]);\n\n let nextLinkName: string | undefined;\n let itemName: string | undefined;\n\n for (const name of nextLinkNames) {\n const nextLink = (initialResponse.body as Record<string, unknown>)[\n name\n ] as string;\n if (nextLink) {\n nextLinkName = name;\n break;\n }\n }\n\n for (const name of itemNames) {\n const item = (initialResponse.body as Record<string, unknown>)[\n name\n ] as string;\n if (item) {\n itemName = name;\n break;\n }\n }\n\n if (!itemName) {\n throw new Error(\n `Couldn't paginate response\\n Body doesn't contain an array property with name: ${[\n ...itemNames\n ].join(\" OR \")}`\n );\n }\n\n return { itemName, nextLinkName };\n}\n{{/if}}\n";

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc