Skip to content

Commit

Permalink
refactor to remove node-fetch (#137)
Browse files Browse the repository at this point in the history
  • Loading branch information
swyxio authored Nov 28, 2022
1 parent 06df5bd commit a5f5803
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 192 deletions.
196 changes: 18 additions & 178 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@
"type": "module",
"dependencies": {
"date-fns": "^2.29.3",
"node-fetch": "3.3.0",
"parse-link-header": "^2.0.0",
"prism-themes": "^1.9.0",
"remark": "^14.0.2",
Expand Down
9 changes: 4 additions & 5 deletions src/lib/content.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { compile } from 'mdsvex';
import { dev } from '$app/environment';
import grayMatter from 'gray-matter';
import fetch from 'node-fetch';
import {
GH_USER_REPO,
APPROVED_POSTERS_GH_USERNAME,
Expand Down Expand Up @@ -57,7 +56,7 @@ function readingTime(text) {
return minutes > 1 ? `${minutes} minutes` : `${minutes} minute`;
}

export async function listContent() {
export async function listContent(providedFetch) {
// use a diff var so as to not have race conditions while fetching
// TODO: make sure to handle this better when doing etags or cache restore

Expand All @@ -80,7 +79,7 @@ export async function listContent() {
url += '&' + new URLSearchParams({ creator: REPO_OWNER });
}
do {
const res = await fetch(next?.url ?? url, {
const res = await providedFetch(next?.url ?? url, {
headers: authheader
});

Expand All @@ -107,11 +106,11 @@ export async function listContent() {
return _allBlogposts;
}

export async function getContent(slug) {
export async function getContent(providedFetch, slug) {
// get all blogposts if not already done - or in development
if (dev || allBlogposts.length === 0) {
console.log('loading allBlogposts');
allBlogposts = await listContent();
allBlogposts = await listContent(providedFetch);
console.log('loaded ' + allBlogposts.length + ' blogposts');
if (!allBlogposts.length)
throw new Error(
Expand Down
4 changes: 2 additions & 2 deletions src/routes/api/blog/[slug].json/+server.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ import { error } from '@sveltejs/kit';
/**
* @type {import('@sveltejs/kit').RequestHandler}
*/
export async function GET({ params }) {
export async function GET({ fetch, params }) {
const { slug } = params;
let data;
try {
data = await getContent(slug);
data = await getContent(fetch, slug);
return new Response(JSON.stringify(data), {
headers: {
'Cache-Control': `max-age=0, s-maxage=${60}` // 1 minute.. for now
Expand Down
4 changes: 2 additions & 2 deletions src/routes/api/listContent.json/+server.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import { listContent } from '$lib/content';
/**
* @type {import('./$types').RequestHandler}
*/
export async function GET({ setHeaders }) {
const list = await listContent();
export async function GET({ fetch, setHeaders }) {
const list = await listContent(fetch);
setHeaders({
'Cache-Control': `max-age=0, s-maxage=${60}` // 1 minute.. for now
});
Expand Down
4 changes: 2 additions & 2 deletions src/routes/rss.xml/+server.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ import { listContent } from '$lib/content';

// Reference: https://github.com/sveltejs/kit/blob/master/examples/hn.svelte.dev/src/routes/%5Blist%5D/rss.js
/** @type {import('@sveltejs/kit').RequestHandler} */
export async function GET() {
export async function GET({ fetch }) {
const feed = new RSS({
title: SITE_TITLE + ' RSS Feed',
site_url: SITE_URL,
feed_url: SITE_URL + '/api/rss.xml'
});

const allBlogs = await listContent();
const allBlogs = await listContent(fetch);
allBlogs.forEach((post) => {
feed.item({
title: post.title,
Expand Down
Loading

0 comments on commit a5f5803

Please sign in to comment.