From 53741e82e24efb84636cb1ab2cc262da2cc4ba3c Mon Sep 17 00:00:00 2001 From: Yukai Huang Date: Thu, 16 Feb 2023 14:27:52 +0800 Subject: [PATCH] feat: type hacking --- nodejs/src/index.ts | 100 +++++++++++++++----------------------------- 1 file changed, 33 insertions(+), 67 deletions(-) diff --git a/nodejs/src/index.ts b/nodejs/src/index.ts index d550bce..ec72a5c 100644 --- a/nodejs/src/index.ts +++ b/nodejs/src/index.ts @@ -1,5 +1,5 @@ import axios, { AxiosInstance, AxiosRequestConfig, AxiosError, AxiosResponse } from 'axios' -import { User, Note, Team, CreateNoteOptions, GetMe, GetUserHistory, GetUserNotes, GetUserNote, CreateUserNote, GetUserTeams, GetTeamNotes, CreateTeamNote, SingleNote } from './type' +import { CreateNoteOptions, GetMe, GetUserHistory, GetUserNotes, GetUserNote, CreateUserNote, GetUserTeams, GetTeamNotes, CreateTeamNote, SingleNote } from './type' import * as HackMDErrors from './error' export type RequestOptions = { @@ -10,6 +10,8 @@ const defaultOption: RequestOptions = { unwrapData: true, } +type OptionReturnType = Opt extends { unwrapData: false } ? AxiosResponse : Opt extends { unwrapData: true } ? T : T + export class API { private axios: AxiosInstance @@ -61,92 +63,48 @@ export class API { ) } - async getMe (option = defaultOption) { - if (option.unwrapData) { - return this.axios.get("me").then(response => response.data) as Promise - } else { - return this.axios.get("me") - } + async getMe (options = defaultOption as Opt): Promise> { + return this.unwrapData(this.axios.get("me"), options.unwrapData) as unknown as OptionReturnType } - async getHistory (options = defaultOption) { - if (options.unwrapData) { - return this.axios.get("history").then(response => response.data) as Promise - } else { - return this.axios.get("history") - } + async getHistory (options = defaultOption as Opt): Promise> { + return this.unwrapData(this.axios.get("history"), options.unwrapData) as unknown as OptionReturnType } - async getNoteList (options = defaultOption) { - if (options.unwrapData) { - return this.axios.get("notes").then(response => response.data) as Promise - } else { - return this.axios.get("notes") - } + async getNoteList (options = defaultOption as Opt): Promise> { + return this.unwrapData(this.axios.get("notes"), options.unwrapData) as unknown as OptionReturnType } - async getNote (noteId: string, options = defaultOption) { - if (options.unwrapData) { - return this.axios.get(`notes/${noteId}`).then(response => response.data) as Promise - } else { - return this.axios.get(`notes/${noteId}`) - } + async getNote (noteId: string, options = defaultOption as Opt): Promise> { + return this.unwrapData(this.axios.get(`notes/${noteId}`), options.unwrapData) as unknown as OptionReturnType } - async createNote (payload: CreateNoteOptions, options = defaultOption) { - if (options.unwrapData) { - return this.axios.post("notes", payload).then(response => response.data) as Promise - } else { - return this.axios.post("notes", payload) - } + async createNote (payload: CreateNoteOptions, options = defaultOption as Opt): Promise> { + return this.unwrapData(this.axios.post("notes", payload), options.unwrapData) as unknown as OptionReturnType } - async updateNoteContent (noteId: string, content?: string, options = defaultOption) { - if (options.unwrapData) { - return this.axios.patch(`notes/${noteId}`, { content }).then(response => response.data) - } else { - return this.axios.patch(`notes/${noteId}`, { content }) - } + async updateNoteContent (noteId: string, content?: string, options = defaultOption as Opt): Promise> { + return this.unwrapData(this.axios.patch(`notes/${noteId}`, { content }), options.unwrapData) as unknown as OptionReturnType } - async updateNote (noteId: string, payload: Partial>, options = defaultOption) { - if (options.unwrapData) { - return this.axios.patch(`notes/${noteId}`, payload).then(response => response.data) - } else { - return this.axios.patch(`notes/${noteId}`, payload) - } + async updateNote (noteId: string, payload: Partial>, options = defaultOption as Opt): Promise> { + return this.unwrapData(this.axios.patch(`notes/${noteId}`, payload), options.unwrapData) as unknown as OptionReturnType } - async deleteNote (noteId: string, options = defaultOption) { - if (options.unwrapData) { - return this.axios.delete(`notes/${noteId}`).then(response => response.data) - } else { - return this.axios.delete(`notes/${noteId}`) - } + async deleteNote (noteId: string, options = defaultOption as Opt): Promise> { + return this.unwrapData(this.axios.delete(`notes/${noteId}`), options.unwrapData) as unknown as OptionReturnType } - async getTeams (options = defaultOption) { - if (options.unwrapData) { - return this.axios.get("teams").then(response => response.data) as Promise - } else { - return this.axios.get("teams") - } + async getTeams (options = defaultOption as Opt): Promise> { + return this.unwrapData(this.axios.get("teams"), options.unwrapData) as unknown as OptionReturnType } - async getTeamNotes (teamPath: string, options = defaultOption) { - if (options.unwrapData) { - return this.axios.get(`teams/${teamPath}/notes`).then(response => response.data) as Promise - } else { - return this.axios.get(`teams/${teamPath}/notes`) - } + async getTeamNotes (teamPath: string, options = defaultOption as Opt): Promise> { + return this.unwrapData(this.axios.get(`teams/${teamPath}/notes`), options.unwrapData) as unknown as OptionReturnType } - async createTeamNote (teamPath: string, payload: CreateNoteOptions, options = defaultOption) { - if (options.unwrapData) { - return this.axios.post(`teams/${teamPath}/notes`, payload).then(response => response.data) as Promise - } else { - return this.axios.post(`teams/${teamPath}/notes`, payload) - } + async createTeamNote (teamPath: string, payload: CreateNoteOptions, options = defaultOption as Opt): Promise> { + return this.unwrapData(this.axios.post(`teams/${teamPath}/notes`, payload), options.unwrapData) as unknown as OptionReturnType } async updateTeamNoteContent (teamPath: string, noteId: string, content?: string): Promise { @@ -160,6 +118,14 @@ export class API { async deleteTeamNote (teamPath: string, noteId: string): Promise { return this.axios.delete(`teams/${teamPath}/notes/${noteId}`) } + + private unwrapData (reqP: Promise>, unwrap = true) { + if (unwrap) { + return reqP.then(response => response.data) + } else { + return reqP + } + } } export default API