-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathroute.ts
79 lines (59 loc) · 2.6 KB
/
route.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import { createClient } from '@/utils/supabase/server'
import { SupabaseClient } from '@supabase/supabase-js'
import { keepAliveConfig as config } from '@/config/keep-alive-config'
import { QueryResponse, determineAction, generateRandomString } from './helper'
export const dynamic = 'force-dynamic' // defaults to auto
const querySupabase = async (supabase: SupabaseClient, randomStringLength: number = 12): Promise<QueryResponse> => {
const currentRandomString = generateRandomString(randomStringLength)
const { data, error } = await supabase
.from(config.table)
.select('*')
.eq(config.column, currentRandomString)
const messageInfo: string = `Results for retrieving\n'${currentRandomString}' from '${config.table}' at column '${config.column}'`
if (error) {
const errorInfo = `${messageInfo}: ${error.message}`
if (config.consoleLogOnError) console.log(errorInfo)
return {
successful: false,
message: errorInfo
}
}
return {
successful: true,
message: `${messageInfo}: ${JSON.stringify(data)}`
}
}
const fetchOtherEndpoints = async (): Promise<string[]> => {
if (config?.otherEndpoints != null && config?.otherEndpoints.length > 0) {
const fetchPromises = config.otherEndpoints.map(async (endpoint) => {
const endpointResults = await fetch(endpoint, { cache: 'no-store' })
const passOrFail = (endpointResults?.status == 200) ? 'Passed' : 'Failed'
return `${endpoint} - ${passOrFail}`
})
const fetchResults = await Promise.all(fetchPromises)
return fetchResults
}
return []
}
export async function GET() {
const supabase = createClient() // maybe switch to ClientSide Client
let responseMessage: string = ''
let successfulResponses: boolean = true
if (config?.disableRandomStringQuery != true) {
const querySupabaseResponse: QueryResponse = await querySupabase(supabase)
successfulResponses = successfulResponses && querySupabaseResponse.successful
responseMessage += querySupabaseResponse.message + '\n\n'
}
if (config?.allowInsertionAndDeletion == true) {
const insertOrDeleteResults: QueryResponse = await determineAction(supabase)
successfulResponses = successfulResponses && insertOrDeleteResults.successful
responseMessage += insertOrDeleteResults.message + '\n\n'
}
if (config?.otherEndpoints != null && config?.otherEndpoints.length > 0) {
const fetchResults: string[] = await fetchOtherEndpoints()
responseMessage += `\n\nOther Endpoint Results:\n${fetchResults.join('\n')}`
}
return new Response(responseMessage, {
status: (successfulResponses == true) ? 200 : 400
})
}