Nyro HTTP Request Library
Nyro is a promise-based HTTP and HTTP/2 request library for JavaScript and TypeScript, currently stable at version 2.0.4. It offers comprehensive support for all HTTP methods and focuses on performance and flexibility, incorporating features like proxy support, request queueing, caching mechanisms, and built-in pagination APIs. The library provides robust error handling, retries, and configurable timeouts. While it ships with TypeScript types, it is primarily designed for Node.js environments. Its "browser support" refers to browser-like features within Node rather than client-side browser usage. The project appears actively maintained with recent updates and differentiates itself by providing advanced functionalities such as `bodySchema` for response validation and specialized proxy tools.
Common errors
-
TypeError: nyro is not a function
cause Attempting to call `nyro` as a function after using `const nyro = require('nyro');` in a CommonJS module, or due to incorrect default import in ESM.fixFor ESM, ensure `import nyro from 'nyro';` is used. For CommonJS, if the library is ESM-only in v2+, you might need `const { default: nyro } = await import('nyro');` or configure your environment for ESM. -
ERR_REQUIRE_ESM: require() of ES Module .../node_modules/nyro/index.mjs from ... not supported.
cause You are attempting to `require()` Nyro (which is an ESM module) from a CommonJS module, or your Node.js environment is not configured for ESM.fixEither convert your consuming module to ESM by adding `"type": "module"` to your `package.json` and using `import` statements, or use a dynamic import: `const { default: nyro } = await import('nyro');`. -
Property 'body' does not exist on type 'NyroResponse<any>'
cause When using TypeScript, the inferred type of the response might not match the expected structure, or the `bodySchema` is incorrectly defined.fixEnsure `bodySchema` correctly reflects the expected runtime shape of the response body. If the schema is dynamic or very complex, you might need to manually assert the type: `const { body } = await nyro(...) as { body: ExpectedBodyType };` -
Error: Protocol 'http:' not supported by agent
cause Incorrect proxy agent configuration, often when mixing HTTP and HTTPS protocols for the proxy or target URL, or using an agent not compatible with the protocol.fixEnsure your proxy agent (e.g., `http-proxy-agent`, `https-proxy-agent`, `socks-proxy-agent`) matches the protocol of your proxy server. Also, verify that the `url` in your Nyro request matches the protocol expected by the agent.
Warnings
- breaking Version 2.0.0 introduced significant changes, likely transitioning to an ESM-first architecture. This means direct `require()` statements in CommonJS modules might no longer work as expected and may require configuration changes or dynamic imports.
- gotcha The documentation mentions 'Browser Support (Node Only)'. This implies that certain browser-like features or configurations are available when running Nyro in a Node.js environment, but the library is not intended for direct client-side browser usage.
- gotcha The `bodySchema` option is for defining the *expected structure* of the request body for validation and type safety, not for defining the actual data payload to be sent. Incorrectly assuming it's the request body itself can lead to empty or malformed requests.
- gotcha Nyro includes a 'Layer7 Attack' feature. While potentially intended for security testing or load generation, using this functionality for malicious purposes is explicitly prohibited by the library's license and can lead to legal consequences.
Install
-
npm install nyro -
yarn add nyro -
pnpm add nyro
Imports
- nyro
const nyro = require('nyro');import nyro from 'nyro';
- Method
import nyro, { Method } from 'nyro';import { Method } from 'nyro'; - ResponseType
import * as NyroTypes from 'nyro';
import { ResponseType } from 'nyro'; - NyroResponse
import type { NyroResponse } from 'nyro';
Quickstart
import nyro, { ResponseType, Method } from 'nyro';
(async() => {
const { body, statusCode } = await nyro({
url: 'https://hercai.onrender.com/v3/hercai',
params: {
question: 'Hi How Are You?'
},
method: Method.Get, // Or 'GET'
responseType: ResponseType.Json, // Or 'json'
headers: {
'User-Agent': 'Nyro-App/1.0'
},
bodySchema: {
content: String,
reply: String
}
});
console.log(`Status Code: ${statusCode}`);
console.log('Your Question; ' + body.content);
console.log('AI Reply; ' + body.reply);
})();