Skip to content

Commit

Permalink
chore: synchronize workspaces
Browse files Browse the repository at this point in the history
  • Loading branch information
aeneasr committed Jan 10, 2025
1 parent a990a6e commit 6500458
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 63 deletions.
14 changes: 11 additions & 3 deletions clients/client/typescript-fetch/src/contrib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@ export type ValidationErrorHandler<T> = (body: T) => void;
type FlowErrorHandlerProps<T> = {
/**
* When the SDK returns an error indicating that the flow needs to be restarted, this function is called.
*
* @param useFlowId - If provided, the SDK should use this flow ID to not lose context of the flow.
*/
onRestartFlow: () => void;
onRestartFlow: (useFlowId?: string) => void;

/**
* When the SDK returns a validation error, this function is called. The result should be used to update the
Expand All @@ -43,13 +45,19 @@ type FlowErrorHandlerProps<T> = {
*/
export const handleFlowError =
<T>(opts: FlowErrorHandlerProps<T>) =>
async (err: unknown) => {
async (err: unknown): Promise<void |T> => {
if (isResponseError(err)) {
switch (err.response.status) {
case 404: // Does not exist
opts.onRestartFlow();
return;
case 410: // Expired
const body = await toBody(err.response);
console.log({body})
if (isSelfServiceFlowExpiredError(body)) {
opts.onRestartFlow(body.use_flow_id);
return;
}
// Re-initialize the flow
opts.onRestartFlow();
return;
Expand All @@ -68,7 +76,7 @@ export const handleFlowError =
opts.onRedirect(body.redirect_browser_to, true);
return;
} else if (isSelfServiceFlowExpiredError(body)) {
opts.onRestartFlow();
opts.onRestartFlow(body.use_flow_id);
return;
} else if (isNeedsPrivilegedSessionError(body)) {
opts.onRedirect(body.redirect_browser_to, true);
Expand Down
127 changes: 67 additions & 60 deletions contrib/fetch/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@ export type ValidationErrorHandler<T> = (body: T) => void;
type FlowErrorHandlerProps<T> = {
/**
* When the SDK returns an error indicating that the flow needs to be restarted, this function is called.
*
* @param useFlowId - If provided, the SDK should use this flow ID to not lose context of the flow.
*/
onRestartFlow: () => void;
onRestartFlow: (useFlowId?: string) => void;

/**
* When the SDK returns a validation error, this function is called. The result should be used to update the
Expand All @@ -42,74 +44,79 @@ type FlowErrorHandlerProps<T> = {
* @param opts - The configuration object.
*/
export const handleFlowError =
<T>(opts: FlowErrorHandlerProps<T>) =>
async (err: unknown) => {
if (isResponseError(err)) {
switch (err.response.status) {
case 404: // Does not exist
opts.onRestartFlow();
return;
case 410: // Expired
// Re-initialize the flow
opts.onRestartFlow();
return;
case 400:
return opts.onValidationError(
// TODO: maybe we can do actual type checking here?
(await err.response.json()) as unknown as T
);
case 403: // This typically happens with CSRF violations.
case 422: {
const body = await toBody(err.response);
if (
isBrowserLocationChangeRequired(body) &&
body.redirect_browser_to
) {
opts.onRedirect(body.redirect_browser_to, true);
return;
} else if (isSelfServiceFlowExpiredError(body)) {
opts.onRestartFlow();
return;
} else if (isNeedsPrivilegedSessionError(body)) {
opts.onRedirect(body.redirect_browser_to, true);
return;
} else if (isCsrfError(body)) {
opts.onRestartFlow();
return;
}
<T>(opts: FlowErrorHandlerProps<T>) =>
async (err: unknown): Promise<void |T> => {
if (isResponseError(err)) {
switch (err.response.status) {
case 404: // Does not exist
opts.onRestartFlow();
return;
case 410: // Expired
const body = await toBody(err.response);
if (isSelfServiceFlowExpiredError(body)) {
opts.onRestartFlow(body.use_flow_id);
return;
}
// Re-initialize the flow
opts.onRestartFlow();
return;
case 400:
return opts.onValidationError(
// TODO: maybe we can do actual type checking here?
(await err.response.json()) as unknown as T
);
case 403: // This typically happens with CSRF violations.
case 422: {
const body = await toBody(err.response);
if (
isBrowserLocationChangeRequired(body) &&
body.redirect_browser_to
) {
opts.onRedirect(body.redirect_browser_to, true);
return;
} else if (isSelfServiceFlowExpiredError(body)) {
opts.onRestartFlow(body.use_flow_id);
return;
} else if (isNeedsPrivilegedSessionError(body)) {
opts.onRedirect(body.redirect_browser_to, true);
return;
} else if (isCsrfError(body)) {
opts.onRestartFlow();
return;
}

throw new ResponseError(
err.response,
"The Ory API endpoint returned a response code the SDK does not know how to handle. Please check the network tab for more information:" +
JSON.stringify(body)
);
}
throw new ResponseError(
err.response,
"The Ory API endpoint returned a response code the SDK does not know how to handle. Please check the network tab for more information:" +
JSON.stringify(body)
);
}

default:
throw new ResponseError(
err.response,
"The Ory API endpoint returned a response code the SDK does not know how to handle. Please check the network tab for more information."
);
}
} else if (err instanceof FetchError) {
throw new FetchError(
err,
"Unable to call the API endpoint. Ensure that CORS is set up correctly and that you have provided a valid SDK URL to Ory Elements."
);
} else if (err instanceof RequiredError) {
// TODO (@aeneasr) Happens on submit usually. Not sure how to handle yet.
}
default:
throw new ResponseError(
err.response,
"The Ory API endpoint returned a response code the SDK does not know how to handle. Please check the network tab for more information."
);
}
} else if (err instanceof FetchError) {
throw new FetchError(
err,
"Unable to call the API endpoint. Ensure that CORS is set up correctly and that you have provided a valid SDK URL to Ory Elements."
);
} else if (err instanceof RequiredError) {
// TODO (@aeneasr) Happens on submit usually. Not sure how to handle yet.
}

throw err;
};
throw err;
};

export async function toBody(response: Response): Promise<unknown> {
try {
return (await response.clone().json()) as unknown;
} catch (e) {
throw new ResponseError(
response,
"The Ory API endpoint returned a response the SDK does not know how to handle:" +
response,
"The Ory API endpoint returned a response the SDK does not know how to handle:" +
(await response.text())
);
}
Expand Down

0 comments on commit 6500458

Please sign in to comment.