diff --git a/packages/pl-api/lib/client-base.ts b/packages/pl-api/lib/client-base.ts index 9bcc579af..70ff502c3 100644 --- a/packages/pl-api/lib/client-base.ts +++ b/packages/pl-api/lib/client-base.ts @@ -3,7 +3,7 @@ import * as v from 'valibot'; import { instanceSchema } from '@/entities/instance'; import { filteredArray } from '@/entities/utils'; import { type Features, getFeatures } from '@/features'; -import request, { getNextLink, getPrevLink, type RequestBody } from '@/request'; +import request, { getLinks, type RequestBody } from '@/request'; import { PaginatedResponse } from '@/responses'; import type { Instance } from '@/entities/instance'; @@ -69,15 +69,18 @@ class PlApiBaseClient { ) => { const targetSchema = isArray ? filteredArray(schema) : schema; - const processResponse = (response: PlApiResponse) => - new PaginatedResponse( + const processResponse = (response: PlApiResponse) => { + const { prev: prevLink, next: nextLink } = getLinks(response); + + return new PaginatedResponse( v.parse(targetSchema, response.json) as IsArray extends true ? Array : T, { - previous: getMore(getPrevLink(response)), - next: getMore(getNextLink(response)), + previous: getMore(prevLink), + next: getMore(nextLink), partial: response.status === 206, }, ); + }; const getMore = (input: string | null) => input ? () => this.request(input).then(processResponse) : null; diff --git a/packages/pl-api/lib/client/my-account.ts b/packages/pl-api/lib/client/my-account.ts index f11c4a5cd..4dc233def 100644 --- a/packages/pl-api/lib/client/my-account.ts +++ b/packages/pl-api/lib/client/my-account.ts @@ -11,7 +11,7 @@ import { } from '@/entities'; import { filteredArray } from '@/entities/utils'; import { GOTOSOCIAL, ICESHRIMP_NET, MITRA, PIXELFED, PLEROMA } from '@/features'; -import { getNextLink, getPrevLink } from '@/request'; +import { getLinks } from '@/request'; import { PaginatedResponse } from '@/responses'; import type { accounts } from './accounts'; @@ -40,8 +40,7 @@ const paginatedIceshrimpAccountsList = async ( const items = await client.accounts.getAccounts(ids); - const prevLink = getPrevLink(response); - const nextLink = getNextLink(response); + const { prev: prevLink, next: nextLink } = getLinks(response); return new PaginatedResponse(items, { previous: prevLink ? () => paginatedIceshrimpAccountsList(client, prevLink, fn) : null, diff --git a/packages/pl-api/lib/request.ts b/packages/pl-api/lib/request.ts index cf0935ddf..25d0e1a6b 100644 --- a/packages/pl-api/lib/request.ts +++ b/packages/pl-api/lib/request.ts @@ -22,14 +22,17 @@ type Response = { @param {object} response - Fetch API response object @returns {object} Link object */ -const getLinks = (response: Pick): LinkHeader => - new LinkHeader(response.headers?.get('link') || undefined); +const getLinks = ( + response: Pick, +): { next: string | null; prev: string | null } => { + const headers = response.headers?.get('link'); + const linkHeader = (headers && new LinkHeader(headers)) || null; -const getNextLink = (response: Pick): string | null => - getLinks(response).refs.find((link) => link.rel.toLocaleLowerCase() === 'next')?.uri || null; - -const getPrevLink = (response: Pick): string | null => - getLinks(response).refs.find((link) => link.rel.toLocaleLowerCase() === 'prev')?.uri || null; + return { + next: linkHeader?.refs.find((link) => link.rel.toLocaleLowerCase() === 'next')?.uri || null, + prev: linkHeader?.refs.find((link) => link.rel.toLocaleLowerCase() === 'prev')?.uri || null, + }; +}; interface AsyncRefreshHeader { id: string; @@ -188,8 +191,6 @@ export { type RequestMeta, type AsyncRefreshHeader, getLinks, - getNextLink, - getPrevLink, getAsyncRefreshHeader, request as default, };