-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(app-builder): add support for raw uuids in decision routes (#362)
* feat(app-builder): add http status codes * fix(i18n): add missing translations to fix hydration issue * feat(app-builder): add support for raw uuids in decision routes * refactor(http): define http responses
- Loading branch information
Showing
7 changed files
with
482 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,29 @@ | ||
import { | ||
BAD_REQUEST, | ||
CONFLICT, | ||
FORBIDDEN, | ||
NOT_FOUND, | ||
} from '@app-builder/utils/http/http-status-codes'; | ||
import { type HttpError } from 'oazapfts'; | ||
|
||
export function isHttpError(error: unknown): error is HttpError { | ||
return error instanceof Error && 'status' in error; | ||
} | ||
|
||
export function isStatusConflictHttpError(error: unknown): error is HttpError { | ||
return isHttpError(error) && error.status === 409; | ||
return isHttpError(error) && error.status === CONFLICT; | ||
} | ||
|
||
export function isStatusBadRequestHttpError( | ||
error: unknown, | ||
): error is HttpError { | ||
return isHttpError(error) && error.status === 400; | ||
return isHttpError(error) && error.status === BAD_REQUEST; | ||
} | ||
|
||
export function isNotFoundHttpError(error: unknown): error is HttpError { | ||
return isHttpError(error) && error.status === 404; | ||
return isHttpError(error) && error.status === NOT_FOUND; | ||
} | ||
|
||
export function isForbiddenHttpError(error: unknown): error is HttpError { | ||
return isHttpError(error) && error.status === 403; | ||
return isHttpError(error) && error.status === FORBIDDEN; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import { redirect } from '@remix-run/node'; | ||
import { type ZodError, type ZodIssueOptionalMessage } from 'zod'; | ||
|
||
import { isRawUUIDIssue } from '../schema/shortUUIDSchema'; | ||
import { fromUUID } from '../short-uuid'; | ||
import { badRequest } from './http-responses'; | ||
|
||
/** | ||
* Handles a ZodError that is thrown when parsing request parameters. | ||
* | ||
* If the error is due to a UUID being invalid, it will redirect to the same URL with the UUID replaced by its short form. | ||
* Otherwise, it will return a 400 Bad Request response. | ||
*/ | ||
export function handleParseParamError<Input>( | ||
request: Request, | ||
error: ZodError<Input>, | ||
) { | ||
const { issues } = error; | ||
if (issues.some(isRawUUIDIssue)) { | ||
const redirectURL = (issues as ZodIssueOptionalMessage[]) | ||
.filter(isRawUUIDIssue) | ||
.reduce((acc, { params: { value } }) => { | ||
return acc.replace(value, fromUUID(value)); | ||
}, request.url); | ||
return redirect(redirectURL); | ||
} | ||
return badRequest(error.issues); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { json } from '@remix-run/node'; | ||
|
||
import { | ||
BAD_REQUEST, | ||
FORBIDDEN, | ||
INTERNAL_SERVER_ERROR, | ||
NOT_FOUND, | ||
UNAUTHORIZED, | ||
} from './http-status-codes'; | ||
|
||
function errorResponse<Data>(status: number) { | ||
return (data: Data, init?: Omit<ResponseInit, 'status'>) => { | ||
throw json(data, { status, ...init }); | ||
}; | ||
} | ||
|
||
export const badRequest = errorResponse(BAD_REQUEST); | ||
export const unauthorized = errorResponse(UNAUTHORIZED); | ||
export const forbidden = errorResponse(FORBIDDEN); | ||
export const notFound = errorResponse(NOT_FOUND); | ||
|
||
export const internalServerError = errorResponse(INTERNAL_SERVER_ERROR); |
Oops, something went wrong.