Skip to content

Commit

Permalink
refactor: use result type 📦
Browse files Browse the repository at this point in the history
  • Loading branch information
ejithon committed May 21, 2024
1 parent c5b14ea commit c3f65b8
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 7 deletions.
8 changes: 7 additions & 1 deletion packages/app/src/hooks/endpoint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
24 changes: 18 additions & 6 deletions packages/app/src/utils/oas/oauth.ts
Original file line number Diff line number Diff line change
@@ -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<string, EndpointOAuthIllegalResponseError> => {
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]);
};

0 comments on commit c3f65b8

Please sign in to comment.