Skip to content

Commit

Permalink
[fix] pageview api struct refactor (#2343)
Browse files Browse the repository at this point in the history
  • Loading branch information
lizheming authored Feb 19, 2024
1 parent 2b13036 commit 3f740f5
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 26 deletions.
29 changes: 22 additions & 7 deletions packages/api/src/articleCounter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,23 @@ export interface GetArticleCounterOptions extends BaseAPIOptions {
signal?: AbortSignal;
}

export type GetArticleCounterResponse =
| Record<string, number>[]
| Record<string, number>
| 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<string, number> &
CounterFields;

export type GetArticleCounterResponse = GetArticleCounterResponseItem[];

export const getArticleCounter = ({
serverURL,
Expand Down Expand Up @@ -80,13 +92,16 @@ export const updateArticleCounter = ({
path,
type,
action,
}: UpdateArticleCounterOptions): Promise<number[]> =>
}: UpdateArticleCounterOptions): Promise<GetArticleCounterResponse> =>
fetch(`${getFetchPrefix(serverURL)}article?lang=${lang}`, {
method: 'POST',
headers: JSON_HEADERS,
body: JSON.stringify({ path, type, action }),
})
.then(
(resp) => <Promise<{ data: number[] } & ErrorStatusResponse>>resp.json(),
(resp) =>
<Promise<{ data: GetArticleCounterResponse } & ErrorStatusResponse>>(
resp.json()
),
)
.then((data) => errorCheck(data, 'Update counter').data);
12 changes: 3 additions & 9 deletions packages/api/src/pageview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,14 @@ export const getPageview = ({
lang,
paths,
signal,
}: GetPageviewOptions): Promise<number[]> =>
}: GetPageviewOptions) =>

Check warning on line 25 in packages/api/src/pageview.ts

View workflow job for this annotation

GitHub Actions / test (18, ubuntu-latest)

Missing return type on function

Check warning on line 25 in packages/api/src/pageview.ts

View workflow job for this annotation

GitHub Actions / test (20, ubuntu-latest)

Missing return type on function
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 {
/**
Expand All @@ -44,9 +40,7 @@ export interface UpdatePageviewOptions extends BaseAPIOptions {
path: string;
}

export const updatePageview = (
options: UpdatePageviewOptions,
): Promise<number[]> =>
export const updatePageview = (options: UpdatePageviewOptions) =>

Check warning on line 43 in packages/api/src/pageview.ts

View workflow job for this annotation

GitHub Actions / test (18, ubuntu-latest)

Missing return type on function

Check warning on line 43 in packages/api/src/pageview.ts

View workflow job for this annotation

GitHub Actions / test (20, ubuntu-latest)

Missing return type on function
updateArticleCounter({
...options,
type: 'time',
Expand Down
23 changes: 14 additions & 9 deletions packages/client/src/pageview.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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();
});
};

Expand Down Expand Up @@ -98,12 +108,7 @@ export const pageviewCount = ({
serverURL: getServerURL(serverURL),
path,
lang,
}).then(([count]) =>
renderVisitorCount(
new Array<number>(normalElements.length).fill(count),
normalElements,
),
);
}).then((counts) => renderVisitorCount(counts, normalElements));

// if we should fetch count of other pages
if (elementsNeedstoBeFetched.length) {
Expand Down
4 changes: 3 additions & 1 deletion packages/server/src/controller/article.js
Original file line number Diff line number Diff line change
Expand Up @@ -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] }],
);
}
};

0 comments on commit 3f740f5

Please sign in to comment.