28.0.1 • Published 13 days ago

@mwcp/fetch v28.0.1

Weekly downloads
-
License
MIT
Repository
github
Last release
13 days ago

midway-component-fetch

HTTP fetch component for midway.js

Version License npm.io Conventional Commits

Note

ESM build only, requires @midwayjs >= 3.12 and set "type": "module" in packages.json

Install

npm i @mwcp/fetch

Usage

Update project src/configuration.ts

import * as fetch from '@mwcp/fetch'

@Configuration({
  imports: [
    fetch,
  ],
})
export class ContainerConfiguration implements ILifeCycle {
}

Update project src/config/config.(prod | local | unittest).ts

import { FetchComponentConfig, genRequestHeaders } from 'midway-component-fetch'

export const fetch: FetchComponentConfig = {
  // change to only authorization
  traceLoggingReqHeaders: ['authorization'],
}

Update project base service like src/core/base.service.ts

import { Inject } from '@midwayjs/decorator'
import {
  FetchComponent,
  ResponseData,
  Options as FetchOptions,
} from 'midway-component-fetch'

export class BaseService extends RootClass {
  @Inject() readonly fetch: FetchComponent

  /**
   * 返回类型为 `text` 或者 `html`
   */
  getText<T extends string = string>(
    url: string,
    data?: FetchOptions['data'],
  ): Promise<T> {

    const opts: FetchOptions = {
      ...this.initFetchOptions,
      dataType: 'text',
    }
    if (typeof data !== 'undefined') {
      opts.data = data
    }
    const ret = this.fetch.get<T>(url, opts)
    return ret as Promise<T>
  }

  /**
   * Generate an RxRequestInit variable,
   * @default
   *   - contentType: 'application/json; charset=utf-8'
   *   - dataType: 'json'
   */
  get initFetchOptions(): FetchOptions {
    const args: FetchOptions = {
      url: '',
      method: 'GET',
      dataType: 'json',
      contentType: 'application/json; charset=utf-8',
    }
    return args
  }
}

Update project service like src/home/home.service.ts

import { Provide } from '@midwayjs/decorator'

import { BaseService } from '~/interface'


@Provide()
export class HomeService extends BaseService {

  /**
   * 获取网关 IP
   */
  async retrieveGatewayIp(): Promise<string> {
    const url = 'https://www.taobao.com/help/getip.php'
    // ipCallback({ip:"222.233.10.1"})
    const text = await this.getText(url)
    let ip = ''
    if (text) {
      const arr = /"([\d.]+)"/ui.exec(text)
      ip = arr && arr.length >= 1 && arr[1] ? arr[1] : ''
    }
    this.logger.info({ ip })
    return ip
  }

}

License

MIT

28.0.1

13 days ago

28.0.0

16 days ago

27.1.0

1 month ago

27.0.3

1 month ago

27.0.2

2 months ago

27.0.1

2 months ago

27.0.0

2 months ago

26.0.2

2 months ago

26.0.1

3 months ago

26.0.0

4 months ago

23.3.5

9 months ago

25.0.0

8 months ago

24.0.3

8 months ago

24.0.2

8 months ago

24.0.1

9 months ago

24.0.0

9 months ago

23.3.1

12 months ago

23.2.2

1 year ago

23.3.0

1 year ago

23.3.3

12 months ago

23.3.2

12 months ago

23.2.3

1 year ago

23.3.4

11 months ago

23.2.1

1 year ago

23.0.0

1 year ago

23.2.0

1 year ago

23.0.2

1 year ago

23.1.0

1 year ago

23.0.1

1 year ago

22.0.3

1 year ago

22.2.0

1 year ago

22.0.2

1 year ago

22.1.0

1 year ago

22.0.1

1 year ago

22.0.0

1 year ago

22.0.5

1 year ago

22.0.4

1 year ago

21.2.0

1 year ago

21.1.3

1 year ago

21.1.2

1 year ago

21.1.1

2 years ago

21.1.0

2 years ago

21.0.1

2 years ago

20.4.0

2 years ago

21.0.0

2 years ago

20.3.4

2 years ago

20.3.3

2 years ago

20.3.2

2 years ago

20.3.1

2 years ago

20.3.0

2 years ago