remove invariant dep

This commit is contained in:
Adam Matthiesen 2024-01-25 09:15:17 -08:00
parent 260a7a3f56
commit 4df1a3a80b
16 changed files with 61 additions and 43 deletions

View File

@ -51,7 +51,6 @@
}, },
"dependencies": { "dependencies": {
"@matthiesenxyz/astro-ghostcms": "^3.1.2", "@matthiesenxyz/astro-ghostcms": "^3.1.2",
"tiny-invariant": "^1.3.1",
"astro-font": "^0.0.77" "astro-font": "^0.0.77"
} }
} }

View File

@ -1,8 +1,7 @@
--- ---
import PostHero from "../components/PostHero.astro"; import PostHero from "../components/PostHero.astro";
import PostFooter from "../components/PostFooter.astro"; import PostFooter from "../components/PostFooter.astro";
import invariant from "tiny-invariant"; import {invariant, type Post, type Settings } from "@matthiesenxyz/astro-ghostcms/api";
import type {Post, Settings } from "@matthiesenxyz/astro-ghostcms/api";
export type Props = { export type Props = {
post: Post; post: Post;
settings: Settings; settings: Settings;

View File

@ -3,8 +3,7 @@ import type { InferGetStaticPropsType } from 'astro';
import DefaultPageLayout from "../layouts/default.astro"; import DefaultPageLayout from "../layouts/default.astro";
import Page from "../components/Page.astro"; import Page from "../components/Page.astro";
import Post from "../components/Post.astro"; import Post from "../components/Post.astro";
import { getSettings, getAllPages, getAllPosts } from "@matthiesenxyz/astro-ghostcms/api"; import { getSettings, getAllPages, getAllPosts, invariant } from "@matthiesenxyz/astro-ghostcms/api";
import invariant from 'tiny-invariant';
export async function getStaticPaths() { export async function getStaticPaths() {
const [posts, pages, settings] = await Promise.all([getAllPosts(), await getAllPages(), await getSettings()]); const [posts, pages, settings] = await Promise.all([getAllPosts(), await getAllPages(), await getSettings()]);

View File

@ -1,11 +1,10 @@
--- ---
import type { GetStaticPathsOptions, Page } from 'astro'; import type { GetStaticPathsOptions, Page } from 'astro';
import invariant from "tiny-invariant";
import DefaultPageLayout from "../../layouts/default.astro"; import DefaultPageLayout from "../../layouts/default.astro";
import PostPreviewList from "../../components/PostPreviewList.astro"; import PostPreviewList from "../../components/PostPreviewList.astro";
import HeroContent from "../../components/HeroContent.astro"; import HeroContent from "../../components/HeroContent.astro";
import Paginator from "../../components/Paginator.astro"; import Paginator from "../../components/Paginator.astro";
import { getSettings, getAllPosts, type Post } from "@matthiesenxyz/astro-ghostcms/api"; import { getSettings, getAllPosts, invariant, type Post } from "@matthiesenxyz/astro-ghostcms/api";
export async function getStaticPaths({ paginate }:GetStaticPathsOptions) { export async function getStaticPaths({ paginate }:GetStaticPathsOptions) {
const posts = await getAllPosts(); const posts = await getAllPosts();

View File

@ -2,8 +2,7 @@
import type { InferGetStaticParamsType, InferGetStaticPropsType } from 'astro'; import type { InferGetStaticParamsType, InferGetStaticPropsType } from 'astro';
import DefaultPageLayout from "../../layouts/default.astro"; import DefaultPageLayout from "../../layouts/default.astro";
import PostPreviewList from "../../components/PostPreviewList.astro"; import PostPreviewList from "../../components/PostPreviewList.astro";
import { getAllPosts, getAllAuthors, getSettings, twitter, facebook } from "@matthiesenxyz/astro-ghostcms/api"; import { getAllPosts, getAllAuthors, getSettings, twitter, facebook, invariant } from "@matthiesenxyz/astro-ghostcms/api";
import invariant from "tiny-invariant";
export async function getStaticPaths() { export async function getStaticPaths() {
const posts = await getAllPosts(); const posts = await getAllPosts();

View File

@ -1,8 +1,7 @@
--- ---
import DefaultPageLayout from "../layouts/default.astro"; import DefaultPageLayout from "../layouts/default.astro";
import AuthorCard from "../components/AuthorCard.astro"; import AuthorCard from "../components/AuthorCard.astro";
import { getAllAuthors, getSettings } from "@matthiesenxyz/astro-ghostcms/api"; import { getAllAuthors, getSettings, invariant } from "@matthiesenxyz/astro-ghostcms/api";
import invariant from "tiny-invariant";
let title = "All Authors"; let title = "All Authors";
let description = "All the authors"; let description = "All the authors";

View File

@ -2,8 +2,7 @@
import DefaultPageLayout from "../layouts/default.astro"; import DefaultPageLayout from "../layouts/default.astro";
import PostPreviewList from "../components/PostPreviewList.astro"; import PostPreviewList from "../components/PostPreviewList.astro";
import HeroContent from "../components/HeroContent.astro"; import HeroContent from "../components/HeroContent.astro";
import { getPosts, getSettings } from "@matthiesenxyz/astro-ghostcms/api"; import { getPosts, getSettings, invariant } from "@matthiesenxyz/astro-ghostcms/api";
import invariant from 'tiny-invariant';
const { posts } = await getPosts(); const { posts } = await getPosts();
const settings = await getSettings(); const settings = await getSettings();
invariant(settings, 'Settings not found'); invariant(settings, 'Settings not found');

View File

@ -2,9 +2,8 @@
import type { InferGetStaticParamsType, InferGetStaticPropsType } from 'astro'; import type { InferGetStaticParamsType, InferGetStaticPropsType } from 'astro';
import DefaultPageLayout from "../../layouts/default.astro"; import DefaultPageLayout from "../../layouts/default.astro";
import PostPreview from "../../components/PostPreview.astro"; import PostPreview from "../../components/PostPreview.astro";
import { getAllPosts, getAllTags, getSettings } from "@matthiesenxyz/astro-ghostcms/api"; import { getAllPosts, getAllTags, getSettings, invariant } from "@matthiesenxyz/astro-ghostcms/api";
import { getGhostImgPath } from "../../utils"; import { getGhostImgPath } from "../../utils";
import invariant from "tiny-invariant";
export async function getStaticPaths() { export async function getStaticPaths() {
const posts = await getAllPosts(); const posts = await getAllPosts();

View File

@ -1,8 +1,7 @@
--- ---
import DefaultPageLayout from "../layouts/default.astro"; import DefaultPageLayout from "../layouts/default.astro";
import TagCard from "../components/TagCard.astro"; import TagCard from "../components/TagCard.astro";
import { getSettings, getAllTags } from "@matthiesenxyz/astro-ghostcms/api"; import { getSettings, getAllTags, invariant } from "@matthiesenxyz/astro-ghostcms/api";
import invariant from 'tiny-invariant';
let title = "All Tags"; let title = "All Tags";

View File

@ -88,7 +88,6 @@
"@astrojs/sitemap": "^3.0.5", "@astrojs/sitemap": "^3.0.5",
"@ts-ghost/core-api": "^5.1.2", "@ts-ghost/core-api": "^5.1.2",
"astro-robots-txt": "^1.0.0", "astro-robots-txt": "^1.0.0",
"tiny-invariant": "^1.3.1",
"vite": "^5.0.12", "vite": "^5.0.12",
"vite-tsconfig-paths": "^4.2.2", "vite-tsconfig-paths": "^4.2.2",
"zod": "^3.22.4", "zod": "^3.22.4",

View File

@ -12,14 +12,12 @@ const {
const ghostApiKey = CONTENT_API_KEY; const ghostApiKey = CONTENT_API_KEY;
const ghostUrl = CONTENT_API_URL; const ghostUrl = CONTENT_API_URL;
const version = "v5.0"; const version = "v5.0";
const api = new TSGhostContentAPI(ghostUrl, ghostApiKey, version);
export const getAllAuthors = async () => { export const getAllAuthors = async () => {
const api = new TSGhostContentAPI(ghostUrl, ghostApiKey, version);
const results = await api.authors const results = await api.authors
.browse() .browse()
.include({ .include({ "count.posts": true })
"count.posts": true,
})
.fetch(); .fetch();
if (!results.success) { if (!results.success) {
throw new Error(results.errors.map((e) => e.message).join(", ")); throw new Error(results.errors.map((e) => e.message).join(", "));
@ -31,7 +29,6 @@ export const getAllAuthors = async () => {
}; };
export const getPosts = async () => { export const getPosts = async () => {
const api = new TSGhostContentAPI(ghostUrl, ghostApiKey, version);
const results = await api.posts const results = await api.posts
.browse() .browse()
.include({ .include({
@ -49,7 +46,6 @@ export const getPosts = async () => {
}; };
export const getAllPosts = async () => { export const getAllPosts = async () => {
const api = new TSGhostContentAPI(ghostUrl, ghostApiKey, version);
const posts: Post[] = []; const posts: Post[] = [];
let cursor = await api.posts let cursor = await api.posts
.browse() .browse()
@ -67,7 +63,6 @@ export const getAllPosts = async () => {
}; };
export const getAllPages = async () => { export const getAllPages = async () => {
const api = new TSGhostContentAPI(ghostUrl, ghostApiKey, version);
const pages: Page[] = []; const pages: Page[] = [];
let cursor = await api.pages let cursor = await api.pages
.browse() .browse()
@ -85,7 +80,6 @@ export const getAllPages = async () => {
}; };
export const getSettings = async () => { export const getSettings = async () => {
const api = new TSGhostContentAPI(ghostUrl, ghostApiKey, version);
const res = await api.settings.fetch(); const res = await api.settings.fetch();
if (res.success) { if (res.success) {
return res.data; return res.data;
@ -94,12 +88,9 @@ export const getSettings = async () => {
}; };
export const getAllTags = async () => { export const getAllTags = async () => {
const api = new TSGhostContentAPI(ghostUrl, ghostApiKey, version);
const results = await api.tags const results = await api.tags
.browse() .browse()
.include({ .include({ "count.posts": true })
"count.posts": true,
})
.fetch(); .fetch();
if (!results.success) { if (!results.success) {
throw new Error(results.errors.map((e) => e.message).join(", ")); throw new Error(results.errors.map((e) => e.message).join(", "));
@ -111,11 +102,8 @@ export const getAllTags = async () => {
}; };
export const getFeaturedPosts = async () => { export const getFeaturedPosts = async () => {
const api = new TSGhostContentAPI(ghostUrl, ghostApiKey, version);
const results = await api.posts const results = await api.posts
.browse({ .browse({ filter: "featured:true" })
filter: "featured:true"
})
.include({ .include({
authors: true, authors: true,
tags: true, tags: true,

View File

@ -21,7 +21,8 @@ export enum BrowseEndpointType {
settings = "settings", settings = "settings",
} }
export class TSGhostContentAPI<Version extends `v5.${string}` = any> { export class TSGhostContentAPI<Version extends `v5.${// biome-ignore lint/suspicious/noExplicitAny: shhhhh
string}` = any> {
private httpClient: HTTPClient; private httpClient: HTTPClient;
constructor( constructor(

View File

@ -1,2 +1,3 @@
export * from './api-functions'; export * from './api-functions';
export * from './content-api/schemas'; export * from './content-api/schemas';
export * from './invariant';

View File

@ -0,0 +1,34 @@
const isProduction: boolean = process.env.NODE_ENV === 'production';
const prefix: string = 'Invariant failed';
// Throw an error if the condition fails
// Strip out error messages for production
// > Not providing an inline default argument for message as the result is smaller
export function invariant(
// biome-ignore lint/suspicious/noExplicitAny: we know what we are doing
condition: any,
// Can provide a string, or a function that returns a string for cases where
// the message takes a fair amount of effort to compute
message?: string | (() => string),
): asserts condition {
if (condition) {
return;
}
// Condition not passed
// In production we strip the message but still throw
if (isProduction) {
throw new Error(prefix);
}
// When not in production we allow the message to pass through
// *This block will be removed in production builds*
const provided: string | undefined = typeof message === 'function' ? message() : message;
// Options:
// 1. message provided: `${prefix}: ${provided}`
// 2. message not provided: prefix
const value: string = provided ? `${prefix}: ${provided}` : prefix;
throw new Error(value);
}

View File

@ -1,10 +1,13 @@
--- ---
import './404.css'; import './404.css';
import { getSettings, invariant } from '../../api';
const settings = await getSettings();
invariant(settings, "Settings not found");
--- ---
<html lang="en"> <html lang="en">
<head> <head>
<title>404 Error</title> <title>404 | {settings.title}</title>
</head> </head>
<body class="wrapper"> <body class="wrapper">

View File

@ -1,17 +1,18 @@
import rss from "@astrojs/rss"; import rss from "@astrojs/rss";
import { getAllPosts, getSettings } from '../api'; import { getAllPosts, getSettings, invariant } from '../api';
import invariant from "tiny-invariant"; import type { APIContext } from 'astro';
export async function GET(context: { site: string; }) { const posts = await getAllPosts();
const posts = await getAllPosts(); const settings = await getSettings();
const settings = await getSettings();
export async function GET({ site, generator }: APIContext) {
invariant(settings, "Settings not found"); invariant(settings, "Settings not found");
const title = settings.title; const title = settings.title;
const description = settings.description; const description = settings.description;
return rss({ return rss({
title: title, title: `${title} [Built on ${generator.slice(0, 8)}]`,
description: description, description: description,
site: context.site, site: site?site:"",
items: posts.map((post) => ({ items: posts.map((post) => ({
title: post.title, title: post.title,
pubDate: new Date(post.published_at?post.published_at:post.created_at), pubDate: new Date(post.published_at?post.published_at:post.created_at),