rsnext/examples/with-turbopack/lib/getCategories.ts
Jared Palmer 24787089cf
Add turbopack example (#41789)
<!--
Thanks for opening a PR! Your contribution is much appreciated.
To make sure your PR is handled as smoothly as possible we request that
you follow the checklist sections below.
Choose the right checklist for the change that you're making:
-->

## Bug

- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Errors have a helpful link attached, see `contributing.md`

## Feature

- [ ] Implements an existing feature request or RFC. Make sure the
feature request has been accepted for implementation before opening a
PR.
- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Documentation added
- [ ] Telemetry added. In case of a feature if it's used or not.
- [ ] Errors have a helpful link attached, see `contributing.md`

## Documentation / Examples

- [x] Make sure the linting passes by running `pnpm lint`
- [x] The "examples guidelines" are followed from [our contributing
doc](https://github.com/vercel/next.js/blob/canary/contributing/examples/adding-examples.md)
2022-10-25 10:21:53 -07:00

67 lines
1.7 KiB
TypeScript

export type PageProps = {
params?: any;
children?: React.ReactNode;
};
export type Category = {
name: string;
slug: string;
count: number;
items: Omit<Category, 'items'>[];
};
export const getCategories = (): Category[] => [
{
name: 'Electronics',
slug: 'electronics',
count: 11,
items: [
{ name: 'Phones', slug: 'phones', count: 4 },
{ name: 'Tablets', slug: 'tablets', count: 5 },
{ name: 'Laptops', slug: 'laptops', count: 2 },
],
},
{
name: 'Clothing',
slug: 'clothing',
count: 12,
items: [
{ name: 'Tops', slug: 'tops', count: 3 },
{ name: 'Shorts', slug: 'shorts', count: 4 },
{ name: 'Shoes', slug: 'shoes', count: 5 },
],
},
{
name: 'Books',
slug: 'books',
count: 10,
items: [
{ name: 'Fiction', slug: 'fiction', count: 5 },
{ name: 'Biography', slug: 'biography', count: 2 },
{ name: 'Education', slug: 'education', count: 3 },
],
},
];
export async function fetchCategoryBySlug(slug: string | undefined) {
// Assuming it always return expected categories
return getCategories().find((category) => category.slug === slug);
}
export async function fetchCategories(): Promise<Category[]> {
return getCategories();
}
async function findSubCategory(
category: Category | undefined,
subCategorySlug: string | undefined,
) {
return category?.items.find((category) => category.slug === subCategorySlug);
}
export async function fetchSubCategory(
categorySlug: string | undefined,
subCategorySlug: string | undefined,
) {
const category = await fetchCategoryBySlug(categorySlug);
return findSubCategory(category, subCategorySlug);
}