|
| 1 | +/* |
| 2 | + * Copyright 2024 Palantir Technologies, Inc. All rights reserved. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +import { outdent } from "outdent"; |
| 18 | + |
| 19 | +export const loadObjectPageGuide = outdent` |
| 20 | + import { {{objectType}} } from "{{{packageName}}}"; |
| 21 | + import { isOk, Osdk, PageResult, Result } from "@osdk/client"; |
| 22 | + const firstPage: Result<PageResult<Osdk.Instance<{{objectType}}>>> |
| 23 | + = await client({{objectType}}).fetchPageWithErrors({ $pageSize: 30 }); |
| 24 | + if (isOk(firstPage)) { |
| 25 | + const secondPage: Result<PageResult<Osdk.Instance<{{objectType}}, never, "{{titleProperty}}">>> |
| 26 | + // You can also down select properties to only get the properties you need from the object |
| 27 | + = await client({{objectType}}).fetchPageWithErrors({ $select: ["{{titleProperty}}"], $pageSize: 30, $nextPageToken: firstPage.value.nextPageToken }); |
| 28 | + const objects = isOk(secondPage) ? [...firstPage.value.data, ...secondPage.value.data] : firstPage.value.data; |
| 29 | + const object = objects[0]; |
| 30 | + } |
| 31 | + // To fetch a page without a result wrapper, use fetchPage with a try/catch instead |
| 32 | + try { |
| 33 | + const firstPage: PageResult<Osdk.Instance<{{objectType}}>> |
| 34 | + = await client({{objectType}}).fetchPage({ $pageSize: 30 }); |
| 35 | + const secondPage: PageResult<Osdk.Instance<{{objectType}}>> |
| 36 | + = await client({{objectType}}).fetchPage({ $pageSize: 30, $nextPageToken: firstPage.nextPageToken }); |
| 37 | + const objects = [...firstPage.data, ...secondPage.data]; |
| 38 | + const object = objects[0]; |
| 39 | + } |
| 40 | + catch (e) { |
| 41 | + console.error(e); |
| 42 | + } |
| 43 | +`; |
0 commit comments