Skip to content

Commit

Permalink
feat(重构): Context移除requestKey,不再生成公共key;添加beforeRequest,handleCancele…
Browse files Browse the repository at this point in the history
…r移动到beforeRequest
  • Loading branch information
mengxinssfd committed Apr 26, 2022
1 parent dcc1876 commit 291857a
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 8 deletions.
4 changes: 2 additions & 2 deletions __test__/retry.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,8 @@ describe('retry', () => {
};
}

protected handleCanceler(ctx) {
super.handleCanceler(ctx);
protected beforeRequest(ctx) {
super.beforeRequest(ctx);
this.handleRetry(ctx);
}

Expand Down
14 changes: 9 additions & 5 deletions src/AxiosRequestTemplate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,8 @@ export class AxiosRequestTemplate<CC extends CustomConfig = CustomConfig> {

// 请求
protected execRequest(ctx: Context<CC>) {
const { requestConfig, customConfig, requestKey } = ctx;
const { requestConfig, customConfig } = ctx;
const requestKey = this.generateRequestKey(ctx);
// 使用缓存
const cacheConfig = customConfig.cache as CustomCacheConfig;
const useCache = cacheConfig.enable;
Expand All @@ -182,6 +183,10 @@ export class AxiosRequestTemplate<CC extends CustomConfig = CustomConfig> {
return res;
}

protected beforeRequest(ctx: Context<CC>) {
this.handleCanceler(ctx);
}

protected afterRequest(ctx: Context<CC>) {
ctx.clearSet.forEach((clear) => clear());
ctx.clearSet.clear();
Expand All @@ -207,13 +212,11 @@ export class AxiosRequestTemplate<CC extends CustomConfig = CustomConfig> {
const ctx: Context<CC> = {
requestConfig,
customConfig,
requestKey: '',
clearSet: new Set(),
};
ctx.requestKey = this.generateRequestKey(ctx);
this.handleRequestData(ctx, data);
// 2、处理cancel handler
this.handleCanceler(ctx);
// 2、处理cancel handler等等
this.beforeRequest(ctx);
try {
// 3、请求
const response: AxiosResponse = await this.execRequest(ctx);
Expand All @@ -234,6 +237,7 @@ export class AxiosRequestTemplate<CC extends CustomConfig = CustomConfig> {
// 如未命中error处理 则再次抛出error
return await this.handleError(ctx, e);
} finally {
// 6、处理清理canceler等操作
this.afterRequest(ctx);
}
}
Expand Down
1 change: 0 additions & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ export type DynamicCustomConfig<CC extends CustomConfig, RC extends boolean> = O

export interface Context<CC> {
customConfig: CC;
requestKey: string;
requestConfig: AxiosRequestConfig;
clearSet: Set<Function>;
}

0 comments on commit 291857a

Please sign in to comment.