From 5864915464c92354d40073a67282df4c12a937a6 Mon Sep 17 00:00:00 2001 From: Jose Manuel Heredia Hidalgo Date: Wed, 31 Jul 2024 14:12:50 -0700 Subject: [PATCH] Re-generate cadl ranch clients --- .../azure/core/basic/src/api/operations.ts | 6 +- .../azure/core/basic/src/basicClient.ts | 2 +- .../generated/azure/core/basic/src/index.ts | 10 +- .../azure/core/basic/src/models/index.ts | 5 - .../core/basic/src/models/pagingTypes.ts | 89 ------------ .../{api => static-helpers}/pagingHelpers.ts | 124 ++++++++++++++-- .../lro/rpc/generated/src/api/operations.ts | 4 +- .../rpc/generated/src/restorePollerHelpers.ts | 16 +-- .../core/lro/rpc/generated/src/rpcClient.ts | 2 +- .../src/static-helpers}/pollingHelpers.ts | 49 ++++++- .../standard/generated/src/api/operations.ts | 4 +- .../generated/src/restorePollerHelpers.ts | 16 +-- .../standard/generated/src/standardClient.ts | 2 +- .../src/static-helpers}/pollingHelpers.ts | 49 ++++++- .../src/api/nestedProxyResources/index.ts | 10 +- .../src/api/topLevelTrackedResources/index.ts | 10 +- .../src/classic/nestedProxyResources/index.ts | 2 +- .../classic/topLevelTrackedResources/index.ts | 2 +- .../models/resources/src/index.ts | 10 +- .../models/resources/src/models/index.ts | 5 - .../resources/src/models/pagingTypes.ts | 89 ------------ .../resources/src/restorePollerHelpers.ts | 16 +-- .../{api => static-helpers}/pagingHelpers.ts | 124 ++++++++++++++-- .../src/static-helpers}/pollingHelpers.ts | 49 ++++++- .../payload/pageable/src/api/operations.ts | 6 +- .../generated/payload/pageable/src/index.ts | 15 +- .../payload/pageable/src/models/index.ts | 5 - .../pageable/src/models/pagingTypes.ts | 89 ------------ .../payload/pageable/src/pageableClient.ts | 2 +- .../{api => static-helpers}/pagingHelpers.ts | 135 ++++++++++++++---- 30 files changed, 544 insertions(+), 403 deletions(-) delete mode 100644 packages/typespec-ts/test/modularIntegration/generated/azure/core/basic/src/models/pagingTypes.ts rename packages/typespec-ts/test/modularIntegration/generated/azure/core/basic/src/{api => static-helpers}/pagingHelpers.ts (64%) rename packages/typespec-ts/test/modularIntegration/generated/azure/{resource-manager/models/resources/src/api => core/lro/rpc/generated/src/static-helpers}/pollingHelpers.ts (75%) rename packages/typespec-ts/test/modularIntegration/generated/azure/core/lro/{rpc/generated/src/api => standard/generated/src/static-helpers}/pollingHelpers.ts (75%) delete mode 100644 packages/typespec-ts/test/modularIntegration/generated/azure/resource-manager/models/resources/src/models/pagingTypes.ts rename packages/typespec-ts/test/modularIntegration/generated/azure/resource-manager/models/resources/src/{api => static-helpers}/pagingHelpers.ts (64%) rename packages/typespec-ts/test/modularIntegration/generated/azure/{core/lro/standard/generated/src/api => resource-manager/models/resources/src/static-helpers}/pollingHelpers.ts (75%) delete mode 100644 packages/typespec-ts/test/modularIntegration/generated/payload/pageable/src/models/pagingTypes.ts rename packages/typespec-ts/test/modularIntegration/generated/payload/pageable/src/{api => static-helpers}/pagingHelpers.ts (64%) diff --git a/packages/typespec-ts/test/modularIntegration/generated/azure/core/basic/src/api/operations.ts b/packages/typespec-ts/test/modularIntegration/generated/azure/core/basic/src/api/operations.ts index 5a01d33ee3..105f42b30d 100644 --- a/packages/typespec-ts/test/modularIntegration/generated/azure/core/basic/src/api/operations.ts +++ b/packages/typespec-ts/test/modularIntegration/generated/azure/core/basic/src/api/operations.ts @@ -12,8 +12,6 @@ import { _PagedUser, _UserListResults, } from "../models/models.js"; -import { PagedAsyncIterableIterator } from "../models/pagingTypes.js"; -import { buildPagedAsyncIterator } from "./pagingHelpers.js"; import { isUnexpected, BasicContext as Client, @@ -48,6 +46,10 @@ import { operationOptionsToRequestParameters, createRestError, } from "@azure-rest/core-client"; +import { + PagedAsyncIterableIterator, + buildPagedAsyncIterator, +} from "../static-helpers/pagingHelpers.js"; import { CreateOrUpdateOptionalParams, CreateOrReplaceOptionalParams, diff --git a/packages/typespec-ts/test/modularIntegration/generated/azure/core/basic/src/basicClient.ts b/packages/typespec-ts/test/modularIntegration/generated/azure/core/basic/src/basicClient.ts index 3afa8f9958..0155fdecaf 100644 --- a/packages/typespec-ts/test/modularIntegration/generated/azure/core/basic/src/basicClient.ts +++ b/packages/typespec-ts/test/modularIntegration/generated/azure/core/basic/src/basicClient.ts @@ -21,7 +21,6 @@ import { ListFirstItemOptionalParams, ListSecondItemOptionalParams, } from "./models/options.js"; -import { PagedAsyncIterableIterator } from "./models/pagingTypes.js"; import { createBasic, BasicClientOptionalParams, @@ -38,6 +37,7 @@ import { listFirstItem, listSecondItem, } from "./api/index.js"; +import { PagedAsyncIterableIterator } from "./static-helpers/pagingHelpers.js"; export { BasicClientOptionalParams } from "./api/basicContext.js"; diff --git a/packages/typespec-ts/test/modularIntegration/generated/azure/core/basic/src/index.ts b/packages/typespec-ts/test/modularIntegration/generated/azure/core/basic/src/index.ts index 36a1132d4f..db4767a012 100644 --- a/packages/typespec-ts/test/modularIntegration/generated/azure/core/basic/src/index.ts +++ b/packages/typespec-ts/test/modularIntegration/generated/azure/core/basic/src/index.ts @@ -1,6 +1,12 @@ // Copyright (c) Microsoft Corporation. // Licensed under the MIT license. +import { + PageSettings, + ContinuablePage, + PagedAsyncIterableIterator, +} from "./static-helpers/pagingHelpers.js"; + export { BasicClient, BasicClientOptionalParams } from "./basicClient.js"; export { User, @@ -21,7 +27,5 @@ export { ExportOptionalParams, ListFirstItemOptionalParams, ListSecondItemOptionalParams, - PageSettings, - ContinuablePage, - PagedAsyncIterableIterator, } from "./models/index.js"; +export { PageSettings, ContinuablePage, PagedAsyncIterableIterator }; diff --git a/packages/typespec-ts/test/modularIntegration/generated/azure/core/basic/src/models/index.ts b/packages/typespec-ts/test/modularIntegration/generated/azure/core/basic/src/models/index.ts index 5504635e56..274cd44721 100644 --- a/packages/typespec-ts/test/modularIntegration/generated/azure/core/basic/src/models/index.ts +++ b/packages/typespec-ts/test/modularIntegration/generated/azure/core/basic/src/models/index.ts @@ -23,8 +23,3 @@ export { ListFirstItemOptionalParams, ListSecondItemOptionalParams, } from "./options.js"; -export { - PageSettings, - ContinuablePage, - PagedAsyncIterableIterator, -} from "./pagingTypes.js"; diff --git a/packages/typespec-ts/test/modularIntegration/generated/azure/core/basic/src/models/pagingTypes.ts b/packages/typespec-ts/test/modularIntegration/generated/azure/core/basic/src/models/pagingTypes.ts deleted file mode 100644 index f734b48e62..0000000000 --- a/packages/typespec-ts/test/modularIntegration/generated/azure/core/basic/src/models/pagingTypes.ts +++ /dev/null @@ -1,89 +0,0 @@ -// Copyright (c) Microsoft Corporation. -// Licensed under the MIT license. - -/** - * Options for the byPage method - */ -export interface PageSettings { - /** - * A reference to a specific page to start iterating from. - */ - continuationToken?: string; -} - -/** - * An interface that describes a page of results. - */ -export type ContinuablePage = TPage & { - /** - * 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 extends PageSettings = PageSettings, -> { - /** - * The next method, part of the iteration protocol - */ - next(): Promise>; - /** - * 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>; -} - -/** - * An interface that describes how to communicate with the service. - */ -export interface PagedResult< - TElement, - TPage = TElement[], - TPageSettings extends PageSettings = PageSettings, -> { - /** - * Link to the first page of results. - */ - firstPageLink?: string; - /** - * A method that returns a page of results. - */ - getPage: ( - pageLink?: string, - ) => Promise<{ page: TPage; nextPageLink?: string } | undefined>; - /** - * a function to implement the `byPage` method on the paged async iterator. - */ - byPage?: ( - settings?: TPageSettings, - ) => AsyncIterableIterator>; - - /** - * A function to extract elements from a page. - */ - toElements?: (page: TPage) => TElement[]; -} - -/** - * Options for the paging helper - */ -export interface BuildPagedAsyncIteratorOptions { - itemName?: string; - nextLinkName?: string; -} diff --git a/packages/typespec-ts/test/modularIntegration/generated/azure/core/basic/src/api/pagingHelpers.ts b/packages/typespec-ts/test/modularIntegration/generated/azure/core/basic/src/static-helpers/pagingHelpers.ts similarity index 64% rename from packages/typespec-ts/test/modularIntegration/generated/azure/core/basic/src/api/pagingHelpers.ts rename to packages/typespec-ts/test/modularIntegration/generated/azure/core/basic/src/static-helpers/pagingHelpers.ts index 9780e76e04..45360a0c0c 100644 --- a/packages/typespec-ts/test/modularIntegration/generated/azure/core/basic/src/api/pagingHelpers.ts +++ b/packages/typespec-ts/test/modularIntegration/generated/azure/core/basic/src/static-helpers/pagingHelpers.ts @@ -1,20 +1,105 @@ // Copyright (c) Microsoft Corporation. // Licensed under the MIT license. +// Copyright (c) Microsoft Corporation. +// Licensed under the MIT license. + import { Client, createRestError, PathUncheckedResponse, } from "@azure-rest/core-client"; import { RestError } from "@azure/core-rest-pipeline"; -import { - BuildPagedAsyncIteratorOptions, - ContinuablePage, - PageSettings, - PagedAsyncIterableIterator, - PagedResult, -} from "../models/pagingTypes.js"; -import { isUnexpected } from "../rest/index.js"; + +// Copyright (c) Microsoft Corporation. +// Licensed under the MIT license. + +/** + * Options for the byPage method + */ +export interface PageSettings { + /** + * A reference to a specific page to start iterating from. + */ + continuationToken?: string; +} + +/** + * An interface that describes a page of results. + */ +export type ContinuablePage = TPage & { + /** + * 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 extends PageSettings = PageSettings, +> { + /** + * The next method, part of the iteration protocol + */ + next(): Promise>; + /** + * 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>; +} + +/** + * An interface that describes how to communicate with the service. + */ +export interface PagedResult< + TElement, + TPage = TElement[], + TPageSettings extends PageSettings = PageSettings, +> { + /** + * Link to the first page of results. + */ + firstPageLink?: string; + /** + * A method that returns a page of results. + */ + getPage: ( + pageLink?: string, + ) => Promise<{ page: TPage; nextPageLink?: string } | undefined>; + /** + * a function to implement the `byPage` method on the paged async iterator. + */ + byPage?: ( + settings?: TPageSettings, + ) => AsyncIterableIterator>; + + /** + * A function to extract elements from a page. + */ + toElements?: (page: TPage) => TElement[]; +} + +/** + * Options for the paging helper + */ +export interface BuildPagedAsyncIteratorOptions { + itemName?: string; + nextLinkName?: string; +} /** * Helper to paginate results in a generic way and return a PagedAsyncIterableIterator @@ -182,10 +267,23 @@ function getElements(body: unknown, itemName: string): T[] { * Checks if a request failed */ function checkPagingRequest(response: PathUncheckedResponse): void { - if (isUnexpected(response)) { - throw createRestError( - `Pagination failed with unexpected statusCode ${response.status}`, - response, - ); + const statusCode = Number(response.status); + if (statusCode < 200 || statusCode >= 300) { + if (statusCode >= 400 && statusCode < 500) { + throw createRestError( + `Pagination failed with client error statusCode ${response.status}`, + response, + ); + } else if (statusCode >= 500) { + throw createRestError( + `Pagination failed with server error statusCode ${response.status}`, + response, + ); + } else { + throw createRestError( + `Pagination failed with unexpected statusCode ${response.status}`, + response, + ); + } } } diff --git a/packages/typespec-ts/test/modularIntegration/generated/azure/core/lro/rpc/generated/src/api/operations.ts b/packages/typespec-ts/test/modularIntegration/generated/azure/core/lro/rpc/generated/src/api/operations.ts index 581e5cb86a..e0d2a49506 100644 --- a/packages/typespec-ts/test/modularIntegration/generated/azure/core/lro/rpc/generated/src/api/operations.ts +++ b/packages/typespec-ts/test/modularIntegration/generated/azure/core/lro/rpc/generated/src/api/operations.ts @@ -1,8 +1,6 @@ // Copyright (c) Microsoft Corporation. // Licensed under the MIT license. -import { getLongRunningPoller } from "./pollingHelpers.js"; -import { PollerLike, OperationState } from "@azure/core-lro"; import { GenerationOptions, GenerationResult } from "../models/models.js"; import { isUnexpected, @@ -16,6 +14,8 @@ import { operationOptionsToRequestParameters, createRestError, } from "@azure-rest/core-client"; +import { getLongRunningPoller } from "../static-helpers/pollingHelpers.js"; +import { PollerLike, OperationState } from "@azure/core-lro"; import { LongRunningRpcOptionalParams } from "../models/options.js"; export function _longRunningRpcSend( diff --git a/packages/typespec-ts/test/modularIntegration/generated/azure/core/lro/rpc/generated/src/restorePollerHelpers.ts b/packages/typespec-ts/test/modularIntegration/generated/azure/core/lro/rpc/generated/src/restorePollerHelpers.ts index f74c0f2398..c4a2eb2eb3 100644 --- a/packages/typespec-ts/test/modularIntegration/generated/azure/core/lro/rpc/generated/src/restorePollerHelpers.ts +++ b/packages/typespec-ts/test/modularIntegration/generated/azure/core/lro/rpc/generated/src/restorePollerHelpers.ts @@ -1,20 +1,20 @@ // Copyright (c) Microsoft Corporation. // Licensed under the MIT license. -import { - PollerLike, - OperationState, - deserializeState, - ResourceLocationConfig, -} from "@azure/core-lro"; import { RpcClient } from "./rpcClient.js"; -import { getLongRunningPoller } from "./api/pollingHelpers.js"; import { _longRunningRpcDeserialize } from "./api/operations.js"; +import { getLongRunningPoller } from "./static-helpers/pollingHelpers.js"; import { - PathUncheckedResponse, OperationOptions, + PathUncheckedResponse, } from "@azure-rest/core-client"; import { AbortSignalLike } from "@azure/abort-controller"; +import { + PollerLike, + OperationState, + deserializeState, + ResourceLocationConfig, +} from "@azure/core-lro"; export interface RestorePollerOptions< TResult, diff --git a/packages/typespec-ts/test/modularIntegration/generated/azure/core/lro/rpc/generated/src/rpcClient.ts b/packages/typespec-ts/test/modularIntegration/generated/azure/core/lro/rpc/generated/src/rpcClient.ts index 35e6cc9a09..6c1e5ca8ca 100644 --- a/packages/typespec-ts/test/modularIntegration/generated/azure/core/lro/rpc/generated/src/rpcClient.ts +++ b/packages/typespec-ts/test/modularIntegration/generated/azure/core/lro/rpc/generated/src/rpcClient.ts @@ -1,7 +1,6 @@ // Copyright (c) Microsoft Corporation. // Licensed under the MIT license. -import { PollerLike, OperationState } from "@azure/core-lro"; import { Pipeline } from "@azure/core-rest-pipeline"; import { GenerationOptions, GenerationResult } from "./models/models.js"; import { LongRunningRpcOptionalParams } from "./models/options.js"; @@ -11,6 +10,7 @@ import { RpcClientOptionalParams, RpcContext, } from "./api/index.js"; +import { PollerLike, OperationState } from "@azure/core-lro"; export { RpcClientOptionalParams } from "./api/rpcContext.js"; diff --git a/packages/typespec-ts/test/modularIntegration/generated/azure/resource-manager/models/resources/src/api/pollingHelpers.ts b/packages/typespec-ts/test/modularIntegration/generated/azure/core/lro/rpc/generated/src/static-helpers/pollingHelpers.ts similarity index 75% rename from packages/typespec-ts/test/modularIntegration/generated/azure/resource-manager/models/resources/src/api/pollingHelpers.ts rename to packages/typespec-ts/test/modularIntegration/generated/azure/core/lro/rpc/generated/src/static-helpers/pollingHelpers.ts index f3bd59e41b..7388775c58 100644 --- a/packages/typespec-ts/test/modularIntegration/generated/azure/resource-manager/models/resources/src/api/pollingHelpers.ts +++ b/packages/typespec-ts/test/modularIntegration/generated/azure/core/lro/rpc/generated/src/static-helpers/pollingHelpers.ts @@ -1,6 +1,9 @@ // Copyright (c) Microsoft Corporation. // Licensed under the MIT license. +// Copyright (c) Microsoft Corporation. +// Licensed under the MIT license. + import { PollerLike, OperationState, @@ -12,11 +15,11 @@ import { import { Client, + OperationOptions, PathUncheckedResponse, createRestError, } from "@azure-rest/core-client"; import { AbortSignalLike } from "@azure/abort-controller"; -import { isUnexpected } from "../rest/index.js"; export interface GetLongRunningPollerOptions { /** Delay to wait until next poll, in milliseconds. */ @@ -125,9 +128,7 @@ export function getLongRunningPoller< function getLroResponse( response: TResponse, ): OperationResponse { - if (isUnexpected(response as PathUncheckedResponse)) { - throw createRestError(response); - } + checkResponse(response); return { flatResponse: response, rawResponse: { @@ -137,3 +138,43 @@ function getLroResponse( }, }; } + +/** + * Checks if a request failed + */ +function checkResponse(response: PathUncheckedResponse): void { + const statusCode = Number(response.status); + if (statusCode < 200 || statusCode >= 300) { + const errorMessage = response.body?.message ?? ""; + if (statusCode >= 400 && statusCode < 500) { + throw createRestError( + `Poller failed with client error statusCode ${response.status}\n ${errorMessage}`, + response, + ); + } else if (statusCode >= 500) { + throw createRestError( + `Poller failed with server error statusCode ${response.status}\n ${errorMessage}`, + response, + ); + } else { + throw createRestError( + `Poller failed with unexpected statusCode ${response.status}\n ${errorMessage}`, + response, + ); + } + } +} + +export interface RestorePollerOptions< + TResult, + TResponse extends PathUncheckedResponse = PathUncheckedResponse, +> extends OperationOptions { + /** Delay to wait until next poll, in milliseconds. */ + updateIntervalInMs?: number; + /** + * The signal which can be used to abort requests. + */ + abortSignal?: AbortSignalLike; + /** Deserialization function for raw response body */ + processResponseBody?: (result: TResponse) => Promise; +} diff --git a/packages/typespec-ts/test/modularIntegration/generated/azure/core/lro/standard/generated/src/api/operations.ts b/packages/typespec-ts/test/modularIntegration/generated/azure/core/lro/standard/generated/src/api/operations.ts index 4ecbafcac5..dfffd6b0e3 100644 --- a/packages/typespec-ts/test/modularIntegration/generated/azure/core/lro/standard/generated/src/api/operations.ts +++ b/packages/typespec-ts/test/modularIntegration/generated/azure/core/lro/standard/generated/src/api/operations.ts @@ -1,8 +1,6 @@ // Copyright (c) Microsoft Corporation. // Licensed under the MIT license. -import { getLongRunningPoller } from "./pollingHelpers.js"; -import { PollerLike, OperationState } from "@azure/core-lro"; import { User, ExportedUser } from "../models/models.js"; import { isUnexpected, @@ -23,6 +21,8 @@ import { operationOptionsToRequestParameters, createRestError, } from "@azure-rest/core-client"; +import { getLongRunningPoller } from "../static-helpers/pollingHelpers.js"; +import { PollerLike, OperationState } from "@azure/core-lro"; import { CreateOrReplaceOptionalParams, DeleteOptionalParams, diff --git a/packages/typespec-ts/test/modularIntegration/generated/azure/core/lro/standard/generated/src/restorePollerHelpers.ts b/packages/typespec-ts/test/modularIntegration/generated/azure/core/lro/standard/generated/src/restorePollerHelpers.ts index 6a538994a3..468314cb9f 100644 --- a/packages/typespec-ts/test/modularIntegration/generated/azure/core/lro/standard/generated/src/restorePollerHelpers.ts +++ b/packages/typespec-ts/test/modularIntegration/generated/azure/core/lro/standard/generated/src/restorePollerHelpers.ts @@ -1,24 +1,24 @@ // Copyright (c) Microsoft Corporation. // Licensed under the MIT license. -import { - PollerLike, - OperationState, - deserializeState, - ResourceLocationConfig, -} from "@azure/core-lro"; import { StandardClient } from "./standardClient.js"; -import { getLongRunningPoller } from "./api/pollingHelpers.js"; import { _createOrReplaceDeserialize, _$deleteDeserialize, _$exportDeserialize, } from "./api/operations.js"; +import { getLongRunningPoller } from "./static-helpers/pollingHelpers.js"; import { - PathUncheckedResponse, OperationOptions, + PathUncheckedResponse, } from "@azure-rest/core-client"; import { AbortSignalLike } from "@azure/abort-controller"; +import { + PollerLike, + OperationState, + deserializeState, + ResourceLocationConfig, +} from "@azure/core-lro"; export interface RestorePollerOptions< TResult, diff --git a/packages/typespec-ts/test/modularIntegration/generated/azure/core/lro/standard/generated/src/standardClient.ts b/packages/typespec-ts/test/modularIntegration/generated/azure/core/lro/standard/generated/src/standardClient.ts index 249801e684..2619440d19 100644 --- a/packages/typespec-ts/test/modularIntegration/generated/azure/core/lro/standard/generated/src/standardClient.ts +++ b/packages/typespec-ts/test/modularIntegration/generated/azure/core/lro/standard/generated/src/standardClient.ts @@ -1,7 +1,6 @@ // Copyright (c) Microsoft Corporation. // Licensed under the MIT license. -import { PollerLike, OperationState } from "@azure/core-lro"; import { Pipeline } from "@azure/core-rest-pipeline"; import { User, ExportedUser } from "./models/models.js"; import { @@ -17,6 +16,7 @@ import { StandardClientOptionalParams, StandardContext, } from "./api/index.js"; +import { PollerLike, OperationState } from "@azure/core-lro"; export { StandardClientOptionalParams } from "./api/standardContext.js"; diff --git a/packages/typespec-ts/test/modularIntegration/generated/azure/core/lro/rpc/generated/src/api/pollingHelpers.ts b/packages/typespec-ts/test/modularIntegration/generated/azure/core/lro/standard/generated/src/static-helpers/pollingHelpers.ts similarity index 75% rename from packages/typespec-ts/test/modularIntegration/generated/azure/core/lro/rpc/generated/src/api/pollingHelpers.ts rename to packages/typespec-ts/test/modularIntegration/generated/azure/core/lro/standard/generated/src/static-helpers/pollingHelpers.ts index f3bd59e41b..7388775c58 100644 --- a/packages/typespec-ts/test/modularIntegration/generated/azure/core/lro/rpc/generated/src/api/pollingHelpers.ts +++ b/packages/typespec-ts/test/modularIntegration/generated/azure/core/lro/standard/generated/src/static-helpers/pollingHelpers.ts @@ -1,6 +1,9 @@ // Copyright (c) Microsoft Corporation. // Licensed under the MIT license. +// Copyright (c) Microsoft Corporation. +// Licensed under the MIT license. + import { PollerLike, OperationState, @@ -12,11 +15,11 @@ import { import { Client, + OperationOptions, PathUncheckedResponse, createRestError, } from "@azure-rest/core-client"; import { AbortSignalLike } from "@azure/abort-controller"; -import { isUnexpected } from "../rest/index.js"; export interface GetLongRunningPollerOptions { /** Delay to wait until next poll, in milliseconds. */ @@ -125,9 +128,7 @@ export function getLongRunningPoller< function getLroResponse( response: TResponse, ): OperationResponse { - if (isUnexpected(response as PathUncheckedResponse)) { - throw createRestError(response); - } + checkResponse(response); return { flatResponse: response, rawResponse: { @@ -137,3 +138,43 @@ function getLroResponse( }, }; } + +/** + * Checks if a request failed + */ +function checkResponse(response: PathUncheckedResponse): void { + const statusCode = Number(response.status); + if (statusCode < 200 || statusCode >= 300) { + const errorMessage = response.body?.message ?? ""; + if (statusCode >= 400 && statusCode < 500) { + throw createRestError( + `Poller failed with client error statusCode ${response.status}\n ${errorMessage}`, + response, + ); + } else if (statusCode >= 500) { + throw createRestError( + `Poller failed with server error statusCode ${response.status}\n ${errorMessage}`, + response, + ); + } else { + throw createRestError( + `Poller failed with unexpected statusCode ${response.status}\n ${errorMessage}`, + response, + ); + } + } +} + +export interface RestorePollerOptions< + TResult, + TResponse extends PathUncheckedResponse = PathUncheckedResponse, +> extends OperationOptions { + /** Delay to wait until next poll, in milliseconds. */ + updateIntervalInMs?: number; + /** + * The signal which can be used to abort requests. + */ + abortSignal?: AbortSignalLike; + /** Deserialization function for raw response body */ + processResponseBody?: (result: TResponse) => Promise; +} diff --git a/packages/typespec-ts/test/modularIntegration/generated/azure/resource-manager/models/resources/src/api/nestedProxyResources/index.ts b/packages/typespec-ts/test/modularIntegration/generated/azure/resource-manager/models/resources/src/api/nestedProxyResources/index.ts index 965195a639..26ae5a5dea 100644 --- a/packages/typespec-ts/test/modularIntegration/generated/azure/resource-manager/models/resources/src/api/nestedProxyResources/index.ts +++ b/packages/typespec-ts/test/modularIntegration/generated/azure/resource-manager/models/resources/src/api/nestedProxyResources/index.ts @@ -1,15 +1,11 @@ // Copyright (c) Microsoft Corporation. // Licensed under the MIT license. -import { getLongRunningPoller } from "../pollingHelpers.js"; -import { PollerLike, OperationState } from "@azure/core-lro"; import { nestedProxyResourcePropertiesSerializer, NestedProxyResource, _NestedProxyResourceListResult, } from "../../models/models.js"; -import { PagedAsyncIterableIterator } from "../../models/pagingTypes.js"; -import { buildPagedAsyncIterator } from "../pagingHelpers.js"; import { isUnexpected, ResourcesContext as Client, @@ -35,6 +31,12 @@ import { operationOptionsToRequestParameters, createRestError, } from "@azure-rest/core-client"; +import { + PagedAsyncIterableIterator, + buildPagedAsyncIterator, +} from "../../static-helpers/pagingHelpers.js"; +import { getLongRunningPoller } from "../../static-helpers/pollingHelpers.js"; +import { PollerLike, OperationState } from "@azure/core-lro"; import { NestedProxyResourcesGetOptionalParams, NestedProxyResourcesCreateOrReplaceOptionalParams, diff --git a/packages/typespec-ts/test/modularIntegration/generated/azure/resource-manager/models/resources/src/api/topLevelTrackedResources/index.ts b/packages/typespec-ts/test/modularIntegration/generated/azure/resource-manager/models/resources/src/api/topLevelTrackedResources/index.ts index 9717bc579b..b77e787bcf 100644 --- a/packages/typespec-ts/test/modularIntegration/generated/azure/resource-manager/models/resources/src/api/topLevelTrackedResources/index.ts +++ b/packages/typespec-ts/test/modularIntegration/generated/azure/resource-manager/models/resources/src/api/topLevelTrackedResources/index.ts @@ -1,15 +1,11 @@ // Copyright (c) Microsoft Corporation. // Licensed under the MIT license. -import { getLongRunningPoller } from "../pollingHelpers.js"; -import { PollerLike, OperationState } from "@azure/core-lro"; import { topLevelTrackedResourcePropertiesSerializer, TopLevelTrackedResource, _TopLevelTrackedResourceListResult, } from "../../models/models.js"; -import { PagedAsyncIterableIterator } from "../../models/pagingTypes.js"; -import { buildPagedAsyncIterator } from "../pagingHelpers.js"; import { isUnexpected, ResourcesContext as Client, @@ -38,6 +34,12 @@ import { createRestError, } from "@azure-rest/core-client"; import { serializeRecord } from "../../helpers/serializerHelpers.js"; +import { + PagedAsyncIterableIterator, + buildPagedAsyncIterator, +} from "../../static-helpers/pagingHelpers.js"; +import { getLongRunningPoller } from "../../static-helpers/pollingHelpers.js"; +import { PollerLike, OperationState } from "@azure/core-lro"; import { TopLevelTrackedResourcesGetOptionalParams, TopLevelTrackedResourcesCreateOrReplaceOptionalParams, diff --git a/packages/typespec-ts/test/modularIntegration/generated/azure/resource-manager/models/resources/src/classic/nestedProxyResources/index.ts b/packages/typespec-ts/test/modularIntegration/generated/azure/resource-manager/models/resources/src/classic/nestedProxyResources/index.ts index b0bad9d496..09899061ec 100644 --- a/packages/typespec-ts/test/modularIntegration/generated/azure/resource-manager/models/resources/src/classic/nestedProxyResources/index.ts +++ b/packages/typespec-ts/test/modularIntegration/generated/azure/resource-manager/models/resources/src/classic/nestedProxyResources/index.ts @@ -10,7 +10,7 @@ import { nestedProxyResourcesDelete, nestedProxyResourcesListByTopLevelTrackedResource, } from "../../api/nestedProxyResources/index.js"; -import { PagedAsyncIterableIterator } from "../../models/pagingTypes.js"; +import { PagedAsyncIterableIterator } from "../../static-helpers/pagingHelpers.js"; import { PollerLike, OperationState } from "@azure/core-lro"; import { NestedProxyResourcesGetOptionalParams, diff --git a/packages/typespec-ts/test/modularIntegration/generated/azure/resource-manager/models/resources/src/classic/topLevelTrackedResources/index.ts b/packages/typespec-ts/test/modularIntegration/generated/azure/resource-manager/models/resources/src/classic/topLevelTrackedResources/index.ts index 6c476068c9..c25e356075 100644 --- a/packages/typespec-ts/test/modularIntegration/generated/azure/resource-manager/models/resources/src/classic/topLevelTrackedResources/index.ts +++ b/packages/typespec-ts/test/modularIntegration/generated/azure/resource-manager/models/resources/src/classic/topLevelTrackedResources/index.ts @@ -11,7 +11,7 @@ import { topLevelTrackedResourcesListByResourceGroup, topLevelTrackedResourcesListBySubscription, } from "../../api/topLevelTrackedResources/index.js"; -import { PagedAsyncIterableIterator } from "../../models/pagingTypes.js"; +import { PagedAsyncIterableIterator } from "../../static-helpers/pagingHelpers.js"; import { PollerLike, OperationState } from "@azure/core-lro"; import { TopLevelTrackedResourcesGetOptionalParams, diff --git a/packages/typespec-ts/test/modularIntegration/generated/azure/resource-manager/models/resources/src/index.ts b/packages/typespec-ts/test/modularIntegration/generated/azure/resource-manager/models/resources/src/index.ts index f808222977..09869413d9 100644 --- a/packages/typespec-ts/test/modularIntegration/generated/azure/resource-manager/models/resources/src/index.ts +++ b/packages/typespec-ts/test/modularIntegration/generated/azure/resource-manager/models/resources/src/index.ts @@ -1,6 +1,12 @@ // Copyright (c) Microsoft Corporation. // Licensed under the MIT license. +import { + PageSettings, + ContinuablePage, + PagedAsyncIterableIterator, +} from "./static-helpers/pagingHelpers.js"; + export { ResourcesClient, ResourcesClientOptionalParams, @@ -35,11 +41,9 @@ export { NestedProxyResourcesUpdateOptionalParams, NestedProxyResourcesDeleteOptionalParams, NestedProxyResourcesListByTopLevelTrackedResourceOptionalParams, - PageSettings, - ContinuablePage, - PagedAsyncIterableIterator, } from "./models/index.js"; export { NestedProxyResourcesOperations, TopLevelTrackedResourcesOperations, } from "./classic/index.js"; +export { PageSettings, ContinuablePage, PagedAsyncIterableIterator }; diff --git a/packages/typespec-ts/test/modularIntegration/generated/azure/resource-manager/models/resources/src/models/index.ts b/packages/typespec-ts/test/modularIntegration/generated/azure/resource-manager/models/resources/src/models/index.ts index e6469947af..762f7b8372 100644 --- a/packages/typespec-ts/test/modularIntegration/generated/azure/resource-manager/models/resources/src/models/index.ts +++ b/packages/typespec-ts/test/modularIntegration/generated/azure/resource-manager/models/resources/src/models/index.ts @@ -33,8 +33,3 @@ export { NestedProxyResourcesDeleteOptionalParams, NestedProxyResourcesListByTopLevelTrackedResourceOptionalParams, } from "./options.js"; -export { - PageSettings, - ContinuablePage, - PagedAsyncIterableIterator, -} from "./pagingTypes.js"; diff --git a/packages/typespec-ts/test/modularIntegration/generated/azure/resource-manager/models/resources/src/models/pagingTypes.ts b/packages/typespec-ts/test/modularIntegration/generated/azure/resource-manager/models/resources/src/models/pagingTypes.ts deleted file mode 100644 index f734b48e62..0000000000 --- a/packages/typespec-ts/test/modularIntegration/generated/azure/resource-manager/models/resources/src/models/pagingTypes.ts +++ /dev/null @@ -1,89 +0,0 @@ -// Copyright (c) Microsoft Corporation. -// Licensed under the MIT license. - -/** - * Options for the byPage method - */ -export interface PageSettings { - /** - * A reference to a specific page to start iterating from. - */ - continuationToken?: string; -} - -/** - * An interface that describes a page of results. - */ -export type ContinuablePage = TPage & { - /** - * 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 extends PageSettings = PageSettings, -> { - /** - * The next method, part of the iteration protocol - */ - next(): Promise>; - /** - * 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>; -} - -/** - * An interface that describes how to communicate with the service. - */ -export interface PagedResult< - TElement, - TPage = TElement[], - TPageSettings extends PageSettings = PageSettings, -> { - /** - * Link to the first page of results. - */ - firstPageLink?: string; - /** - * A method that returns a page of results. - */ - getPage: ( - pageLink?: string, - ) => Promise<{ page: TPage; nextPageLink?: string } | undefined>; - /** - * a function to implement the `byPage` method on the paged async iterator. - */ - byPage?: ( - settings?: TPageSettings, - ) => AsyncIterableIterator>; - - /** - * A function to extract elements from a page. - */ - toElements?: (page: TPage) => TElement[]; -} - -/** - * Options for the paging helper - */ -export interface BuildPagedAsyncIteratorOptions { - itemName?: string; - nextLinkName?: string; -} diff --git a/packages/typespec-ts/test/modularIntegration/generated/azure/resource-manager/models/resources/src/restorePollerHelpers.ts b/packages/typespec-ts/test/modularIntegration/generated/azure/resource-manager/models/resources/src/restorePollerHelpers.ts index d21d42ba41..ba6ca39436 100644 --- a/packages/typespec-ts/test/modularIntegration/generated/azure/resource-manager/models/resources/src/restorePollerHelpers.ts +++ b/packages/typespec-ts/test/modularIntegration/generated/azure/resource-manager/models/resources/src/restorePollerHelpers.ts @@ -1,14 +1,7 @@ // Copyright (c) Microsoft Corporation. // Licensed under the MIT license. -import { - PollerLike, - OperationState, - deserializeState, - ResourceLocationConfig, -} from "@azure/core-lro"; import { ResourcesClient } from "./resourcesClient.js"; -import { getLongRunningPoller } from "./api/pollingHelpers.js"; import { _topLevelTrackedResourcesCreateOrReplaceDeserialize, _topLevelTrackedResourcesUpdateDeserialize, @@ -19,11 +12,18 @@ import { _nestedProxyResourcesUpdateDeserialize, _nestedProxyResourcesDeleteDeserialize, } from "./api/nestedProxyResources/index.js"; +import { getLongRunningPoller } from "./static-helpers/pollingHelpers.js"; import { - PathUncheckedResponse, OperationOptions, + PathUncheckedResponse, } from "@azure-rest/core-client"; import { AbortSignalLike } from "@azure/abort-controller"; +import { + PollerLike, + OperationState, + deserializeState, + ResourceLocationConfig, +} from "@azure/core-lro"; export interface RestorePollerOptions< TResult, diff --git a/packages/typespec-ts/test/modularIntegration/generated/azure/resource-manager/models/resources/src/api/pagingHelpers.ts b/packages/typespec-ts/test/modularIntegration/generated/azure/resource-manager/models/resources/src/static-helpers/pagingHelpers.ts similarity index 64% rename from packages/typespec-ts/test/modularIntegration/generated/azure/resource-manager/models/resources/src/api/pagingHelpers.ts rename to packages/typespec-ts/test/modularIntegration/generated/azure/resource-manager/models/resources/src/static-helpers/pagingHelpers.ts index 9780e76e04..45360a0c0c 100644 --- a/packages/typespec-ts/test/modularIntegration/generated/azure/resource-manager/models/resources/src/api/pagingHelpers.ts +++ b/packages/typespec-ts/test/modularIntegration/generated/azure/resource-manager/models/resources/src/static-helpers/pagingHelpers.ts @@ -1,20 +1,105 @@ // Copyright (c) Microsoft Corporation. // Licensed under the MIT license. +// Copyright (c) Microsoft Corporation. +// Licensed under the MIT license. + import { Client, createRestError, PathUncheckedResponse, } from "@azure-rest/core-client"; import { RestError } from "@azure/core-rest-pipeline"; -import { - BuildPagedAsyncIteratorOptions, - ContinuablePage, - PageSettings, - PagedAsyncIterableIterator, - PagedResult, -} from "../models/pagingTypes.js"; -import { isUnexpected } from "../rest/index.js"; + +// Copyright (c) Microsoft Corporation. +// Licensed under the MIT license. + +/** + * Options for the byPage method + */ +export interface PageSettings { + /** + * A reference to a specific page to start iterating from. + */ + continuationToken?: string; +} + +/** + * An interface that describes a page of results. + */ +export type ContinuablePage = TPage & { + /** + * 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 extends PageSettings = PageSettings, +> { + /** + * The next method, part of the iteration protocol + */ + next(): Promise>; + /** + * 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>; +} + +/** + * An interface that describes how to communicate with the service. + */ +export interface PagedResult< + TElement, + TPage = TElement[], + TPageSettings extends PageSettings = PageSettings, +> { + /** + * Link to the first page of results. + */ + firstPageLink?: string; + /** + * A method that returns a page of results. + */ + getPage: ( + pageLink?: string, + ) => Promise<{ page: TPage; nextPageLink?: string } | undefined>; + /** + * a function to implement the `byPage` method on the paged async iterator. + */ + byPage?: ( + settings?: TPageSettings, + ) => AsyncIterableIterator>; + + /** + * A function to extract elements from a page. + */ + toElements?: (page: TPage) => TElement[]; +} + +/** + * Options for the paging helper + */ +export interface BuildPagedAsyncIteratorOptions { + itemName?: string; + nextLinkName?: string; +} /** * Helper to paginate results in a generic way and return a PagedAsyncIterableIterator @@ -182,10 +267,23 @@ function getElements(body: unknown, itemName: string): T[] { * Checks if a request failed */ function checkPagingRequest(response: PathUncheckedResponse): void { - if (isUnexpected(response)) { - throw createRestError( - `Pagination failed with unexpected statusCode ${response.status}`, - response, - ); + const statusCode = Number(response.status); + if (statusCode < 200 || statusCode >= 300) { + if (statusCode >= 400 && statusCode < 500) { + throw createRestError( + `Pagination failed with client error statusCode ${response.status}`, + response, + ); + } else if (statusCode >= 500) { + throw createRestError( + `Pagination failed with server error statusCode ${response.status}`, + response, + ); + } else { + throw createRestError( + `Pagination failed with unexpected statusCode ${response.status}`, + response, + ); + } } } diff --git a/packages/typespec-ts/test/modularIntegration/generated/azure/core/lro/standard/generated/src/api/pollingHelpers.ts b/packages/typespec-ts/test/modularIntegration/generated/azure/resource-manager/models/resources/src/static-helpers/pollingHelpers.ts similarity index 75% rename from packages/typespec-ts/test/modularIntegration/generated/azure/core/lro/standard/generated/src/api/pollingHelpers.ts rename to packages/typespec-ts/test/modularIntegration/generated/azure/resource-manager/models/resources/src/static-helpers/pollingHelpers.ts index f3bd59e41b..7388775c58 100644 --- a/packages/typespec-ts/test/modularIntegration/generated/azure/core/lro/standard/generated/src/api/pollingHelpers.ts +++ b/packages/typespec-ts/test/modularIntegration/generated/azure/resource-manager/models/resources/src/static-helpers/pollingHelpers.ts @@ -1,6 +1,9 @@ // Copyright (c) Microsoft Corporation. // Licensed under the MIT license. +// Copyright (c) Microsoft Corporation. +// Licensed under the MIT license. + import { PollerLike, OperationState, @@ -12,11 +15,11 @@ import { import { Client, + OperationOptions, PathUncheckedResponse, createRestError, } from "@azure-rest/core-client"; import { AbortSignalLike } from "@azure/abort-controller"; -import { isUnexpected } from "../rest/index.js"; export interface GetLongRunningPollerOptions { /** Delay to wait until next poll, in milliseconds. */ @@ -125,9 +128,7 @@ export function getLongRunningPoller< function getLroResponse( response: TResponse, ): OperationResponse { - if (isUnexpected(response as PathUncheckedResponse)) { - throw createRestError(response); - } + checkResponse(response); return { flatResponse: response, rawResponse: { @@ -137,3 +138,43 @@ function getLroResponse( }, }; } + +/** + * Checks if a request failed + */ +function checkResponse(response: PathUncheckedResponse): void { + const statusCode = Number(response.status); + if (statusCode < 200 || statusCode >= 300) { + const errorMessage = response.body?.message ?? ""; + if (statusCode >= 400 && statusCode < 500) { + throw createRestError( + `Poller failed with client error statusCode ${response.status}\n ${errorMessage}`, + response, + ); + } else if (statusCode >= 500) { + throw createRestError( + `Poller failed with server error statusCode ${response.status}\n ${errorMessage}`, + response, + ); + } else { + throw createRestError( + `Poller failed with unexpected statusCode ${response.status}\n ${errorMessage}`, + response, + ); + } + } +} + +export interface RestorePollerOptions< + TResult, + TResponse extends PathUncheckedResponse = PathUncheckedResponse, +> extends OperationOptions { + /** Delay to wait until next poll, in milliseconds. */ + updateIntervalInMs?: number; + /** + * The signal which can be used to abort requests. + */ + abortSignal?: AbortSignalLike; + /** Deserialization function for raw response body */ + processResponseBody?: (result: TResponse) => Promise; +} diff --git a/packages/typespec-ts/test/modularIntegration/generated/payload/pageable/src/api/operations.ts b/packages/typespec-ts/test/modularIntegration/generated/payload/pageable/src/api/operations.ts index 322a397206..e840405b81 100644 --- a/packages/typespec-ts/test/modularIntegration/generated/payload/pageable/src/api/operations.ts +++ b/packages/typespec-ts/test/modularIntegration/generated/payload/pageable/src/api/operations.ts @@ -2,14 +2,16 @@ // Licensed under the MIT license. import { _PagedUser, User } from "../models/models.js"; -import { PagedAsyncIterableIterator } from "../models/pagingTypes.js"; -import { buildPagedAsyncIterator } from "./pagingHelpers.js"; import { PageableContext as Client, List200Response } from "../rest/index.js"; import { StreamableMethod, operationOptionsToRequestParameters, createRestError, } from "@azure-rest/core-client"; +import { + PagedAsyncIterableIterator, + buildPagedAsyncIterator, +} from "../static-helpers/pagingHelpers.js"; import { ListOptionalParams } from "../models/options.js"; export function _listSend( diff --git a/packages/typespec-ts/test/modularIntegration/generated/payload/pageable/src/index.ts b/packages/typespec-ts/test/modularIntegration/generated/payload/pageable/src/index.ts index 6c2d8402a2..cfdd203110 100644 --- a/packages/typespec-ts/test/modularIntegration/generated/payload/pageable/src/index.ts +++ b/packages/typespec-ts/test/modularIntegration/generated/payload/pageable/src/index.ts @@ -1,14 +1,15 @@ // Copyright (c) Microsoft Corporation. // Licensed under the MIT license. +import { + PageSettings, + ContinuablePage, + PagedAsyncIterableIterator, +} from "./static-helpers/pagingHelpers.js"; + export { PageableClient, PageableClientOptionalParams, } from "./pageableClient.js"; -export { - User, - ListOptionalParams, - PageSettings, - ContinuablePage, - PagedAsyncIterableIterator, -} from "./models/index.js"; +export { User, ListOptionalParams } from "./models/index.js"; +export { PageSettings, ContinuablePage, PagedAsyncIterableIterator }; diff --git a/packages/typespec-ts/test/modularIntegration/generated/payload/pageable/src/models/index.ts b/packages/typespec-ts/test/modularIntegration/generated/payload/pageable/src/models/index.ts index 9cbfa6c472..9acd55b6e3 100644 --- a/packages/typespec-ts/test/modularIntegration/generated/payload/pageable/src/models/index.ts +++ b/packages/typespec-ts/test/modularIntegration/generated/payload/pageable/src/models/index.ts @@ -3,8 +3,3 @@ export { User } from "./models.js"; export { ListOptionalParams } from "./options.js"; -export { - PageSettings, - ContinuablePage, - PagedAsyncIterableIterator, -} from "./pagingTypes.js"; diff --git a/packages/typespec-ts/test/modularIntegration/generated/payload/pageable/src/models/pagingTypes.ts b/packages/typespec-ts/test/modularIntegration/generated/payload/pageable/src/models/pagingTypes.ts deleted file mode 100644 index f734b48e62..0000000000 --- a/packages/typespec-ts/test/modularIntegration/generated/payload/pageable/src/models/pagingTypes.ts +++ /dev/null @@ -1,89 +0,0 @@ -// Copyright (c) Microsoft Corporation. -// Licensed under the MIT license. - -/** - * Options for the byPage method - */ -export interface PageSettings { - /** - * A reference to a specific page to start iterating from. - */ - continuationToken?: string; -} - -/** - * An interface that describes a page of results. - */ -export type ContinuablePage = TPage & { - /** - * 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 extends PageSettings = PageSettings, -> { - /** - * The next method, part of the iteration protocol - */ - next(): Promise>; - /** - * 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>; -} - -/** - * An interface that describes how to communicate with the service. - */ -export interface PagedResult< - TElement, - TPage = TElement[], - TPageSettings extends PageSettings = PageSettings, -> { - /** - * Link to the first page of results. - */ - firstPageLink?: string; - /** - * A method that returns a page of results. - */ - getPage: ( - pageLink?: string, - ) => Promise<{ page: TPage; nextPageLink?: string } | undefined>; - /** - * a function to implement the `byPage` method on the paged async iterator. - */ - byPage?: ( - settings?: TPageSettings, - ) => AsyncIterableIterator>; - - /** - * A function to extract elements from a page. - */ - toElements?: (page: TPage) => TElement[]; -} - -/** - * Options for the paging helper - */ -export interface BuildPagedAsyncIteratorOptions { - itemName?: string; - nextLinkName?: string; -} diff --git a/packages/typespec-ts/test/modularIntegration/generated/payload/pageable/src/pageableClient.ts b/packages/typespec-ts/test/modularIntegration/generated/payload/pageable/src/pageableClient.ts index 911d23713c..d62dbe2d6a 100644 --- a/packages/typespec-ts/test/modularIntegration/generated/payload/pageable/src/pageableClient.ts +++ b/packages/typespec-ts/test/modularIntegration/generated/payload/pageable/src/pageableClient.ts @@ -4,13 +4,13 @@ import { Pipeline } from "@azure/core-rest-pipeline"; import { User } from "./models/models.js"; import { ListOptionalParams } from "./models/options.js"; -import { PagedAsyncIterableIterator } from "./models/pagingTypes.js"; import { list, createPageable, PageableClientOptionalParams, PageableContext, } from "./api/index.js"; +import { PagedAsyncIterableIterator } from "./static-helpers/pagingHelpers.js"; export { PageableClientOptionalParams } from "./api/pageableContext.js"; diff --git a/packages/typespec-ts/test/modularIntegration/generated/payload/pageable/src/api/pagingHelpers.ts b/packages/typespec-ts/test/modularIntegration/generated/payload/pageable/src/static-helpers/pagingHelpers.ts similarity index 64% rename from packages/typespec-ts/test/modularIntegration/generated/payload/pageable/src/api/pagingHelpers.ts rename to packages/typespec-ts/test/modularIntegration/generated/payload/pageable/src/static-helpers/pagingHelpers.ts index 5b00e0b6a2..45360a0c0c 100644 --- a/packages/typespec-ts/test/modularIntegration/generated/payload/pageable/src/api/pagingHelpers.ts +++ b/packages/typespec-ts/test/modularIntegration/generated/payload/pageable/src/static-helpers/pagingHelpers.ts @@ -1,19 +1,105 @@ // Copyright (c) Microsoft Corporation. // Licensed under the MIT license. +// Copyright (c) Microsoft Corporation. +// Licensed under the MIT license. + import { Client, createRestError, PathUncheckedResponse, } from "@azure-rest/core-client"; import { RestError } from "@azure/core-rest-pipeline"; -import { - BuildPagedAsyncIteratorOptions, - ContinuablePage, - PageSettings, - PagedAsyncIterableIterator, - PagedResult, -} from "../models/pagingTypes.js"; + +// Copyright (c) Microsoft Corporation. +// Licensed under the MIT license. + +/** + * Options for the byPage method + */ +export interface PageSettings { + /** + * A reference to a specific page to start iterating from. + */ + continuationToken?: string; +} + +/** + * An interface that describes a page of results. + */ +export type ContinuablePage = TPage & { + /** + * 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 extends PageSettings = PageSettings, +> { + /** + * The next method, part of the iteration protocol + */ + next(): Promise>; + /** + * 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>; +} + +/** + * An interface that describes how to communicate with the service. + */ +export interface PagedResult< + TElement, + TPage = TElement[], + TPageSettings extends PageSettings = PageSettings, +> { + /** + * Link to the first page of results. + */ + firstPageLink?: string; + /** + * A method that returns a page of results. + */ + getPage: ( + pageLink?: string, + ) => Promise<{ page: TPage; nextPageLink?: string } | undefined>; + /** + * a function to implement the `byPage` method on the paged async iterator. + */ + byPage?: ( + settings?: TPageSettings, + ) => AsyncIterableIterator>; + + /** + * A function to extract elements from a page. + */ + toElements?: (page: TPage) => TElement[]; +} + +/** + * Options for the paging helper + */ +export interface BuildPagedAsyncIteratorOptions { + itemName?: string; + nextLinkName?: string; +} /** * Helper to paginate results in a generic way and return a PagedAsyncIterableIterator @@ -181,22 +267,23 @@ function getElements(body: unknown, itemName: string): T[] { * Checks if a request failed */ function checkPagingRequest(response: PathUncheckedResponse): void { - const Http2xxStatusCodes = [ - "200", - "201", - "202", - "203", - "204", - "205", - "206", - "207", - "208", - "226", - ]; - if (!Http2xxStatusCodes.includes(response.status)) { - throw createRestError( - `Pagination failed with unexpected statusCode ${response.status}`, - response, - ); + const statusCode = Number(response.status); + if (statusCode < 200 || statusCode >= 300) { + if (statusCode >= 400 && statusCode < 500) { + throw createRestError( + `Pagination failed with client error statusCode ${response.status}`, + response, + ); + } else if (statusCode >= 500) { + throw createRestError( + `Pagination failed with server error statusCode ${response.status}`, + response, + ); + } else { + throw createRestError( + `Pagination failed with unexpected statusCode ${response.status}`, + response, + ); + } } }