feat: switch fetch function
This commit is contained in:
1 parent
bf63026108
commit
2035ee052a
10 files changed
+38
-29
No files matched your search
+15
-3
@@ -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<Response>,
|
||||
input: RequestInfo | URL,
|
||||
init?: RequestInit | undefined
|
||||
): Promise<Response> {
|
||||
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<Response>
|
||||
): LemmyHttp {
|
||||
if (!instanceURL) {
|
||||
instanceURL = get(instance)
|
||||
}
|
||||
|
||||
return new LemmyHttp(`https://${instanceURL}`, {
|
||||
fetchFunction: customFetch,
|
||||
fetchFunction: func
|
||||
? (input, init) => customFetch(func, input, init)
|
||||
: undefined,
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
+3
-3
@@ -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) {
|
||||
|
||||
@@ -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,
|
||||
})
|
||||
|
||||
@@ -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',
|
||||
|
||||
@@ -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'
|
||||
|
||||
@@ -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))
|
||||
)
|
||||
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -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({
|
||||
|
||||
@@ -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,
|
||||
})
|
||||
|
||||
@@ -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,
|
||||
|
||||
Reference in new issue
Block a user