Skip to content

Commit

Permalink
fix: add more typescript helpers (#395)
Browse files Browse the repository at this point in the history
  • Loading branch information
aeneasr authored Jan 10, 2025
1 parent 73b6f88 commit 010abb5
Show file tree
Hide file tree
Showing 9 changed files with 233 additions and 178 deletions.
9 changes: 9 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@

# Formats the code
.PHONY: format
format: node_modules
npm exec -- prettier --write 'contrib/**/*{.ts,.js}'

node_modules: package-lock.json
npm ci
touch node_modules
76 changes: 38 additions & 38 deletions contrib/fetch/src/continueWith.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ import {
ContinueWithSettingsUi,
ContinueWithVerificationUi,
ContinueWithRedirectBrowserTo,
} from "../";
} from "../"

export type OnRedirectHandler = (url: string, external: boolean) => void;
export type OnRedirectHandler = (url: string, external: boolean) => void

// The order in which the actions are defined here is the order in which they are expected to be executed.
const continueWithPriority = [
Expand All @@ -19,46 +19,46 @@ const continueWithPriority = [
"show_verification_ui",
"redirect_browser_to",
"set_ory_session_token",
];
]

export function handleContinueWith(
continueWith: ContinueWith[] | undefined,
{ onRedirect }: { onRedirect: OnRedirectHandler }
{ onRedirect }: { onRedirect: OnRedirectHandler },
): boolean {
if (!continueWith || continueWith.length === 0) {
return false;
return false
}

const action = pickBestContinueWith(continueWith);
const action = pickBestContinueWith(continueWith)
if (!action) {
return false;
return false
}

const redirectFlow = (id: string, flow: string, url?: string) => {
if (url) {
onRedirect(url, true);
return true;
onRedirect(url, true)
return true
}

onRedirect("/" + flow + "?flow=" + id, false);
return true;
};
onRedirect("/" + flow + "?flow=" + id, false)
return true
}

if (isSetOrySessionToken(action)) {
throw new Error("Ory Elements does not support API flows yet.");
throw new Error("Ory Elements does not support API flows yet.")
} else if (isRedirectBrowserTo(action) && action.redirect_browser_to) {
// console.log("Redirecting to", action.redirect_browser_to)
onRedirect(action.redirect_browser_to, true);
return true;
onRedirect(action.redirect_browser_to, true)
return true
} else if (isShowVerificationUi(action)) {
return redirectFlow(action.flow.id, "verification", action.flow.url);
return redirectFlow(action.flow.id, "verification", action.flow.url)
} else if (isShowRecoveryUi(action)) {
return redirectFlow(action.flow.id, "recovery", action.flow.url);
return redirectFlow(action.flow.id, "recovery", action.flow.url)
} else if (isShowSettingsUi(action)) {
// TODO: re-add url
return redirectFlow(action.flow.id, "settings", action.flow.url);
return redirectFlow(action.flow.id, "settings", action.flow.url)
} else {
throw new Error("Unknown action: " + JSON.stringify(action));
throw new Error("Unknown action: " + JSON.stringify(action))
}
}

Expand All @@ -69,15 +69,15 @@ export function handleContinueWith(
*/
export function pickBestContinueWith(continueWith: ContinueWith[]) {
if (!continueWith || continueWith.length === 0) {
return;
return
}

const sorted = continueWith.sort(
(a, b) =>
continueWithPriority.indexOf(a.action) -
continueWithPriority.indexOf(b.action)
);
return sorted[0];
continueWithPriority.indexOf(b.action),
)
return sorted[0]
}

/**
Expand All @@ -86,11 +86,11 @@ export function pickBestContinueWith(continueWith: ContinueWith[]) {
* @param continueWith - The continue with action.
*/
export function isSetOrySessionToken(
continueWith: ContinueWith
continueWith: ContinueWith,
): continueWith is ContinueWithSetOrySessionToken & {
action: "set_ory_session_token";
action: "set_ory_session_token"
} {
return continueWith.action === "set_ory_session_token";
return continueWith.action === "set_ory_session_token"
}

/**
Expand All @@ -99,11 +99,11 @@ export function isSetOrySessionToken(
* @param continueWith - The continue with action.
*/
export function isRedirectBrowserTo(
continueWith: ContinueWith
continueWith: ContinueWith,
): continueWith is ContinueWithRedirectBrowserTo & {
action: "redirect_browser_to";
action: "redirect_browser_to"
} {
return continueWith.action === "redirect_browser_to";
return continueWith.action === "redirect_browser_to"
}

/**
Expand All @@ -112,11 +112,11 @@ export function isRedirectBrowserTo(
* @param continueWith - The continue with action.
*/
export function isShowRecoveryUi(
continueWith: ContinueWith
continueWith: ContinueWith,
): continueWith is ContinueWithRecoveryUi & {
action: "show_recovery_ui";
action: "show_recovery_ui"
} {
return continueWith.action === "show_recovery_ui";
return continueWith.action === "show_recovery_ui"
}

/**
Expand All @@ -125,11 +125,11 @@ export function isShowRecoveryUi(
* @param continueWith - The continue with action.
*/
export function isShowSettingsUi(
continueWith: ContinueWith
continueWith: ContinueWith,
): continueWith is ContinueWithSettingsUi & {
action: "show_settings_ui";
action: "show_settings_ui"
} {
return continueWith.action === "show_settings_ui";
return continueWith.action === "show_settings_ui"
}

/**
Expand All @@ -138,9 +138,9 @@ export function isShowSettingsUi(
* @param continueWith - The continue with action.
*/
export function isShowVerificationUi(
continueWith: ContinueWith
continueWith: ContinueWith,
): continueWith is ContinueWithVerificationUi & {
action: "show_verification_ui";
action: "show_verification_ui"
} {
return continueWith.action === "show_verification_ui";
return continueWith.action === "show_verification_ui"
}
Loading

0 comments on commit 010abb5

Please sign in to comment.