-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
108 additions
and
103 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { corsHeaders } from "./headers"; | ||
|
||
export function handleOptions (request) { | ||
if ( | ||
request.headers.get("Origin") !== null && | ||
request.headers.get("Access-Control-Request-Method") !== null && | ||
request.headers.get("Access-Control-Request-Headers") !== null | ||
) { | ||
return new Response(null, { | ||
headers: { | ||
...corsHeaders, | ||
"Access-Control-Allow-Headers": request.headers.get("Access-Control-Request-Headers"), | ||
}, | ||
}) | ||
} else { | ||
return new Response(null, { | ||
headers: { | ||
Allow: "GET, HEAD, POST, OPTIONS", | ||
}, | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import { addRow } from "./addRow"; | ||
import { checkCaptcha } from "./checkCaptcha"; | ||
import { getTableHeader } from "./getTableHeader"; | ||
import { googleAuth } from "./googleAuth"; | ||
import { handleOptions } from "./handleOptions"; | ||
import { responseHeaders } from "./headers"; | ||
import { responseError } from "./responseError"; | ||
import { validateData } from "./validateData"; | ||
|
||
export async function handleRequest(request, env) { | ||
if (request.method === "OPTIONS") { | ||
return handleOptions(request) | ||
} | ||
|
||
const data = await request.json(); | ||
const { pathname } = new URL(request.url); | ||
|
||
if (!pathname.startsWith('/api/word-error')) { | ||
return responseError('invalidPath'); | ||
} | ||
|
||
const dataIsValid = validateData(data); | ||
|
||
if (!dataIsValid) { | ||
return responseError('invalidData'); | ||
} | ||
|
||
const ip = request.headers.get('CF-Connecting-IP'); | ||
const captchaIsOk = await checkCaptcha(env.CLOUDFLARE_CAPTCHA_SECRET_KEY, data.captchaToken, ip); | ||
|
||
if (!captchaIsOk) { | ||
return responseError('invalidCaptcha'); | ||
} | ||
|
||
const googleAccessToken = await googleAuth(env.GOOGLE_SERVICE_ACCOUNT_EMAIL, env.GOOGLE_PRIVATE_KEY); | ||
|
||
if (!googleAccessToken) { | ||
return responseError('invalidGoogleAccessToken'); | ||
} | ||
|
||
const tableHeader = await getTableHeader(env.GOOGLE_WORD_ERRORS_TABLE_ID, googleAccessToken); | ||
|
||
if (!tableHeader || !tableHeader.length) { | ||
return responseError('invalidTableHeader'); | ||
} | ||
|
||
const newRow = tableHeader.map((fieldName) => { | ||
if (fieldName === 'timestamp') { | ||
return Math.round(new Date().getTime() / 1000).toString(); | ||
} | ||
|
||
return data[fieldName]; | ||
}); | ||
|
||
const addRowResponse = await addRow(env.GOOGLE_WORD_ERRORS_TABLE_ID, newRow, googleAccessToken); | ||
|
||
if (addRowResponse.status === 200) { | ||
return new Response(JSON.stringify({ error: null }), { | ||
status: addRowResponse.status, | ||
headers: responseHeaders, | ||
}); | ||
} else { | ||
return responseError('invalidAddRow'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
export const corsHeaders = { | ||
'Access-Control-Allow-Origin': '*', | ||
'Access-Control-Allow-Methods': 'GET,HEAD,POST,OPTIONS', | ||
'Access-Control-Max-Age': '86400', | ||
} | ||
|
||
export const responseHeaders = { | ||
...corsHeaders, | ||
'Content-Type': 'application/json', | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { responseHeaders } from "./headers"; | ||
|
||
export function responseError(error) { | ||
return new Response(JSON.stringify({ error }), { | ||
status: 500, | ||
headers: responseHeaders, | ||
}); | ||
} |