pl-api: cleanup
Signed-off-by: marcin mikołajczak <git@mkljczk.pl>
This commit is contained in:
parent
05fff4c381
commit
071c46e09a
6 changed files with 27 additions and 56 deletions
|
@ -206,6 +206,7 @@ import type {
|
|||
Status,
|
||||
StreamingEvent,
|
||||
} from './entities';
|
||||
import type { PlApiResponse } from './main';
|
||||
import type {
|
||||
AdminAccountAction,
|
||||
AdminCreateAnnouncementParams,
|
||||
|
@ -235,7 +236,7 @@ import type {
|
|||
AdminUpdateRuleParams,
|
||||
AdminUpdateStatusParams,
|
||||
} from './params/admin';
|
||||
import type { PaginatedResponse, PaginatedSingleResponse } from './responses';
|
||||
import type { PaginatedResponse } from './responses';
|
||||
|
||||
const GROUPED_TYPES = ['favourite', 'reblog', 'emoji_reaction', 'event_reminder', 'participation_accepted', 'participation_request'];
|
||||
|
||||
|
@ -279,48 +280,21 @@ class PlApiClient {
|
|||
}
|
||||
}
|
||||
|
||||
#paginatedGet = async <T>(input: URL | RequestInfo, body: RequestBody, schema: v.BaseSchema<any, T, v.BaseIssue<unknown>>): Promise<PaginatedResponse<T>> => {
|
||||
const getMore = (input: string | null) => input ? async () => {
|
||||
const response = await this.request(input);
|
||||
#paginatedGet = async <T, IsArray extends true | false = true>(input: URL | RequestInfo, body: RequestBody, schema: v.BaseSchema<any, T, v.BaseIssue<unknown>>, isArray = true as IsArray): Promise<PaginatedResponse<T, typeof isArray>> => {
|
||||
const targetSchema = isArray ? filteredArray(schema) : schema;
|
||||
|
||||
return {
|
||||
previous: getMore(getPrevLink(response)),
|
||||
next: getMore(getNextLink(response)),
|
||||
items: v.parse(filteredArray(schema), response.json),
|
||||
partial: response.status === 206,
|
||||
};
|
||||
} : null;
|
||||
const processResponse = (response: PlApiResponse<any>) => ({
|
||||
previous: getMore(getPrevLink(response)),
|
||||
next: getMore(getNextLink(response)),
|
||||
items: v.parse(targetSchema, response.json),
|
||||
partial: response.status === 206,
|
||||
} as PaginatedResponse<T, IsArray>);
|
||||
|
||||
const getMore = (input: string | null) => input ? () => this.request(input).then(processResponse) : null;
|
||||
|
||||
const response = await this.request(input, body);
|
||||
|
||||
return {
|
||||
previous: getMore(getPrevLink(response)),
|
||||
next: getMore(getNextLink(response)),
|
||||
items: v.parse(filteredArray(schema), response.json),
|
||||
partial: response.status === 206,
|
||||
};
|
||||
};
|
||||
|
||||
#paginatedSingleGet = async <T>(input: URL | RequestInfo, body: RequestBody, schema: v.BaseSchema<any, T, v.BaseIssue<unknown>>): Promise<PaginatedSingleResponse<T>> => {
|
||||
const getMore = (input: string | null) => input ? async () => {
|
||||
const response = await this.request(input);
|
||||
|
||||
return {
|
||||
previous: getMore(getPrevLink(response)),
|
||||
next: getMore(getNextLink(response)),
|
||||
items: v.parse(schema, response.json),
|
||||
partial: response.status === 206,
|
||||
};
|
||||
} : null;
|
||||
|
||||
const response = await this.request(input, body);
|
||||
|
||||
return {
|
||||
previous: getMore(getPrevLink(response)),
|
||||
next: getMore(getNextLink(response)),
|
||||
items: v.parse(schema, response.json),
|
||||
partial: response.status === 206,
|
||||
};
|
||||
return processResponse(response);
|
||||
};
|
||||
|
||||
#paginatedPleromaAccounts = async (params: {
|
||||
|
@ -384,7 +358,7 @@ class PlApiClient {
|
|||
};
|
||||
};
|
||||
|
||||
#groupNotifications = ({ previous, next, items, ...response }: PaginatedResponse<Notification>, params?: GetGroupedNotificationsParams): PaginatedSingleResponse<GroupedNotificationsResults> => {
|
||||
#groupNotifications = ({ previous, next, items, ...response }: PaginatedResponse<Notification>, params?: GetGroupedNotificationsParams): PaginatedResponse<GroupedNotificationsResults, false> => {
|
||||
const notificationGroups: Array<NotificationGroup> = [];
|
||||
|
||||
for (const notification of items) {
|
||||
|
@ -2808,7 +2782,7 @@ class PlApiClient {
|
|||
*/
|
||||
getGroupedNotifications: async (params: GetGroupedNotificationsParams, meta?: RequestMeta) => {
|
||||
if (this.features.groupedNotifications) {
|
||||
return this.#paginatedSingleGet('/api/v2/notifications', { ...meta, params }, groupedNotificationsResultsSchema);
|
||||
return this.#paginatedGet('/api/v2/notifications', { ...meta, params }, groupedNotificationsResultsSchema, false);
|
||||
} else {
|
||||
const response = await this.notifications.getNotifications(
|
||||
pick(params, ['max_id', 'since_id', 'limit', 'min_id', 'types', 'exclude_types', 'account_id', 'include_filtered']),
|
||||
|
|
|
@ -1,14 +1,11 @@
|
|||
interface PaginatedSingleResponse<T> {
|
||||
previous: (() => Promise<PaginatedSingleResponse<T>>) | null;
|
||||
next: (() => Promise<PaginatedSingleResponse<T>>) | null;
|
||||
items: T;
|
||||
interface PaginatedResponse<T, IsArray extends boolean = true> {
|
||||
previous: (() => Promise<PaginatedResponse<T, IsArray>>) | null;
|
||||
next: (() => Promise<PaginatedResponse<T, IsArray>>) | null;
|
||||
items: IsArray extends true ? Array<T> : T;
|
||||
partial: boolean;
|
||||
total?: number;
|
||||
}
|
||||
|
||||
type PaginatedResponse<T> = PaginatedSingleResponse<Array<T>>;
|
||||
|
||||
export type {
|
||||
PaginatedSingleResponse,
|
||||
PaginatedResponse,
|
||||
};
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
{
|
||||
"name": "pl-api",
|
||||
"version": "0.1.8",
|
||||
"version": "0.1.9",
|
||||
"type": "module",
|
||||
"homepage": "https://github.com/mkljczk/pl-fe/tree/develop/packages/pl-api",
|
||||
"repository": {
|
||||
|
|
|
@ -102,7 +102,7 @@
|
|||
"mini-css-extract-plugin": "^2.9.1",
|
||||
"multiselect-react-dropdown": "^2.0.25",
|
||||
"path-browserify": "^1.0.1",
|
||||
"pl-api": "^0.1.8",
|
||||
"pl-api": "^0.1.9",
|
||||
"postcss": "^8.4.47",
|
||||
"process": "^0.11.10",
|
||||
"punycode": "^2.1.1",
|
||||
|
|
|
@ -18,7 +18,7 @@ import { importEntities } from './importer';
|
|||
import { saveMarker } from './markers';
|
||||
import { saveSettings } from './settings';
|
||||
|
||||
import type { Notification as BaseNotification, GetGroupedNotificationsParams, GroupedNotificationsResults, NotificationGroup, PaginatedSingleResponse } from 'pl-api';
|
||||
import type { Notification as BaseNotification, GetGroupedNotificationsParams, GroupedNotificationsResults, NotificationGroup, PaginatedResponse } from 'pl-api';
|
||||
import type { AppDispatch, RootState } from 'pl-fe/store';
|
||||
|
||||
const NOTIFICATIONS_UPDATE = 'NOTIFICATIONS_UPDATE' as const;
|
||||
|
@ -240,7 +240,7 @@ const expandNotifications = ({ maxId }: Record<string, any> = {}, done: () => an
|
|||
|
||||
const expandNotificationsRequest = () => ({ type: NOTIFICATIONS_EXPAND_REQUEST });
|
||||
|
||||
const expandNotificationsSuccess = (notifications: Array<NotificationGroup>, next: (() => Promise<PaginatedSingleResponse<GroupedNotificationsResults>>) | null) => ({
|
||||
const expandNotificationsSuccess = (notifications: Array<NotificationGroup>, next: (() => Promise<PaginatedResponse<GroupedNotificationsResults, false>>) | null) => ({
|
||||
type: NOTIFICATIONS_EXPAND_SUCCESS,
|
||||
notifications,
|
||||
next,
|
||||
|
|
|
@ -7575,10 +7575,10 @@ pkg-dir@^4.1.0:
|
|||
dependencies:
|
||||
find-up "^4.0.0"
|
||||
|
||||
pl-api@^0.1.8:
|
||||
version "0.1.8"
|
||||
resolved "https://registry.yarnpkg.com/pl-api/-/pl-api-0.1.8.tgz#f8fa5bcd50fb2cf322dc345b7658d232b330d73a"
|
||||
integrity sha512-RsSYXjgOM4IjyVnDyJxvnmC64APCRwT6+J9q7qDu4ZfxR4kSgoDSMUoMDzdkUFXQ9E3I2o4Nt2x+sd2Dpob9BA==
|
||||
pl-api@^0.1.9:
|
||||
version "0.1.9"
|
||||
resolved "https://registry.yarnpkg.com/pl-api/-/pl-api-0.1.9.tgz#250aacb4533de0acbe0aa487c2810806a2d99415"
|
||||
integrity sha512-5r+TZXbEBU9BednnYXxdI1KgnvRSy/lhSLLU8ZsmMgapy+IyxG2yGmxSc7j0NuC59kBYxlKKwaRgdRvwclQOaQ==
|
||||
dependencies:
|
||||
blurhash "^2.0.5"
|
||||
http-link-header "^1.1.3"
|
||||
|
|
Loading…
Reference in a new issue