Skip to content

Commit

Permalink
Merge pull request #230 from nekochans/feature/issue227/change-api-re…
Browse files Browse the repository at this point in the history
…sponse

API Routeのエラーレスポンス型をRFC7807を参考に変更
  • Loading branch information
keitakn authored Nov 17, 2022
2 parents 0f589d9 + dfc88e0 commit 39cc192
Showing 1 changed file with 12 additions and 16 deletions.
28 changes: 12 additions & 16 deletions src/pages/api/oidc/token.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@ import {
cognitoClientId,
cognitoClientSecret,
httpStatusCode,
type HttpStatusCode,
} from '../../../constants';
import { cognitoTokenEndpointUrl, type AccessToken } from '../../../features';
import {
cognitoTokenEndpointUrl,
IssueAccessTokenError,
type AccessToken,
} from '../../../features';

export type CognitoTokenResponseBody = {
// eslint-disable-next-line camelcase
Expand All @@ -21,10 +24,9 @@ export type CognitoTokenResponseBody = {
export type IssueClientCredentialsAccessTokenResponse = AccessToken;

export type IssueClientCredentialsAccessTokenErrorResponse = {
error?: {
code: HttpStatusCode;
message: string;
};
type: 'MethodNotAllowed';
title: 'This method is not allowed.';
status: 405;
};

const methodNotAllowedErrorResponse = (
Expand All @@ -34,10 +36,9 @@ const methodNotAllowedErrorResponse = (
>
) =>
res.status(httpStatusCode.methodNotAllowed).json({
error: {
code: httpStatusCode.methodNotAllowed,
message: 'Method Not Allowed',
},
type: 'MethodNotAllowed',
title: 'This method is not allowed.',
status: httpStatusCode.methodNotAllowed,
});

const issueClientCredentialsAccessToken = async (
Expand All @@ -61,12 +62,7 @@ const issueClientCredentialsAccessToken = async (

const response = await fetch(cognitoTokenEndpointUrl(), options);
if (!response.ok) {
return res.status(httpStatusCode.internalServerError).json({
error: {
code: httpStatusCode.internalServerError,
message: 'Internal Server Error',
},
});
throw new IssueAccessTokenError(response.statusText);
}

const responseBody = (await response.json()) as CognitoTokenResponseBody;
Expand Down

1 comment on commit 39cc192

@vercel
Copy link

@vercel vercel bot commented on 39cc192 Nov 17, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.