Registry / devops / think-fetch

think-fetch

JSON →
library1.1.2jsnpmunverified

ThinkJS 3.x wrapper around node-fetch providing an extendable fetch method on controllers. Current version 1.1.2 (stable, low release cadence). It integrates the node-fetch library (v1.x) into ThinkJS's extend system, making fetch available via `this.fetch()` in controller actions. Supports plain text, JSON, file streams, and standard node-fetch options. Key differentiator: it is the official fetch wrapper for ThinkJS 3, simplifying HTTP requests in ThinkJS apps without additional configuration. Only works with ThinkJS 3.x, not standalone. ESM/CJS hybrid; requires CommonJS require due to ThinkJS extend API.

npm install think-fetch
INSTALL
IMPORT
SIG · THINK-FETCH
T
think-fetch
devopsjavascriptv1.1.2
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

default import
const fetch = require('think-fetch');
import fetch from 'think-fetch';
ThinkJS extend system uses CommonJS require. ESM import is not supported by ThinkJS 3. Additionally, the export is used as an extend config object, not a direct fetch function.
this.fetch
const text = await this.fetch('https://example.com/');
const text = await fetch('https://example.com/');
After extending ThinkJS with think-fetch, the fetch method is available via `this.fetch` inside controller actions. Calling `fetch` globally (without `this`) will not work unless you also have a global fetch polyfill.
think-fetch extend config
module.exports = [ require('think-fetch'), ];
module.exports = [ fetch, ];
The export from think-fetch is a function used as an extend object. It must be required and placed in the extend config array. Referencing it without require will cause a ReferenceError.

Shows how to register think-fetch in the extend config and use this.fetch in a controller to perform HTTP requests.

// src/config/extend.js const fetch = require('think-fetch'); module.exports = [ fetch, ]; // src/controller/user.js module.exports = class extends think.Controller { async indexAction() { const data = await this.fetch('https://api.github.com/repos/thinkjs/think-fetch').then(res => res.json()); return this.success(data); } };
Debug
Known issues
breakingthink-fetch requires ThinkJS 3.x. It will not work with ThinkJS 2.x.
fix
Upgrade to ThinkJS 3.x or use a compatible version.
affects: <1.0.0
deprecatedThe underlying node-fetch v1 is deprecated. It may contain unpatched security vulnerabilities. Consider migrating to node-fetch v2/v3 or native fetch.
fix
Update to a version of think-fetch that uses node-fetch v2 or v3, or replace with direct node-fetch usage.
affects: >=1.0.0
gotchaUsing `this.fetch` outside of a controller context will fail because `this` is not bound to the controller instance.
fix
Ensure fetch is only called inside controller actions where `this` refers to the controller.
affects: >=1.0.0
gotchaThe `User-Agent` header defaults to `node-fetch/1.0`. Some APIs may reject this user agent. Set a custom header via the options.
fix
Pass `headers: { 'User-Agent': 'custom' }` in fetch options.
affects: >=1.0.0
breakingURIs must be absolute; relative URLs like `/api/foo` will reject the promise.
fix
Prefix relative URLs with the base URL (e.g., `https://example.com/api/foo`).
affects: >=1.0.0
Errors
Common errors & fixes
TypeError: this.fetch is not a function
think-fetch not registered in extend config, or registered incorrectly.
fix
Add `const fetch = require('think-fetch');` to src/config/extend.js and export it in the array.
ReferenceError: fetch is not defined
Using `fetch` directly instead of `this.fetch` inside controller.
fix
Use `this.fetch(url, options)` inside controller actions.
Error: URI malformed
Provided a relative URL like '/path' instead of absolute URL.
fix
Supply a full URL with protocol and host (e.g., 'https://example.com/path').
Upgrade
Version history
1.1.2latest on npm
Audit
Dependencies
node-fetchrequiredCore dependency providing the underlying fetch implementation.
Agent activity
7 hits · last 30 days
node
6
Bingbot
1
Resources
think-fetch — npm install think-fetch · libregistry