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)
|
export let instance = writable(DEFAULT_INSTANCE_URL)
|
||||||
|
|
||||||
async function customFetch(
|
async function customFetch(
|
||||||
|
func: (
|
||||||
|
input: RequestInfo | URL,
|
||||||
|
init?: RequestInit | undefined
|
||||||
|
) => Promise<Response>,
|
||||||
input: RequestInfo | URL,
|
input: RequestInfo | URL,
|
||||||
init?: RequestInit | undefined
|
init?: RequestInit | undefined
|
||||||
): Promise<Response> {
|
): Promise<Response> {
|
||||||
const res = await fetch(input, init)
|
const res = await func(input, init)
|
||||||
if (!res.ok) throw error(res.status, res.statusText)
|
if (!res.ok) throw error(res.status, res.statusText)
|
||||||
return res
|
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) {
|
if (!instanceURL) {
|
||||||
instanceURL = get(instance)
|
instanceURL = get(instance)
|
||||||
}
|
}
|
||||||
|
|
||||||
return new LemmyHttp(`https://${instanceURL}`, {
|
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 { error } from '@sveltejs/kit'
|
||||||
import { profile } from '$lib/auth.js'
|
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 page = Number(url.searchParams.get('page') || 1) || 1
|
||||||
|
|
||||||
const sort: SortType =
|
const sort: SortType =
|
||||||
@@ -20,7 +20,7 @@ export async function load({ url }) {
|
|||||||
sort: sort,
|
sort: sort,
|
||||||
listingType: listingType,
|
listingType: listingType,
|
||||||
page: page,
|
page: page,
|
||||||
posts: await getClient().getPosts({
|
posts: await getClient(undefined, fetch).getPosts({
|
||||||
limit: 20,
|
limit: 20,
|
||||||
page: page,
|
page: page,
|
||||||
sort: sort,
|
sort: sort,
|
||||||
@@ -28,7 +28,7 @@ export async function load({ url }) {
|
|||||||
auth: get(profile)?.jwt,
|
auth: get(profile)?.jwt,
|
||||||
}),
|
}),
|
||||||
streamed: {
|
streamed: {
|
||||||
site: getClient().getSite({}),
|
site: getClient(undefined, fetch).getSite({}),
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
|
|||||||
@@ -14,7 +14,7 @@ export async function load(req: any) {
|
|||||||
return {
|
return {
|
||||||
sort: sort,
|
sort: sort,
|
||||||
page: page,
|
page: page,
|
||||||
posts: await getClient().getPosts({
|
posts: await getClient(undefined, req.fetch).getPosts({
|
||||||
limit: 40,
|
limit: 40,
|
||||||
community_name: req.params.name,
|
community_name: req.params.name,
|
||||||
page: page,
|
page: page,
|
||||||
@@ -22,7 +22,7 @@ export async function load(req: any) {
|
|||||||
auth: get(profile)?.jwt,
|
auth: get(profile)?.jwt,
|
||||||
}),
|
}),
|
||||||
community: (
|
community: (
|
||||||
await getClient().getCommunity({
|
await getClient(undefined, req.fetch).getCommunity({
|
||||||
name: req.params.name,
|
name: req.params.name,
|
||||||
auth: get(profile)?.jwt,
|
auth: get(profile)?.jwt,
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -3,13 +3,13 @@ import { getClient } from '$lib/lemmy.js'
|
|||||||
import type { ListingType } from 'lemmy-js-client'
|
import type { ListingType } from 'lemmy-js-client'
|
||||||
import { get } from 'svelte/store'
|
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 type = url.searchParams.get('type') as ListingType
|
||||||
const page = Number(url.searchParams.get('page')) || 1
|
const page = Number(url.searchParams.get('page')) || 1
|
||||||
const query = url.searchParams.get('q')
|
const query = url.searchParams.get('q')
|
||||||
|
|
||||||
if (query) {
|
if (query) {
|
||||||
return await getClient().search({
|
return await getClient(undefined, fetch).search({
|
||||||
limit: 40,
|
limit: 40,
|
||||||
page: page,
|
page: page,
|
||||||
sort: 'TopAll',
|
sort: 'TopAll',
|
||||||
@@ -19,7 +19,7 @@ export async function load({ url }) {
|
|||||||
auth: get(profile)?.jwt,
|
auth: get(profile)?.jwt,
|
||||||
})
|
})
|
||||||
} else {
|
} else {
|
||||||
return await getClient().listCommunities({
|
return await getClient(undefined, fetch).listCommunities({
|
||||||
limit: 40,
|
limit: 40,
|
||||||
page: page,
|
page: page,
|
||||||
sort: 'TopAll',
|
sort: 'TopAll',
|
||||||
|
|||||||
@@ -5,13 +5,13 @@ import { get } from 'svelte/store'
|
|||||||
|
|
||||||
type InboxFeedType = 'replies' | 'mentions' | 'messages' | 'all'
|
type InboxFeedType = 'replies' | 'mentions' | 'messages' | 'all'
|
||||||
|
|
||||||
export async function load({ url }) {
|
export async function load({ url, fetch }) {
|
||||||
const auth = get(profile)
|
const auth = get(profile)
|
||||||
if (!auth?.jwt) return
|
if (!auth?.jwt) return
|
||||||
|
|
||||||
const type: InboxFeedType =
|
const type: InboxFeedType =
|
||||||
(url.searchParams.get('type') as InboxFeedType) || 'all'
|
(url.searchParams.get('type') as InboxFeedType) || 'all'
|
||||||
const client = getClient()
|
const client = getClient(undefined, fetch)
|
||||||
const page = Number(url.searchParams.get('page')) || 1
|
const page = Number(url.searchParams.get('page')) || 1
|
||||||
const unreadOnly: boolean =
|
const unreadOnly: boolean =
|
||||||
(url.searchParams.get('unreadOnly') || 'true') == 'true'
|
(url.searchParams.get('unreadOnly') || 'true') == 'true'
|
||||||
|
|||||||
@@ -6,7 +6,7 @@ import { get } from 'svelte/store'
|
|||||||
type ReportListType = 'unread' | 'all'
|
type ReportListType = 'unread' | 'all'
|
||||||
type ReportFilter = 'all' | 'posts' | 'comments' | 'messages'
|
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 page = Number(url.searchParams.get('page')) || 1
|
||||||
const type: ReportListType =
|
const type: ReportListType =
|
||||||
(url.searchParams.get('type') as ReportListType) || 'unread'
|
(url.searchParams.get('type') as ReportListType) || 'unread'
|
||||||
@@ -24,7 +24,7 @@ export async function load({ url }) {
|
|||||||
// the interfaces don't even extend each other
|
// the interfaces don't even extend each other
|
||||||
// meaning I have to make a separate function to get dates
|
// meaning I have to make a separate function to get dates
|
||||||
// for EACH FREAKING ITEM KAJSHDAWLD auwdi awiody
|
// for EACH FREAKING ITEM KAJSHDAWLD auwdi awiody
|
||||||
const client = getClient()
|
const client = getClient(undefined, fetch)
|
||||||
|
|
||||||
const params = {
|
const params = {
|
||||||
auth: jwt,
|
auth: jwt,
|
||||||
@@ -42,10 +42,7 @@ export async function load({ url }) {
|
|||||||
}),
|
}),
|
||||||
])
|
])
|
||||||
|
|
||||||
const everything = [
|
const everything = [...posts.post_reports, ...comments.comment_reports].sort(
|
||||||
...posts.post_reports,
|
|
||||||
...comments.comment_reports,
|
|
||||||
].sort(
|
|
||||||
(a, b) => Date.parse(getItemPublished(b)) - Date.parse(getItemPublished(a))
|
(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 { error } from '@sveltejs/kit'
|
||||||
import { get } from 'svelte/store'
|
import { get } from 'svelte/store'
|
||||||
|
|
||||||
export async function load({ params, url }) {
|
export async function load({ params, url, fetch }) {
|
||||||
const post = await getClient(params.instance.toLowerCase()).getPost({
|
const post = await getClient(params.instance.toLowerCase(), fetch).getPost({
|
||||||
id: Number(params.id),
|
id: Number(params.id),
|
||||||
auth: get(profile)?.jwt,
|
auth: get(profile)?.jwt,
|
||||||
})
|
})
|
||||||
@@ -30,7 +30,7 @@ export async function load({ params, url }) {
|
|||||||
singleThread: parentId != undefined,
|
singleThread: parentId != undefined,
|
||||||
post: post,
|
post: post,
|
||||||
streamed: {
|
streamed: {
|
||||||
comments: getClient(params.instance.toLowerCase()).getComments({
|
comments: getClient(params.instance.toLowerCase(), fetch).getComments({
|
||||||
post_id: Number(params.id),
|
post_id: Number(params.id),
|
||||||
type_: 'All',
|
type_: 'All',
|
||||||
limit: 25,
|
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: [] }
|
if (!get(profile)) return { posts: [] }
|
||||||
const page = Number(url.searchParams.get('page')) || 1
|
const page = Number(url.searchParams.get('page')) || 1
|
||||||
|
|
||||||
const client = getClient()
|
const client = getClient(undefined, fetch)
|
||||||
|
|
||||||
const [posts, comments] = await Promise.all([
|
const [posts, comments] = await Promise.all([
|
||||||
client.getPosts({
|
client.getPosts({
|
||||||
|
|||||||
@@ -12,7 +12,7 @@ import type {
|
|||||||
} from 'lemmy-js-client'
|
} from 'lemmy-js-client'
|
||||||
import { get } from 'svelte/store'
|
import { get } from 'svelte/store'
|
||||||
|
|
||||||
export async function load({ url }) {
|
export async function load({ url, fetch }) {
|
||||||
const query = url.searchParams.get('q')
|
const query = url.searchParams.get('q')
|
||||||
const page = Number(url.searchParams.get('page')) || 1
|
const page = Number(url.searchParams.get('page')) || 1
|
||||||
const community = url.searchParams.get('community_name')
|
const community = url.searchParams.get('community_name')
|
||||||
@@ -20,7 +20,7 @@ export async function load({ url }) {
|
|||||||
const type = url.searchParams.get('type')
|
const type = url.searchParams.get('type')
|
||||||
|
|
||||||
if (query) {
|
if (query) {
|
||||||
const results = await getClient().search({
|
const results = await getClient(undefined, fetch).search({
|
||||||
q: query,
|
q: query,
|
||||||
auth: get(profile)?.jwt,
|
auth: get(profile)?.jwt,
|
||||||
community_name: community ?? undefined,
|
community_name: community ?? undefined,
|
||||||
@@ -48,7 +48,7 @@ export async function load({ url }) {
|
|||||||
results: everything,
|
results: everything,
|
||||||
streamed: {
|
streamed: {
|
||||||
object: get(profile)?.jwt
|
object: get(profile)?.jwt
|
||||||
? getClient().resolveObject({
|
? getClient(undefined, fetch).resolveObject({
|
||||||
auth: get(profile)!.jwt!,
|
auth: get(profile)!.jwt!,
|
||||||
q: query,
|
q: query,
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -4,13 +4,13 @@ import { getItemPublished } from '$lib/lemmy/item.js'
|
|||||||
import type { SortType } from 'lemmy-js-client'
|
import type { SortType } from 'lemmy-js-client'
|
||||||
import { get } from 'svelte/store'
|
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 page = Number(url.searchParams.get('page')) || 1
|
||||||
const type: 'comments' | 'posts' | 'all' =
|
const type: 'comments' | 'posts' | 'all' =
|
||||||
(url.searchParams.get('type') as 'comments' | 'posts' | 'all') || 'all'
|
(url.searchParams.get('type') as 'comments' | 'posts' | 'all') || 'all'
|
||||||
const sort: SortType = (url.searchParams.get('sort') as SortType) || 'New'
|
const sort: SortType = (url.searchParams.get('sort') as SortType) || 'New'
|
||||||
|
|
||||||
const user = await getClient().getPersonDetails({
|
const user = await getClient(undefined, fetch).getPersonDetails({
|
||||||
limit: 20,
|
limit: 20,
|
||||||
page: page,
|
page: page,
|
||||||
username: params.name,
|
username: params.name,
|
||||||
|
|||||||
Reference in new issue
Block a user