Add handleResponse function
This commit is contained in:
parent
0c5d03044e
commit
e297bd9ae0
2 changed files with 33 additions and 42 deletions
|
@ -66,3 +66,21 @@ export async function http(
|
||||||
const response = await fetcher.fetch(url as string, params)
|
const response = await fetcher.fetch(url as string, params)
|
||||||
return response
|
return response
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export async function handleResponse(
|
||||||
|
response: Response,
|
||||||
|
expectedStatus: number = 200,
|
||||||
|
): Promise<any> {
|
||||||
|
if (response.status === expectedStatus) {
|
||||||
|
if (expectedStatus === 204) {
|
||||||
|
// No data
|
||||||
|
return null
|
||||||
|
} else {
|
||||||
|
const data = await response.json()
|
||||||
|
return data
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
const data = await response.json()
|
||||||
|
throw new Error(data.error_description)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
import { BACKEND_URL } from "@/constants"
|
import { BACKEND_URL } from "@/constants"
|
||||||
import { PAGE_SIZE, http } from "./common"
|
import { handleResponse, http, PAGE_SIZE } from "./common"
|
||||||
import { CustomEmoji } from "./emojis"
|
import { CustomEmoji } from "./emojis"
|
||||||
import { defaultProfile, Profile } from "./users"
|
import { defaultProfile, Profile } from "./users"
|
||||||
|
|
||||||
|
@ -20,10 +20,7 @@ export async function uploadAttachment(
|
||||||
json: { file: base64data, media_type: mediaType },
|
json: { file: base64data, media_type: mediaType },
|
||||||
authToken,
|
authToken,
|
||||||
})
|
})
|
||||||
const data = await response.json()
|
const data = await handleResponse(response)
|
||||||
if (response.status !== 200) {
|
|
||||||
throw new Error(data.error_description)
|
|
||||||
}
|
|
||||||
return data
|
return data
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -89,7 +86,7 @@ export async function getHomeTimeline(
|
||||||
queryParams,
|
queryParams,
|
||||||
authToken,
|
authToken,
|
||||||
})
|
})
|
||||||
const data = await response.json()
|
const data = await handleResponse(response)
|
||||||
return data
|
return data
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -104,7 +101,7 @@ export async function getPublicTimeline(
|
||||||
queryParams,
|
queryParams,
|
||||||
authToken,
|
authToken,
|
||||||
})
|
})
|
||||||
const data = await response.json()
|
const data = await handleResponse(response)
|
||||||
return data
|
return data
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -120,7 +117,7 @@ export async function getTagTimeline(
|
||||||
queryParams,
|
queryParams,
|
||||||
authToken,
|
authToken,
|
||||||
})
|
})
|
||||||
const data = await response.json()
|
const data = await handleResponse(response)
|
||||||
return data
|
return data
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -141,7 +138,7 @@ export async function getProfileTimeline(
|
||||||
queryParams,
|
queryParams,
|
||||||
authToken,
|
authToken,
|
||||||
})
|
})
|
||||||
const data = await response.json()
|
const data = await handleResponse(response)
|
||||||
return data
|
return data
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -151,10 +148,7 @@ export async function getPostThread(
|
||||||
): Promise<Post[]> {
|
): Promise<Post[]> {
|
||||||
const url = `${BACKEND_URL}/api/v1/statuses/${postId}/thread`
|
const url = `${BACKEND_URL}/api/v1/statuses/${postId}/thread`
|
||||||
const response = await http(url, { authToken })
|
const response = await http(url, { authToken })
|
||||||
const data = await response.json()
|
const data = await handleResponse(response)
|
||||||
if (response.status !== 200) {
|
|
||||||
throw new Error(data.error_description)
|
|
||||||
}
|
|
||||||
return data
|
return data
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -171,7 +165,7 @@ export async function previewPost(
|
||||||
},
|
},
|
||||||
authToken,
|
authToken,
|
||||||
})
|
})
|
||||||
const data = await response.json()
|
const data = await handleResponse(response)
|
||||||
return {
|
return {
|
||||||
id: "",
|
id: "",
|
||||||
uri: "",
|
uri: "",
|
||||||
|
@ -225,10 +219,7 @@ export async function createPost(
|
||||||
json: statusData,
|
json: statusData,
|
||||||
authToken,
|
authToken,
|
||||||
})
|
})
|
||||||
const data = await response.json()
|
const data = await handleResponse(response)
|
||||||
if (response.status !== 200) {
|
|
||||||
throw new Error(data.error_description)
|
|
||||||
}
|
|
||||||
return data
|
return data
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -238,10 +229,7 @@ export async function getPost(
|
||||||
): Promise<Post> {
|
): Promise<Post> {
|
||||||
const url = `${BACKEND_URL}/api/v1/statuses/${postId}`
|
const url = `${BACKEND_URL}/api/v1/statuses/${postId}`
|
||||||
const response = await http(url, { authToken })
|
const response = await http(url, { authToken })
|
||||||
const data = await response.json()
|
const data = await handleResponse(response)
|
||||||
if (response.status !== 200) {
|
|
||||||
throw new Error(data.error_description)
|
|
||||||
}
|
|
||||||
return data
|
return data
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -254,10 +242,7 @@ export async function deletePost(
|
||||||
method: "DELETE",
|
method: "DELETE",
|
||||||
authToken,
|
authToken,
|
||||||
})
|
})
|
||||||
if (response.status !== 204) {
|
await handleResponse(response, 204)
|
||||||
const data = await response.json()
|
|
||||||
throw new Error(data.error_description)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function favourite(
|
export async function favourite(
|
||||||
|
@ -266,10 +251,7 @@ export async function favourite(
|
||||||
): Promise<Post> {
|
): Promise<Post> {
|
||||||
const url = `${BACKEND_URL}/api/v1/statuses/${postId}/favourite`
|
const url = `${BACKEND_URL}/api/v1/statuses/${postId}/favourite`
|
||||||
const response = await http(url, { method: "POST", authToken })
|
const response = await http(url, { method: "POST", authToken })
|
||||||
const data = await response.json()
|
const data = await handleResponse(response)
|
||||||
if (response.status !== 200) {
|
|
||||||
throw new Error(data.error_description)
|
|
||||||
}
|
|
||||||
return data
|
return data
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -279,10 +261,7 @@ export async function unfavourite(
|
||||||
): Promise<Post> {
|
): Promise<Post> {
|
||||||
const url = `${BACKEND_URL}/api/v1/statuses/${postId}/unfavourite`
|
const url = `${BACKEND_URL}/api/v1/statuses/${postId}/unfavourite`
|
||||||
const response = await http(url, { method: "POST", authToken })
|
const response = await http(url, { method: "POST", authToken })
|
||||||
const data = await response.json()
|
const data = await handleResponse(response)
|
||||||
if (response.status !== 200) {
|
|
||||||
throw new Error(data.error_description)
|
|
||||||
}
|
|
||||||
return data
|
return data
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -292,10 +271,7 @@ export async function createRepost(
|
||||||
): Promise<Post> {
|
): Promise<Post> {
|
||||||
const url = `${BACKEND_URL}/api/v1/statuses/${postId}/reblog`
|
const url = `${BACKEND_URL}/api/v1/statuses/${postId}/reblog`
|
||||||
const response = await http(url, { method: "POST", authToken })
|
const response = await http(url, { method: "POST", authToken })
|
||||||
const data = await response.json()
|
const data = await handleResponse(response)
|
||||||
if (response.status !== 200) {
|
|
||||||
throw new Error(data.error_description)
|
|
||||||
}
|
|
||||||
if (data.reblog === null) {
|
if (data.reblog === null) {
|
||||||
throw new Error("reblog property is null")
|
throw new Error("reblog property is null")
|
||||||
}
|
}
|
||||||
|
@ -308,9 +284,6 @@ export async function deleteRepost(
|
||||||
): Promise<Post> {
|
): Promise<Post> {
|
||||||
const url = `${BACKEND_URL}/api/v1/statuses/${postId}/unreblog`
|
const url = `${BACKEND_URL}/api/v1/statuses/${postId}/unreblog`
|
||||||
const response = await http(url, { method: "POST", authToken })
|
const response = await http(url, { method: "POST", authToken })
|
||||||
const data = await response.json()
|
const data = await handleResponse(response)
|
||||||
if (response.status !== 200) {
|
|
||||||
throw new Error(data.error_description)
|
|
||||||
}
|
|
||||||
return data
|
return data
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue