-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #311 from nekochans/feature/issue310/change-api-route
API用のBFFはEdge Runtime利用のRoute Handlersを利用するように変更
- Loading branch information
Showing
43 changed files
with
1,040 additions
and
160 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,29 @@ | ||
type Options = { | ||
statusCode?: number; | ||
statusText?: string; | ||
headers?: Record<string, string>; | ||
responseBody?: unknown; | ||
}; | ||
|
||
export class IssueClientCredentialsAccessTokenError extends Error { | ||
static { | ||
this.prototype.name = 'IssueClientCredentialsAccessTokenError'; | ||
} | ||
|
||
private readonly statusCode: number | undefined; | ||
|
||
private readonly statusText: string | undefined; | ||
|
||
private readonly headers: Record<string, string> | undefined; | ||
|
||
private readonly responseBody: unknown; | ||
|
||
constructor(message = '', options: Options = {}) { | ||
const { statusCode, statusText, headers, responseBody, ...rest } = options; | ||
super(message, rest); | ||
this.statusCode = statusCode; | ||
this.statusText = statusText; | ||
this.headers = headers; | ||
this.responseBody = responseBody; | ||
} | ||
} |
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 @@ | ||
export { issueClientCredentialsAccessToken } from './openIdConnect'; |
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,122 @@ | ||
import { upstashRedisRestToken, upstashRedisRestUrl } from '@/constants'; | ||
import { validation, type IssueClientCredentialsAccessToken } from '@/features'; | ||
import { Redis } from '@upstash/redis'; | ||
import { z } from 'zod'; | ||
import { IssueClientCredentialsAccessTokenError } from './IssueClientCredentialsAccessTokenError'; | ||
|
||
type CognitoTokenResponseBody = { | ||
access_token: string; | ||
// eslint-disable-next-line no-magic-numbers | ||
expires_in: 3600; | ||
token_type: 'Bearer'; | ||
}; | ||
|
||
const cognitoTokenResponseBodySchema = z.object({ | ||
access_token: z.string(), | ||
expires_in: z.number().min(3600), | ||
token_type: z.literal('Bearer'), | ||
}); | ||
|
||
const isCognitoTokenResponseBody = ( | ||
value: unknown, | ||
): value is CognitoTokenResponseBody => { | ||
return validation(cognitoTokenResponseBodySchema, value).isValidate; | ||
}; | ||
|
||
const cognitoClientId = (): string => { | ||
if (process.env.COGNITO_CLIENT_ID != null) { | ||
return process.env.COGNITO_CLIENT_ID; | ||
} | ||
|
||
throw new IssueClientCredentialsAccessTokenError( | ||
'COGNITO_CLIENT_ID is not defined', | ||
); | ||
}; | ||
|
||
const cognitoClientSecret = (): string => { | ||
if (process.env.COGNITO_CLIENT_SECRET != null) { | ||
return process.env.COGNITO_CLIENT_SECRET; | ||
} | ||
|
||
throw new IssueClientCredentialsAccessTokenError( | ||
'COGNITO_CLIENT_SECRET is not defined', | ||
); | ||
}; | ||
|
||
const cognitoTokenEndpoint = (): string => { | ||
if (process.env.COGNITO_TOKEN_ENDPOINT != null) { | ||
return process.env.COGNITO_TOKEN_ENDPOINT; | ||
} | ||
|
||
throw new IssueClientCredentialsAccessTokenError( | ||
'COGNITO_TOKEN_ENDPOINT is not defined', | ||
); | ||
}; | ||
|
||
export const issueClientCredentialsAccessToken: IssueClientCredentialsAccessToken = | ||
async () => { | ||
const redis = new Redis({ | ||
url: upstashRedisRestUrl(), | ||
token: upstashRedisRestToken(), | ||
}); | ||
|
||
const cachedAccessToken = await redis.get(cognitoClientId()); | ||
if (typeof cachedAccessToken === 'string') { | ||
return cachedAccessToken; | ||
} | ||
|
||
const authorization = btoa(`${cognitoClientId()}:${cognitoClientSecret()}`); | ||
|
||
const options = { | ||
method: 'POST', | ||
headers: { | ||
'Content-Type': 'application/x-www-form-urlencoded', | ||
Authorization: `Basic ${authorization}`, | ||
}, | ||
body: 'grant_type=client_credentials&scope=api.lgtmeow/all image-recognition-api.lgtmeow/all', | ||
}; | ||
|
||
const response = await fetch(cognitoTokenEndpoint(), options); | ||
if (!response.ok) { | ||
const responseBody = await response.text(); | ||
const headers: Record<string, string> = {}; | ||
response.headers.forEach((value, key) => { | ||
headers[key] = value; | ||
}); | ||
|
||
throw new IssueClientCredentialsAccessTokenError( | ||
`failed to issueAccessToken: ${response.status} ${response.statusText}`, | ||
{ | ||
statusCode: response.status, | ||
statusText: response.statusText, | ||
headers, | ||
responseBody, | ||
}, | ||
); | ||
} | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment | ||
const responseBody = (await response.json()) as unknown; | ||
|
||
if (isCognitoTokenResponseBody(responseBody)) { | ||
await redis.set(cognitoClientId(), responseBody.access_token); | ||
await redis.expire(cognitoClientId(), 3000); | ||
|
||
return responseBody.access_token; | ||
} | ||
|
||
const headers: Record<string, string> = {}; | ||
response.headers.forEach((value, key) => { | ||
headers[key] = value; | ||
}); | ||
|
||
throw new IssueClientCredentialsAccessTokenError( | ||
'response body is invalid', | ||
{ | ||
statusCode: response.status, | ||
statusText: response.statusText, | ||
headers, | ||
responseBody, | ||
}, | ||
); | ||
}; |
This file was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -1 +1,2 @@ | ||
export * from './fetch'; | ||
export * from './cognito'; | ||
export * from './lgtmeow'; |
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,2 @@ | ||
export * from './lgtmImage'; | ||
export { isFetchLgtmImagesResponseBody } from './validation'; |
Oops, something went wrong.