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": {
"@matthiesenxyz/astro-ghostcms": "^3.1.2",
"tiny-invariant": "^1.3.1",
"astro-font": "^0.0.77"
}
}

View File

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

View File

@ -3,8 +3,7 @@ import type { InferGetStaticPropsType } from 'astro';
import DefaultPageLayout from "../layouts/default.astro";
import Page from "../components/Page.astro";
import Post from "../components/Post.astro";
import { getSettings, getAllPages, getAllPosts } from "@matthiesenxyz/astro-ghostcms/api";
import invariant from 'tiny-invariant';
import { getSettings, getAllPages, getAllPosts, invariant } from "@matthiesenxyz/astro-ghostcms/api";
export async function getStaticPaths() {
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 invariant from "tiny-invariant";
import DefaultPageLayout from "../../layouts/default.astro";
import PostPreviewList from "../../components/PostPreviewList.astro";
import HeroContent from "../../components/HeroContent.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) {
const posts = await getAllPosts();

View File

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

View File

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

View File

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

View File

@ -2,9 +2,8 @@
import type { InferGetStaticParamsType, InferGetStaticPropsType } from 'astro';
import DefaultPageLayout from "../../layouts/default.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 invariant from "tiny-invariant";
export async function getStaticPaths() {
const posts = await getAllPosts();

View File

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

View File

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

View File

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

View File

@ -21,7 +21,8 @@ export enum BrowseEndpointType {
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;
constructor(

View File

@ -1,2 +1,3 @@
export * from './api-functions';
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 { getSettings, invariant } from '../../api';
const settings = await getSettings();
invariant(settings, "Settings not found");
---
<html lang="en">
<head>
<title>404 Error</title>
<title>404 | {settings.title}</title>
</head>
<body class="wrapper">

View File

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