Skip to content

Commit

Permalink
Merge pull request #2 from MageCoven/master
Browse files Browse the repository at this point in the history
Update
  • Loading branch information
MageCoven committed May 3, 2023
2 parents 130c081 + 58f80df commit 17cef71
Show file tree
Hide file tree
Showing 4 changed files with 207 additions and 2 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "http-status-codes",
"sideEffects": false,
"version": "2.2.0",
"version": "3.1.0",
"description": "Constants enumerating the HTTP status codes. Based on the Java Apache HttpStatus API.",
"scripts": {
"update-codes": "ts-node --project ./scripts/tsconfig.json ./scripts/update-codes",
Expand Down
36 changes: 35 additions & 1 deletion scripts/update-codes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,40 @@ const run = async () => {
};
});

const statusClassNamespaces: StatementStructures[] = Classes
.map(({
constant, range, comment,
}) => {
const codesInRange = Codes.filter(
(code) => code.code >= range.min && code.code <= range.max,
);

const codesAsTypeString = codesInRange.map((code) => `StatusCodes.${code.constant}`);
const rangeDescriptorString = `Union of all status codes between ${range.min} and ${range.max}:`;
const comprehensiveListOfTypes = '- '.concat(codesAsTypeString.join('\n- '));

const temp: StatementStructures = {
docs: [`${comment.doc}\n\n${rangeDescriptorString}\n${comprehensiveListOfTypes}`],
kind: StructureKind.Namespace,
name: constant,
isExported: true,
statements: [
{
docs: ['List of all codes'],
kind: StructureKind.VariableStatement,
isExported: true,
declarationKind: VariableDeclarationKind.Const,
declarations: [{
name: 'LIST',
type: 'Number[]',
initializer: `[\n${(codesInRange.map((code) => `StatusCodes.${code.constant}`)).join(',\n')}\n]`,
}],
},
],
};
return temp;
});

