Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: add more typescript helpers #395

Merged
merged 1 commit into from
Jan 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading