├── .gitignore ├── .vscode ├── extensions.json └── launch.json ├── astro.config.mjs ├── package-lock.json ├── package.json ├── public └── favicon.svg ├── src ├── components │ ├── APICard.tsx │ └── Card.astro ├── content │ ├── config.ts │ └── product │ │ ├── 1.yaml │ │ ├── 2.yaml │ │ ├── 3.yaml │ │ ├── 4.yaml │ │ └── 5.yaml ├── data │ ├── data3.json │ ├── data3.md │ ├── data3.ts │ └── data3_example2.md ├── env.d.ts └── pages │ ├── endpoint.json.ts │ └── index.astro └── tsconfig.json /.gitignore: -------------------------------------------------------------------------------- 1 | # build output 2 | dist/ 3 | # generated types 4 | .astro/ 5 | 6 | # dependencies 7 | node_modules/ 8 | 9 | # logs 10 | npm-debug.log* 11 | yarn-debug.log* 12 | yarn-error.log* 13 | pnpm-debug.log* 14 | 15 | 16 | # environment variables 17 | .env 18 | .env.production 19 | 20 | # macOS-specific files 21 | .DS_Store 22 | -------------------------------------------------------------------------------- /.vscode/extensions.json: -------------------------------------------------------------------------------- 1 | { 2 | "recommendations": ["astro-build.astro-vscode"], 3 | "unwantedRecommendations": [] 4 | } 5 | -------------------------------------------------------------------------------- /.vscode/launch.json: -------------------------------------------------------------------------------- 1 | { 2 | "version": "0.2.0", 3 | "configurations": [ 4 | { 5 | "command": "./node_modules/.bin/astro dev", 6 | "name": "Development server", 7 | "request": "launch", 8 | "type": "node-terminal" 9 | } 10 | ] 11 | } 12 | -------------------------------------------------------------------------------- /astro.config.mjs: -------------------------------------------------------------------------------- 1 | import { defineConfig } from 'astro/config'; 2 | 3 | import react from "@astrojs/react"; 4 | 5 | // https://astro.build/config 6 | export default defineConfig({ 7 | integrations: [react()] 8 | }); -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "working-with-data", 3 | "type": "module", 4 | "version": "0.0.1", 5 | "scripts": { 6 | "dev": "astro dev", 7 | "start": "astro dev", 8 | "build": "astro build", 9 | "preview": "astro preview", 10 | "astro": "astro" 11 | }, 12 | "dependencies": { 13 | "@astrojs/react": "^3.0.2", 14 | "@types/react": "^18.0.21", 15 | "@types/react-dom": "^18.0.6", 16 | "astro": "^3.1.0", 17 | "react": "^18.0.0", 18 | "react-dom": "^18.0.0" 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /public/favicon.svg: -------------------------------------------------------------------------------- 1 | 2 | 3 | 9 | 10 | -------------------------------------------------------------------------------- /src/components/APICard.tsx: -------------------------------------------------------------------------------- 1 | import { useState } from "react"; 2 | 3 | type Data = { 4 | id: string; 5 | collection: "product"; 6 | data: { 7 | name: string; 8 | price: number; 9 | color: string; 10 | brand: string; 11 | category: 12 | | "Automotive" 13 | | "Home & Garden" 14 | | "Fashion" 15 | | "Electronics" 16 | | "Toys"; 17 | }; 18 | }; 19 | 20 | const APICard = () => { 21 | const [products, setProducts] = useState([]); 22 | 23 | const handleClick = async () => { 24 | const res = await fetch("/endpoint.json"); 25 | const data = await res.json(); 26 | setProducts(data); 27 | }; 28 | return ( 29 |
30 |

Get more items

