Skip to content
This repository was archived by the owner on Nov 2, 2024. It is now read-only.

Commit efdd763

Browse files
committed
Fix typing and update tests
1 parent 64c3895 commit efdd763

File tree

7 files changed

+18
-18
lines changed

7 files changed

+18
-18
lines changed

src/App/components/Loading/Loading.test.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@ describe('Loading', () => {
1919
});
2020

2121
test('Displays given error', () => {
22-
const message = 'given error';
22+
const statusText = 'given error';
2323
render(
24-
<Loading status="error" error={{ message }}>
24+
<Loading status="error" error={{ statusText, status: 0 }}>
2525
{children}
2626
</Loading>,
2727
);

src/App/components/Loading/Loading.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import { FC, ReactNode } from 'react';
22
import Info from '../Info';
3-
import type { Status, Error } from '@/types';
3+
import type { Status, HttpError } from '@/types';
44

55
interface LoadingProps {
66
status: Status;
7-
error: Error | null;
7+
error: HttpError | null;
88
children: ReactNode;
99
}
1010

@@ -19,7 +19,7 @@ const Loading: FC<LoadingProps> = ({ status, error, children }) => {
1919
default: {
2020
return (
2121
<Info error>
22-
Error: {error?.message || 'an unknown error occurred'}
22+
Error: {error?.statusText || 'an unknown error occurred'}
2323
</Info>
2424
);
2525
}

src/App/components/Post/useData.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import { useQuery } from '@tanstack/react-query';
22
import { getJson } from '@/api';
3-
import type { PostData } from '@/types';
3+
import type { PostData, HttpError } from '@/types';
44

55
const fetchPostById = async (id: number) =>
66
await getJson<PostData>(`posts/${id}`);
77

88
const useData = (postId: number) =>
9-
useQuery<PostData, Error>(['post', postId], () => fetchPostById(postId), {
9+
useQuery<PostData, HttpError>(['post', postId], () => fetchPostById(postId), {
1010
enabled: !!postId,
1111
});
1212

src/App/components/Posts/Posts.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import css from './Posts.module.css';
55
import Info from '../Info';
66
import Loading from '../Loading';
77
import useData from './useData';
8+
import { PostData } from '@/types';
89

910
interface PostsProps {
1011
setPostId: (id: number | null) => void;
@@ -25,7 +26,7 @@ const Posts: FC<PostsProps> = ({ setPostId }) => {
2526
Background Updating
2627
</Info>
2728
<ul className={css.list}>
28-
{data.map(({ id, title }) => (
29+
{data.map(({ id, title }: PostData) => (
2930
<li key={id}>
3031
<a
3132
href="#"

src/App/components/Posts/useData.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import { useQuery } from '@tanstack/react-query';
22
import type { PostData } from '@/types';
3-
import { getJson } from '@/api';
3+
import { getJson, HttpError } from '@/api';
44

55
const useData = () =>
6-
useQuery<PostData[], Error>(['posts'], async () =>
6+
useQuery<PostData[], HttpError, PostData[]>(['posts'], async () =>
77
getJson<PostData[]>('posts'),
88
);
99

src/api/api.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
export const API_ROOT = 'https://jsonplaceholder.typicode.com/';
2+
3+
// TODO: should HttpError be a subclass of Error? (so we can throw an Error)
24
export type HttpError = Partial<Response> & {
35
status: number;
46
statusText: string;
@@ -12,7 +14,7 @@ type FetchArgs = Parameters<typeof globalThis.fetch>;
1214
export const fetch = async <RData = unknown>(
1315
input: FetchArgs[0],
1416
init: FetchArgs[1] = {},
15-
) => {
17+
): Promise<RData> => {
1618
const initWithDefaults = {
1719
...init,
1820

@@ -29,15 +31,15 @@ export const fetch = async <RData = unknown>(
2931
const status = 0;
3032
const statusText: string =
3133
(err as Error)?.message || 'Failed to fetch (unknown error)';
32-
return Promise.reject<HttpError>({ status, statusText });
34+
throw { status, statusText } as HttpError;
3335
}
3436

3537
if (res.ok) {
3638
return resJson;
3739
} else {
38-
return Promise.reject<HttpError>(res);
40+
throw res as HttpError;
3941
}
4042
};
4143

42-
export const getJson = <RData = unknown>(path: string) =>
44+
export const getJson = <RData = unknown>(path: string): Promise<RData> =>
4345
fetch<RData>(`${API_ROOT}${path}`);

src/types.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { UseQueryResult } from '@tanstack/react-query';
2+
export type { HttpError } from '@/api';
23

34
export type Status = UseQueryResult['status'];
45

@@ -7,7 +8,3 @@ export interface PostData {
78
title: string;
89
body: string;
910
}
10-
11-
export type Error = {
12-
message: string;
13-
};

0 commit comments

Comments
 (0)