From a3335a1f88f5a963e125c7849830d281583bf391 Mon Sep 17 00:00:00 2001 From: Zhiyuan Zheng Date: Tue, 15 Dec 2020 00:01:48 +0100 Subject: [PATCH] Add promise type to fetches --- src/@types/mastodon.d.ts | 10 +++++++++ .../Timeline/Shared/HeaderNotification.tsx | 10 ++++----- src/screens/Me/Root/Login.tsx | 12 +++++------ src/screens/Shared/Account/Header.tsx | 2 +- src/screens/Shared/Compose/Root.tsx | 2 +- src/utils/fetches/accountFetch.ts | 5 ++++- src/utils/fetches/applicationFetch.ts | 4 ++-- src/utils/fetches/emojisFetch.ts | 2 +- src/utils/fetches/instanceFetch.ts | 2 +- src/utils/fetches/listsFetch.ts | 2 +- src/utils/fetches/relationshipFetch.ts | 2 +- src/utils/fetches/searchFetch.ts | 21 +++++++++++++++---- 12 files changed, 50 insertions(+), 24 deletions(-) diff --git a/src/@types/mastodon.d.ts b/src/@types/mastodon.d.ts index 5fe5c511..e28a7ae7 100644 --- a/src/@types/mastodon.d.ts +++ b/src/@types/mastodon.d.ts @@ -38,6 +38,16 @@ declare namespace Mastodon { vapid_key?: string } + type AppOauth = { + id: string + name: string + website?: string + redirect_uri: string + client_id: string + client_secret: string + vapid_key: string + } + type Attachment = | AttachmentImage | AttachmentVideo diff --git a/src/components/Timelines/Timeline/Shared/HeaderNotification.tsx b/src/components/Timelines/Timeline/Shared/HeaderNotification.tsx index 96b21051..cd824f45 100644 --- a/src/components/Timelines/Timeline/Shared/HeaderNotification.tsx +++ b/src/components/Timelines/Timeline/Shared/HeaderNotification.tsx @@ -64,12 +64,12 @@ const TimelineHeaderNotification: React.FC = ({ notification }) => { ? updateData.following || updateData.requested ? 'un' : '' - : data.following || data.requested + : data!.following || data!.requested ? 'un' : '' }follow` }).then(res => { - if (res.body.id === (updateData && updateData.id) || data.id) { + if (res.body.id === (updateData && updateData.id) || data!.id) { setUpdateData(res.body) return Promise.resolve() } else { @@ -100,9 +100,9 @@ const TimelineHeaderNotification: React.FC = ({ notification }) => { : updateData.requested ? 'loader' : 'user-plus' - : data.following + : data!.following ? 'user-check' - : data.requested + : data!.requested ? 'loader' : 'user-plus' } @@ -111,7 +111,7 @@ const TimelineHeaderNotification: React.FC = ({ notification }) => { ? updateData.following ? theme.primary : theme.secondary - : data.following + : data!.following ? theme.primary : theme.secondary } diff --git a/src/screens/Me/Root/Login.tsx b/src/screens/Me/Root/Login.tsx index 6ef33042..9138b137 100644 --- a/src/screens/Me/Root/Login.tsx +++ b/src/screens/Me/Root/Login.tsx @@ -131,7 +131,7 @@ const Login: React.FC = () => { parse }: { header: string - content: string + content?: string parse?: boolean }) => { return ( @@ -140,7 +140,7 @@ const Login: React.FC = () => { {header} { style={[styles.instanceInfoContent, { color: theme.primary }]} > {parse ? ( - + ) : ( content )} @@ -230,7 +230,7 @@ const Login: React.FC = () => { 用户总数 { 嘟嘟总数 { 连结总数 { ListHeaderComponent={} ListFooterComponent={} ListEmptyComponent={listEmpty} - data={data} + data={data as Mastodon.Account[] & Mastodon.Tag[]} keyExtractor={listKey} renderItem={listItem} /> diff --git a/src/utils/fetches/accountFetch.ts b/src/utils/fetches/accountFetch.ts index 69fe0371..e35b9753 100644 --- a/src/utils/fetches/accountFetch.ts +++ b/src/utils/fetches/accountFetch.ts @@ -1,6 +1,9 @@ import client from '@api/client' -export const accountFetch = async (key: string, { id }: { id: string }) => { +export const accountFetch = async ( + key: string, + { id }: { id: string } +): Promise => { const res = await client({ method: 'get', instance: 'local', diff --git a/src/utils/fetches/applicationFetch.ts b/src/utils/fetches/applicationFetch.ts index 9804791d..3bcdeef1 100644 --- a/src/utils/fetches/applicationFetch.ts +++ b/src/utils/fetches/applicationFetch.ts @@ -2,8 +2,8 @@ import client from '@api/client' export const applicationFetch = async ( key: string, - { instanceDomain, body }: { instanceDomain: string; body: FormData } -) => { + { instanceDomain }: { instanceDomain: string } +): Promise => { const formData = new FormData() formData.append('client_name', 'test_dudu') formData.append('redirect_uris', 'exp://127.0.0.1:19000') diff --git a/src/utils/fetches/emojisFetch.ts b/src/utils/fetches/emojisFetch.ts index 4b1c7d4c..f25b5c59 100644 --- a/src/utils/fetches/emojisFetch.ts +++ b/src/utils/fetches/emojisFetch.ts @@ -1,6 +1,6 @@ import client from '@api/client' -export const emojisFetch = async () => { +export const emojisFetch = async (): Promise => { const res = await client({ method: 'get', instance: 'local', diff --git a/src/utils/fetches/instanceFetch.ts b/src/utils/fetches/instanceFetch.ts index 828705cc..3620eff0 100644 --- a/src/utils/fetches/instanceFetch.ts +++ b/src/utils/fetches/instanceFetch.ts @@ -3,7 +3,7 @@ import client from '@api/client' export const instanceFetch = async ( key: string, { instanceDomain }: { instanceDomain: string } -) => { +): Promise => { const res = await client({ method: 'get', instance: 'remote', diff --git a/src/utils/fetches/listsFetch.ts b/src/utils/fetches/listsFetch.ts index 2f0f7312..6bd80f89 100644 --- a/src/utils/fetches/listsFetch.ts +++ b/src/utils/fetches/listsFetch.ts @@ -1,6 +1,6 @@ import client from '@api/client' -export const listsFetch = async () => { +export const listsFetch = async (): Promise => { const res = await client({ method: 'get', instance: 'local', diff --git a/src/utils/fetches/relationshipFetch.ts b/src/utils/fetches/relationshipFetch.ts index 549ff462..51681f27 100644 --- a/src/utils/fetches/relationshipFetch.ts +++ b/src/utils/fetches/relationshipFetch.ts @@ -3,7 +3,7 @@ import client from '@api/client' export const relationshipFetch = async ( key: string, { id }: { id: string } -) => { +): Promise => { const res = await client({ method: 'get', instance: 'local', diff --git a/src/utils/fetches/searchFetch.ts b/src/utils/fetches/searchFetch.ts index 2e9d2f7a..2e905ee5 100644 --- a/src/utils/fetches/searchFetch.ts +++ b/src/utils/fetches/searchFetch.ts @@ -7,17 +7,30 @@ export const searchFetch = async ( term, limit = 20 }: { - type: 'accounts' | 'hashtags' | 'statuses' + type?: 'accounts' | 'hashtags' | 'statuses' term: string limit?: number } -) => { +): Promise< + | Mastodon.Account[] + | Mastodon.Tag[] + | Mastodon.Status[] + | { + accounts: Mastodon.Account[] + hashtags: Mastodon.Tag[] + statuses: Mastodon.Status[] + } +> => { const res = await client({ version: 'v2', method: 'get', instance: 'local', url: 'search', - params: { type, q: term, limit } + params: { ...(type && { type }), q: term, limit } }) - return Promise.resolve(res.body[type]) + if (type) { + return Promise.resolve(res.body[type]) + } else { + return Promise.resolve(res.body) + } }