diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/definitions/internet.ts | 6 | ||||
| -rw-r--r-- | src/locales/en/internet/http_status_code.ts | 12 | ||||
| -rw-r--r-- | src/locales/en/internet/index.ts | 2 | ||||
| -rw-r--r-- | src/modules/internet/index.ts | 31 |
4 files changed, 50 insertions, 1 deletions
diff --git a/src/definitions/internet.ts b/src/definitions/internet.ts index 96a79a76..a7801066 100644 --- a/src/definitions/internet.ts +++ b/src/definitions/internet.ts @@ -1,4 +1,4 @@ -import type { EmojiType } from '../modules/internet'; +import type { EmojiType, HTTPStatusCodeType } from '../modules/internet'; import type { LocaleEntry } from './definitions'; /** @@ -21,4 +21,8 @@ export type InternetDefinitions = LocaleEntry<{ * List of all fully-qualified emojis. */ emoji: Record<EmojiType, string[]>; + /** + * List of some HTTP status codes. + */ + http_status_code: Record<HTTPStatusCodeType, number[]>; }>; diff --git a/src/locales/en/internet/http_status_code.ts b/src/locales/en/internet/http_status_code.ts new file mode 100644 index 00000000..8027f6f3 --- /dev/null +++ b/src/locales/en/internet/http_status_code.ts @@ -0,0 +1,12 @@ +// Source: https://developer.mozilla.org/en-US/docs/Web/HTTP/Status + +export default { + informational: [100, 101, 102, 103], + success: [200, 201, 202, 203, 204, 205, 206, 207, 208, 226], + redirection: [300, 301, 302, 303, 304, 305, 306, 307, 308], + clientError: [ + 400, 401, 402, 403, 404, 405, 406, 407, 408, 409, 410, 411, 412, 413, 414, + 415, 416, 417, 418, 421, 422, 423, 424, 425, 426, 428, 429, 431, 451, + ], + serverError: [500, 501, 502, 503, 504, 505, 506, 507, 508, 510, 511], +}; diff --git a/src/locales/en/internet/index.ts b/src/locales/en/internet/index.ts index 9da00b43..6ee266fb 100644 --- a/src/locales/en/internet/index.ts +++ b/src/locales/en/internet/index.ts @@ -8,6 +8,7 @@ import domain_suffix from './domain_suffix'; import emoji from './emoji'; import example_email from './example_email'; import free_email from './free_email'; +import http_status_code from './http_status_code'; const internet: InternetDefinitions = { avatar_uri, @@ -15,6 +16,7 @@ const internet: InternetDefinitions = { emoji, example_email, free_email, + http_status_code, }; export default internet; diff --git a/src/modules/internet/index.ts b/src/modules/internet/index.ts index 7e0baaec..f08ff321 100644 --- a/src/modules/internet/index.ts +++ b/src/modules/internet/index.ts @@ -13,6 +13,13 @@ export type EmojiType = | 'symbol' | 'flag'; +export type HTTPStatusCodeType = + | 'informational' + | 'success' + | 'clientError' + | 'serverError' + | 'redirection'; + /** * Module to generate internet related entries. */ @@ -181,6 +188,30 @@ export class Internet { } /** + * Generates a random HTTP status code. + * + * @param options Options object. + * @param options.types A list of the HTTP status code types that should be used. + * + * @example + * faker.internet.httpStatusCode() // 200 + * faker.internet.httpStatusCode({ types: ['success', 'serverError'] }) // 500 + */ + httpStatusCode( + options: { types?: ReadonlyArray<HTTPStatusCodeType> } = {} + ): number { + const { + types = Object.keys( + this.faker.definitions.internet.http_status_code + ) as Array<HTTPStatusCodeType>, + } = options; + const httpStatusCodeType = this.faker.helpers.arrayElement(types); + return this.faker.helpers.arrayElement( + this.faker.definitions.internet.http_status_code[httpStatusCodeType] + ); + } + + /** * Generates a random url. * * @example |
