diff --git a/src/bookmark-tweets.ts b/src/bookmark-tweets.ts index 97cd011..3d4c092 100644 --- a/src/bookmark-tweets.ts +++ b/src/bookmark-tweets.ts @@ -151,7 +151,7 @@ const getTweetDetails = async (url: string): Promise => { const cleanText = await expandShortLink( formattedText, /(https:\/\/t.co\/[a-zA-z0-9]+)/g - ) + ); const tweet = cleanText .replace(/“/g, `"`) .replace(/”/g, `"`) diff --git a/src/handler.ts b/src/handler.ts index 0a615d7..b8b42e4 100644 --- a/src/handler.ts +++ b/src/handler.ts @@ -17,7 +17,7 @@ import { CountColumn, RecordData, RequestPayload, - TableAggregate, + TablesAggregate, } from './typings.d'; // default responses @@ -73,6 +73,7 @@ const tagsList: { const handleAction = async (payload: RequestPayload): Promise => { // payload data is present at time point const data: RecordData = payload.type === 'Insert' ? payload.data : {}; + const fmtTable = payload.table.toLowerCase(); // default helps to determine if switch statement runs and correct table is used let location: string = 'None'; let response: BookmarkingResponse; @@ -96,9 +97,9 @@ const handleAction = async (payload: RequestPayload): Promise => { ); } case payload.type === 'Query': { - location = `${payload.type}-${payload.table}`; + location = `${payload.type}-${fmtTable}`; - const queryResults = await queryBookmarkItems(payload.table); + const queryResults = await queryBookmarkItems(fmtTable); return new Response( JSON.stringify({ @@ -109,10 +110,10 @@ const handleAction = async (payload: RequestPayload): Promise => { ); } case payload.type === 'Search': { - location = `${payload.type}-${payload.table}`; + location = `${payload.type}-${fmtTable}`; const searchResults = await searchBookmarkItems( - payload.table, + fmtTable, payload.query ?? '', payload.column ?? '' ); @@ -126,10 +127,10 @@ const handleAction = async (payload: RequestPayload): Promise => { ); } case payload.type === 'Count': { - location = `${payload.type}-${payload.table}`; + location = `${payload.type}-${fmtTable}`; const queryResults = await queryBookmarkAggregateCount( - payload.table as TableAggregate, + fmtTable as TablesAggregate, payload.countColumn as CountColumn ); @@ -142,23 +143,23 @@ const handleAction = async (payload: RequestPayload): Promise => { ); } case payload.table === 'Podcasts': - location = payload.table; + location = fmtTable; response = await bookmarkPodcasts(data.url, data.tags); break; case payload.table === 'Reddits': - location = payload.table; + location = fmtTable; response = await bookmarkReddits(data.url, data.tags); break; case payload.table === 'StackExchange': - location = payload.table; + location = fmtTable; response = await bookmarkStackExchange(data.url, data.tags); break; case payload.table === 'Tweets': - location = payload.table; + location = fmtTable; response = await bookmarkTweets(data.url, data.tags); break; case payload.table === 'Videos': - location = payload.table; + location = fmtTable; if (data.url.includes('vimeo')) { response = await bookmarkVimeo(data.url, data.tags); diff --git a/src/hasura.ts b/src/hasura.ts index 9ed16e1..492cc8c 100644 --- a/src/hasura.ts +++ b/src/hasura.ts @@ -7,7 +7,7 @@ import { CountColumn, RecordColumnAggregateCount, RecordData, - TableAggregate, + TablesAggregate, } from './typings.d'; const BK_FIELDS = { @@ -158,12 +158,12 @@ export const queryBookmarkItems = async ( * @function * @async * - * @param {string} table + * @param {TablesAggregate} table * @param {CountColumn} column * @returns {Promise} */ export const queryBookmarkAggregateCount = async ( - table: TableAggregate, + table: TablesAggregate, column: CountColumn ): Promise => { const sort = column === 'tags' ? 'title' : column; diff --git a/src/typings.d.ts b/src/typings.d.ts index 71a84b1..534d7a9 100644 --- a/src/typings.d.ts +++ b/src/typings.d.ts @@ -1139,7 +1139,7 @@ export type CountColumn = | 'tags' | 'user'; -export type TableAggregate = +export type TablesAggregate = | 'articles' | 'comics' | 'podcasts' @@ -1147,15 +1147,25 @@ export type TableAggregate = | 'tweets' | 'videos'; +export type Tables = + | 'Articles' + | 'Comics' + | 'Podcasts' + | 'Reddits' + | 'Tweets' + | 'Videos'; + export interface BookmarkingResponse { success: boolean; message: string; source: string; } +export type Types = 'Count' | 'Tags' | 'Query' | 'Search' | 'Insert' | 'Update'; + export interface RequestPayload { - type: string; - table: string; + type: Types; + table: Tables; tagList?: string; query?: string; column?: string;