Fix astro check errors on strict typescript, issue #293

This commit is contained in:
prototypa
2023-12-02 09:09:13 -05:00
parent cf40b9d093
commit 788acd9321
7 changed files with 42 additions and 26 deletions

View File

@ -4,8 +4,8 @@ import { getPermalink } from '~/utils/permalinks';
import Button from '~/components/ui/Button.astro';
export interface Props {
prevUrl: string;
nextUrl: string;
prevUrl?: string;
nextUrl?: string;
prevText?: string;
nextText?: string;
}

View File

@ -1,4 +1,6 @@
---
import type { InferGetStaticPropsType, GetStaticPaths } from 'astro';
import Layout from '~/layouts/PageLayout.astro';
import BlogList from '~/components/blog/List.astro';
import Headline from '~/components/blog/Headline.astro';
@ -9,11 +11,13 @@ import { blogListRobots, getStaticPathsBlogList } from '~/utils/blog';
export const prerender = true;
export async function getStaticPaths ({ paginate }) {
export const getStaticPaths = (async ({ paginate }) => {
return await getStaticPathsBlogList({ paginate });
}
}) satisfies GetStaticPaths;
const { page } = Astro.props;
type Props = InferGetStaticPropsType<typeof getStaticPaths>;
const { page } = Astro.props as Props;
const currentPage = page.currentPage ?? 1;
// const allCategories = await findCategories();

View File

@ -1,4 +1,5 @@
---
import type { InferGetStaticPropsType, GetStaticPaths } from 'astro';
import { blogCategoryRobots, getStaticPathsBlogCategory } from '~/utils/blog';
import Layout from '~/layouts/PageLayout.astro';
@ -8,11 +9,13 @@ import Pagination from '~/components/blog/Pagination.astro';
export const prerender = true;
export async function getStaticPaths ({ paginate }) {
export const getStaticPaths = (async ({ paginate }) => {
return await getStaticPathsBlogCategory({ paginate });
}
}) satisfies GetStaticPaths;
const { page, category } = Astro.props;
type Props = InferGetStaticPropsType<typeof getStaticPaths> & { category: string };
const { page, category } = Astro.props as Props;
const currentPage = page.currentPage ?? 1;

View File

@ -1,4 +1,5 @@
---
import type { InferGetStaticPropsType, GetStaticPaths } from 'astro';
import { blogTagRobots, getStaticPathsBlogTag } from '~/utils/blog';
import Layout from '~/layouts/PageLayout.astro';
@ -8,11 +9,13 @@ import Pagination from '~/components/blog/Pagination.astro';
export const prerender = true;
export async function getStaticPaths ({ paginate }) {
export const getStaticPaths = (async ({ paginate }) => {
return await getStaticPathsBlogTag({ paginate });
}
}) satisfies GetStaticPaths;
const { page, tag } = Astro.props;
type Props = InferGetStaticPropsType<typeof getStaticPaths>;
const { page, tag } = Astro.props as Props;
const currentPage = page.currentPage ?? 1;

View File

@ -1,4 +1,6 @@
---
import type { InferGetStaticPropsType, GetStaticPaths } from 'astro';
import merge from 'lodash.merge';
import type { ImageMetadata } from 'astro';
import Layout from '~/layouts/PageLayout.astro';
@ -8,17 +10,20 @@ import ToBlogLink from '~/components/blog/ToBlogLink.astro';
import { getCanonical, getPermalink } from '~/utils/permalinks';
import { getStaticPathsBlogPost, blogPostRobots } from '~/utils/blog';
import { findImage } from '~/utils/images';
import type { MetaData } from '~/types';
export const prerender = true;
export async function getStaticPaths () {
export const getStaticPaths = (async () => {
return await getStaticPathsBlogPost();
}
}) satisfies GetStaticPaths;
const { post } = Astro.props;
type Props = InferGetStaticPropsType<typeof getStaticPaths>;
const { post } = Astro.props as Props;
const url = getCanonical(getPermalink(post.permalink, 'post'));
const image = (await findImage(post.image)) as ImageMetadata | undefined;
const image = (await findImage(post.image)) as ImageMetadata | string | undefined;
const metadata = merge(
{
@ -30,11 +35,11 @@ const metadata = merge(
},
openGraph: {
type: 'article',
...(image ? { images: [{ url: image, width: image?.width, height: image?.height }] } : {}),
...(image ? { images: [{ url: image, width: (image as ImageMetadata)?.width, height: (image as ImageMetadata)?.height }] } : {}),
},
},
{ ...(post?.metadata ? { ...post.metadata, canonical: post.metadata?.canonical || url } : {}) }
);
) as MetaData;
---
<Layout metadata={metadata}>

4
src/types.d.ts vendored
View File

@ -1,5 +1,5 @@
import type { AstroComponentFactory } from 'astro/runtime/server/index.js';
import type { HTMLAttributes } from 'astro/types';
import type { HTMLAttributes, ImageMetadata } from 'astro/types';
export interface Post {
/** A unique ID number that identifies a post. */
@ -21,7 +21,7 @@ export interface Post {
/** Optional summary of post content. */
excerpt?: string;
/** */
image?: string;
image?: ImageMetadata | string;
/** */
category?: string;

View File

@ -1,3 +1,4 @@
import type { PaginateFunction } from 'astro';
import { getCollection } from 'astro:content';
import type { CollectionEntry } from 'astro:content';
import type { Post } from '~/types';
@ -162,7 +163,7 @@ export const findLatestPosts = async ({ count }: { count?: number }): Promise<Ar
};
/** */
export const getStaticPathsBlogList = async ({ paginate }) => {
export const getStaticPathsBlogList = async ({ paginate }: { paginate: PaginateFunction }) => {
if (!isBlogEnabled || !isBlogListRouteEnabled) return [];
return paginate(await fetchPosts(), {
params: { blog: BLOG_BASE || undefined },
@ -182,16 +183,16 @@ export const getStaticPathsBlogPost = async () => {
};
/** */
export const getStaticPathsBlogCategory = async ({ paginate }) => {
export const getStaticPathsBlogCategory = async ({ paginate }: { paginate: PaginateFunction }) => {
if (!isBlogEnabled || !isBlogCategoryRouteEnabled) return [];
const posts = await fetchPosts();
const categories = new Set();
const categories = new Set<string>();
posts.map((post) => {
typeof post.category === 'string' && categories.add(post.category.toLowerCase());
});
return Array.from(categories).flatMap((category: string) =>
return Array.from(categories).flatMap((category) =>
paginate(
posts.filter((post) => typeof post.category === 'string' && category === post.category.toLowerCase()),
{
@ -204,16 +205,16 @@ export const getStaticPathsBlogCategory = async ({ paginate }) => {
};
/** */
export const getStaticPathsBlogTag = async ({ paginate }) => {
export const getStaticPathsBlogTag = async ({ paginate }: { paginate: PaginateFunction }) => {
if (!isBlogEnabled || !isBlogTagRouteEnabled) return [];
const posts = await fetchPosts();
const tags = new Set();
const tags = new Set<string>();
posts.map((post) => {
Array.isArray(post.tags) && post.tags.map((tag) => tags.add(tag.toLowerCase()));
});
return Array.from(tags).flatMap((tag: string) =>
return Array.from(tags).flatMap((tag) =>
paginate(
posts.filter((post) => Array.isArray(post.tags) && post.tags.find((elem) => elem.toLowerCase() === tag)),
{