14.0.0 • Published 3 years ago

@waiting/egg-fetch v14.0.0

Weekly downloads
9
License
MIT
Repository
github
Last release
3 years ago

egg-fetch

RxxFetch for midway/egg framework.

Version License npm.io Conventional Commits

Installation

npm i @waiting/egg-fetch

Configuration

Enable Plugin

Edit ${app_root}/src/config/plugin.ts:

export const fetch = {
  enable: true,
  package: '@waiting/egg-fetch',
}

Add Configurations

/* location: ${app_root}/src/config/config.${env}.ts */

import { FetchConfig } from '@waiting/egg-fetch'

export const fetch: FetchConfig = {
  client: {
    timeout: 60 * 1000, // ms
  },
}

Usage

import { provide, plugin } from 'midway'
import { Fetch, JsonType, RxRequestInit } from '@waiting/egg-fetch'

@provide()
export class UserService {

  constructor(
    @plugin() private readonly fetch: Fetch,
  ) { }

  @get('/test_json')
  async public testJson(ctx: Context) {
    const url = 'https://httpbin.org/get'
    const json = await this.fetch.get<HttpbinGetResponse>(url).toPromise()
    ctx.body = `\nurl: ${json.url}`
  }

  @get('/test_html')
  async public testHtml(ctx: Context) {
    const url = 'https://httpbin.org/get'
    const args: RxRequestInit = {
      dataType: 'text'
    }
    const html = await this.fetch.get<string>(url, args).toPromise()
    ctx.body = `\nhtml: ${html}`
  }

}

/** GET Response Interface of httpbin.org */
export interface HttpbinGetResponse extends JsonType {
  args: any
  headers: {
    Accept: string
    Connection: string
    Host: string
    'User-Agent': string,
  }
  origin: string  // ip
  url: string
}

Documentation

License

MIT

Languages

14.0.0

3 years ago

13.0.0

3 years ago

12.6.4

3 years ago

12.6.2

3 years ago

12.6.3

3 years ago

12.6.1

3 years ago

12.6.0

3 years ago

12.5.1

3 years ago

12.4.0

3 years ago

12.4.1

3 years ago

12.5.0

3 years ago

12.2.1

3 years ago

12.0.0

3 years ago

12.2.0

3 years ago

11.4.2

3 years ago

11.4.3

3 years ago

11.4.0

3 years ago

11.4.1

3 years ago

12.3.0

3 years ago

12.1.0

3 years ago

11.3.1

3 years ago

11.5.0

3 years ago

11.3.0

3 years ago

10.0.0

3 years ago

10.0.1

3 years ago

8.5.0

3 years ago

8.4.0

3 years ago

8.7.0

3 years ago

8.6.0

3 years ago

8.5.1

3 years ago

8.3.0

3 years ago

8.2.1

3 years ago

8.1.2

3 years ago

8.2.0

3 years ago

8.1.1

3 years ago

11.2.2

3 years ago

11.2.3

3 years ago

9.2.0

3 years ago

9.1.1

3 years ago

9.1.0

3 years ago

9.0.0

3 years ago

11.2.0

3 years ago

11.2.1

3 years ago

11.0.0

3 years ago

11.1.0

3 years ago

5.5.0

3 years ago

3.2.1

3 years ago

5.4.0

3 years ago

3.2.0

3 years ago

5.3.0

3 years ago

3.1.0

3 years ago

5.2.0

3 years ago

5.1.0

3 years ago

5.0.0

3 years ago

8.1.0

3 years ago

8.0.0

3 years ago

6.0.0

3 years ago

4.1.0

3 years ago

4.0.0

3 years ago

7.0.0

3 years ago

7.2.0

3 years ago

7.1.0

3 years ago

2.2.0

4 years ago

3.0.0

4 years ago

2.1.1

5 years ago

0.1.0

5 years ago