Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,16 @@ response
| 507 | INSUFFICIENT_STORAGE | Insufficient Storage |
| 511 | NETWORK_AUTHENTICATION_REQUIRED | Network Authentication Required |

## Classes

| Constant | Range |
| ------------- | --------- |
| Informational | 100 - 199 |
| Successful | 200 - 299 |
| Redirection | 300 - 399 |
| ClientError | 400 - 499 |
| ServerError | 500 - 599 |

## Migrating from v1.x.x

http-status-codes v2 is mostly backwards compatible with v1. There is a single breaking change and two recommended changes.
Expand Down
57 changes: 57 additions & 0 deletions classes.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
[
{
"name": "Informational",
"constant": "Informational",
"range": {
"min": 100,
"max": 199
},
"comment": {
"doc": "Official Documentation @ https://tools.ietf.org/html/rfc7231#section-6.2"
}
},
{
"name": "Successful",
"constant": "Successful",
"range": {
"min": 200,
"max": 299
},
"comment": {
"doc": "Official Documentation @ https://tools.ietf.org/html/rfc7231#section-6.3"
}
},
{
"name": "Redirection",
"constant": "Redirection",
"range": {
"min": 300,
"max": 399
},
"comment": {
"doc": "Official Documentation @ https://tools.ietf.org/html/rfc7231#section-6.4"
}
},
{
"name": "ClientError",
"constant": "ClientError",
"range": {
"min": 400,
"max": 499
},
"comment": {
"doc": "Official Documentation @ https://tools.ietf.org/html/rfc7231#section-6.5"
}
},
{
"name": "ServerError",
"constant": "ServerError",
"range": {
"min": 500,
"max": 599
},
"comment": {
"doc": "Official Documentation @ https://tools.ietf.org/html/rfc7231#section-6.6"
}
}
]
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
98 changes: 95 additions & 3 deletions scripts/update-codes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,19 @@
import fs from 'fs-extra';

import {
Writers, VariableDeclarationKind, Project, StructureKind, EnumMemberStructure, OptionalKind,
Writers,
VariableDeclarationKind,
Project,
StructureKind,
EnumMemberStructure,
OptionalKind,
StatementStructures,
} from 'ts-morph';

import markdownTable from 'markdown-table';

import Codes from '../codes.json';
import Classes from '../classes.json';

interface JsonCodeComment {
doc: string;
Expand Down Expand Up @@ -59,7 +66,62 @@ const run = async () => {
value: code,
docs: [`${deprecatedString}${doc}\n\n${description}`],
};
}).sort(({value: aValue}, {value : bValue}) => aValue - bValue);
}).sort(({ value: aValue }, { value: bValue }) => aValue - bValue);

const statusClassStatements: 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- '));

return {
docs: [`${comment.doc}\n\n${rangeDescriptorString}\n${comprehensiveListOfTypes}`],
kind: StructureKind.TypeAlias,
name: constant,
type: '\n| '.concat(codesAsTypeString.join('\n| ')),
isExported: true,
};
});

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 }) => {
Expand All @@ -85,6 +147,27 @@ const run = async () => {
overwrite: true,
});

const statusClassFile = project.createSourceFile('src/status-classes.ts', {
statements: [
{
kind: StructureKind.Namespace,
name: 'StatusClasses',
isExported: true,
statements: [...statusClassStatements, ...statusClassNamespaces],
},
],
}, {
overwrite: true,
});
statusClassFile.addImportDeclaration({
namedImports: [
{
name: 'StatusCodes',
},
],
moduleSpecifier: './status-codes',
});

const reasonPhraseFile = project.createSourceFile('src/reason-phrases.ts', {
statements: [
{
Expand Down Expand Up @@ -126,7 +209,7 @@ const run = async () => {
overwrite: true,
});

[statusCodeFile, reasonPhraseFile, utilsFile].forEach((sf) => {
[statusCodeFile, statusClassFile, reasonPhraseFile, utilsFile].forEach((sf) => {
sf.insertStatements(0, '// Generated file. Do not edit\n');
});

Expand All @@ -145,6 +228,15 @@ const run = async () => {
const readmeRegex = /## Codes\n\n([^#]*)##/g;
readmeFile = readmeFile.replace(readmeRegex, `## Codes\n\n${table}\n\n##`);

const sortedClasses = Classes.sort((a, b) => (a.range.min - b.range.min));
const classTable = markdownTable([
['Constant', 'Range'],
...sortedClasses.map(({ constant, range }) => [constant, `${range.min} - ${range.max}`]),
]);

const readmeClassRegex = /## Classes\n\n([^#]*)##/g;
readmeFile = readmeFile.replace(readmeClassRegex, `## Classes\n\n${classTable}\n\n##`);

fs.writeFile('./README.md', readmeFile);
console.log('Successfully updated README.md table');
};
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
Loading