From 3a71193ff850bdc8fcc232ceda5bcfc0d769d837 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20L=C3=B3pez=20Guimaraes?= Date: Thu, 29 Jun 2023 19:55:02 +0100 Subject: [PATCH 1/3] Add support for search_key to be string, not array The `search_key` parameter isn't always going to be a string array, but it can be a single string. Support both cases accordingly. --- src/services/api/routes/posts.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/services/api/routes/posts.ts b/src/services/api/routes/posts.ts index 6ad8aa7..b504b02 100644 --- a/src/services/api/routes/posts.ts +++ b/src/services/api/routes/posts.ts @@ -27,7 +27,7 @@ const newPostSchema = z.object({ screenshot: z.string().optional(), body: z.string().optional(), feeling_id: z.string(), - search_key: z.string().array().optional(), + search_key: z.string().array().or(z.string()).optional(), topic_tag: z.string().optional(), is_autopost: z.string(), is_spoiler: z.string().optional(), @@ -236,7 +236,7 @@ async function newPost(request: express.Request, response: express.Response): Pr const screenshot: string = bodyCheck.data.screenshot?.replace(/\0/g, '').trim() || ''; const appData: string = bodyCheck.data.app_data?.replace(/[^A-Za-z0-9+/=\s]/g, '').trim() || ''; const feelingID: number = parseInt(bodyCheck.data.feeling_id); - const searchKey: string[] | undefined = bodyCheck.data.search_key || []; + const searchKey: string[] | undefined = (typeof bodyCheck.data.search_key === 'string' ? [bodyCheck.data.search_key as string] : bodyCheck.data.search_key as string[]) || []; const topicTag: string | undefined = bodyCheck.data.topic_tag || ''; const autopost: string = bodyCheck.data.is_autopost; const spoiler: string | undefined = bodyCheck.data.is_spoiler; From 2eb969c0f17d14b58e6fe5a48a23aa287c1b4bc2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20L=C3=B3pez=20Guimaraes?= Date: Thu, 29 Jun 2023 20:59:19 +0100 Subject: [PATCH 2/3] Simplify check a bit We don't have to assert the string array, as it could be undefined and we can handle that. --- src/services/api/routes/posts.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/services/api/routes/posts.ts b/src/services/api/routes/posts.ts index b504b02..cd09119 100644 --- a/src/services/api/routes/posts.ts +++ b/src/services/api/routes/posts.ts @@ -236,7 +236,7 @@ async function newPost(request: express.Request, response: express.Response): Pr const screenshot: string = bodyCheck.data.screenshot?.replace(/\0/g, '').trim() || ''; const appData: string = bodyCheck.data.app_data?.replace(/[^A-Za-z0-9+/=\s]/g, '').trim() || ''; const feelingID: number = parseInt(bodyCheck.data.feeling_id); - const searchKey: string[] | undefined = (typeof bodyCheck.data.search_key === 'string' ? [bodyCheck.data.search_key as string] : bodyCheck.data.search_key as string[]) || []; + const searchKey: string[] | undefined = (typeof bodyCheck.data.search_key === 'string' ? [bodyCheck.data.search_key as string] : bodyCheck.data.search_key) || []; const topicTag: string | undefined = bodyCheck.data.topic_tag || ''; const autopost: string = bodyCheck.data.is_autopost; const spoiler: string | undefined = bodyCheck.data.is_spoiler; From d92defbf9f17a64565db25486f241db3e8c733ce Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20L=C3=B3pez=20Guimaraes?= Date: Fri, 30 Jun 2023 02:21:21 +0100 Subject: [PATCH 3/3] Change search_key type check We check if it's an array, and if it isn't, we make an array with itself. --- src/services/api/routes/posts.ts | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/services/api/routes/posts.ts b/src/services/api/routes/posts.ts index cd09119..9cc0935 100644 --- a/src/services/api/routes/posts.ts +++ b/src/services/api/routes/posts.ts @@ -236,7 +236,7 @@ async function newPost(request: express.Request, response: express.Response): Pr const screenshot: string = bodyCheck.data.screenshot?.replace(/\0/g, '').trim() || ''; const appData: string = bodyCheck.data.app_data?.replace(/[^A-Za-z0-9+/=\s]/g, '').trim() || ''; const feelingID: number = parseInt(bodyCheck.data.feeling_id); - const searchKey: string[] | undefined = (typeof bodyCheck.data.search_key === 'string' ? [bodyCheck.data.search_key as string] : bodyCheck.data.search_key) || []; + let searchKey: string | string[] = bodyCheck.data.search_key || []; const topicTag: string | undefined = bodyCheck.data.topic_tag || ''; const autopost: string = bodyCheck.data.is_autopost; const spoiler: string | undefined = bodyCheck.data.is_spoiler; @@ -326,6 +326,10 @@ async function newPost(request: express.Request, response: express.Response): Pr return; } + if (!Array.isArray(searchKey)) { + searchKey = [searchKey]; + } + const document: IPost = { id: '', // * This gets changed when saving the document for the first time title_id: request.paramPack.title_id,