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,
|
Status,
|
||||||
StreamingEvent,
|
StreamingEvent,
|
||||||
} from './entities';
|
} from './entities';
|
||||||
|
import type { PlApiResponse } from './main';
|
||||||
import type {
|
import type {
|
||||||
AdminAccountAction,
|
AdminAccountAction,
|
||||||
AdminCreateAnnouncementParams,
|
AdminCreateAnnouncementParams,
|
||||||
|
@ -235,7 +236,7 @@ import type {
|
||||||
AdminUpdateRuleParams,
|
AdminUpdateRuleParams,
|
||||||
AdminUpdateStatusParams,
|
AdminUpdateStatusParams,
|
||||||
} from './params/admin';
|
} 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'];
|
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>> => {
|
#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 getMore = (input: string | null) => input ? async () => {
|
const targetSchema = isArray ? filteredArray(schema) : schema;
|
||||||
const response = await this.request(input);
|
|
||||||
|
|
||||||
return {
|
const processResponse = (response: PlApiResponse<any>) => ({
|
||||||
previous: getMore(getPrevLink(response)),
|
previous: getMore(getPrevLink(response)),
|
||||||
next: getMore(getNextLink(response)),
|
next: getMore(getNextLink(response)),
|
||||||
items: v.parse(filteredArray(schema), response.json),
|
items: v.parse(targetSchema, response.json),
|
||||||
partial: response.status === 206,
|
partial: response.status === 206,
|
||||||
};
|
} as PaginatedResponse<T, IsArray>);
|
||||||
} : null;
|
|
||||||
|
const getMore = (input: string | null) => input ? () => this.request(input).then(processResponse) : null;
|
||||||
|
|
||||||
const response = await this.request(input, body);
|
const response = await this.request(input, body);
|
||||||
|
|
||||||
return {
|
return processResponse(response);
|
||||||
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,
|
|
||||||
};
|
|
||||||
};
|
};
|
||||||
|
|
||||||
#paginatedPleromaAccounts = async (params: {
|
#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> = [];
|
const notificationGroups: Array<NotificationGroup> = [];
|
||||||
|
|
||||||
for (const notification of items) {
|
for (const notification of items) {
|
||||||
|
@ -2808,7 +2782,7 @@ class PlApiClient {
|
||||||
*/
|
*/
|
||||||
getGroupedNotifications: async (params: GetGroupedNotificationsParams, meta?: RequestMeta) => {
|
getGroupedNotifications: async (params: GetGroupedNotificationsParams, meta?: RequestMeta) => {
|
||||||
if (this.features.groupedNotifications) {
|
if (this.features.groupedNotifications) {
|
||||||
return this.#paginatedSingleGet('/api/v2/notifications', { ...meta, params }, groupedNotificationsResultsSchema);
|
return this.#paginatedGet('/api/v2/notifications', { ...meta, params }, groupedNotificationsResultsSchema, false);
|
||||||
} else {
|
} else {
|
||||||
const response = await this.notifications.getNotifications(
|
const response = await this.notifications.getNotifications(
|
||||||
pick(params, ['max_id', 'since_id', 'limit', 'min_id', 'types', 'exclude_types', 'account_id', 'include_filtered']),
|
pick(params, ['max_id', 'since_id', 'limit', 'min_id', 'types', 'exclude_types', 'account_id', 'include_filtered']),
|
||||||
|
|
|
@ -1,14 +1,11 @@
|
||||||
interface PaginatedSingleResponse<T> {
|
interface PaginatedResponse<T, IsArray extends boolean = true> {
|
||||||
previous: (() => Promise<PaginatedSingleResponse<T>>) | null;
|
previous: (() => Promise<PaginatedResponse<T, IsArray>>) | null;
|
||||||
next: (() => Promise<PaginatedSingleResponse<T>>) | null;
|
next: (() => Promise<PaginatedResponse<T, IsArray>>) | null;
|
||||||
items: T;
|
items: IsArray extends true ? Array<T> : T;
|
||||||
partial: boolean;
|
partial: boolean;
|
||||||
total?: number;
|
total?: number;
|
||||||
}
|
}
|
||||||
|
|
||||||
type PaginatedResponse<T> = PaginatedSingleResponse<Array<T>>;
|
|
||||||
|
|
||||||
export type {
|
export type {
|
||||||
PaginatedSingleResponse,
|
|
||||||
PaginatedResponse,
|
PaginatedResponse,
|
||||||
};
|
};
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
{
|
{
|
||||||
"name": "pl-api",
|
"name": "pl-api",
|
||||||
"version": "0.1.8",
|
"version": "0.1.9",
|
||||||
"type": "module",
|
"type": "module",
|
||||||
"homepage": "https://github.com/mkljczk/pl-fe/tree/develop/packages/pl-api",
|
"homepage": "https://github.com/mkljczk/pl-fe/tree/develop/packages/pl-api",
|
||||||
"repository": {
|
"repository": {
|
||||||
|
|
|
@ -102,7 +102,7 @@
|
||||||
"mini-css-extract-plugin": "^2.9.1",
|
"mini-css-extract-plugin": "^2.9.1",
|
||||||
"multiselect-react-dropdown": "^2.0.25",
|
"multiselect-react-dropdown": "^2.0.25",
|
||||||
"path-browserify": "^1.0.1",
|
"path-browserify": "^1.0.1",
|
||||||
"pl-api": "^0.1.8",
|
"pl-api": "^0.1.9",
|
||||||
"postcss": "^8.4.47",
|
"postcss": "^8.4.47",
|
||||||
"process": "^0.11.10",
|
"process": "^0.11.10",
|
||||||
"punycode": "^2.1.1",
|
"punycode": "^2.1.1",
|
||||||
|
|
|
@ -18,7 +18,7 @@ import { importEntities } from './importer';
|
||||||
import { saveMarker } from './markers';
|
import { saveMarker } from './markers';
|
||||||
import { saveSettings } from './settings';
|
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';
|
import type { AppDispatch, RootState } from 'pl-fe/store';
|
||||||
|
|
||||||
const NOTIFICATIONS_UPDATE = 'NOTIFICATIONS_UPDATE' as const;
|
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 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,
|
type: NOTIFICATIONS_EXPAND_SUCCESS,
|
||||||
notifications,
|
notifications,
|
||||||
next,
|
next,
|
||||||
|
|
|
@ -7575,10 +7575,10 @@ pkg-dir@^4.1.0:
|
||||||
dependencies:
|
dependencies:
|
||||||
find-up "^4.0.0"
|
find-up "^4.0.0"
|
||||||
|
|
||||||
pl-api@^0.1.8:
|
pl-api@^0.1.9:
|
||||||
version "0.1.8"
|
version "0.1.9"
|
||||||
resolved "https://registry.yarnpkg.com/pl-api/-/pl-api-0.1.8.tgz#f8fa5bcd50fb2cf322dc345b7658d232b330d73a"
|
resolved "https://registry.yarnpkg.com/pl-api/-/pl-api-0.1.9.tgz#250aacb4533de0acbe0aa487c2810806a2d99415"
|
||||||
integrity sha512-RsSYXjgOM4IjyVnDyJxvnmC64APCRwT6+J9q7qDu4ZfxR4kSgoDSMUoMDzdkUFXQ9E3I2o4Nt2x+sd2Dpob9BA==
|
integrity sha512-5r+TZXbEBU9BednnYXxdI1KgnvRSy/lhSLLU8ZsmMgapy+IyxG2yGmxSc7j0NuC59kBYxlKKwaRgdRvwclQOaQ==
|
||||||
dependencies:
|
dependencies:
|
||||||
blurhash "^2.0.5"
|
blurhash "^2.0.5"
|
||||||
http-link-header "^1.1.3"
|
http-link-header "^1.1.3"
|
||||||
|
|
Loading…
Reference in a new issue