@azure-rest/agrifood-farming 1.0.0-beta.1 → 1.0.0-beta.2
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.
- package/README.md +42 -50
- package/dist/index.js +424 -5
- package/dist/index.js.map +1 -1
- package/dist-esm/src/clientDefinitions.js +4 -0
- package/dist-esm/src/clientDefinitions.js.map +1 -0
- package/dist-esm/src/farmBeats.js +18 -4
- package/dist-esm/src/farmBeats.js.map +1 -1
- package/dist-esm/src/index.js +7 -1
- package/dist-esm/src/index.js.map +1 -1
- package/dist-esm/src/isUnexpected.js +269 -0
- package/dist-esm/src/isUnexpected.js.map +1 -0
- package/dist-esm/src/models.js.map +1 -1
- package/dist-esm/src/outputModels.js +4 -0
- package/dist-esm/src/outputModels.js.map +1 -0
- package/dist-esm/src/paginateHelper.js +70 -0
- package/dist-esm/src/paginateHelper.js.map +1 -0
- package/dist-esm/src/parameters.js.map +1 -1
- package/dist-esm/src/pollingHelper.js +50 -0
- package/dist-esm/src/pollingHelper.js.map +1 -0
- package/dist-esm/src/responses.js.map +1 -1
- package/dist-esm/src/serializeHelper.js +13 -0
- package/dist-esm/src/serializeHelper.js.map +1 -0
- package/package.json +30 -29
- package/types/agrifood-farming-rest.d.ts +10281 -2670
- package/CHANGELOG.md +0 -5
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
// Copyright (c) Microsoft Corporation.
|
|
2
|
+
// Licensed under the MIT license.
|
|
3
|
+
import { getPagedAsyncIterator } from "@azure/core-paging";
|
|
4
|
+
import { createRestError } from "@azure-rest/core-client";
|
|
5
|
+
/**
|
|
6
|
+
* Helper to paginate results from an initial response that follows the specification of Autorest `x-ms-pageable` extension
|
|
7
|
+
* @param client - Client to use for sending the next page requests
|
|
8
|
+
* @param initialResponse - Initial response containing the nextLink and current page of elements
|
|
9
|
+
* @param customGetPage - Optional - Function to define how to extract the page and next link to be used to paginate the results
|
|
10
|
+
* @returns - PagedAsyncIterableIterator to iterate the elements
|
|
11
|
+
*/
|
|
12
|
+
export function paginate(client, initialResponse, options = {}) {
|
|
13
|
+
let firstRun = true;
|
|
14
|
+
const itemName = "value";
|
|
15
|
+
const nextLinkName = "nextLink";
|
|
16
|
+
const { customGetPage } = options;
|
|
17
|
+
const pagedResult = {
|
|
18
|
+
firstPageLink: "",
|
|
19
|
+
getPage: typeof customGetPage === "function"
|
|
20
|
+
? customGetPage
|
|
21
|
+
: async (pageLink) => {
|
|
22
|
+
const result = firstRun ? initialResponse : await client.pathUnchecked(pageLink).get();
|
|
23
|
+
firstRun = false;
|
|
24
|
+
checkPagingRequest(result);
|
|
25
|
+
const nextLink = getNextLink(result.body, nextLinkName);
|
|
26
|
+
const values = getElements(result.body, itemName);
|
|
27
|
+
return {
|
|
28
|
+
page: values,
|
|
29
|
+
nextPageLink: nextLink,
|
|
30
|
+
};
|
|
31
|
+
},
|
|
32
|
+
};
|
|
33
|
+
return getPagedAsyncIterator(pagedResult);
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Gets for the value of nextLink in the body
|
|
37
|
+
*/
|
|
38
|
+
function getNextLink(body, nextLinkName) {
|
|
39
|
+
if (!nextLinkName) {
|
|
40
|
+
return undefined;
|
|
41
|
+
}
|
|
42
|
+
const nextLink = body[nextLinkName];
|
|
43
|
+
if (typeof nextLink !== "string" && typeof nextLink !== "undefined") {
|
|
44
|
+
throw new Error(`Body Property ${nextLinkName} should be a string or undefined`);
|
|
45
|
+
}
|
|
46
|
+
return nextLink;
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Gets the elements of the current request in the body.
|
|
50
|
+
*/
|
|
51
|
+
function getElements(body, itemName) {
|
|
52
|
+
const value = body[itemName];
|
|
53
|
+
// value has to be an array according to the x-ms-pageable extension.
|
|
54
|
+
// The fact that this must be an array is used above to calculate the
|
|
55
|
+
// type of elements in the page in PaginateReturn
|
|
56
|
+
if (!Array.isArray(value)) {
|
|
57
|
+
throw new Error(`Couldn't paginate response\n Body doesn't contain an array property with name: ${itemName}`);
|
|
58
|
+
}
|
|
59
|
+
return value !== null && value !== void 0 ? value : [];
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* Checks if a request failed
|
|
63
|
+
*/
|
|
64
|
+
function checkPagingRequest(response) {
|
|
65
|
+
const Http2xxStatusCodes = ["200", "201", "202", "203", "204", "205", "206", "207", "208", "226"];
|
|
66
|
+
if (!Http2xxStatusCodes.includes(response.status)) {
|
|
67
|
+
throw createRestError(`Pagination failed with unexpected statusCode ${response.status}`, response);
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
//# sourceMappingURL=paginateHelper.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"paginateHelper.js","sourceRoot":"","sources":["../../src/paginateHelper.ts"],"names":[],"mappings":"AAAA,uCAAuC;AACvC,kCAAkC;AAElC,OAAO,EAAE,qBAAqB,EAA2C,MAAM,oBAAoB,CAAC;AACpG,OAAO,EAAU,eAAe,EAAyB,MAAM,yBAAyB,CAAC;AAyCzF;;;;;;GAMG;AACH,MAAM,UAAU,QAAQ,CACtB,MAAc,EACd,eAA0B,EAC1B,UAAoC,EAAE;IAItC,IAAI,QAAQ,GAAG,IAAI,CAAC;IACpB,MAAM,QAAQ,GAAG,OAAO,CAAC;IACzB,MAAM,YAAY,GAAG,UAAU,CAAC;IAChC,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,EAAE,QAAgB,EAAE,EAAE;gBACzB,MAAM,MAAM,GAAG,QAAQ,CAAC,CAAC,CAAC,eAAe,CAAC,CAAC,CAAC,MAAM,MAAM,CAAC,aAAa,CAAC,QAAQ,CAAC,CAAC,GAAG,EAAE,CAAC;gBACvF,QAAQ,GAAG,KAAK,CAA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AE,KAAK,EAAE,KAAK,EAAE,KAAK,CAAC,CAAC;IAClG,IAAI,CAAC,kBAAkB,CAAC,QAAQ,CAAC,QAAQ,CAAC,MAAM,CAAC,EAAE;QACjD,MAAM,eAAe,CACnB,gDAAgD,QAAQ,CAAC,MAAM,EAAE,EACjE,QAAQ,CACT,CAAC;KACH;AACH,CAAC","sourcesContent":["// Copyright (c) Microsoft Corporation.\n// Licensed under the MIT license.\n\nimport { getPagedAsyncIterator, PagedAsyncIterableIterator, PagedResult } from \"@azure/core-paging\";\nimport { Client, createRestError, PathUncheckedResponse } 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 const itemName = \"value\";\n const nextLinkName = \"nextLink\";\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"]}
|