Skip to content

Commit

Permalink
try to setup an API
Browse files Browse the repository at this point in the history
  • Loading branch information
skamansam committed Apr 14, 2024
1 parent c9e7a02 commit 07e0e06
Show file tree
Hide file tree
Showing 8 changed files with 296 additions and 7 deletions.
19 changes: 18 additions & 1 deletion .astro/icon.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Automatically generated by astro-icon
// e5c0fc0eb3d33158f5f6f437f5db228377727e61d76a0a51b052e85ec3b5ea80
// 5a07c3cf60070fcbe2b36b583820e39f6dfb51faebb26f04a545c8ed99b29a8e

declare module 'virtual:astro-icon' {
export type Icon =
Expand Down Expand Up @@ -66,7 +66,13 @@ declare module 'virtual:astro-icon' {
| "ion:arrow-down-circle"
| "ion:arrow-down-circle-outline"
| "ion:arrow-down-circle-sharp"
| "ion:arrow-down-left-box"
| "ion:arrow-down-left-box-outline"
| "ion:arrow-down-left-box-sharp"
| "ion:arrow-down-outline"
| "ion:arrow-down-right-box"
| "ion:arrow-down-right-box-outline"
| "ion:arrow-down-right-box-sharp"
| "ion:arrow-down-sharp"
| "ion:arrow-expand"
| "ion:arrow-forward"
Expand Down Expand Up @@ -110,7 +116,13 @@ declare module 'virtual:astro-icon' {
| "ion:arrow-up-circle"
| "ion:arrow-up-circle-outline"
| "ion:arrow-up-circle-sharp"
| "ion:arrow-up-left-box"
| "ion:arrow-up-left-box-outline"
| "ion:arrow-up-left-box-sharp"
| "ion:arrow-up-outline"
| "ion:arrow-up-right-box"
| "ion:arrow-up-right-box-outline"
| "ion:arrow-up-right-box-sharp"
| "ion:arrow-up-sharp"
| "ion:asterisk"
| "ion:at"
Expand Down Expand Up @@ -193,6 +205,9 @@ declare module 'virtual:astro-icon' {
| "ion:bicycle"
| "ion:bicycle-outline"
| "ion:bicycle-sharp"
| "ion:binoculars"
| "ion:binoculars-outline"
| "ion:binoculars-sharp"
| "ion:bluetooth"
| "ion:bluetooth-outline"
| "ion:bluetooth-sharp"
Expand Down Expand Up @@ -1301,6 +1316,7 @@ declare module 'virtual:astro-icon' {
| "ion:logo-amplify"
| "ion:logo-android"
| "ion:logo-angular"
| "ion:logo-appflow"
| "ion:logo-apple"
| "ion:logo-apple-appstore"
| "ion:logo-apple-ar"
Expand Down Expand Up @@ -1372,6 +1388,7 @@ declare module 'virtual:astro-icon' {
| "ion:logo-stencil"
| "ion:logo-tableau"
| "ion:logo-tiktok"
| "ion:logo-trapeze"
| "ion:logo-tumblr"
| "ion:logo-tux"
| "ion:logo-twitch"
Expand Down
164 changes: 164 additions & 0 deletions .astro/types.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
declare module 'astro:content' {
interface Render {
'.mdx': Promise<{
Content: import('astro').MarkdownInstance<{}>['Content'];
headings: import('astro').MarkdownHeading[];
remarkPluginFrontmatter: Record<string, any>;
}>;
}
}

declare module 'astro:content' {
interface Render {
'.md': Promise<{
Content: import('astro').MarkdownInstance<{}>['Content'];
headings: import('astro').MarkdownHeading[];
remarkPluginFrontmatter: Record<string, any>;
}>;
}
}

declare module 'astro:content' {
type Flatten<T> = T extends { [K: string]: infer U } ? U : never;

export type CollectionKey = keyof AnyEntryMap;
export type CollectionEntry<C extends CollectionKey> = Flatten<AnyEntryMap[C]>;

export type ContentCollectionKey = keyof ContentEntryMap;
export type DataCollectionKey = keyof DataEntryMap;

type AllValuesOf<T> = T extends any ? T[keyof T] : never;
type ValidContentEntrySlug<C extends keyof ContentEntryMap> = AllValuesOf<
ContentEntryMap[C]
>['slug'];

export function getEntryBySlug<
C extends keyof ContentEntryMap,
E extends ValidContentEntrySlug<C> | (string & {}),
>(
collection: C,
// Note that this has to accept a regular string too, for SSR
entrySlug: E
): E extends ValidContentEntrySlug<C>
? Promise<CollectionEntry<C>>
: Promise<CollectionEntry<C> | undefined>;

export function getDataEntryById<C extends keyof DataEntryMap, E extends keyof DataEntryMap[C]>(
collection: C,
entryId: E
): Promise<CollectionEntry<C>>;

export function getCollection<C extends keyof AnyEntryMap, E extends CollectionEntry<C>>(
collection: C,
filter?: (entry: CollectionEntry<C>) => entry is E
): Promise<E[]>;
export function getCollection<C extends keyof AnyEntryMap>(
collection: C,
filter?: (entry: CollectionEntry<C>) => unknown
): Promise<CollectionEntry<C>[]>;

export function getEntry<
C extends keyof ContentEntryMap,
E extends ValidContentEntrySlug<C> | (string & {}),
>(entry: {
collection: C;
slug: E;
}): E extends ValidContentEntrySlug<C>
? Promise<CollectionEntry<C>>
: Promise<CollectionEntry<C> | undefined>;
export function getEntry<
C extends keyof DataEntryMap,
E extends keyof DataEntryMap[C] | (string & {}),
>(entry: {
collection: C;
id: E;
}): E extends keyof DataEntryMap[C]
? Promise<DataEntryMap[C][E]>
: Promise<CollectionEntry<C> | undefined>;
export function getEntry<
C extends keyof ContentEntryMap,
E extends ValidContentEntrySlug<C> | (string & {}),
>(
collection: C,
slug: E
): E extends ValidContentEntrySlug<C>
? Promise<CollectionEntry<C>>
: Promise<CollectionEntry<C> | undefined>;
export function getEntry<
C extends keyof DataEntryMap,
E extends keyof DataEntryMap[C] | (string & {}),
>(
collection: C,
id: E
): E extends keyof DataEntryMap[C]
? Promise<DataEntryMap[C][E]>
: Promise<CollectionEntry<C> | undefined>;

/** Resolve an array of entry references from the same collection */
export function getEntries<C extends keyof ContentEntryMap>(
entries: {
collection: C;
slug: ValidContentEntrySlug<C>;
}[]
): Promise<CollectionEntry<C>[]>;
export function getEntries<C extends keyof DataEntryMap>(
entries: {
collection: C;
id: keyof DataEntryMap[C];
}[]
): Promise<CollectionEntry<C>[]>;

export function reference<C extends keyof AnyEntryMap>(
collection: C
): import('astro/zod').ZodEffects<
import('astro/zod').ZodString,
C extends keyof ContentEntryMap
? {
collection: C;
slug: ValidContentEntrySlug<C>;
}
: {
collection: C;
id: keyof DataEntryMap[C];
}
>;
// Allow generic `string` to avoid excessive type errors in the config
// if `dev` is not running to update as you edit.
// Invalid collection names will be caught at build time.
export function reference<C extends string>(
collection: C
): import('astro/zod').ZodEffects<import('astro/zod').ZodString, never>;

type ReturnTypeOrOriginal<T> = T extends (...args: any[]) => infer R ? R : T;
type InferEntrySchema<C extends keyof AnyEntryMap> = import('astro/zod').infer<
ReturnTypeOrOriginal<Required<ContentConfig['collections'][C]>['schema']>
>;

type ContentEntryMap = {
"posts": {
"the-old-man.md": {
id: "the-old-man.md";
slug: "the-old-man";
body: string;
collection: "posts";
data: InferEntrySchema<"posts">
} & { render(): Render[".md"] };
};

};

type DataEntryMap = {
"authors": {
"samuel-c-tyler": {
id: "samuel-c-tyler";
collection: "authors";
data: any
};
};

};

type AnyEntryMap = ContentEntryMap & DataEntryMap;

export type ContentConfig = typeof import("../src/content/config.js");
}
3 changes: 3 additions & 0 deletions src/content/authors/samuel-c-tyler.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"name": "Samuel C Tyler"
}
22 changes: 22 additions & 0 deletions src/content/config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// Import utilities from `astro:content`
import { z, defineCollection } from "astro:content";
// Define a `type` and `schema` for each collection
const postsCollection = defineCollection({
type: 'content',
schema: z.object({
title: z.string(),
pubDate: z.date(),
description: z.string(),
author: z.string(),
order: z.number(),
image: z.object({
url: z.string(),
alt: z.string()
}),
tags: z.array(z.string())
})
});
// Export a single `collections` object to register your collection(s)
export const collections = {
posts: postsCollection,
};
19 changes: 19 additions & 0 deletions src/content/posts/the-old-man.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
---
title: "Welcome to my blog"
pubDate: 2024-01-01
description: Welcommen
author: samuel-c-tyler # references `src/content/authors/ben-holmes.json`
order: 1
image:
url: "null"
alt: the old man
tags:
- old man
- characters
- deities
relatedPosts:
- about-me # references `src/content/blog/about-me.md`
- my-year-in-review # references `src/content/blog/my-year-in-review.md`
---

# The Old Man
17 changes: 12 additions & 5 deletions src/pages/stories/[...page].astro
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
---
import DefaultLayout from '../../layouts/DefaultLayout.astro'
import { Card, Pagination } from 'accessible-astro-components'
import { getCollection } from "astro:content";
export async function getStaticPaths({ paginate }) {
const response = await fetch('https://jsonplaceholder.typicode.com/posts')
const data = await response.json()
// const response = await fetch('https://jsonplaceholder.typicode.com/posts')
const data = await getCollection("posts");
// const data = await response.json()
return paginate(data, { pageSize: 6 })
}
Expand All @@ -23,6 +26,9 @@ const { page } = Astro.props
An example of a blog with dynamic content fetched from <a href="https://jsonplaceholder.typicode.com/posts"
>JSONPlaceholder</a
> using the title, body and userId. The Accessible Astro Card Component is used here to display al the posts.
<pre>
{page.data.length}
</pre>
</p>
</div>
</section>
Expand All @@ -33,10 +39,11 @@ const { page } = Astro.props
{
page.data.map((post) => (
<li>
{post}
<Card
url={'/blog/' + post.title.replaceAll(' ', '-').toLowerCase()}
title={post.title}
footer={'userId:' + post.userId}
url={`/blog/${post.slug}`}
title={post.data.title}
footer={'userId:' + post?.userId}
>
{post.body}
</Card>
Expand Down
55 changes: 55 additions & 0 deletions src/pages/stories/[...slug].astro
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
---
import { getCollection } from 'astro:content';
import DefaultLayout from '../../layouts/DefaultLayout.astro'
export async function getStaticPaths() {
const blogEntries = await getCollection('posts');
return blogEntries.map(entry => ({
params: { slug: entry.slug }, props: { entry },
}));
}
const { post } = Astro.props;
const { Content } = await entry.render();
---

<DefaultLayout title={post.title} description={post.body} url={post.title}>
<div class="container">
<div class="mt-12">
<Breadcrumbs>
<BreadcrumbsItem href="/" label="Home" />
<BreadcrumbsItem href="/blog" label="Blog" />
<BreadcrumbsItem currentPage={true} label={post.title} />
</Breadcrumbs>
</div>
</div>
<section class="my-12">
<div class="container">
<h1>{post.title}</h1><br />
<p>By userId: {post.userId}</p>
</div>
</section>
<section class="my-12">
<div class="container">
<p class="text-2xl">{post.body}</p>
</div>
</section>
</DefaultLayout>

<style lang="scss">
ul {
display: grid;
grid-template-columns: 1fr;
grid-gap: 4rem;

@media (min-width: 550px) {
grid-template-columns: repeat(2, 1fr);
grid-gap: 2rem;
}

@media (min-width: 950px) {
grid-template-columns: repeat(3, 1fr);
}
}
</style>

4 changes: 3 additions & 1 deletion src/pages/stories/[post].astro
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
---
import DefaultLayout from '../../layouts/DefaultLayout.astro'
import { Breadcrumbs, BreadcrumbsItem } from 'accessible-astro-components'
import { getCollection } from "astro:content";
export async function getStaticPaths() {
const data = await fetch('https://jsonplaceholder.typicode.com/posts').then((response) => response.json())
// const data = await fetch('https://jsonplaceholder.typicode.com/posts').then((response) => response.json())
const data = await getCollection("posts");
return data.map((post) => {
return {
Expand Down

0 comments on commit 07e0e06

Please sign in to comment.