From 2a4479f410b47b1e58a6f7f30e312fef4e8c7097 Mon Sep 17 00:00:00 2001 From: leon Date: Fri, 21 Oct 2022 14:31:58 +0800 Subject: [PATCH] refactor: api --- android/app/build.gradle | 4 ++-- ios/app.xcodeproj/project.pbxproj | 4 ++-- package.json | 2 +- src/actions/MemberActions.ts | 4 ++-- src/api/index.ts | 30 ++++++++++++------------- src/api/lib/member/index.ts | 6 ++--- src/api/lib/node/index.ts | 2 +- src/api/lib/notification/index.ts | 2 +- src/api/lib/reply/index.ts | 2 +- src/api/lib/topic/index.ts | 2 +- src/api/types.ts | 37 ++++++++++++++++--------------- src/screens/my/Home.tsx | 2 +- src/store/types.ts | 2 +- 13 files changed, 50 insertions(+), 49 deletions(-) diff --git a/android/app/build.gradle b/android/app/build.gradle index c8c6f6f5..bdb008e1 100644 --- a/android/app/build.gradle +++ b/android/app/build.gradle @@ -138,8 +138,8 @@ android { applicationId "github.funnyzak.v2ex" minSdkVersion rootProject.ext.minSdkVersion targetSdkVersion rootProject.ext.targetSdkVersion - versionCode 35 - versionName "0.8.3" + versionCode 36 + versionName "0.8.4" buildConfigField "boolean", "IS_NEW_ARCHITECTURE_ENABLED", isNewArchitectureEnabled().toString() if (isNewArchitectureEnabled()) { diff --git a/ios/app.xcodeproj/project.pbxproj b/ios/app.xcodeproj/project.pbxproj index 90fea332..ecd417fe 100644 --- a/ios/app.xcodeproj/project.pbxproj +++ b/ios/app.xcodeproj/project.pbxproj @@ -494,7 +494,7 @@ "$(inherited)", "@executable_path/Frameworks", ); - MARKETING_VERSION = 0.8.3; + MARKETING_VERSION = 0.8.4; OTHER_LDFLAGS = ( "$(inherited)", "-ObjC", @@ -528,7 +528,7 @@ "$(inherited)", "@executable_path/Frameworks", ); - MARKETING_VERSION = 0.8.3; + MARKETING_VERSION = 0.8.4; OTHER_LDFLAGS = ( "$(inherited)", "-ObjC", diff --git a/package.json b/package.json index e4e633c0..c2789621 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "v2hub", - "version": "0.8.3", + "version": "0.8.4", "description": "This project used React Native to build a V2EX mobile client application. The main goal was to build a React Native rapid development scaffold.", "main": "index.js", "private": false, diff --git a/src/actions/MemberActions.ts b/src/actions/MemberActions.ts index 527747eb..7ab88d4d 100644 --- a/src/actions/MemberActions.ts +++ b/src/actions/MemberActions.ts @@ -105,7 +105,7 @@ export const unFollowPeople = (member: AppObject.Member) => async (dispatch: Dis dispatch(cacheMemberFollowing(getState().member.followPeoples)) } -export const setCurrentToken = (token?: AppObject.MToken) => ({ +export const setCurrentToken = (token?: AppObject.MemberToken) => ({ type: APP_AUTH, payload: token }) @@ -121,7 +121,7 @@ export const loginByToken = (token: string) => async (dispatch: Dispatch) => { } } -const loginByTokenSuccess = (token: AppObject.MToken) => async (dispatch: Dispatch, getState: () => RootState) => { +const loginByTokenSuccess = (token: AppObject.MemberToken) => async (dispatch: Dispatch, getState: () => RootState) => { await AsyncStorage.setItem(MEMBER_TOKEN_KEY, token.token) ApiLib.setToken(token.token) diff --git a/src/api/index.ts b/src/api/index.ts index b1e8ca77..d840cb8e 100644 --- a/src/api/index.ts +++ b/src/api/index.ts @@ -23,16 +23,23 @@ const defaultConfiguration = { } class V2ex { - configuration: AppAPI.BaseConfiguration = defaultConfiguration + configuration: AppAPI.APIConfiguration = defaultConfiguration root_path?: string token?: string - reply: AppAPI.Reply = reply(this) - member: AppAPI.Member = member(this) - node: AppAPI.Node = node(this) - topic: AppAPI.Topic = topic(this) - notification: AppAPI.Notification = notification(this) + reply: AppAPI.ReplyAPI = reply(this) + member: AppAPI.MemberAPI = member(this) + node: AppAPI.NodeAPI = node(this) + topic: AppAPI.TopicAPI = topic(this) + notification: AppAPI.NotificationAPI = notification(this) + + constructor(options?: AppAPI.APIConfiguration) { + if (options) { + this.setOptions(options) + } + this.init() + } - setOptions(options: AppAPI.BaseConfiguration) { + setOptions(options: AppAPI.APIConfiguration) { this.configuration = _.merge(this.configuration, options) this.root_path = `/${this.configuration.store}` @@ -106,7 +113,7 @@ class V2ex { send( path: string, - method: AppAPI.Method, + method: AppAPI.HttpMethod, headers?: { [name: string]: string }, params?: Record, data?: any, @@ -138,13 +145,6 @@ class V2ex { headers = _.merge(_headers, headers) return new Promise((resolve, reject) => { - // console.log({ - // uri, - // method, - // headers, - // data, - // ...params - // }) fetch(uri, { method, headers, body: JSON.stringify(data) }) .then((response: Response) => { if (response.ok) { diff --git a/src/api/lib/member/index.ts b/src/api/lib/member/index.ts index 5c3639b7..4dad82d8 100644 --- a/src/api/lib/member/index.ts +++ b/src/api/lib/member/index.ts @@ -1,7 +1,7 @@ import { AppAPI, AppObject } from '../../types' -export default (v2ex: AppAPI.APP): AppAPI.Member => ({ - myToken: () => v2ex.get('/token', undefined, undefined, undefined, 'v2'), +export default (v2ex: AppAPI.APP): AppAPI.MemberAPI => ({ + myToken: () => v2ex.get('/token', undefined, undefined, undefined, 'v2'), myProfile: () => v2ex.get('/member', undefined, undefined, undefined, 'v2'), @@ -15,7 +15,7 @@ export default (v2ex: AppAPI.APP): AppAPI.Member => ({ ), token: (token: string) => - v2ex.get( + v2ex.get( `/token`, { Authorization: `Bearer ${token}` diff --git a/src/api/lib/node/index.ts b/src/api/lib/node/index.ts index 9cdfc62c..735438af 100644 --- a/src/api/lib/node/index.ts +++ b/src/api/lib/node/index.ts @@ -1,6 +1,6 @@ import { AppAPI, AppObject } from '../../types' -export default (v2ex: AppAPI.APP): AppAPI.Node => ({ +export default (v2ex: AppAPI.APP): AppAPI.NodeAPI => ({ get: (id: string | number, version: AppAPI.API_VERSION): Promise => v2ex.get( version === 'v2' ? `/nodes/${id}` : `/nodes/show.json?${typeof id === 'string' ? 'name' : 'id'}=${id}`, diff --git a/src/api/lib/notification/index.ts b/src/api/lib/notification/index.ts index 5faf820d..726d9268 100644 --- a/src/api/lib/notification/index.ts +++ b/src/api/lib/notification/index.ts @@ -1,6 +1,6 @@ import { AppAPI, AppObject } from '../../types' -export default (v2ex: AppAPI.APP): AppAPI.Notification => ({ +export default (v2ex: AppAPI.APP): AppAPI.NotificationAPI => ({ /** * Get my latest notifications */ diff --git a/src/api/lib/reply/index.ts b/src/api/lib/reply/index.ts index a4697f01..6c0e2130 100644 --- a/src/api/lib/reply/index.ts +++ b/src/api/lib/reply/index.ts @@ -1,6 +1,6 @@ import { AppAPI, AppObject } from '../../types' -export default (v2ex: AppAPI.APP): AppAPI.Reply => ({ +export default (v2ex: AppAPI.APP): AppAPI.ReplyAPI => ({ /** * Get topic replies * @param topic_id : topic id diff --git a/src/api/lib/topic/index.ts b/src/api/lib/topic/index.ts index 33ddb357..0b31e596 100644 --- a/src/api/lib/topic/index.ts +++ b/src/api/lib/topic/index.ts @@ -1,6 +1,6 @@ import { AppAPI, AppObject } from '../../types' -export default (v2ex: AppAPI.APP): AppAPI.Topic => ({ +export default (v2ex: AppAPI.APP): AppAPI.TopicAPI => ({ /** * pager note topic list by api version 2 * @param name : node name diff --git a/src/api/types.ts b/src/api/types.ts index e2616b86..e06ca34d 100644 --- a/src/api/types.ts +++ b/src/api/types.ts @@ -13,7 +13,7 @@ export declare namespace AppAPI { /** * V2ex API Configuration */ - export interface BaseConfiguration { + export interface APIConfiguration { url?: string store?: string userAgent?: string @@ -25,7 +25,7 @@ export declare namespace AppAPI { extend?: { [name: string]: string | undefined } } - export type Method = + export type HttpMethod = | 'get' | 'GET' | 'delete' @@ -50,16 +50,17 @@ export declare namespace AppAPI { /** * V2ex Main API */ - export interface APP { - configuration: BaseConfiguration + export class APP { + constructor(configuration?: APIConfiguration) + configuration: APIConfiguration root_path?: string token?: string - node: Node - topic: Topic - notification: Notification - member: Member - reply: Reply - setOptions: (options: BaseConfiguration) => void + node: NodeAPI + topic: TopicAPI + notification: NotificationAPI + member: MemberAPI + reply: ReplyAPI + setOptions: (options: APIConfiguration) => void init: () => void setToken(token?: string): void setUserAgent(userAgent?: string): void @@ -100,11 +101,11 @@ export declare namespace AppAPI { ): Promise getErrorMessageForResponse(data: any): string } - export interface Member { + export interface MemberAPI { /** * Get my token info */ - myToken: () => Promise + myToken: () => Promise /** * Get my profile @@ -119,10 +120,10 @@ export declare namespace AppAPI { /** * check user token */ - token: (token: string) => Promise + token: (token: string) => Promise } - export interface Node { + export interface NodeAPI { /** * Get node info by node name/id * @param node id or node name @@ -136,7 +137,7 @@ export declare namespace AppAPI { all(): Promise } - export interface Notification { + export interface NotificationAPI { /** * Get my latest notifications */ @@ -148,7 +149,7 @@ export declare namespace AppAPI { remove: (id: string) => Promise } - export interface Topic { + export interface TopicAPI { /** * Get latest topic list by api version 1 */ @@ -180,7 +181,7 @@ export declare namespace AppAPI { topics(id: string | number, get_type: 'username' | 'node_id' | 'node_name' | 'id'): Promise } - export interface Reply { + export interface ReplyAPI { /** * Get topic replies by api version 1 * @param topic_id : topic id @@ -211,7 +212,7 @@ export declare namespace AppObject { /** * Member Token Info */ - export interface MToken { + export interface MemberToken { token: string scope: string expiration: number diff --git a/src/screens/my/Home.tsx b/src/screens/my/Home.tsx index cbf01c13..8a5070c4 100644 --- a/src/screens/my/Home.tsx +++ b/src/screens/my/Home.tsx @@ -21,7 +21,7 @@ const My = ({ }: ScreenProps & IState.State & { profile?: AppObject.Member - token?: AppObject.MToken + token?: AppObject.MemberToken readedTopics?: AppObject.Topic[] topics?: AppObject.Topic[] likeTopics: AppObject.Topic[] diff --git a/src/store/types.ts b/src/store/types.ts index bef5a8e9..3c356cfb 100644 --- a/src/store/types.ts +++ b/src/store/types.ts @@ -163,7 +163,7 @@ export declare module IState { /** * 用户令牌信息 */ - token?: AppObject.MToken + token?: AppObject.MemberToken /** * 用户信息