aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorjohge201 <[email protected]>2022-05-21 16:15:20 +0200
committerGitHub <[email protected]>2022-05-21 16:15:20 +0200
commit05f555bc7e304afaa657586ae88f2173507e084f (patch)
tree4abbb28774d092c9369b7adc170fcea2f1400c21 /src
parentb0cdf29d3a7470b1211e724b68e243b8c96a7f4c (diff)
downloadfaker-05f555bc7e304afaa657586ae88f2173507e084f.tar.xz
faker-05f555bc7e304afaa657586ae88f2173507e084f.zip
feat(internet): HTTP random status code (#945)
Co-authored-by: ST-DDT <[email protected]> Co-authored-by: Piotr Kuczynski <[email protected]> Co-authored-by: johge201 <[email protected]> Co-authored-by: Sofia Bertmar <[email protected]> Co-authored-by: Sofia Bertmar <[email protected]> Co-authored-by: Shinigami <[email protected]>
Diffstat (limited to 'src')
-rw-r--r--src/definitions/internet.ts6
-rw-r--r--src/locales/en/internet/http_status_code.ts12
-rw-r--r--src/locales/en/internet/index.ts2
-rw-r--r--src/modules/internet/index.ts31
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