Registry / devops / midway-component-fetch

midway-component-fetch

JSON →
library12.4.0jsnpmunverified

An HTTP fetch component for Midway.js (v12.4.0) that integrates fetch functionality into Midway's IoC container. It provides a FetchComponent class with get/post/put/delete methods, supports dataType (json/text), and includes callbacks for logging and error handling. Released under conventional commits, with TypeScript types bundled. Differentiators: Out-of-the-box Midway integration, RxJS-based underlying library (rxxfetch), and built-in tracing/logging support. Releases follow semver; current stable is v12.4.0. Requires Midway.js v3+.

npm install midway-component-fetch
INSTALL
IMPORT
SIG · MIDWAY-COMPONENT-F
M
midway-component-fetch
devopsjavascriptv12.4.0
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.

FetchComponent
import { FetchComponent } from 'midway-component-fetch'
const { FetchComponent } = require('midway-component-fetch')
ESM-only; CommonJS require may work but not recommended for Midway projects.
FetchComponentConfig
import { FetchComponentConfig } from 'midway-component-fetch'
import { FetchConfig } from 'midway-component-fetch'
Type for configuration object; exported as named export.
FetchResponse
import { FetchResponse } from 'midway-component-fetch'
Type for response objects; import when using with TypeScript.
genRequestHeaders
import { genRequestHeaders } from 'midway-component-fetch'
Helper function to generate default request headers.
default (namespace import)
import * as fetch from 'midway-component-fetch'
import fetch from 'midway-component-fetch'
Namespace import used in configuration to register component; default export does not exist.

Shows how to register the component in a Midway configuration, configure it, and use FetchComponent in a service.

// src/configuration.ts import * as fetch from 'midway-component-fetch'; import { Configuration } from '@midwayjs/core'; import { join } from 'path'; @Configuration({ imports: [fetch], importConfigs: [join(__dirname, 'config')], }) export class ContainerConfiguration {} // src/config/config.local.ts import { FetchComponentConfig, genRequestHeaders } from 'midway-component-fetch'; export const fetch: FetchComponentConfig = { enableTraceLoggingReqBody: true, enableTraceLoggingRespData: true, enableDefaultCallbacks: true, traceLoggingReqHeaders: ['authorization'], genRequestHeaders, }; // src/core/base.service.ts import { Inject } from '@midwayjs/decorator'; import { FetchComponent, FetchOptions } from 'midway-component-fetch'; export class BaseService { @Inject() readonly fetch: FetchComponent; async getText(url: string): Promise<string> { const text = await this.fetch.get<string>(url, { dataType: 'text' }); return text; } }
Debug
Known issues
breakingv12 dropped Midway v2 support. Requires @midwayjs/core v3+.
fix
Upgrade to Midway v3 or use v11.x for Midway v2.
affects: >=12.0.0
breakingv10 removed default export. Use namespace import.
fix
Replace 'import fetch from' with 'import * as fetch from'.
affects: >=10.0.0
deprecatedgenRequestHeaders is deprecated in v12. Use default headers config instead.
fix
Remove genRequestHeaders from config; the component now auto-generates headers.
affects: >=12.0.0
gotchaMust register component in imports array of @Configuration, otherwise FetchComponent will not be injectable.
fix
Add `import * as fetch from 'midway-component-fetch'` and include it in `imports: [fetch]`.
affects: >=9.0.0
gotchaTypeScript types are included but require 'moduleResolution' to be 'node' or 'node16' in tsconfig.
fix
Set moduleResolution to 'node' or 'node16' in tsconfig.json.
affects: >=9.0.0
Errors
Common errors & fixes
Cannot find module 'midway-component-fetch' or its corresponding type declarations.
Package not installed or TypeScript moduleResolution setting incorrect.
fix
Run `npm install midway-component-fetch` and ensure tsconfig has 'moduleResolution': 'node'.
FetchComponent is not injectable. Did you forget to import the component?
Component not registered in configuration imports.
fix
In src/configuration.ts, add `import * as fetch from 'midway-component-fetch'` and `imports: [fetch]`.
TypeError: Cannot read properties of undefined (reading 'get')
FetchComponent not injected because dependency tree not resolved properly.
fix
Ensure the component is imported in configuration and the class using @Inject is managed by Midway container.
Upgrade
Version history
12.4.0latest on npm
Audit
Dependencies
midwayjs/corerequiredUses Midway's IoC container and decorators
rxxfetchrequiredUnderlying HTTP fetch implementation using RxJS
Agent activity
2 hits · last 30 days
node
2
Resources
midway-component-fetch — npm install midway-component-fetch · libregistry