Skip to content

narrow down possible status codes for redirects to 300-308 #7615

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

Merged
merged 6 commits into from
Nov 11, 2022

Conversation

ivanhofer
Copy link
Contributor

closes #7614

Please don't delete this checklist! Before submitting the PR, please make sure you do the following:

  • It's really useful if your PR references an issue where it is discussed ahead of time. In many cases, features are absent for a reason. For large changes, please create an RFC: https://github.com/sveltejs/rfcs
  • This message body should clearly illustrate what problems it solves.
  • Ideally, include a test that fails without this PR but passes with it.

Tests

  • Run the tests with pnpm test and lint the project with pnpm lint and pnpm check

Changesets

  • If your PR makes a change that should be noted in one or more packages' changelogs, generate a changeset by running pnpm changeset and following the prompts. All changesets should be patch until SvelteKit 1.0

@changeset-bot
Copy link

changeset-bot bot commented Nov 11, 2022

🦋 Changeset detected

Latest commit: eed49f0

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@sveltejs/kit Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@ghost
Copy link

ghost commented Nov 11, 2022

Can't we make the type more generic ?

type RangeStartArray<
	LENGTH extends number,
	ACC extends unknown[] = [],
> = ACC['length'] extends LENGTH
	? ACC
	: RangeStartArray<LENGTH, [...ACC, 1]>

type NumericRangeBuilder<
	START_ARR extends number[],
	END extends number,
	ACC extends number = never>
	= START_ARR['length'] extends END
	? ACC | END
	: NumericRangeBuilder<[...START_ARR, 1], END, ACC | START_ARR['length']>

/**
 * A type that represents a range of numbers 'FROM' to `END` (inclusive)
 * 
 * @example
 * type Range = NumericRange<100, 200> // 100 | 101 ... 199 | 200
 */
export type NumericRange<FROM extends number, TO extends number> = NumericRangeBuilder<RangeStartArray<FROM>, TO>

/**
 * 
 * @property {number} informational - 1xx
 * @property {number} success - 2xx
 * @property {number} redirection - 3xx
 * @property {number} clientError - 4xx
 * @property {number} serverError - 5xx
 * 
 * @see https://developer.mozilla.org/en-US/docs/Web/HTTP/Status
 */
export type HttpStatusCode = {
	informational: NumericRange<1, 199>;
	success: NumericRange<200, 299>;
	redirect: NumericRange<300, 399>;
	clientError: NumericRange<400, 499>;
	serverError: NumericRange<500, 599>;
}
/**
 * Creates a `Redirect` object. If thrown during request handling, SvelteKit will
 * return a redirect response.
 * @param status The HTTP redirect status code (300-399)
 * @param location The location to redirect to
 */
export function redirect(status: HttpStatusCode['redirect'], location: string): Redirect;

@ivanhofer
Copy link
Contributor Author

Can't we make the type more generic ?

If it would be more performant.. then probably yes.
I haven't seen any performant NumberRange implementation yet. This one takes a few minutes on my machine to tell me that 500 is not allowed.

Copy link
Member

@dummdidumm dummdidumm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Number range generator is something for another PR, if ever - I'd rather have more simple types. In this case it's ok because it's only a handful of numbers, which can be hardcoded.

@Rich-Harris
Copy link
Member

thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

DX improvement: narrow down possible status codes for the redirect helper
4 participants