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;
}
}
Errors
Common errors & fixes
Cannot find module 'midway-component-fetch' or its corresponding type declarations.
Package not installed or TypeScript moduleResolution setting incorrect.
fixRun `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.
fixIn 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.
fixEnsure the component is imported in configuration and the class using @Inject is managed by Midway container.
Audit
Dependencies
midwayjs/corerequiredUses Midway's IoC container and decorators
rxxfetchrequiredUnderlying HTTP fetch implementation using RxJS