From c3f65b8e788b4813357ab3cfd5bd48a2cdfac6c3 Mon Sep 17 00:00:00 2001 From: ejithon Date: Tue, 21 May 2024 20:54:01 +0900 Subject: [PATCH] =?UTF-8?q?refactor:=20use=20result=20type=20=F0=9F=93=A6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/app/src/hooks/endpoint.ts | 8 +++++++- packages/app/src/utils/oas/oauth.ts | 24 ++++++++++++++++++------ 2 files changed, 25 insertions(+), 7 deletions(-) diff --git a/packages/app/src/hooks/endpoint.ts b/packages/app/src/hooks/endpoint.ts index 4bca3a5e5..207349bbc 100644 --- a/packages/app/src/hooks/endpoint.ts +++ b/packages/app/src/hooks/endpoint.ts @@ -554,10 +554,16 @@ export const useEndpoint = (): UseEndpointReturn => { error: await getHTTPError(response), }; } - globalThis.location.href = extractAuthorizationUrl( + const authorizationUrlResult = extractAuthorizationUrl( request.operation.responses, await response.json() ); + if (authorizationUrlResult.isFailure()) { + return { + error: authorizationUrlResult.value, + }; + } + globalThis.location.href = authorizationUrlResult.value; } } catch (e: unknown) { remove(KEY.OAUTH_ENDPOINT_ID); diff --git a/packages/app/src/utils/oas/oauth.ts b/packages/app/src/utils/oas/oauth.ts index 56db894fa..060f52577 100644 --- a/packages/app/src/utils/oas/oauth.ts +++ b/packages/app/src/utils/oas/oauth.ts @@ -1,20 +1,32 @@ +import { + Success, + Failure, + EndpointOAuthIllegalResponseError, + Result, +} from '~/errors'; import { Responses } from '~/types/oas'; -// eslint-disable-next-line @typescript-eslint/no-explicit-any -export const extractAuthorizationUrl = (responses: Responses, json: any) => { +export const extractAuthorizationUrl = ( + responses: Responses, + // eslint-disable-next-line @typescript-eslint/no-explicit-any + json: any +): Result => { const response = responses['200']; if (!response || !response.content?.['application/json']) { - return null; + return new Failure(new EndpointOAuthIllegalResponseError()); } const schema = response.content['application/json'].schema; if (!schema.properties) { - return null; + return new Failure(new EndpointOAuthIllegalResponseError()); } + const uriKey = Object.keys(schema.properties).find((key) => { const property = schema.properties?.[key]; return property?.type === 'string' && property.format === 'uri'; }); - if (uriKey) { - return json[uriKey]; + if (!uriKey) { + return new Failure(new EndpointOAuthIllegalResponseError()); } + + return new Success(json[uriKey]); };