jbilcke-hf HF Staff commited on
Commit
7e9affd
·
1 Parent(s): 42e4293

tiny tweak on the typ

Browse files
Files changed (2) hide show
  1. src/index.mts +5 -5
  2. src/types.mts +7 -3
src/index.mts CHANGED
@@ -5,7 +5,7 @@ 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 { Post } from "./types.mts"
9
  import { savePost } from "./core/savePost.mts"
10
  import { getAppPosts } from "./core/getAppPosts.mts"
11
  import { deletePost } from "./core/deletePost.mts"
@@ -97,13 +97,13 @@ app.post("/post", async (req, res) => {
97
  } catch (err) {
98
  console.error(`failed to save the post: ${err}`)
99
  res.status(400)
100
- res.write(JSON.stringify({ error: `failed to save the post: ${err}` }))
101
  res.end()
102
  return
103
  }
104
 
105
  res.status(201)
106
- res.write(JSON.stringify({ success: true, error: "", post }))
107
  res.end()
108
  })
109
 
@@ -122,14 +122,14 @@ app.get("/posts/:appId", async (req, res) => {
122
  try {
123
  const posts = await getAppPosts(appId)
124
  res.status(200)
125
- res.write(JSON.stringify({ posts }))
126
  res.end()
127
  return
128
  } catch (err) {
129
  const error = `failed to load the posts: ${err}`
130
  console.error(error)
131
  res.status(500)
132
- res.write(JSON.stringify({ posts: [], error }))
133
  res.end()
134
  return
135
  }
 
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 } from "./types.mts"
9
  import { savePost } from "./core/savePost.mts"
10
  import { getAppPosts } from "./core/getAppPosts.mts"
11
  import { deletePost } from "./core/deletePost.mts"
 
97
  } catch (err) {
98
  console.error(`failed to save the post: ${err}`)
99
  res.status(400)
100
+ res.write(JSON.stringify({ error: `failed to save the post: ${err}`, post: undefined } as CreatePostResponse))
101
  res.end()
102
  return
103
  }
104
 
105
  res.status(201)
106
+ res.write(JSON.stringify({ success: true, error: "", post } as CreatePostResponse))
107
  res.end()
108
  })
109
 
 
122
  try {
123
  const posts = await getAppPosts(appId)
124
  res.status(200)
125
+ res.write(JSON.stringify({ posts } as GetAppPostsResponse))
126
  res.end()
127
  return
128
  } catch (err) {
129
  const error = `failed to load the posts: ${err}`
130
  console.error(error)
131
  res.status(500)
132
+ res.write(JSON.stringify({ posts: [], error } as GetAppPostsResponse))
133
  res.end()
134
  return
135
  }
src/types.mts CHANGED
@@ -11,10 +11,14 @@ export type Post = {
11
  downvotes: number
12
  }
13
 
14
- export type PostAPIResponse = {
15
  success?: boolean
16
  error?: string
17
- posts: Post[]
18
  }
19
 
20
- export type PostAPIRequest = Partial<Post>
 
 
 
 
 
11
  downvotes: number
12
  }
13
 
14
+ export type CreatePostResponse = {
15
  success?: boolean
16
  error?: string
17
+ post: Post
18
  }
19
 
20
+ export type GetAppPostsResponse = {
21
+ success?: boolean
22
+ error?: string
23
+ posts: Post[]
24
+ }