Spaces:
Runtime error
Runtime error
Commit
·
2cb5570
1
Parent(s):
f3f1f99
add GET post
Browse files- src/index.mts +44 -21
- src/types.mts +6 -0
src/index.mts
CHANGED
|
@@ -5,10 +5,11 @@ import { v4 as uuidv4 } from "uuid"
|
|
| 5 |
import { hasValidAuthorization } from "./utils/hasValidAuthorization.mts"
|
| 6 |
import { initFolders } from "./initFolders.mts"
|
| 7 |
import { getValidNumber } from "./utils/getValidNumber.mts"
|
| 8 |
-
import { CreatePostResponse, GetAppPostsResponse, Post, PostVisibility } from "./types.mts"
|
| 9 |
import { savePost } from "./core/savePost.mts"
|
| 10 |
import { getAppPosts } from "./core/getAppPosts.mts"
|
| 11 |
import { deletePost } from "./core/deletePost.mts"
|
|
|
|
| 12 |
|
| 13 |
initFolders()
|
| 14 |
|
|
@@ -111,7 +112,7 @@ app.post("/posts/:appId", async (req, res) => {
|
|
| 111 |
res.end()
|
| 112 |
})
|
| 113 |
|
| 114 |
-
app.get("/posts/:appId/:
|
| 115 |
|
| 116 |
const appId = `${req.params.appId}`
|
| 117 |
|
|
@@ -123,25 +124,47 @@ app.get("/posts/:appId/:visibility", async (req, res) => {
|
|
| 123 |
return
|
| 124 |
}
|
| 125 |
|
| 126 |
-
const
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 145 |
}
|
| 146 |
})
|
| 147 |
|
|
|
|
| 5 |
import { hasValidAuthorization } from "./utils/hasValidAuthorization.mts"
|
| 6 |
import { initFolders } from "./initFolders.mts"
|
| 7 |
import { getValidNumber } from "./utils/getValidNumber.mts"
|
| 8 |
+
import { CreatePostResponse, GetAppPostResponse, GetAppPostsResponse, Post, PostVisibility } from "./types.mts"
|
| 9 |
import { savePost } from "./core/savePost.mts"
|
| 10 |
import { getAppPosts } from "./core/getAppPosts.mts"
|
| 11 |
import { deletePost } from "./core/deletePost.mts"
|
| 12 |
+
import { getPost } from "./core/getPost.mts"
|
| 13 |
|
| 14 |
initFolders()
|
| 15 |
|
|
|
|
| 112 |
res.end()
|
| 113 |
})
|
| 114 |
|
| 115 |
+
app.get("/posts/:appId/:param", async (req, res) => {
|
| 116 |
|
| 117 |
const appId = `${req.params.appId}`
|
| 118 |
|
|
|
|
| 124 |
return
|
| 125 |
}
|
| 126 |
|
| 127 |
+
const param = `${req.params.param}`
|
| 128 |
+
const isVisibility = !uuidValidate(param)
|
| 129 |
+
|
| 130 |
+
if (isVisibility) {
|
| 131 |
+
const visibility = param
|
| 132 |
+
try {
|
| 133 |
+
const posts = await getAppPosts(
|
| 134 |
+
appId,
|
| 135 |
+
visibility === "all" ? undefined : visibility as PostVisibility
|
| 136 |
+
)
|
| 137 |
+
res.status(200)
|
| 138 |
+
console.log(`returning ${posts.length} community posts for app ${appId} (visibility: ${visibility})`)
|
| 139 |
+
res.write(JSON.stringify({ posts } as GetAppPostsResponse))
|
| 140 |
+
res.end()
|
| 141 |
+
return
|
| 142 |
+
|
| 143 |
+
} catch (err) {
|
| 144 |
+
const error = `failed to load the posts for app ${appId} and visibility ${visibility}: ${err}`
|
| 145 |
+
console.error(error)
|
| 146 |
+
res.status(500)
|
| 147 |
+
res.write(JSON.stringify({ posts: [], error } as GetAppPostsResponse))
|
| 148 |
+
res.end()
|
| 149 |
+
return
|
| 150 |
+
}
|
| 151 |
+
} else {
|
| 152 |
+
const postId = param
|
| 153 |
+
try {
|
| 154 |
+
const post = await getPost(appId, postId)
|
| 155 |
+
res.status(200)
|
| 156 |
+
console.log(`returning post ${postId} for app ${appId}`)
|
| 157 |
+
res.write(JSON.stringify({ post } as GetAppPostResponse))
|
| 158 |
+
res.end()
|
| 159 |
+
return
|
| 160 |
+
} catch (err) {
|
| 161 |
+
const error = `failed to load the post for app ${appId} and postId: ${postId}: ${err}`
|
| 162 |
+
console.error(error)
|
| 163 |
+
res.status(500)
|
| 164 |
+
res.write(JSON.stringify({ error } as GetAppPostResponse))
|
| 165 |
+
res.end()
|
| 166 |
+
return
|
| 167 |
+
}
|
| 168 |
}
|
| 169 |
})
|
| 170 |
|
src/types.mts
CHANGED
|
@@ -26,4 +26,10 @@ export type GetAppPostsResponse = {
|
|
| 26 |
success?: boolean
|
| 27 |
error?: string
|
| 28 |
posts: Post[]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
}
|
|
|
|
| 26 |
success?: boolean
|
| 27 |
error?: string
|
| 28 |
posts: Post[]
|
| 29 |
+
}
|
| 30 |
+
|
| 31 |
+
export type GetAppPostResponse = {
|
| 32 |
+
success?: boolean
|
| 33 |
+
error?: string
|
| 34 |
+
post: Post
|
| 35 |
}
|