@azure-rest/batch 1.0.0-beta.2 → 1.0.0-beta.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (152) hide show
  1. package/LICENSE +4 -4
  2. package/README.md +4 -4
  3. package/dist/browser/batchClient.js +17 -22
  4. package/dist/browser/batchClient.js.map +1 -1
  5. package/dist/browser/clientDefinitions.d.ts +42 -2
  6. package/dist/browser/clientDefinitions.js.map +1 -1
  7. package/dist/browser/index.d.ts +0 -1
  8. package/dist/browser/index.js +0 -1
  9. package/dist/browser/index.js.map +1 -1
  10. package/dist/browser/isUnexpected.d.ts +6 -1
  11. package/dist/browser/isUnexpected.js +11 -5
  12. package/dist/browser/isUnexpected.js.map +1 -1
  13. package/dist/browser/models.d.ts +199 -114
  14. package/dist/browser/models.js.map +1 -1
  15. package/dist/browser/outputModels.d.ts +196 -93
  16. package/dist/browser/outputModels.js.map +1 -1
  17. package/dist/browser/paginateHelper.d.ts +27 -2
  18. package/dist/browser/paginateHelper.js +69 -2
  19. package/dist/browser/paginateHelper.js.map +1 -1
  20. package/dist/browser/parameters.d.ts +444 -58
  21. package/dist/browser/parameters.js.map +1 -1
  22. package/dist/browser/responses.d.ts +113 -2
  23. package/dist/browser/responses.js.map +1 -1
  24. package/dist/commonjs/batchClient.js +17 -22
  25. package/dist/commonjs/batchClient.js.map +1 -1
  26. package/dist/commonjs/clientDefinitions.d.ts +42 -2
  27. package/dist/commonjs/clientDefinitions.js.map +1 -1
  28. package/dist/commonjs/credentials/batchSharedKeyCredentials.js +1 -2
  29. package/dist/commonjs/credentials/batchSharedKeyCredentials.js.map +1 -1
  30. package/dist/commonjs/index.d.ts +0 -1
  31. package/dist/commonjs/index.js +0 -1
  32. package/dist/commonjs/index.js.map +1 -1
  33. package/dist/commonjs/isUnexpected.d.ts +6 -1
  34. package/dist/commonjs/isUnexpected.js +11 -5
  35. package/dist/commonjs/isUnexpected.js.map +1 -1
  36. package/dist/commonjs/models.d.ts +199 -114
  37. package/dist/commonjs/models.js.map +1 -1
  38. package/dist/commonjs/outputModels.d.ts +196 -93
  39. package/dist/commonjs/outputModels.js.map +1 -1
  40. package/dist/commonjs/paginateHelper.d.ts +27 -2
  41. package/dist/commonjs/paginateHelper.js +70 -3
  42. package/dist/commonjs/paginateHelper.js.map +1 -1
  43. package/dist/commonjs/parameters.d.ts +444 -58
  44. package/dist/commonjs/parameters.js.map +1 -1
  45. package/dist/commonjs/responses.d.ts +113 -2
  46. package/dist/commonjs/responses.js.map +1 -1
  47. package/dist/commonjs/tsdoc-metadata.json +11 -11
  48. package/dist/esm/batchClient.js +17 -22
  49. package/dist/esm/batchClient.js.map +1 -1
  50. package/dist/esm/clientDefinitions.d.ts +42 -2
  51. package/dist/esm/clientDefinitions.js.map +1 -1
  52. package/dist/esm/credentials/batchSharedKeyCredentials.js +1 -2
  53. package/dist/esm/credentials/batchSharedKeyCredentials.js.map +1 -1
  54. package/dist/esm/index.d.ts +0 -1
  55. package/dist/esm/index.js +0 -1
  56. package/dist/esm/index.js.map +1 -1
  57. package/dist/esm/isUnexpected.d.ts +6 -1
  58. package/dist/esm/isUnexpected.js +11 -5
  59. package/dist/esm/isUnexpected.js.map +1 -1
  60. package/dist/esm/models.d.ts +199 -114
  61. package/dist/esm/models.js.map +1 -1
  62. package/dist/esm/outputModels.d.ts +196 -93
  63. package/dist/esm/outputModels.js.map +1 -1
  64. package/dist/esm/paginateHelper.d.ts +27 -2
  65. package/dist/esm/paginateHelper.js +69 -2
  66. package/dist/esm/paginateHelper.js.map +1 -1
  67. package/dist/esm/parameters.d.ts +444 -58
  68. package/dist/esm/parameters.js.map +1 -1
  69. package/dist/esm/responses.d.ts +113 -2
  70. package/dist/esm/responses.js.map +1 -1
  71. package/dist/react-native/batchClient.js +17 -22
  72. package/dist/react-native/batchClient.js.map +1 -1
  73. package/dist/react-native/clientDefinitions.d.ts +42 -2
  74. package/dist/react-native/clientDefinitions.js.map +1 -1
  75. package/dist/react-native/index.d.ts +0 -1
  76. package/dist/react-native/index.js +0 -1
  77. package/dist/react-native/index.js.map +1 -1
  78. package/dist/react-native/isUnexpected.d.ts +6 -1
  79. package/dist/react-native/isUnexpected.js +11 -5
  80. package/dist/react-native/isUnexpected.js.map +1 -1
  81. package/dist/react-native/models.d.ts +199 -114
  82. package/dist/react-native/models.js.map +1 -1
  83. package/dist/react-native/outputModels.d.ts +196 -93
  84. package/dist/react-native/outputModels.js.map +1 -1
  85. package/dist/react-native/paginateHelper.d.ts +27 -2
  86. package/dist/react-native/paginateHelper.js +69 -2
  87. package/dist/react-native/paginateHelper.js.map +1 -1
  88. package/dist/react-native/parameters.d.ts +444 -58
  89. package/dist/react-native/parameters.js.map +1 -1
  90. package/dist/react-native/responses.d.ts +113 -2
  91. package/dist/react-native/responses.js.map +1 -1
  92. package/package.json +26 -36
  93. package/dist/browser/batchClient.d.ts.map +0 -1
  94. package/dist/browser/clientDefinitions.d.ts.map +0 -1
  95. package/dist/browser/credentials/batchSharedKeyCredentials-browser.d.mts.map +0 -1
  96. package/dist/browser/index.d.ts.map +0 -1
  97. package/dist/browser/isUnexpected.d.ts.map +0 -1
  98. package/dist/browser/logger.d.ts.map +0 -1
  99. package/dist/browser/models.d.ts.map +0 -1
  100. package/dist/browser/outputModels.d.ts.map +0 -1
  101. package/dist/browser/paginateHelper.d.ts.map +0 -1
  102. package/dist/browser/parameters.d.ts.map +0 -1
  103. package/dist/browser/replacePoolPropertiesPolicy.d.ts +0 -11
  104. package/dist/browser/replacePoolPropertiesPolicy.d.ts.map +0 -1
  105. package/dist/browser/replacePoolPropertiesPolicy.js +0 -33
  106. package/dist/browser/replacePoolPropertiesPolicy.js.map +0 -1
  107. package/dist/browser/responses.d.ts.map +0 -1
  108. package/dist/commonjs/batchClient.d.ts.map +0 -1
  109. package/dist/commonjs/clientDefinitions.d.ts.map +0 -1
  110. package/dist/commonjs/credentials/batchSharedKeyCredentials.d.ts.map +0 -1
  111. package/dist/commonjs/index.d.ts.map +0 -1
  112. package/dist/commonjs/isUnexpected.d.ts.map +0 -1
  113. package/dist/commonjs/logger.d.ts.map +0 -1
  114. package/dist/commonjs/models.d.ts.map +0 -1
  115. package/dist/commonjs/outputModels.d.ts.map +0 -1
  116. package/dist/commonjs/paginateHelper.d.ts.map +0 -1
  117. package/dist/commonjs/parameters.d.ts.map +0 -1
  118. package/dist/commonjs/replacePoolPropertiesPolicy.d.ts +0 -11
  119. package/dist/commonjs/replacePoolPropertiesPolicy.d.ts.map +0 -1
  120. package/dist/commonjs/replacePoolPropertiesPolicy.js +0 -36
  121. package/dist/commonjs/replacePoolPropertiesPolicy.js.map +0 -1
  122. package/dist/commonjs/responses.d.ts.map +0 -1
  123. package/dist/esm/batchClient.d.ts.map +0 -1
  124. package/dist/esm/clientDefinitions.d.ts.map +0 -1
  125. package/dist/esm/credentials/batchSharedKeyCredentials.d.ts.map +0 -1
  126. package/dist/esm/index.d.ts.map +0 -1
  127. package/dist/esm/isUnexpected.d.ts.map +0 -1
  128. package/dist/esm/logger.d.ts.map +0 -1
  129. package/dist/esm/models.d.ts.map +0 -1
  130. package/dist/esm/outputModels.d.ts.map +0 -1
  131. package/dist/esm/paginateHelper.d.ts.map +0 -1
  132. package/dist/esm/parameters.d.ts.map +0 -1
  133. package/dist/esm/replacePoolPropertiesPolicy.d.ts +0 -11
  134. package/dist/esm/replacePoolPropertiesPolicy.d.ts.map +0 -1
  135. package/dist/esm/replacePoolPropertiesPolicy.js +0 -33
  136. package/dist/esm/replacePoolPropertiesPolicy.js.map +0 -1
  137. package/dist/esm/responses.d.ts.map +0 -1
  138. package/dist/react-native/batchClient.d.ts.map +0 -1
  139. package/dist/react-native/clientDefinitions.d.ts.map +0 -1
  140. package/dist/react-native/credentials/batchSharedKeyCredentials-react-native.d.mts.map +0 -1
  141. package/dist/react-native/index.d.ts.map +0 -1
  142. package/dist/react-native/isUnexpected.d.ts.map +0 -1
  143. package/dist/react-native/logger.d.ts.map +0 -1
  144. package/dist/react-native/models.d.ts.map +0 -1
  145. package/dist/react-native/outputModels.d.ts.map +0 -1
  146. package/dist/react-native/paginateHelper.d.ts.map +0 -1
  147. package/dist/react-native/parameters.d.ts.map +0 -1
  148. package/dist/react-native/replacePoolPropertiesPolicy.d.ts +0 -11
  149. package/dist/react-native/replacePoolPropertiesPolicy.d.ts.map +0 -1
  150. package/dist/react-native/replacePoolPropertiesPolicy.js +0 -33
  151. package/dist/react-native/replacePoolPropertiesPolicy.js.map +0 -1
  152. package/dist/react-native/responses.d.ts.map +0 -1
@@ -1,5 +1,30 @@
1
- import type { PagedAsyncIterableIterator } from "@azure/core-paging";
2
1
  import type { Client, PathUncheckedResponse } from "@azure-rest/core-client";
2
+ /**
3
+ * An interface that tracks the settings for paged iteration
4
+ */
5
+ export interface PageSettings {
6
+ /**
7
+ * The token that keeps track of where to continue the iterator
8
+ */
9
+ continuationToken?: string;
10
+ }
11
+ /**
12
+ * An interface that allows async iterable iteration both to completion and by page.
13
+ */
14
+ export interface PagedAsyncIterableIterator<TElement, TPage = TElement[], TPageSettings = PageSettings> {
15
+ /**
16
+ * The next method, part of the iteration protocol
17
+ */
18
+ next(): Promise<IteratorResult<TElement>>;
19
+ /**
20
+ * The connection to the async iterator, part of the iteration protocol
21
+ */
22
+ [Symbol.asyncIterator](): PagedAsyncIterableIterator<TElement, TPage, TPageSettings>;
23
+ /**
24
+ * Return an AsyncIterableIterator that works a page at a time
25
+ */
26
+ byPage: (settings?: TPageSettings) => AsyncIterableIterator<TPage>;
27
+ }
3
28
  /**
4
29
  * Helper type to extract the type of an array
5
30
  */
