Skip to content

Commit

Permalink
[minor] add errorHttpStatusCategories,ErrorHttpStatusCategories,Succe…
Browse files Browse the repository at this point in the history
…ssHttpStatusCategories,isErrorHttpStatus,HttpStatusToCategory,httpStatusToCategory
  • Loading branch information
electrovir committed Jan 6, 2025
1 parent 1562996 commit 3562fe0
Show file tree
Hide file tree
Showing 11 changed files with 234 additions and 48 deletions.
48 changes: 24 additions & 24 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@augment-vir/mono-repo-root",
"version": "31.5.0",
"version": "31.6.0",
"private": true,
"homepage": "https://github.com/electrovir/augment-vir",
"bugs": {
Expand Down
4 changes: 2 additions & 2 deletions packages/assert/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@augment-vir/assert",
"version": "31.5.0",
"version": "31.6.0",
"description": "A collection of assertions for test and production code alike.",
"keywords": [
"augment",
Expand Down Expand Up @@ -41,7 +41,7 @@
"test:update": "npm test"
},
"dependencies": {
"@augment-vir/core": "^31.5.0",
"@augment-vir/core": "^31.6.0",
"@date-vir/duration": "^7.1.1",
"deep-eql": "^5.0.2",
"expect-type": "^1.1.0",
Expand Down
6 changes: 3 additions & 3 deletions packages/common/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@augment-vir/common",
"version": "31.5.0",
"version": "31.6.0",
"description": "A collection of augments, helpers types, functions, and classes for any JavaScript environment.",
"keywords": [
"augment",
Expand Down Expand Up @@ -39,8 +39,8 @@
"test:web": "virmator --no-deps test web"
},
"dependencies": {
"@augment-vir/assert": "^31.5.0",
"@augment-vir/core": "^31.5.0",
"@augment-vir/assert": "^31.6.0",
"@augment-vir/core": "^31.6.0",
"@date-vir/duration": "^7.1.1",
"ansi-styles": "^6.2.1",
"json5": "^2.2.3",
Expand Down
2 changes: 1 addition & 1 deletion packages/core/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@augment-vir/core",
"version": "31.5.0",
"version": "31.6.0",
"description": "Core augment-vir augments. Use @augment-vir/common instead.",
"homepage": "https://github.com/electrovir/augment-vir",
"bugs": {
Expand Down
53 changes: 53 additions & 0 deletions packages/core/src/augments/http/http-status.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import {assert} from '@augment-vir/assert';
import {describe, it, itCases} from '@augment-vir/test';
import {ArrayElement} from '../array/array.js';
import {
ErrorHttpStatusCategories,
HttpStatus,
httpStatusByCategory,
isErrorHttpStatus,
SuccessHttpStatusCategories,
} from './http-status.js';

describe(isErrorHttpStatus.name, () => {
itCases(isErrorHttpStatus, [
{
it: 'passes a client error',
input: HttpStatus.BadRequest,
expect: true,
},
{
it: 'passes a server error',
input: HttpStatus.InternalServerError,
expect: true,
},
{
it: 'rejects an info status',
input: HttpStatus.Continue,
expect: false,
},
{
it: 'rejects a redirect',
input: HttpStatus.MultipleChoices,
expect: false,
},
{
it: 'rejects a success',
input: HttpStatus.Ok,
expect: false,
},
]);
it('type guards the input', () => {
const status: HttpStatus = HttpStatus.Accepted as HttpStatus;

if (isErrorHttpStatus(status)) {
assert
.tsType(status)
.equals<ArrayElement<(typeof httpStatusByCategory)[ErrorHttpStatusCategories]>>();
} else {
assert
.tsType(status)
.equals<ArrayElement<(typeof httpStatusByCategory)[SuccessHttpStatusCategories]>>();
}
});
});
135 changes: 134 additions & 1 deletion packages/core/src/augments/http/http-status.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type {ArrayElement} from '../array/array.js';
import type {ExtractKeysWithMatchingValues} from '../object/object-keys.js';

/**
* All standardized HTTP status codes.
Expand Down Expand Up @@ -510,6 +511,138 @@ export enum HttpStatusCategory {
ServerError = 'serverError',
}

/**
* All {@link HttpStatusCategory} entries that indicate a failed response as a runtime value that can
* be used to check actual response statuses.
*
* @category HTTP
* @category Package : @augment-vir/common
* @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
*/
export const errorHttpStatusCategories = [
HttpStatusCategory.ClientError,
HttpStatusCategory.ServerError,
] as const;

/**
* All {@link HttpStatusCategory} entries that indicate a failed response.
*
* @category HTTP
* @category Package : @augment-vir/common
* @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
*/
export type ErrorHttpStatusCategories = ArrayElement<typeof errorHttpStatusCategories>;
/**
* All {@link HttpStatusCategory} entries that indicate a successful response.
*
* @category HTTP
* @category Package : @augment-vir/common
* @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
*/
export type SuccessHttpStatusCategories = Exclude<HttpStatusCategory, ErrorHttpStatusCategories>;

/**
* Checks if the given HTTP status is an error status. Type guards the input.
*
* @category HTTP
* @category Package : @augment-vir/common
* @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
*/
export function isErrorHttpStatus(
input: HttpStatus,
): input is ExtractKeysWithMatchingValues<typeof httpStatusToCategory, ErrorHttpStatusCategories> {
return (errorHttpStatusCategories as ReadonlyArray<HttpStatusCategory>).includes(
httpStatusToCategory[input],
);
}

/**
* A type that maps the given {@link HttpStatus} type parameter to its respective
* {@link HttpStatusCategory}.
*
* @category HTTP
* @category Package : @augment-vir/common
* @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
*/
export type HttpStatusToCategory<Status extends HttpStatus> = (typeof httpStatusToCategory)[Status];

/**
* All standardized HTTP statuses mapped to their respective category.
*
* @category HTTP
* @category Package : @augment-vir/common
* @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
*/
export const httpStatusToCategory = {
[HttpStatus.Continue]: HttpStatusCategory.Information,
[HttpStatus.SwitchingProtocols]: HttpStatusCategory.Information,
[HttpStatus.Processing]: HttpStatusCategory.Information,
[HttpStatus.EarlyHints]: HttpStatusCategory.Information,

[HttpStatus.Ok]: HttpStatusCategory.Success,
[HttpStatus.Created]: HttpStatusCategory.Success,
[HttpStatus.Accepted]: HttpStatusCategory.Success,
[HttpStatus.NonAuthoritativeInformation]: HttpStatusCategory.Success,
[HttpStatus.NoContent]: HttpStatusCategory.Success,
[HttpStatus.ResetContent]: HttpStatusCategory.Success,
[HttpStatus.PartialContent]: HttpStatusCategory.Success,
[HttpStatus.MultiStatus]: HttpStatusCategory.Success,
[HttpStatus.AlreadyReported]: HttpStatusCategory.Success,
[HttpStatus.ImUsed]: HttpStatusCategory.Success,

[HttpStatus.MultipleChoices]: HttpStatusCategory.Redirect,
[HttpStatus.MovedPermanently]: HttpStatusCategory.Redirect,
[HttpStatus.Found]: HttpStatusCategory.Redirect,
[HttpStatus.SeeOther]: HttpStatusCategory.Redirect,
[HttpStatus.NotModified]: HttpStatusCategory.Redirect,
[HttpStatus.UseProxy]: HttpStatusCategory.Redirect,
[HttpStatus.Unused]: HttpStatusCategory.Redirect,
[HttpStatus.TemporaryRedirect]: HttpStatusCategory.Redirect,
[HttpStatus.PermanentRedirect]: HttpStatusCategory.Redirect,

[HttpStatus.BadRequest]: HttpStatusCategory.ClientError,
[HttpStatus.Unauthorized]: HttpStatusCategory.ClientError,
[HttpStatus.PaymentRequired]: HttpStatusCategory.ClientError,
[HttpStatus.Forbidden]: HttpStatusCategory.ClientError,
[HttpStatus.NotFound]: HttpStatusCategory.ClientError,
[HttpStatus.MethodNotAllowed]: HttpStatusCategory.ClientError,
[HttpStatus.NotAcceptable]: HttpStatusCategory.ClientError,
[HttpStatus.ProxyAuthenticationRequired]: HttpStatusCategory.ClientError,
[HttpStatus.RequestTimeout]: HttpStatusCategory.ClientError,
[HttpStatus.Conflict]: HttpStatusCategory.ClientError,
[HttpStatus.Gone]: HttpStatusCategory.ClientError,
[HttpStatus.LengthRequired]: HttpStatusCategory.ClientError,
[HttpStatus.PreconditionFailed]: HttpStatusCategory.ClientError,
[HttpStatus.PayloadTooLarge]: HttpStatusCategory.ClientError,
[HttpStatus.UriTooLong]: HttpStatusCategory.ClientError,
[HttpStatus.UnsupportedMediaType]: HttpStatusCategory.ClientError,
[HttpStatus.RangeNotSatisfiable]: HttpStatusCategory.ClientError,
[HttpStatus.ExpectationFailed]: HttpStatusCategory.ClientError,
[HttpStatus.ImATeapot]: HttpStatusCategory.ClientError,
[HttpStatus.MisdirectedRequest]: HttpStatusCategory.ClientError,
[HttpStatus.UnprocessableContent]: HttpStatusCategory.ClientError,
[HttpStatus.Locked]: HttpStatusCategory.ClientError,
[HttpStatus.FailedDependency]: HttpStatusCategory.ClientError,
[HttpStatus.TooEarly]: HttpStatusCategory.ClientError,
[HttpStatus.UpgradeRequired]: HttpStatusCategory.ClientError,
[HttpStatus.PreconditionRequired]: HttpStatusCategory.ClientError,
[HttpStatus.TooManyRequests]: HttpStatusCategory.ClientError,
[HttpStatus.RequestHeaderFieldsTooLarge]: HttpStatusCategory.ClientError,
[HttpStatus.UnavailableForLegalReasons]: HttpStatusCategory.ClientError,

[HttpStatus.InternalServerError]: HttpStatusCategory.ServerError,
[HttpStatus.NotImplemented]: HttpStatusCategory.ServerError,
[HttpStatus.BadGateway]: HttpStatusCategory.ServerError,
[HttpStatus.ServiceUnavailable]: HttpStatusCategory.ServerError,
[HttpStatus.GatewayTimeout]: HttpStatusCategory.ServerError,
[HttpStatus.HttpVersionNotSupported]: HttpStatusCategory.ServerError,
[HttpStatus.VariantAlsoNegotiates]: HttpStatusCategory.ServerError,
[HttpStatus.InsufficientStorage]: HttpStatusCategory.ServerError,
[HttpStatus.LoopDetected]: HttpStatusCategory.ServerError,
[HttpStatus.NotExtended]: HttpStatusCategory.ServerError,
[HttpStatus.NetworkAuthenticationRequired]: HttpStatusCategory.ServerError,
} as const satisfies Record<HttpStatus, HttpStatusCategory>;

/**
* All standardized HTTP status codes grouped into their respective categories.
*
Expand Down Expand Up @@ -591,7 +724,7 @@ export const httpStatusByCategory = {
HttpStatus.NotExtended,
HttpStatus.NetworkAuthenticationRequired,
],
} as const;
} as const satisfies Record<HttpStatusCategory, HttpStatus[]>;

/**
* All possible HTTP status codes for the given {@link HttpStatusCategory}.
Expand Down
Loading

0 comments on commit 3562fe0

Please sign in to comment.