pl-api: make getLinks not parse the same twice

Signed-off-by: nicole mikołajczyk <git@mkljczk.pl>
This commit is contained in:
nicole mikołajczyk
2026-03-18 16:45:37 +01:00
parent cee7d9c065
commit decefd73ac
3 changed files with 20 additions and 17 deletions

View File

@ -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<any>) =>
new PaginatedResponse<T, IsArray>(
const processResponse = (response: PlApiResponse<any>) => {
const { prev: prevLink, next: nextLink } = getLinks(response);
return new PaginatedResponse<T, IsArray>(
v.parse(targetSchema, response.json) as IsArray extends true ? Array<T> : 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;

View File

@ -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 <T>(
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,

View File

@ -22,14 +22,17 @@ type Response<T = any> = {
@param {object} response - Fetch API response object
@returns {object} Link object
*/
const getLinks = (response: Pick<Response, 'headers'>): LinkHeader =>
new LinkHeader(response.headers?.get('link') || undefined);
const getLinks = (
response: Pick<Response, 'headers'>,
): { next: string | null; prev: string | null } => {
const headers = response.headers?.get('link');
const linkHeader = (headers && new LinkHeader(headers)) || null;
const getNextLink = (response: Pick<Response, 'headers'>): string | null =>
getLinks(response).refs.find((link) => link.rel.toLocaleLowerCase() === 'next')?.uri || null;
const getPrevLink = (response: Pick<Response, 'headers'>): 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,
};