Skip to content

Commit

Permalink
🏷️ 変数の型情報を明記
Browse files Browse the repository at this point in the history
  • Loading branch information
ZEKE320 committed Apr 6, 2024
1 parent a33a328 commit e8bc360
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 26 deletions.
8 changes: 4 additions & 4 deletions astro/src/lib/api/validateRequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ const validateRequestReturnURL = ({
})
}

const url = new URL(request.url).searchParams.get("url");
const lang = new URL(request.url).searchParams.get("lang");
const url: string | null = new URL(request.url).searchParams.get("url");
const lang: string | null = new URL(request.url).searchParams.get("lang");
if (url === null) {
return createErrResponse({
statusCode: 406,
Expand All @@ -36,10 +36,10 @@ const validateRequestReturnURL = ({
statusCode: 406,
})
}
const decodedUrl = decodeURIComponent(url)
const decodedUrl: string = decodeURIComponent(url)
// SSRF対策
// Productionではない環境についてはlocalhostの実行を許可
const validation = !isNotProduction ? (
const validation: boolean = !isNotProduction ? (
// Productionの場合は厳格なルールを指定
protocol_validation.test(decodedUrl) ||
loopback_validation.test(decodedUrl) ||
Expand Down
46 changes: 24 additions & 22 deletions astro/src/pages/api/getOgpMeta.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { APIContext, APIRoute } from "astro"
import type { ogpMetaData } from "@/lib/api/types"
import type { apiRequest, errorResponse, ogpMetaData } from "@/lib/api/types"
import { corsAllowOrigin } from "@/lib/vars"
import validateRequestReturnURL from "@/lib/api/validateRequest"

Expand All @@ -19,7 +19,7 @@ const findEncoding = async (htmlBlob: Blob): Promise<string> => {

for (const filter of headerRegExp) {
if (charset === undefined) {
const regResult = filter.exec(text)
const regResult: RegExpExecArray | null = filter.exec(text)
if (regResult !== null) {
charset = regResult[1]
}
Expand Down Expand Up @@ -57,7 +57,9 @@ export const GET: APIRoute = async ({
})
}
// APIの事前処理実施
const validateResult = validateRequestReturnURL({ request })
const validateResult: apiRequest | errorResponse = validateRequestReturnURL(
{ request },
)

// エラーの場合はエラーレスポンスを返却
if (validateResult.type === "error") {
Expand Down Expand Up @@ -97,7 +99,7 @@ export const GET: APIRoute = async ({

responseHTML = html

const meta = extractHead({ html })
const meta: ogpMetaData = extractHead({ html })

const response = new Response(JSON.stringify(meta), {
status: 200,
Expand All @@ -110,21 +112,21 @@ export const GET: APIRoute = async ({
name = error.name
msg = error.message
}

const errorObject: errorResponse & { html: string } = {
type: "error",
error: name,
message: msg,
status: 500,
html: responseHTML,
// ogpResult: responseOGPResult,
}

// return new Response(JSON.stringify(<errorResponse>{
return new Response(
JSON.stringify({
type: "error",
error: name,
message: msg,
status: 500,
html: responseHTML,
// ogpResult: responseOGPResult,
}),
{
status: 500,
headers: headers,
},
)
return new Response(JSON.stringify(errorObject), {
status: 500,
headers: headers,
})
}
}

Expand All @@ -141,10 +143,10 @@ const extractHead = ({ html }: { html: string }): ogpMetaData => {
let image: string | undefined = undefined

$("meta").each((_, element) => {
const property = $(element).attr("property")
const name = $(element).attr("name")
const content = $(element).attr("content")
const value = $(element).attr("value")
const property: string | undefined = $(element).attr("property")
const name: string | undefined = $(element).attr("name")
const content: string | undefined = $(element).attr("content")
const value: string | undefined = $(element).attr("value")

if (typeof content === "undefined") {
return
Expand Down

0 comments on commit e8bc360

Please sign in to comment.