const statusCodeToReasonPhrase = Codes
.reduce((acc: Record<string, string>, { code, phrase }) => {
(acc as Record<string, string>)[`"${code.toString()}"`] = `"${phrase}"`;
Expand Down Expand Up @@ -119,7 +153,7 @@ const run = async () => {
kind: StructureKind.Namespace,
name: 'StatusClasses',
isExported: true,
statements: statusClassStatements,
statements: [...statusClassStatements, ...statusClassNamespaces],
},
],
}, {
Expand Down
4 changes: 4 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ export {
StatusCodes,
} from './status-codes';

export {
StatusClasses,
} from './status-classes';

export {
ReasonPhrases,
} from './reason-phrases';
Expand Down
167 changes: 167 additions & 0 deletions src/status-classes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,4 +144,171 @@ export namespace StatusClasses {
| StatusCodes.NETWORK_AUTHENTICATION_REQUIRED
| StatusCodes.NOT_IMPLEMENTED
| StatusCodes.SERVICE_UNAVAILABLE;

/**
* Official Documentation @ https://tools.ietf.org/html/rfc7231#section-6.2
*
* Union of all status codes between 100 and 199:
* - StatusCodes.CONTINUE
* - StatusCodes.PROCESSING
* - StatusCodes.SWITCHING_PROTOCOLS
*/
export namespace Informational {
/** List of all codes */
export const LIST: Number[] = [
StatusCodes.CONTINUE,
StatusCodes.PROCESSING,
StatusCodes.SWITCHING_PROTOCOLS
];
}

/**
* Official Documentation @ https://tools.ietf.org/html/rfc7231#section-6.3
*
* Union of all status codes between 200 and 299:
* - StatusCodes.ACCEPTED
* - StatusCodes.CREATED
* - StatusCodes.MULTI_STATUS
* - StatusCodes.NO_CONTENT
* - StatusCodes.NON_AUTHORITATIVE_INFORMATION
* - StatusCodes.OK
* - StatusCodes.PARTIAL_CONTENT
* - StatusCodes.RESET_CONTENT
*/
export namespace Successful {
/** List of all codes */
export const LIST: Number[] = [
StatusCodes.ACCEPTED,
StatusCodes.CREATED,
StatusCodes.MULTI_STATUS,
StatusCodes.NO_CONTENT,
StatusCodes.NON_AUTHORITATIVE_INFORMATION,
StatusCodes.OK,
StatusCodes.PARTIAL_CONTENT,
StatusCodes.RESET_CONTENT
];
}

/**
* Official Documentation @ https://tools.ietf.org/html/rfc7231#section-6.4
*
* Union of all status codes between 300 and 399:
* - StatusCodes.MOVED_PERMANENTLY
* - StatusCodes.MOVED_TEMPORARILY
* - StatusCodes.MULTIPLE_CHOICES
* - StatusCodes.NOT_MODIFIED
* - StatusCodes.PERMANENT_REDIRECT
* - StatusCodes.SEE_OTHER
* - StatusCodes.TEMPORARY_REDIRECT
* - StatusCodes.USE_PROXY
*/
export namespace Redirection {
/** List of all codes */
export const LIST: Number[] = [
StatusCodes.MOVED_PERMANENTLY,
StatusCodes.MOVED_TEMPORARILY,
StatusCodes.MULTIPLE_CHOICES,
StatusCodes.NOT_MODIFIED,
StatusCodes.PERMANENT_REDIRECT,
StatusCodes.SEE_OTHER,
StatusCodes.TEMPORARY_REDIRECT,
StatusCodes.USE_PROXY
];
}

/**
* Official Documentation @ https://tools.ietf.org/html/rfc7231#section-6.5
*
* Union of all status codes between 400 and 499:
* - StatusCodes.BAD_REQUEST
* - StatusCodes.CONFLICT
* - StatusCodes.EXPECTATION_FAILED
* - StatusCodes.FAILED_DEPENDENCY
* - StatusCodes.FORBIDDEN
* - StatusCodes.GONE
* - StatusCodes.IM_A_TEAPOT
* - StatusCodes.INSUFFICIENT_SPACE_ON_RESOURCE
* - StatusCodes.LENGTH_REQUIRED
* - StatusCodes.LOCKED
* - StatusCodes.METHOD_FAILURE
* - StatusCodes.METHOD_NOT_ALLOWED
* - StatusCodes.NOT_ACCEPTABLE
* - StatusCodes.NOT_FOUND
* - StatusCodes.PAYMENT_REQUIRED
* - StatusCodes.PRECONDITION_FAILED
* - StatusCodes.PRECONDITION_REQUIRED
* - StatusCodes.PROXY_AUTHENTICATION_REQUIRED
* - StatusCodes.REQUEST_HEADER_FIELDS_TOO_LARGE
* - StatusCodes.REQUEST_TIMEOUT
* - StatusCodes.REQUEST_TOO_LONG
* - StatusCodes.REQUEST_URI_TOO_LONG
* - StatusCodes.REQUESTED_RANGE_NOT_SATISFIABLE
* - StatusCodes.TOO_MANY_REQUESTS
* - StatusCodes.UNAUTHORIZED
* - StatusCodes.UNAVAILABLE_FOR_LEGAL_REASONS
* - StatusCodes.UNPROCESSABLE_ENTITY
* - StatusCodes.UNSUPPORTED_MEDIA_TYPE
* - StatusCodes.MISDIRECTED_REQUEST
*/
export namespace ClientError {
/** List of all codes */
export const LIST: Number[] = [
StatusCodes.BAD_REQUEST,
StatusCodes.CONFLICT,
StatusCodes.EXPECTATION_FAILED,
StatusCodes.FAILED_DEPENDENCY,
StatusCodes.FORBIDDEN,
StatusCodes.GONE,
StatusCodes.IM_A_TEAPOT,
StatusCodes.INSUFFICIENT_SPACE_ON_RESOURCE,
StatusCodes.LENGTH_REQUIRED,
StatusCodes.LOCKED,
StatusCodes.METHOD_FAILURE,
StatusCodes.METHOD_NOT_ALLOWED,
StatusCodes.NOT_ACCEPTABLE,
StatusCodes.NOT_FOUND,
StatusCodes.PAYMENT_REQUIRED,
StatusCodes.PRECONDITION_FAILED,
StatusCodes.PRECONDITION_REQUIRED,
StatusCodes.PROXY_AUTHENTICATION_REQUIRED,
StatusCodes.REQUEST_HEADER_FIELDS_TOO_LARGE,
StatusCodes.REQUEST_TIMEOUT,
StatusCodes.REQUEST_TOO_LONG,
StatusCodes.REQUEST_URI_TOO_LONG,
StatusCodes.REQUESTED_RANGE_NOT_SATISFIABLE,
StatusCodes.TOO_MANY_REQUESTS,
StatusCodes.UNAUTHORIZED,
StatusCodes.UNAVAILABLE_FOR_LEGAL_REASONS,
StatusCodes.UNPROCESSABLE_ENTITY,
StatusCodes.UNSUPPORTED_MEDIA_TYPE,
StatusCodes.MISDIRECTED_REQUEST
];
}

/**
* Official Documentation @ https://tools.ietf.org/html/rfc7231#section-6.6
*
* Union of all status codes between 500 and 599:
* - StatusCodes.BAD_GATEWAY
* - StatusCodes.GATEWAY_TIMEOUT
* - StatusCodes.HTTP_VERSION_NOT_SUPPORTED
* - StatusCodes.INSUFFICIENT_STORAGE
* - StatusCodes.INTERNAL_SERVER_ERROR
* - StatusCodes.NETWORK_AUTHENTICATION_REQUIRED
* - StatusCodes.NOT_IMPLEMENTED
* - StatusCodes.SERVICE_UNAVAILABLE
*/
export namespace ServerError {
/** List of all codes */
export const LIST: Number[] = [
StatusCodes.BAD_GATEWAY,
StatusCodes.GATEWAY_TIMEOUT,
StatusCodes.HTTP_VERSION_NOT_SUPPORTED,
StatusCodes.INSUFFICIENT_STORAGE,
StatusCodes.INTERNAL_SERVER_ERROR,
StatusCodes.NETWORK_AUTHENTICATION_REQUIRED,
StatusCodes.NOT_IMPLEMENTED,
StatusCodes.SERVICE_UNAVAILABLE
];
}
}

0 comments on commit 17cef71

Please sign in to comment.