From 2035ee052aa93272a87e65bce5da7f516f898534 Mon Sep 17 00:00:00 2001 From: Xyphyn Date: Fri, 4 Aug 2023 15:08:44 -0700 Subject: [PATCH] feat: switch fetch function --- src/lib/lemmy.ts | 18 +++++++++++++++--- src/routes/+page.ts | 6 +++--- src/routes/c/[name]/+page.ts | 4 ++-- src/routes/communities/+page.ts | 6 +++--- src/routes/inbox/+page.ts | 4 ++-- src/routes/moderation/+page.ts | 9 +++------ .../post/[instance]/[id=integer]/+page.ts | 6 +++--- src/routes/saved/+page.ts | 4 ++-- src/routes/search/+page.ts | 6 +++--- src/routes/u/[name]/+page.ts | 4 ++-- 10 files changed, 38 insertions(+), 29 deletions(-) diff --git a/src/lib/lemmy.ts b/src/lib/lemmy.ts index 6c6f82d4..cc2cfbda 100644 --- a/src/lib/lemmy.ts +++ b/src/lib/lemmy.ts @@ -14,21 +14,33 @@ export const DEFAULT_INSTANCE_URL = env.PUBLIC_INSTANCE_URL || 'lemmy.ml' export let instance = writable(DEFAULT_INSTANCE_URL) async function customFetch( + func: ( + input: RequestInfo | URL, + init?: RequestInit | undefined + ) => Promise, input: RequestInfo | URL, init?: RequestInit | undefined ): Promise { - const res = await fetch(input, init) + const res = await func(input, init) if (!res.ok) throw error(res.status, res.statusText) return res } -export function getClient(instanceURL?: string): LemmyHttp { +export function getClient( + instanceURL?: string, + func?: ( + input: RequestInfo | URL, + init?: RequestInit | undefined + ) => Promise +): LemmyHttp { if (!instanceURL) { instanceURL = get(instance) } return new LemmyHttp(`https://${instanceURL}`, { - fetchFunction: customFetch, + fetchFunction: func + ? (input, init) => customFetch(func, input, init) + : undefined, }) } diff --git a/src/routes/+page.ts b/src/routes/+page.ts index 13726ba3..a82fd68f 100644 --- a/src/routes/+page.ts +++ b/src/routes/+page.ts @@ -5,7 +5,7 @@ import { get } from 'svelte/store' import { error } from '@sveltejs/kit' import { profile } from '$lib/auth.js' -export async function load({ url }) { +export async function load({ url, fetch }) { const page = Number(url.searchParams.get('page') || 1) || 1 const sort: SortType = @@ -20,7 +20,7 @@ export async function load({ url }) { sort: sort, listingType: listingType, page: page, - posts: await getClient().getPosts({ + posts: await getClient(undefined, fetch).getPosts({ limit: 20, page: page, sort: sort, @@ -28,7 +28,7 @@ export async function load({ url }) { auth: get(profile)?.jwt, }), streamed: { - site: getClient().getSite({}), + site: getClient(undefined, fetch).getSite({}), }, } } catch (err) { diff --git a/src/routes/c/[name]/+page.ts b/src/routes/c/[name]/+page.ts index 6476c51d..1aca1dc8 100644 --- a/src/routes/c/[name]/+page.ts +++ b/src/routes/c/[name]/+page.ts @@ -14,7 +14,7 @@ export async function load(req: any) { return { sort: sort, page: page, - posts: await getClient().getPosts({ + posts: await getClient(undefined, req.fetch).getPosts({ limit: 40, community_name: req.params.name, page: page, @@ -22,7 +22,7 @@ export async function load(req: any) { auth: get(profile)?.jwt, }), community: ( - await getClient().getCommunity({ + await getClient(undefined, req.fetch).getCommunity({ name: req.params.name, auth: get(profile)?.jwt, }) diff --git a/src/routes/communities/+page.ts b/src/routes/communities/+page.ts index 2ac352fc..f6d169b0 100644 --- a/src/routes/communities/+page.ts +++ b/src/routes/communities/+page.ts @@ -3,13 +3,13 @@ import { getClient } from '$lib/lemmy.js' import type { ListingType } from 'lemmy-js-client' import { get } from 'svelte/store' -export async function load({ url }) { +export async function load({ url, fetch }) { const type = url.searchParams.get('type') as ListingType const page = Number(url.searchParams.get('page')) || 1 const query = url.searchParams.get('q') if (query) { - return await getClient().search({ + return await getClient(undefined, fetch).search({ limit: 40, page: page, sort: 'TopAll', @@ -19,7 +19,7 @@ export async function load({ url }) { auth: get(profile)?.jwt, }) } else { - return await getClient().listCommunities({ + return await getClient(undefined, fetch).listCommunities({ limit: 40, page: page, sort: 'TopAll', diff --git a/src/routes/inbox/+page.ts b/src/routes/inbox/+page.ts index 2ea9f1f9..b4c64e94 100644 --- a/src/routes/inbox/+page.ts +++ b/src/routes/inbox/+page.ts @@ -5,13 +5,13 @@ import { get } from 'svelte/store' type InboxFeedType = 'replies' | 'mentions' | 'messages' | 'all' -export async function load({ url }) { +export async function load({ url, fetch }) { const auth = get(profile) if (!auth?.jwt) return const type: InboxFeedType = (url.searchParams.get('type') as InboxFeedType) || 'all' - const client = getClient() + const client = getClient(undefined, fetch) const page = Number(url.searchParams.get('page')) || 1 const unreadOnly: boolean = (url.searchParams.get('unreadOnly') || 'true') == 'true' diff --git a/src/routes/moderation/+page.ts b/src/routes/moderation/+page.ts index 322df58e..81b41a90 100644 --- a/src/routes/moderation/+page.ts +++ b/src/routes/moderation/+page.ts @@ -6,7 +6,7 @@ import { get } from 'svelte/store' type ReportListType = 'unread' | 'all' type ReportFilter = 'all' | 'posts' | 'comments' | 'messages' -export async function load({ url }) { +export async function load({ url, fetch }) { const page = Number(url.searchParams.get('page')) || 1 const type: ReportListType = (url.searchParams.get('type') as ReportListType) || 'unread' @@ -24,7 +24,7 @@ export async function load({ url }) { // the interfaces don't even extend each other // meaning I have to make a separate function to get dates // for EACH FREAKING ITEM KAJSHDAWLD auwdi awiody - const client = getClient() + const client = getClient(undefined, fetch) const params = { auth: jwt, @@ -42,10 +42,7 @@ export async function load({ url }) { }), ]) - const everything = [ - ...posts.post_reports, - ...comments.comment_reports, - ].sort( + const everything = [...posts.post_reports, ...comments.comment_reports].sort( (a, b) => Date.parse(getItemPublished(b)) - Date.parse(getItemPublished(a)) ) diff --git a/src/routes/post/[instance]/[id=integer]/+page.ts b/src/routes/post/[instance]/[id=integer]/+page.ts index 7adde7de..dc1ba539 100644 --- a/src/routes/post/[instance]/[id=integer]/+page.ts +++ b/src/routes/post/[instance]/[id=integer]/+page.ts @@ -3,8 +3,8 @@ import { getClient } from '$lib/lemmy.js' import { error } from '@sveltejs/kit' import { get } from 'svelte/store' -export async function load({ params, url }) { - const post = await getClient(params.instance.toLowerCase()).getPost({ +export async function load({ params, url, fetch }) { + const post = await getClient(params.instance.toLowerCase(), fetch).getPost({ id: Number(params.id), auth: get(profile)?.jwt, }) @@ -30,7 +30,7 @@ export async function load({ params, url }) { singleThread: parentId != undefined, post: post, streamed: { - comments: getClient(params.instance.toLowerCase()).getComments({ + comments: getClient(params.instance.toLowerCase(), fetch).getComments({ post_id: Number(params.id), type_: 'All', limit: 25, diff --git a/src/routes/saved/+page.ts b/src/routes/saved/+page.ts index e3c39fbe..c66485cf 100644 --- a/src/routes/saved/+page.ts +++ b/src/routes/saved/+page.ts @@ -11,11 +11,11 @@ function getSavedItemPublished(item: PostView | CommentView) { } } -export async function load({ url }) { +export async function load({ url, fetch }) { if (!get(profile)) return { posts: [] } const page = Number(url.searchParams.get('page')) || 1 - const client = getClient() + const client = getClient(undefined, fetch) const [posts, comments] = await Promise.all([ client.getPosts({ diff --git a/src/routes/search/+page.ts b/src/routes/search/+page.ts index 9fbfe21b..b080bacb 100644 --- a/src/routes/search/+page.ts +++ b/src/routes/search/+page.ts @@ -12,7 +12,7 @@ import type { } from 'lemmy-js-client' import { get } from 'svelte/store' -export async function load({ url }) { +export async function load({ url, fetch }) { const query = url.searchParams.get('q') const page = Number(url.searchParams.get('page')) || 1 const community = url.searchParams.get('community_name') @@ -20,7 +20,7 @@ export async function load({ url }) { const type = url.searchParams.get('type') if (query) { - const results = await getClient().search({ + const results = await getClient(undefined, fetch).search({ q: query, auth: get(profile)?.jwt, community_name: community ?? undefined, @@ -48,7 +48,7 @@ export async function load({ url }) { results: everything, streamed: { object: get(profile)?.jwt - ? getClient().resolveObject({ + ? getClient(undefined, fetch).resolveObject({ auth: get(profile)!.jwt!, q: query, }) diff --git a/src/routes/u/[name]/+page.ts b/src/routes/u/[name]/+page.ts index 5f897904..a838df17 100644 --- a/src/routes/u/[name]/+page.ts +++ b/src/routes/u/[name]/+page.ts @@ -4,13 +4,13 @@ import { getItemPublished } from '$lib/lemmy/item.js' import type { SortType } from 'lemmy-js-client' import { get } from 'svelte/store' -export async function load({ params, url }) { +export async function load({ params, url, fetch }) { const page = Number(url.searchParams.get('page')) || 1 const type: 'comments' | 'posts' | 'all' = (url.searchParams.get('type') as 'comments' | 'posts' | 'all') || 'all' const sort: SortType = (url.searchParams.get('sort') as SortType) || 'New' - const user = await getClient().getPersonDetails({ + const user = await getClient(undefined, fetch).getPersonDetails({ limit: 20, page: page, username: params.name,