Skip to content

Commit

Permalink
feat: export urllib types (#5127)
Browse files Browse the repository at this point in the history
  • Loading branch information
fengmk2 committed Jan 17, 2023
1 parent 4995628 commit 1f7b082
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 14 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/nodejs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,4 @@ jobs:
with:
os: 'ubuntu-latest, macos-latest, windows-latest'
version: '14, 16, 18'
install: 'npm install --legacy-peer-deps --no-package-lock --no-fund'
install: 'npm i -g npminstall && npminstall'
32 changes: 25 additions & 7 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,13 @@ import {
EggLoggerOptions,
EggContextLogger,
} from 'egg-logger';
import { RequestOptions2 as RequestOptions, HttpClientResponse } from 'urllib';
import {
RequestURL, RequestOptions as RequestOptionsNext,
RequestOptions2 as RequestOptionsOld,
HttpClientResponse as HttpClientResponseOld,
} from 'urllib';
import {
RequestURL,
RequestOptions as RequestOptionsNext,
HttpClientResponse as HttpClientResponseNext,
} from 'urllib-next';
import {
Expand Down Expand Up @@ -49,13 +53,27 @@ declare module 'egg' {
// Remove specific property from the specific class
type RemoveSpecProp<T, P> = Pick<T, Exclude<keyof T, P>>;

// Usage:
// ```ts
// import { HttpClientRequestURL, HttpClientRequestOptions, HttpClientResponse } from 'egg';
// async function request(url: HttpClientRequestURL, options: HttpClientRequestOptions): Promise<HttpClientResponse> {
// return await app.httpclient.request(url, options);
// }
// ```
export type HttpClientRequestURL = RequestURL;
export type HttpClientRequestOptions = RequestOptionsNext;
export type HttpClientResponse<T = any> = HttpClientResponseNext<T>;

// Compatible with both urllib@2 and urllib@3 RequestOptions to request
export interface EggHttpClient extends EventEmitter {
request<T = any>(url: RequestURL): Promise<HttpClientResponse<T> | HttpClientResponseNext>;
request<T = any>(url: RequestURL, options: RequestOptions | RequestOptionsNext): Promise<HttpClientResponse<T> | HttpClientResponseNext>;
curl<T = any>(url: RequestURL): Promise<HttpClientResponse<T> | HttpClientResponseNext>;
curl<T = any>(url: RequestURL, options: RequestOptions | RequestOptionsNext): Promise<HttpClientResponse<T> | HttpClientResponseNext>;
request<T = any>(url: HttpClientRequestURL): Promise<HttpClientResponseOld<T> | HttpClientResponse<T>>;
request<T = any>(url: HttpClientRequestURL, options: RequestOptionsOld | HttpClientRequestOptions):
Promise<HttpClientResponseOld<T> | HttpClientResponse<T>>;
curl<T = any>(url: HttpClientRequestURL): Promise<HttpClientResponseOld<T> | HttpClientResponse<T>>;
curl<T = any>(url: HttpClientRequestURL, options: RequestOptionsOld | HttpClientRequestOptions):
Promise<HttpClientResponseOld<T> | HttpClientResponse<T>>;
}

interface EggHttpConstructor {
new(app: Application): EggHttpClient;
}
Expand Down Expand Up @@ -278,7 +296,7 @@ declare module 'egg' {
/** https.Agent */
httpsAgent?: HttpClientBaseConfig;
/** Default request args for httpclient */
request?: RequestOptions;
request?: HttpClientRequestOptions | RequestOptionsOld;
/** Whether enable dns cache */
enableDNSCache?: boolean;
/** Enable proxy request, default is false. */
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,9 @@
"dumi": "^1.1.47",
"dumi-theme-egg": "^1.2.2",
"egg-bin": "^5",
"egg-mock": "^5.4.0",
"egg-mock": "^5.9.2",
"egg-plugin-puml": "^2.4.0",
"egg-tracer": "^1.1.0",
"egg-tracer": "^2.0.0",
"egg-view-nunjucks": "^2.3.0",
"eslint": "^8.23.1",
"eslint-config-egg": "^12.0.0",
Expand Down
8 changes: 4 additions & 4 deletions test/app/extend/request.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,7 @@ describe('test/app/extend/request.test.js', () => {
accept: 'text/html',
},
url: '/',
});
}, { reuseCtxStorage: false });
context.type = 'application/json';
assert(context.request.acceptJSON === true);
});
Expand All @@ -352,8 +352,8 @@ describe('test/app/extend/request.test.js', () => {
accept: 'application/json',
},
url: '/',
});
assert(context.request.acceptJSON === true);
}, { reuseCtxStorage: false });
assert.equal(context.request.acceptJSON, true);
});

it('should false when do not accept json', async () => {
Expand All @@ -362,7 +362,7 @@ describe('test/app/extend/request.test.js', () => {
accept: 'text/html',
},
url: '/',
});
}, { reuseCtxStorage: false });
const request = context.request;
assert(request.acceptJSON === false);
});
Expand Down
12 changes: 12 additions & 0 deletions test/fixtures/apps/app-ts-type-check/normal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ import {
PowerPartial,
Singleton,
start,
HttpClientRequestURL,
HttpClientRequestOptions,
HttpClientResponse,
} from 'egg';

// base context class
Expand Down Expand Up @@ -82,6 +85,15 @@ agent.httpclient.request('http://127.0.0.1', { method: 'GET' }).catch(() => {});
agent.logger.info(agent.Service);
agent.logger.info(agent.Controller);

async function request<T = any>(url: HttpClientRequestURL, options: HttpClientRequestOptions): Promise<HttpClientResponse<T>> {
const response = await agent.httpclient.request<T>(url, options);
return response as HttpClientResponse<T>;
}

request<{ name: 'string' }>('http://127.0.0.1', {}).then(response => {
console.log(response.data.name);
});

// single process mode
start({ baseDir: __dirname,ignoreWarning: true}).then(app=>{
const port= 1002;
Expand Down

0 comments on commit 1f7b082

Please sign in to comment.