This package has been deprecated
Author message:
Please use @azure/core-paging
@azure-rest/core-client-paging
1.0.0-beta.1 • Public • PublishedAzure Rest Core Paging library for JavaScript (Experimental)
This library is primarily intended to be used in code generated byAutoRest andautorest.typescript
. Specifically for rest level clients, as a helper to handle Pageable operations. This package implements support for Autorestx-ms-pageable
specification.
Getting started
Requirements
- Node.js LTS
Installation
This package is primarily used in generated code and not meant to be consumed directly by end users.
Key concepts
paginateResponse
Helper functionPaginate response is a helper function to handle pagination for the user. Given a response that contains a body with a link to the next page and an array with the current page of results, this helper returns a PagedAsyncIterableIterator that can be used to get all the items or page by page.
In order to provide better typings, the library that consumespaginateResponse
can wrap it providing additional types. For example a code generator may consume and export in the following way
Typescript
/** * This is the wrapper function that would be exposed. It is hiding the Pagination Options because it can be * obtained in the case of a generator from the Swagger definition or by a developer context knowledge in case of a * hand written library. */exportfunctionpaginate<TReturnextendsPathUncheckedResponse>(client:Client,initialResponse:TReturn):PagedAsyncIterableIterator<PaginateReturn<TReturn>,PaginateReturn<TReturn>[]>{returnpaginateResponse<PaginateReturn<TReturn>>(client,initialResponse,{// For example these values could come from the swaggeritemName:"items",nextLinkName:"continuationLink",});}// Helper type to extract the type of an arraytypeGetArrayType<T>=TextendsArray<inferTData> ?TData :never;// Helper type to infer the Type of the paged elements from the response type// This type will be generated based on the swagger information for x-ms-pageable// specifically on the itemName property which indicates the property of the response// where the page items are found. The default value is `value`.// This type will allow us to provide strongly typed Iterator based on the response we get as second parameterexporttypePaginateReturn<TResult>=TResultextends{body:{items: inferTPage};} ?GetArrayType<TPage> :Array<unknown>;// Usageconstclient=Client("https://example.org",newDefaultAzureCredentials());constresponse=client.path("/foo").get();constitems=paginate(client,response);forawait(constitemofitems){console.log(item.name);}
JavaScript
/** * This is the wrapper function that would be exposed. It is hiding the Pagination Options because it can be * obtained in the case of a generator from the Swagger definition or by a developer context knowledge in case of a * hand written library. */exportfunctionpaginate(client,initialResponse){returnpaginateResponse(client,initialResponse,{// For example these values could come from the swaggeritemName:"items",nextLinkName:"continuationLink",});}// Usageconstclient=Client("https://example.org",newDefaultAzureCredentials());constresponse=client.path("/foo").get();constitems=paginate(client,response);forawait(constitemofitems){console.log(item.name);}
Examples
Examples can be found in thesamples
folder.
Next steps
You can build and run the tests locally by executingrushx test
. Explore thetest
folder to see advanced usage and behavior of the public classes.
Learn more aboutAutoRest and theautorest.typescript extension for generating a compatible client on top of this package.
Troubleshooting
If you run into issues while using this library, please feel free tofile an issue.
Contributing
If you'd like to contribute to this library, please read thecontributing guide to learn more about how to build and test the code.
Package Sidebar
Install
npm i @azure-rest/core-client-paging
Repository
Weekly Downloads
80
Version
1.0.0-beta.1
License
MIT
Unpacked Size
36.3 kB
Total Files
15