diff --git a/packages/api/src/articleCounter.ts b/packages/api/src/articleCounter.ts index 94be43b007e..8cd8c2f5a3b 100644 --- a/packages/api/src/articleCounter.ts +++ b/packages/api/src/articleCounter.ts @@ -29,11 +29,23 @@ export interface GetArticleCounterOptions extends BaseAPIOptions { signal?: AbortSignal; } -export type GetArticleCounterResponse = - | Record[] - | Record - | number[] - | number; +export interface CounterFields { + time?: number; + reaction0?: number; + reaction1?: number; + reaction2?: number; + reaction3?: number; + reaction4?: number; + reaction5?: number; + reaction6?: number; + reaction7?: number; + reaction8?: number; +} + +export type GetArticleCounterResponseItem = Record & + CounterFields; + +export type GetArticleCounterResponse = GetArticleCounterResponseItem[]; export const getArticleCounter = ({ serverURL, @@ -80,13 +92,16 @@ export const updateArticleCounter = ({ path, type, action, -}: UpdateArticleCounterOptions): Promise => +}: UpdateArticleCounterOptions): Promise => fetch(`${getFetchPrefix(serverURL)}article?lang=${lang}`, { method: 'POST', headers: JSON_HEADERS, body: JSON.stringify({ path, type, action }), }) .then( - (resp) => >resp.json(), + (resp) => + >( + resp.json() + ), ) .then((data) => errorCheck(data, 'Update counter').data); diff --git a/packages/api/src/pageview.ts b/packages/api/src/pageview.ts index 23c90838b1b..58a0aa39d77 100644 --- a/packages/api/src/pageview.ts +++ b/packages/api/src/pageview.ts @@ -22,18 +22,14 @@ export const getPageview = ({ lang, paths, signal, -}: GetPageviewOptions): Promise => +}: GetPageviewOptions) => getArticleCounter({ serverURL, lang, paths, type: ['time'], signal, - }) - // TODO: Improve this API - .then((counts) => (Array.isArray(counts) ? counts : [counts])) as Promise< - number[] - >; + }); export interface UpdatePageviewOptions extends BaseAPIOptions { /** @@ -44,9 +40,7 @@ export interface UpdatePageviewOptions extends BaseAPIOptions { path: string; } -export const updatePageview = ( - options: UpdatePageviewOptions, -): Promise => +export const updatePageview = (options: UpdatePageviewOptions) => updateArticleCounter({ ...options, type: 'time', diff --git a/packages/client/src/pageview.ts b/packages/client/src/pageview.ts index c418d08dbef..aec7243e8dd 100644 --- a/packages/client/src/pageview.ts +++ b/packages/client/src/pageview.ts @@ -1,4 +1,8 @@ -import { getPageview, updatePageview } from '@waline/api'; +import { + GetArticleCounterResponse, + getPageview, + updatePageview, +} from '@waline/api'; import { type WalineAbort } from './typings/index.js'; import { errorHandler, getQuery, getServerURL } from './utils/index.js'; @@ -51,11 +55,17 @@ export interface WalinePageviewCountOptions { export { type WalineAbort } from './typings/index.js'; const renderVisitorCount = ( - counts: number[], + counts: GetArticleCounterResponse, countElements: HTMLElement[], ): void => { countElements.forEach((element, index) => { - element.innerText = counts[index].toString(); + const count = counts[index].time; + + if (typeof count !== 'number') { + return; + } + + element.innerText = count.toString(); }); }; @@ -98,12 +108,7 @@ export const pageviewCount = ({ serverURL: getServerURL(serverURL), path, lang, - }).then(([count]) => - renderVisitorCount( - new Array(normalElements.length).fill(count), - normalElements, - ), - ); + }).then((counts) => renderVisitorCount(counts, normalElements)); // if we should fetch count of other pages if (elementsNeedstoBeFetched.length) { diff --git a/packages/server/src/controller/article.js b/packages/server/src/controller/article.js index 94f2d57f4c7..c205f2452c8 100644 --- a/packages/server/src/controller/article.js +++ b/packages/server/src/controller/article.js @@ -88,6 +88,8 @@ module.exports = class extends BaseRest { { objectId: ['IN', resp.map(({ objectId }) => objectId)] }, ); - return this.jsonOrSuccess(deprecated ? ret[0][type] : [ret[0][type]]); + return this.jsonOrSuccess( + deprecated ? ret[0][type] : [{ [type]: ret[0][type] }], + ); } };