31 | {products.length > 0 && 32 | products.map((p: Data) =>

{p.data.name}

)} 33 | 34 |
35 | ); 36 | }; 37 | export default APICard; 38 | -------------------------------------------------------------------------------- /src/components/Card.astro: -------------------------------------------------------------------------------- 1 |
2 | 3 |
4 | 5 | 13 | -------------------------------------------------------------------------------- /src/content/config.ts: -------------------------------------------------------------------------------- 1 | import { z, defineCollection } from "astro:content"; 2 | 3 | const product = defineCollection({ 4 | type: "data", 5 | schema: z.object({ 6 | name: z.string().max(50, { 7 | message: "You must keep the name to 50 characters or less", 8 | }), 9 | price: z.number(), 10 | color: z.string(), 11 | brand: z.string(), 12 | category: z.enum([ 13 | "Automotive", 14 | "Home & Garden", 15 | "Fashion", 16 | "Electronics", 17 | "Toys", 18 | ]), 19 | }), 20 | }); 21 | 22 | export const collections = { 23 | product, 24 | }; 25 | -------------------------------------------------------------------------------- /src/content/product/1.yaml: -------------------------------------------------------------------------------- 1 | name: Product 1 2 | price: 9.99 3 | color: Red 4 | brand: ABC Company 5 | category: Electronics 6 | -------------------------------------------------------------------------------- /src/content/product/2.yaml: -------------------------------------------------------------------------------- 1 | name: Product 2 2 | price: 19.99 3 | color: Blue 4 | brand: XYZ Corporation 5 | category: Home & Garden 6 | -------------------------------------------------------------------------------- /src/content/product/3.yaml: -------------------------------------------------------------------------------- 1 | name: "Product 3" 2 | price: 49.99 3 | color: Black 4 | brand: DEF Enterprises 5 | category: Fashion 6 | -------------------------------------------------------------------------------- /src/content/product/4.yaml: -------------------------------------------------------------------------------- 1 | name: Product 4 2 | price: 12.99 3 | color: Green 4 | brand: 123 Industries 5 | category: Toys 6 | -------------------------------------------------------------------------------- /src/content/product/5.yaml: -------------------------------------------------------------------------------- 1 | name: Product 5 2 | price: 29.99 3 | color: Silver 4 | brand: GHI Corporation 5 | category: Automotive 6 | -------------------------------------------------------------------------------- /src/data/data3.json: -------------------------------------------------------------------------------- 1 | [ 2 | { 3 | "name": "Jane Austen", 4 | "age": 41, 5 | "nationality": "British", 6 | "genre": ["Classic", "Romance"] 7 | }, 8 | { 9 | "name": "Ernest Hemingway", 10 | "age": 61, 11 | "nationality": "American", 12 | "genre": ["Literary Fiction"] 13 | }, 14 | { 15 | "name": "Toni Morrison", 16 | "age": 88, 17 | "nationality": "American", 18 | "genre": ["Literary Fiction"] 19 | }, 20 | { 21 | "name": "Leo Tolstoy", 22 | "age": 82, 23 | "nationality": "Russian", 24 | "genre": ["Classic", "Realism"] 25 | }, 26 | { 27 | "name": "Maya Angelou", 28 | "age": 86, 29 | "nationality": "American", 30 | "genre": ["Autobiography", "Poetry"] 31 | } 32 | ] 33 | -------------------------------------------------------------------------------- /src/data/data3.md: -------------------------------------------------------------------------------- 1 | --- 2 | 3 | This is a list from my markdown file: 4 | 5 | **name: Jane Austen** 6 | 7 | - age: 41 8 | - nationality: British 9 | - genre: - Classic - Romance 10 | 11 | **name: Ernest Hemingway** 12 | 13 | - age: 61 14 | - nationality: American 15 | - genre: 16 | - Literary Fiction 17 | -------------------------------------------------------------------------------- /src/data/data3.ts: -------------------------------------------------------------------------------- 1 | const authors = [ 2 | { 3 | name: "J.K. Rowling", 4 | age: 56, 5 | nationality: "British", 6 | genre: ["Fantasy", "Young Adult"], 7 | }, 8 | { 9 | name: "Stephen King", 10 | age: 74, 11 | nationality: "American", 12 | genre: ["Horror", "Thriller"], 13 | }, 14 | { 15 | name: "Haruki Murakami", 16 | age: 72, 17 | nationality: "Japanese", 18 | genre: ["Fiction", "Magical Realism"], 19 | }, 20 | { 21 | name: "Agatha Christie", 22 | age: 85, 23 | nationality: "British", 24 | genre: ["Mystery", "Detective Fiction"], 25 | }, 26 | { 27 | name: "Gabriel Garcia Marquez", 28 | age: 87, 29 | nationality: "Colombian", 30 | genre: ["Magic Realism", "Literary Fiction"], 31 | }, 32 | ]; 33 | 34 | export default authors; 35 | -------------------------------------------------------------------------------- /src/data/data3_example2.md: -------------------------------------------------------------------------------- 1 | --- 2 | 3 | This is a list from a second markdown file: 4 | 5 | ### This is a heading 3 6 | 7 | **name: Jane Austen** 8 | 9 | - something 10 | - something else 11 | -------------------------------------------------------------------------------- /src/env.d.ts: -------------------------------------------------------------------------------- 1 | /// 2 | /// 3 | -------------------------------------------------------------------------------- /src/pages/endpoint.json.ts: -------------------------------------------------------------------------------- 1 | import { getCollection } from "astro:content"; 2 | 3 | const products = await getCollection("product"); 4 | 5 | export const GET = async ({}) => { 6 | return new Response(JSON.stringify(products), { 7 | headers: { 8 | "content-type": "application/json", 9 | }, 10 | status: 200, 11 | }); 12 | }; 13 | -------------------------------------------------------------------------------- /src/pages/index.astro: -------------------------------------------------------------------------------- 1 | --- 2 | import { getCollection } from "astro:content"; 3 | import Card from "../components/Card.astro"; 4 | import APICard from "../components/APICard"; 5 | 6 | // Data Method 1: Local Frontmatter Data 7 | const data1 = ["Chris", "Jon", "Jes"]; 8 | 9 | // Data Method 2: Fetch Remote Data 10 | type Post = { 11 | userId: number; 12 | id: number; 13 | title: string; 14 | body: string; 15 | }; 16 | const res = await fetch("https://jsonplaceholder.typicode.com/posts"); 17 | const data2: Post[] = await res.json(); 18 | 19 | // Data Method 3: Import Local Files 20 | import data3Json from "../data/data3.json"; 21 | import data3Ts from "../data/data3.ts"; 22 | const data3Glob = await Astro.glob("../data/**/*.md"); 23 | 24 | // Data Method 4: Content Collections 25 | const data4 = await getCollection("product"); 26 | --- 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 39 | 40 | Astro 41 | 42 | 43 |

Astro Data Options

44 |
45 | 46 |

1: Local Frontmatter Data

47 |
    48 | {data1.map((i) =>
  • {i}
  • )} 49 |
50 |
51 | 52 |

2: Fetch Remote Data

53 |
    54 | {data2.slice(0, 4).map((i) =>
  • {i.title}
  • )} 55 |
56 |
57 | 58 |

3: Import Local Files

59 |
    60 | {data3Json.map((i) =>
  • {i.name}
  • )} 61 |
62 |
    63 | {data3Ts.map((i) =>
  • {i.name}
  • )} 64 |
65 | 66 | 73 |
74 | 75 |

4: Content Collections

76 |
    77 | {data4.map((i) =>
  • {i.data.name}
  • )} 78 |
79 |
80 | 81 |

5: Create JSON endpoints

82 | 83 |
84 |
85 | 86 | 87 | 88 | 111 | -------------------------------------------------------------------------------- /tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "astro/tsconfigs/strict", 3 | "compilerOptions": { 4 | "jsx": "react-jsx", 5 | "jsxImportSource": "react" 6 | } 7 | } --------------------------------------------------------------------------------