@@ -7,7 +32,7 @@ export type GetArrayType<T> = T extends Array<infer TData> ? TData : never;
7
32
  /**
8
33
  * The type of a custom function that defines how to get a page and a link to the next one if any.
9
34
  */
10
- export type GetPage<TPage> = (pageLink: string, maxPageSize?: number) => Promise<{
35
+ export type GetPage<TPage> = (pageLink: string) => Promise<{
11
36
  page: TPage;
12
37
  nextPageLink?: string;
13
38
  }>;
@@ -3,8 +3,75 @@
3
3
  // Licensed under the MIT License.
4
4
  Object.defineProperty(exports, "__esModule", { value: true });
5
5
  exports.paginate = paginate;
6
- const core_paging_1 = require("@azure/core-paging");
7
6
  const core_client_1 = require("@azure-rest/core-client");
7
+ /**
8
+ * returns an async iterator that iterates over results. It also has a `byPage`
9
+ * method that returns pages of items at once.
10
+ *
11
+ * @param pagedResult - an object that specifies how to get pages.
12
+ * @returns a paged async iterator that iterates over results.
13
+ */
14
+ function getPagedAsyncIterator(pagedResult) {
15
+ const iter = getItemAsyncIterator(pagedResult);
16
+ return {
17
+ next() {
18
+ return iter.next();
19
+ },
20
+ [Symbol.asyncIterator]() {
21
+ return this;
22
+ },
23
+ byPage: pagedResult?.byPage ??
24
+ ((settings) => {
25
+ const { continuationToken } = settings ?? {};
26
+ return getPageAsyncIterator(pagedResult, {
27
+ pageLink: continuationToken,
28
+ });
29
+ }),
30
+ };
31
+ }
32
+ async function* getItemAsyncIterator(pagedResult) {
33
+ const pages = getPageAsyncIterator(pagedResult);
34
+ const firstVal = await pages.next();
35
+ // if the result does not have an array shape, i.e. TPage = TElement, then we return it as is
36
+ if (!Array.isArray(firstVal.value)) {
37
+ // can extract elements from this page
38
+ const { toElements } = pagedResult;
39
+ if (toElements) {
40
+ yield* toElements(firstVal.value);
41
+ for await (const page of pages) {
42
+ yield* toElements(page);
43
+ }
44
+ }
45
+ else {
46
+ yield firstVal.value;
47
+ // `pages` is of type `AsyncIterableIterator<TPage>` but TPage = TElement in this case
48
+ yield* pages;
49
+ }
50
+ }
51
+ else {
52
+ yield* firstVal.value;
53
+ for await (const page of pages) {
54
+ // pages is of type `AsyncIterableIterator<TPage>` so `page` is of type `TPage`. In this branch,
55
+ // it must be the case that `TPage = TElement[]`
56
+ yield* page;
57
+ }
58
+ }
59
+ }
60
+ async function* getPageAsyncIterator(pagedResult, options = {}) {
61
+ const { pageLink } = options;
62
+ let response = await pagedResult.getPage(pageLink ?? pagedResult.firstPageLink);
63
+ if (!response) {
64
+ return;
65
+ }
66
+ yield response.page;
67
+ while (response.nextPageLink) {
68
+ response = await pagedResult.getPage(response.nextPageLink);
69
+ if (!response) {
70
+ return;
71
+ }
72
+ yield response.page;
73
+ }
74
+ }
8
75
  /**
9
76
  * Helper to paginate results from an initial response that follows the specification of Autorest `x-ms-pageable` extension
10
77
  * @param client - Client to use for sending the next page requests
@@ -35,7 +102,7 @@ function paginate(client, initialResponse, options = {}) {
35
102
  };
36
103
  },
37
104
  };
38
- return (0, core_paging_1.getPagedAsyncIterator)(pagedResult);
105
+ return getPagedAsyncIterator(pagedResult);
39
106
  }
40
107
  /**
41
108
  * Gets for the value of nextLink in the body
@@ -61,7 +128,7 @@ function getElements(body, itemName) {
61
128
  if (!Array.isArray(value)) {
62
129
  throw new Error(`Couldn't paginate response\n Body doesn't contain an array property with name: ${itemName}`);
63
130
  }
64
- return value !== null && value !== void 0 ? value : [];
131
+ return value ?? [];
65
132
  }
66
133
  /**
67
134
  * Checks if a request failed
@@ -1 +1 @@
1
- {"version":3,"file":"paginateHelper.js","sourceRoot":"","sources":["../../src/paginateHelper.ts"],"names":[],"mappings":";AAAA,uCAAuC;AACvC,kCAAkC;;AAqDlC,4BAgCC;AAlFD,oDAA2D;AAE3D,yDAA0D;AAyC1D;;;;;;GAMG;AACH,SAAgB,QAAQ,CACtB,MAAc,EACd,eAA0B,EAC1B,UAAoC,EAAE;IAItC,IAAI,QAAQ,GAAG,IAAI,CAAC;IACpB,oFAAoF;IACpF,kDAAkD;IAClD,kBAAkB,CAAC,eAAe,CAAC,CAAC;IACpC,MAAM,EAAE,QAAQ,EAAE,YAAY,EAAE,GAAG,uBAAuB,CAAC,eAAe,CAAC,CAAC;IAC5E,MAAM,EAAE,aAAa,EAAE,GAAG,OAAO,CAAC;IAClC,MAAM,WAAW,GAA4B;QAC3C,aAAa,EAAE,EAAE;QACjB,OAAO,EACL,OAAO,aAAa,KAAK,UAAU;YACjC,CAAC,CAAC,aAAa;YACf,CAAC,CAAC,KAAK,EAA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IAAI,KAAK,CACb,kFAAkF;YAChF,GAAG,SAAS;SACb,CAAC,IAAI,CAAC,MAAM,CAAC,EAAE,CACjB,CAAC;IACJ,CAAC;IAED,OAAO,EAAE,QAAQ,EAAE,YAAY,EAAE,CAAC;AACpC,CAAC","sourcesContent":["// Copyright (c) Microsoft Corporation.\n// Licensed under the MIT License.\n\nimport type { PagedAsyncIterableIterator, PagedResult } from \"@azure/core-paging\";\nimport { getPagedAsyncIterator } from \"@azure/core-paging\";\nimport type { Client, PathUncheckedResponse } from \"@azure-rest/core-client\";\nimport { createRestError } 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 */\nexport type PaginateReturn<TResult> = TResult extends {\n body: { value?: infer TPage };\n}\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 */\nexport 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 // 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 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 ? initialResponse : 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(`Body Property ${nextLinkName} should be a string or undefined`);\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 = [\"200\", \"201\", \"202\", \"203\", \"204\", \"205\", \"206\", \"207\", \"208\", \"226\"];\n if (!Http2xxStatusCodes.includes(response.status)) {\n throw createRestError(\n `Pagination failed with unexpected statusCode ${response.status}`,\n response,\n );\n }\n}\n\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([\"nextLink\", \"odata.nextLink\"]);\n\n // Build a set with the passed custom set of itemNames\n const itemNames = new Set([\"value\"]);\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>)[name] 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>)[name] 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"]}
1
+ {"version":3,"file":"paginateHelper.js","sourceRoot":"","sources":["../../src/paginateHelper.ts"],"names":[],"mappings":";AAAA,uCAAuC;AACvC,kCAAkC;;AA2LlC,4BAgCC;AAxND,yDAA0D;AAE1D;;;;;;GAMG;AACH,SAAS,qBAAqB,CAM5B,WAAqD;IAErD,MAAM,IAAI,GAAG,oBAAoB,CAAwC,WAAW,CAAC,CAAC;IACtF,OAAO;QACL,IAAI;YACF,OAAO,IAAI,CAAC,IAAI,EAAE,CAAC;QACrB,CAAC;QACD,CAAC,MAAM,CAAC,aAAa,CAAC;YACpB,OAAO,IAAI,CAAC;QACd,CAAC;QACD,MAAM,EACJ,WAAW,EAAE,MAAM;YAClB,CAAC,CAAC,QAAuB,EAAE,EAAE;gBAC5B,MAAM,EAAE,iBAAiB,EAAE,GAAG,QAAQ,IAAI,EAAE,CAAC;gBAC7C,OAAO,oBAAoB,CAAC,WAAW,EAAE;oBACvC,QAAQ,EAAE,iBAAiD;iBAC5D,CAAC,CAAC;YACL,CAAC,CAA2E;KAC/E,CAAC;AACJ,CAAC;AAED,KAAK,SAAS,CAAC,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EAAE,gBAAgB,CAAC,CAAC,CAAC;IAE9D,sDAAsD;IACtD,MAAM,SAAS,GAAG,IAAI,GAAG,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC;IAErC,IAAI,YAAgC,CAAC;IACrC,IAAI,QAA4B,CAAC;IAEjC,KAAK,MAAM,IAAI,IAAI,aAAa,EAAE,CAAC;QACjC,MAAM,QAAQ,GAAI,eAAe,CAAC,IAAgC,CAAC,IAAI,CAAW,CAAC;QACnF,IAAI,QAAQ,EAAE,CAAC;YACb,YAAY,GAAG,IAAI,CAAC;YACpB,MAAM;QACR,CAAC;IACH,CAAC;IAED,KAAK,MAAM,IAAI,IAAI,SAAS,EAAE,CAAC;QAC7B,MAAM,IAAI,GAAI,eAAe,CAAC,IAAgC,CAAC,IAAI,CAAW,CAAC;QAC/E,IAAI,IAAI,EAAE,CAAC;YACT,QAAQ,GAAG,IAAI,CAAC;YAChB,MAAM;QACR,CAAC;IACH,CAAC;IAED,IAAI,CAAC,QAAQ,EAAE,CAAC;QACd,MAAM,IAAI,KAAK,CACb,kFAAkF;YAChF,GAAG,SAAS;SACb,CAAC,IAAI,CAAC,MAAM,CAAC,EAAE,CACjB,CAAC;IACJ,CAAC;IAED,OAAO,EAAE,QAAQ,EAAE,YAAY,EAAE,CAAC;AACpC,CAAC","sourcesContent":["// Copyright (c) Microsoft Corporation.\n// Licensed under the MIT License.\n\nimport type { Client, PathUncheckedResponse } from \"@azure-rest/core-client\";\nimport { createRestError } 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: (pageLink: TLink) => 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> = (pageLink: string) => 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 */\nexport type PaginateReturn<TResult> = TResult extends {\n body: { value?: infer TPage };\n}\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 */\nexport 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 // 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 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 ? initialResponse : 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(`Body Property ${nextLinkName} should be a string or undefined`);\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 = [\"200\", \"201\", \"202\", \"203\", \"204\", \"205\", \"206\", \"207\", \"208\", \"226\"];\n if (!Http2xxStatusCodes.includes(response.status)) {\n throw createRestError(\n `Pagination failed with unexpected statusCode ${response.status}`,\n response,\n );\n }\n}\n\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([\"nextLink\", \"odata.nextLink\"]);\n\n // Build a set with the passed custom set of itemNames\n const itemNames = new Set([\"value\"]);\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>)[name] 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>)[name] 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"]}