Skip to content

Commit

Permalink
feat: add custom axios config typings merge (#4278)
Browse files Browse the repository at this point in the history
* feat: add custom axios config typings merge

* fix: mqtt use local svc
  • Loading branch information
czy88840616 authored Jan 31, 2025
1 parent a5fe799 commit 8eda160
Show file tree
Hide file tree
Showing 11 changed files with 81 additions and 6 deletions.
8 changes: 8 additions & 0 deletions .github/workflows/nodejs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,14 @@ jobs:
with:
mongodb-version: 4.2

- name: Start Mosquitto
uses: namoshek/mosquitto-github-action@v1
with:
version: 'latest'
ports: '1883:1883 8883:8883'
config: ${{ github.workspace }}/packages/mqtt/scripts/mosquitto.conf
container-name: 'mqtt'

- run: npm run install_npm
- run: npm install && npm install codecov
- run: npm run build --if-present
Expand Down
2 changes: 1 addition & 1 deletion packages/axios/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { AxiosRequestConfig } from 'axios';
import { AxiosRequestConfig } from './dist/index';

export * from './dist/index';

Expand Down
3 changes: 2 additions & 1 deletion packages/axios/src/http-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ import {
Scope,
ScopeEnum,
} from '@midwayjs/core';
import { Axios, AxiosInstance, AxiosRequestConfig, AxiosResponse } from 'axios';
import { Axios, AxiosInstance } from 'axios';
import { HttpServiceFactory } from './http-service.factory';
import { AxiosRequestConfig, AxiosResponse } from './interface';

@Provide()
@Scope(ScopeEnum.Singleton)
Expand Down
1 change: 1 addition & 0 deletions packages/axios/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import * as Axios from 'axios';
export { AxiosConfiguration as Configuration } from './configuration';
export * from './http-service.factory';
export * from './http-service';
export * from './interface';
export {
/**
* @deprecated Use `Axios` directly
Expand Down
15 changes: 15 additions & 0 deletions packages/axios/src/interface.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import {
AxiosRequestConfig as OriginAxiosRequestConfig,
AxiosRequestHeaders,
AxiosResponse as OriginAxiosResponse
} from 'axios';

/**
* Interface for custom axios request config merging.
*/
export interface AxiosRequestConfig<D = any> extends OriginAxiosRequestConfig<D> {}
export interface AxiosResponse<T = any, D = any> extends OriginAxiosResponse<T, D> {
config: AxiosRequestConfig<D> & {
headers: AxiosRequestHeaders;
};
}
8 changes: 8 additions & 0 deletions packages/mqtt/scripts/mosquitto.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
allow_anonymous true
listener 1883
listener 9001
protocol websockets
persistence true
# password_file /mosquitto/config/pwfile
persistence_file mosquitto.db
persistence_location /mosquitto/data/
5 changes: 5 additions & 0 deletions packages/mqtt/scripts/start.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/usr/bin/env bash

# https://atmotube.com/atmocube-support/mqtt-with-atmocube
# https://github.com/sukesh-ak/setup-mosquitto-with-docker
docker run -it -d --name mqtt-local -p 1883:1883 -p 9001:9001 -v $(pwd)/mosquitto.conf:/mosquitto/config/mosquitto.conf eclipse-mosquitto
4 changes: 2 additions & 2 deletions packages/mqtt/test/fixtures/base-app/src/configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import * as mqtt from '../../../../src';
sub: {
test: {
connectOptions: {
host: 'test.mosquitto.org',
host: '127.0.0.1',
port: 1883,
},
subscribeOptions: {
Expand All @@ -23,7 +23,7 @@ import * as mqtt from '../../../../src';
pub: {
clients: {
default: {
host: 'test.mosquitto.org',
host: '127.0.0.1',
port: 1883,
}
}
Expand Down
4 changes: 2 additions & 2 deletions packages/mqtt/test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ describe('/test/index.test.ts', () => {
]
});
await (app.getFramework() as Framework).createSubscriber({
host: 'test.mosquitto.org',
host: '127.0.0.1',
port: 1883,
}, {
topicObject: 'test_midway_dynamic',
Expand All @@ -52,7 +52,7 @@ describe('/test/index.test.ts', () => {
// send
const producerService = await app.getApplicationContext().getAsync(MqttProducerFactory);
const producer = await producerService.createInstance({
host: 'test.mosquitto.org',
host: '127.0.0.1',
port: 1883,
}, 'test');

Expand Down
19 changes: 19 additions & 0 deletions site/docs/extensions/axios.md
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,25 @@ export class MainConfiguration {
}
```

### 扩展 AxiosRequestConfig 类型

当前 Axios 库中 [issue](https://github.com/axios/axios/issues?q=is%3Aissue%20state%3Aopen%20AxiosRequestConfig) 有不少扩展 `AxiosRequestConfig` 类型的需求,但是 axios 本身并不支持扩展这个类型,其中的泛型仅限于设置 data 属性的类型。

Midway 中提供了 `AxiosRequestConfig` 的扩展,可以在组件层面增加新的属性。

```typescript
// interface.ts
import '@midwayjs/axios';

declare module '@midwayjs/axios/dist/interface' {
interface AxiosRequestConfig {
retryDelay?: number;
retry?: number;
}
}
```


### 直接使用 Axios

`@midayjs/axios`导出了原始的`axios`实例,在非应用环境中可以直接使用。
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,24 @@ export class MainConfiguration {
}
```

### Extending AxiosRequestConfig Type

There are many [issues](https://github.com/axios/axios/issues?q=is%3Aissue%20state%3Aopen%20AxiosRequestConfig) in the Axios repository requesting the addition of new properties to the AxiosRequestConfig type. However, Axios itself does not support extending this type, as its generics are limited to defining the type of the data property only.

Midway provides an extension for `AxiosRequestConfig`, allowing you to add new properties at the component level.

```typescript
// interface.ts
import '@midwayjs/axios';

declare module '@midwayjs/axios/dist/interface' {
interface AxiosRequestConfig {
retryDelay?: number;
retry?: number;
}
}
```

### Use Axios directly

`@midayjs/axios` also exported the original instance of `axios`, which could be useful in helper functions.
Expand Down

0 comments on commit 8eda160

Please sign in to comment.