Skip to content

Commit

Permalink
* suffix all old references to types now extending Api with `['resp…
Browse files Browse the repository at this point in the history
…onse']` or `['queryParam']`

* exporting type `ApiErrorClass` and classes that it referring @ index.ts
* fix extending `CursorPagination['pages']` in type `ApiPosts` @ index.d.ts
@ api
* change type of prop `error` to `ApiErrorClass` @ `<PlaceholderError>`

* rename prop `isFetchingPosts` to `isLoading`, partial revert d3eb4e2 @ `<QueryForm>`
@ fe
  • Loading branch information
n0099 committed Feb 14, 2024
1 parent e9eaff0 commit b0c0ef5
Show file tree
Hide file tree
Showing 13 changed files with 34 additions and 29 deletions.
9 changes: 5 additions & 4 deletions fe/src/api/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,11 @@ export type ApiUsers = Api<
>;

export type JsonString = string;
export type ApiPosts = Api<CursorPagination<{
matchQueryPostCount: { [P in PostType]: UInt },
notMatchQueryParentPostCount: { [P in Omit<PostType, 'subRely'>]: UInt }
}> & {
export type ApiPosts = Api<CursorPagination & {
pages: {
matchQueryPostCount: { [P in PostType]: UInt },
notMatchQueryParentPostCount: { [P in Omit<PostType, 'subRely'>]: UInt }
},
type: 'index' | 'search',
forum: Pick<ApiForums[number], 'fid' | 'name'>,
threads: Array<Thread & {
Expand Down
6 changes: 3 additions & 3 deletions fe/src/api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ import nprogress from 'nprogress';
import { stringify } from 'qs';
import * as _ from 'lodash-es';

class ApiResponseError extends Error {
export class ApiResponseError extends Error {
public constructor(
public readonly errorCode: number,
public readonly errorInfo: Record<string, unknown[]> | string
) {
super(JSON.stringify({ errorCode, errorInfo }));
}
}
class FetchResponseError extends Error {
export class FetchResponseError extends Error {
public constructor(public readonly response: Response) {
super(JSON.stringify(response));
}
Expand Down Expand Up @@ -74,7 +74,7 @@ const getRequesterWithReCAPTCHA = <TResponse, TQueryParam>
{ ...queryParam as TQueryParam, ...await reCAPTCHACheck(action) }
)(queryContext);

type ApiErrorClass = ApiResponseError | FetchResponseError;
export type ApiErrorClass = ApiResponseError | FetchResponseError;
type ReqesuterGetter = typeof getRequester | typeof getRequesterWithReCAPTCHA;
const useApi = <
TApi extends Api<TResponse, TQueryParam>,
Expand Down
2 changes: 1 addition & 1 deletion fe/src/components/Post/PostNav.vue
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ import { FontAwesomeIcon } from '@fortawesome/vue-fontawesome';
import scrollIntoView from 'scroll-into-view-if-needed';
import * as _ from 'lodash-es';
const props = defineProps<{ postPages: ApiPosts[] }>();
const props = defineProps<{ postPages: Array<ApiPosts['response']> }>();
const route = useRoute();
const router = useRouter();
const elementRefsStore = useElementRefsStore();
Expand Down
2 changes: 1 addition & 1 deletion fe/src/components/Post/PostPage.vue
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import type { ApiPosts } from '@/api/index.d';
import type { RouteLocationNormalized } from 'vue-router';
const props = defineProps<{
posts: ApiPosts,
posts: ApiPosts['response'],
renderType: PostRenderer,
currentRoute: RouteLocationNormalized,
isLoadingNewPage: boolean,
Expand Down
6 changes: 3 additions & 3 deletions fe/src/components/Post/queryForm/QueryForm.vue
Original file line number Diff line number Diff line change
Expand Up @@ -154,8 +154,8 @@
<SelectParam @paramChange="e => addParam(e)" />
</div>
<div class="row mt-3">
<button :disabled="isFetchingPosts" class="col-auto btn btn-primary" type="submit">
查询 <span v-show="isFetchingPosts" class="spinner-grow spinner-grow-sm" role="status" aria-hidden="true" />
<button :disabled="isLoading" class="col-auto btn btn-primary" type="submit">
查询 <span v-show="isLoading" class="spinner-grow spinner-grow-sm" role="status" aria-hidden="true" />
</button>
<span class="col-auto ms-3 my-auto text-muted">{{ currentQueryTypeDescription }}</span>
</div>
Expand Down Expand Up @@ -185,7 +185,7 @@ import { RangePicker } from 'ant-design-vue';
import { FontAwesomeIcon } from '@fortawesome/vue-fontawesome';
import * as _ from 'lodash-es';
defineProps<{ isFetchingPosts: boolean }>();
defineProps<{ isLoading: boolean }>();
const router = useRouter();
const {
uniqueParams,
Expand Down
6 changes: 3 additions & 3 deletions fe/src/components/Post/renderers/RendererList.vue
Original file line number Diff line number Diff line change
Expand Up @@ -176,15 +176,15 @@ import { FontAwesomeIcon } from '@fortawesome/vue-fontawesome';
import { DateTime } from 'luxon';
import * as _ from 'lodash-es';
const props = defineProps<{ initialPosts: ApiPosts }>();
const props = defineProps<{ initialPosts: ApiPosts['response'] }>();
const elementRefsStore = useElementRefsStore();
const replyElements = ref<HTMLElement[]>([]);
const hoveringSubReplyID = ref(0);
const getUser = baseGetUser(props.initialPosts.users);
const renderUsername = baseRenderUsername(getUser);
const userRoute = (uid: BaiduUserID) => ({ name: 'user/uid', params: { uid } });
const posts = computed(() => {
const newPosts = props.initialPosts as Modify<ApiPosts, { // https://github.com/microsoft/TypeScript/issues/33591
const newPosts = props.initialPosts as Modify<ApiPosts['response'], { // https://github.com/microsoft/TypeScript/issues/33591
threads: Array<Thread & { replies: Array<Reply & { subReplies: Array<SubReply | SubReply[]> }> }>
}>;
newPosts.threads = newPosts.threads.map(thread => {
Expand Down Expand Up @@ -215,7 +215,7 @@ const posts = computed(() => {
return thread;
});
return newPosts as Modify<ApiPosts, {
return newPosts as Modify<ApiPosts['response'], {
threads: Array<Thread & { replies: Array<Reply & { subReplies: SubReply[][] }> }>
}>;
});
Expand Down
6 changes: 3 additions & 3 deletions fe/src/components/Post/renderers/RendererTable.vue
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,9 @@ import { Table } from 'ant-design-vue';
import type { ColumnType } from 'ant-design-vue/es/table/interface';
import * as _ from 'lodash-es';
const props = defineProps<{ posts: ApiPosts }>();
const threads = ref<ApiPosts['threads']>();
const repliesKeyByTid = ref<Record<Tid, ApiPosts['threads'][number]['replies']>>([]);
const props = defineProps<{ posts: ApiPosts['response'] }>();
const threads = ref<ApiPosts['response']['threads']>();
const repliesKeyByTid = ref<Record<Tid, ApiPosts['response']['threads'][number]['replies']>>([]);
const subRepliesKeyByPid = ref<Record<Pid, SubReply[]>>([]);
const threadColumns = ref<ColumnType[]>([
{ title: 'tid', dataIndex: 'tid' },
Expand Down
2 changes: 1 addition & 1 deletion fe/src/components/User/UserPage.vue
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import type { UserGender } from '@/api/user';
import { toUserPortraitImageUrl } from '@/shared';
const props = defineProps<{
users: ApiUsers,
users: ApiUsers['response'],
isLoadingNewPage: boolean,
isLastPageInPages: boolean
}>();
Expand Down
10 changes: 7 additions & 3 deletions fe/src/components/placeholders/PlaceholderError.vue
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@
<template v-if="error === null">
<span class="errorCode text-muted">error</span>
</template>
<template v-else>
<template v-else-if="error instanceof FetchResponseError">
<p class="errorCode text-muted">{{ error.response }}</p>
</template>
<template v-else-if="error instanceof ApiResponseError">
<p class="errorCode text-muted">{{ error.errorCode }}</p>
<template v-if="_.isString(error.errorInfo)">
<p v-for="(info, _k) in error.errorInfo.split('\n')" :key="_k">{{ info }}</p>
Expand All @@ -22,10 +25,11 @@
</template>

<script setup lang="ts">
import type { ApiError } from '@/api/index.d';
import type { ApiErrorClass } from '@/api';
import { ApiResponseError, FetchResponseError } from '@/api';
import * as _ from 'lodash-es';
defineProps<{ error: ApiError | null }>();
defineProps<{ error: ApiErrorClass | null }>();
</script>

<style scoped>
Expand Down
2 changes: 1 addition & 1 deletion fe/src/views/Post.vue
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<template>
<div class="container">
<QueryForm ref="queryFormRef" :isFetchingPosts="isFetching" />
<QueryForm ref="queryFormRef" :isLoading="isFetching" />
<p>当前页数:{{ getRouteCursorParam(route) }}</p>
<Menu v-show="!_.isEmpty(postPages)" v-model:selectedKeys="selectedRenderTypes" mode="horizontal">
<MenuItem key="list">列表视图</MenuItem>
Expand Down
4 changes: 2 additions & 2 deletions fe/src/views/Stats.vue
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ import TimeGranularity from '@/components/widgets/TimeGranularity.vue';
import TimeRange from '@/components/widgets/TimeRange.vue';
import { apiForums, apiStatsForumsPostCount, throwIfApiError } from '@/api';
import type { ApiForums, ApiStatsForumPostCountQueryParam } from '@/api/index.d';
import type { ApiForums, ApiStatsForumPostCount } from '@/api/index.d';
import type { Writable } from '@/shared';
import { titleTemplate } from '@/shared';
import { emptyChartSeriesData, extendCommonToolbox, timeGranularities, timeGranularityAxisPointerLabelFormatter, timeGranularityAxisType } from '@/shared/echarts';
Expand Down Expand Up @@ -111,7 +111,7 @@ const chartInitialOption: echarts.ComposeOption<DataZoomComponentOption | GridCo
};
useHead({ title: titleTemplate('统计') });
const query = ref<ApiStatsForumPostCountQueryParam>({
const query = ref<ApiStatsForumPostCount['queryParam']>({
fid: 0,
timeGranularity: 'day',
startTime: 0,
Expand Down
6 changes: 3 additions & 3 deletions fe/src/views/Status.vue
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ import TimeGranularity from '@/components/widgets/TimeGranularity.vue';
import TimeRange from '@/components/widgets/TimeRange.vue';
import { apiStatus, throwIfApiError } from '@/api';
import type { ApiStatus, ApiStatusQueryParam } from '@/api/index.d';
import type { ApiStatus, ApiStatus } from '@/api/index.d';
import { titleTemplate } from '@/shared';
import { commonToolboxFeatures, emptyChartSeriesData } from '@/shared/echarts';
Expand Down Expand Up @@ -167,7 +167,7 @@ const chartInitialOption: echarts.ComposeOption<DataZoomComponentOption | GridCo
};
useHead({ title: titleTemplate('状态') });
const query = ref<ApiStatusQueryParam>({
const query = ref<ApiStatus['queryParam']>({
timeGranularity: 'minute',
startTime: 0,
endTime: 0
Expand All @@ -189,7 +189,7 @@ const submitQueryForm = async () => {
.finally(() => { chartEl.value?.classList.remove('loading') }));
const series = _.chain(chartInitialOption.series)
.map('id')
.map((seriesName: keyof ApiStatus[0]) => ({
.map((seriesName: keyof ApiStatus['response'][0]) => ({
id: seriesName,
// select column from status, UnixTimestamp * 1000 since echarts only accepts milliseconds
Expand Down
2 changes: 1 addition & 1 deletion fe/src/views/User.vue
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ const route = useRoute();
useHead({ title: titleTemplate('用户查询') });
const params = ref<Pick<SelectUserParams, Exclude<SelectUserBy, '' | 'displayNameNULL' | 'nameNULL'>>>({});
const selectUserBy = ref<SelectUserBy>('');
const userPages = ref<ApiUsers[]>([]);
const userPages = ref<Array<ApiUsers['response']>>([]);
const isLoading = ref<boolean>(false);
const lastFetchError = ref<ApiError | null>(null);
const showPlaceholderPostList = ref<boolean>(false);
Expand Down

0 comments on commit b0c0ef5

Please sign in to comment.