In-takes HTTP status code and language code (default to 'en'), spits out response object with corresponding human-readable text message.
Based on MDN web docs for HTTP response status codes.
- English (
en, default) - German (
de) - Spanish (
es) - French (
fr) - Japanese (
ja) - Chinese (
zh) — see why Chinese useszhinstead ofcn
To get all supported language codes, use listLanguages.
# PNPM (recommended)
pnpm add http-response-text
# Or NPM
npm i http-response-textImport the getStatus method and call it with a numeric status code and, optionally, a language code string — "en" (default), "de", "es", "fr", "ja" or "zh".
The method is async, so await its Promise; the examples below use top-level await inside an ES module. Only the English dictionary is loaded at startup, while other languages are imported on first use and cached.
import { getStatus } from "http-response-text";
// Full output for 404 in the default language (English)
const result = await getStatus(404);
console.log(result);
// Output: { code: 404, message: 'Not Found' }
// Only get message for 404 in German
const { message: deMessage } = await getStatus(404, "de");
console.log(deMessage);
// Output: Nicht gefunden
// Only get message for 404 in Spanish
const { message: esMessage } = await getStatus(404, "es");
console.log(esMessage);
// Output: No encontrado
// Only get message for 404 in French
const { message: frMessage } = await getStatus(404, "fr");
console.log(frMessage);
// Output: Non Trouvé
// Or Japanese
const { message: jaMessage } = await getStatus(404, "ja");
console.log(jaMessage);
// Output: 未検出
// Or Chinese
const { message: zhMessage } = await getStatus(404, "zh");
console.log(zhMessage);
// Output: 未找到For example, the snippet below rejects a user-supplied identifier that is not a valid MongoDB ObjectId before querying the database:
import { getStatus } from "http-response-text";
import { ObjectId } from "mongodb";
const findById = async (collectionName, id) => {
if (!ObjectId.isValid(id)) {
const { code, message } = await getStatus(401);
console.warn(
`findById(${collectionName}): invalid ObjectId "${String(id)}" (${code} ${message})`,
);
return null;
}
// ...
};When the function is a request handler, reply with 404 instead of 401. A malformed identifier then gets the same response as a missing record, so an attacker cannot probe endpoints to enumerate valid identifiers:
if (!ObjectId.isValid(id)) return await getStatus(404);listLanguages takes no arguments and resolves to a Set of the language codes that ship with a dictionary in this package. Sets are inherently de-duplicated, and each call returns a fresh instance, so the result can be mutated by the caller without side effects. Codes are ordered with English first, then the remaining languages alphabetically.
import { listLanguages } from "http-response-text";
const languages = await listLanguages();
console.log(languages);
// Output: Set(6) { 'en', 'de', 'es', 'fr', 'ja', 'zh' }
// Validate user input before requesting a message
const requested = "it";
if (!(await listLanguages()).has(requested)) {
console.warn(`Unsupported language "${requested}", falling back to "en"`);
}- The Nobel Prize Press — lists http-response-text among the open-source software it credits.
zh is the ISO 639-1 language code for Chinese, while CN is the ISO 3166-1 alpha-2 country code for China (People's Republic of China). A language code identifies a language, not a country, so cn alone is not a valid language identifier.
Regional variants combine both in a BCP 47 tag, such as zh-CN for Simplified Chinese as used in China. This package accepts only the base language code, so use zh:
await getStatus(404, "zh"); // { code: 404, message: '未找到' }
await getStatus(404, "cn"); // TypeScript error; returns { code: 404, message: undefined }