diff --git a/packages/pl-api/lib/client.ts b/packages/pl-api/lib/client.ts index 97a81f7ff..0fa26a7ab 100644 --- a/packages/pl-api/lib/client.ts +++ b/packages/pl-api/lib/client.ts @@ -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 (input: URL | RequestInfo, body: RequestBody, schema: v.BaseSchema>): Promise> => { - const getMore = (input: string | null) => input ? async () => { - const response = await this.request(input); + #paginatedGet = async (input: URL | RequestInfo, body: RequestBody, schema: v.BaseSchema>, isArray = true as IsArray): Promise> => { + 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) => ({ + previous: getMore(getPrevLink(response)), + next: getMore(getNextLink(response)), + items: v.parse(targetSchema, response.json), + partial: response.status === 206, + } as PaginatedResponse); + + 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 (input: URL | RequestInfo, body: RequestBody, schema: v.BaseSchema>): Promise> => { - 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, params?: GetGroupedNotificationsParams): PaginatedSingleResponse => { + #groupNotifications = ({ previous, next, items, ...response }: PaginatedResponse, params?: GetGroupedNotificationsParams): PaginatedResponse => { const notificationGroups: Array = []; 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']), diff --git a/packages/pl-api/lib/responses.ts b/packages/pl-api/lib/responses.ts index 34767248c..4a44ad3a5 100644 --- a/packages/pl-api/lib/responses.ts +++ b/packages/pl-api/lib/responses.ts @@ -1,14 +1,11 @@ -interface PaginatedSingleResponse { - previous: (() => Promise>) | null; - next: (() => Promise>) | null; - items: T; +interface PaginatedResponse { + previous: (() => Promise>) | null; + next: (() => Promise>) | null; + items: IsArray extends true ? Array : T; partial: boolean; total?: number; } -type PaginatedResponse = PaginatedSingleResponse>; - export type { - PaginatedSingleResponse, PaginatedResponse, }; diff --git a/packages/pl-api/package.json b/packages/pl-api/package.json index 581a227fb..c80357d11 100644 --- a/packages/pl-api/package.json +++ b/packages/pl-api/package.json @@ -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": { diff --git a/packages/pl-fe/package.json b/packages/pl-fe/package.json index b08ffff32..32664306c 100644 --- a/packages/pl-fe/package.json +++ b/packages/pl-fe/package.json @@ -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", diff --git a/packages/pl-fe/src/actions/notifications.ts b/packages/pl-fe/src/actions/notifications.ts index ea862b9f5..fa2250c36 100644 --- a/packages/pl-fe/src/actions/notifications.ts +++ b/packages/pl-fe/src/actions/notifications.ts @@ -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 = {}, done: () => an const expandNotificationsRequest = () => ({ type: NOTIFICATIONS_EXPAND_REQUEST }); -const expandNotificationsSuccess = (notifications: Array, next: (() => Promise>) | null) => ({ +const expandNotificationsSuccess = (notifications: Array, next: (() => Promise>) | null) => ({ type: NOTIFICATIONS_EXPAND_SUCCESS, notifications, next, diff --git a/packages/pl-fe/yarn.lock b/packages/pl-fe/yarn.lock index 75d3a8d00..4d8e2154f 100644 --- a/packages/pl-fe/yarn.lock +++ b/packages/pl-fe/yarn.lock @@ -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"