|
7 | 7 | RequestResponse |
8 | 8 | } from "umi-request"; |
9 | 9 | import { RefreshTokenParam, OAuth2AccessToken, GrantTypes, LoginParam, CheckTokenResult } from "../oauth2"; |
10 | | -import { ErrorContext, CustomErrorHandler, RequestOptions, RequestContext } from "./types"; |
| 10 | +import { ErrorContext, CustomErrorHandler, RequestOptions, RequestContext, HttpResponse } from "./types"; |
11 | 11 | import { OAuth2Session, ApplicationError } from "../core"; |
12 | 12 | import { clientSession } from "../oauth2/session"; |
13 | 13 | import { isNotNullOrBlankString } from "../utils"; |
@@ -83,15 +83,15 @@ const handleError = (error: ResponseError, options: RequestOptions, skipNotify?: |
83 | 83 |
|
84 | 84 | export interface ExtendedRequestMethod<R = true> extends RequestMethod<R> { |
85 | 85 | <T = any>(url: string, options?: ExtendedRequestOptionsInit): R extends true |
86 | | - ? Promise<RequestResponse<T & ApplicationError>> |
| 86 | + ? Promise<HttpResponse<T>> |
87 | 87 | : Promise<T | ApplicationError>; |
88 | 88 | login: ( |
89 | 89 | param: LoginParam, |
90 | 90 | options?: OAuth2RequestOptions |
91 | | - ) => Promise<RequestResponse<OAuth2AccessToken & ApplicationError>>; |
| 91 | + ) => Promise<HttpResponse<OAuth2AccessToken>>; |
92 | 92 | checkToken: ( |
93 | 93 | options?: OAuth2RequestOptions & { tokenValue?: string } |
94 | | - ) => Promise<RequestResponse<CheckTokenResult & ApplicationError>>; |
| 94 | + ) => Promise<HttpResponse<CheckTokenResult>>; |
95 | 95 | } |
96 | 96 |
|
97 | 97 | export interface ExtendedRequestOptionsInit extends RequestOptionsInit { |
@@ -195,7 +195,7 @@ export function initRequest(options: RequestOptions, session?: OAuth2Session): E |
195 | 195 |
|
196 | 196 | return request<OAuth2AccessToken & ApplicationError>(accessTokenUrl, s); |
197 | 197 | }; |
198 | | - |
| 198 | + |
199 | 199 | req.checkToken = async (ropt?: OAuth2RequestOptions & { tokenValue?: string }) => { |
200 | 200 | const { checkTokenUrl, session: current } = requestContext; |
201 | 201 |
|
@@ -227,3 +227,25 @@ export function initRequest(options: RequestOptions, session?: OAuth2Session): E |
227 | 227 | } |
228 | 228 |
|
229 | 229 |
|
| 230 | +export function makeFailedResponseWithError(err: ApplicationError): HttpResponse<any> { |
| 231 | + const r: HttpResponse<any> = { |
| 232 | + data: err, |
| 233 | + response: { |
| 234 | + ok: false, |
| 235 | + status: 200 |
| 236 | + } as any |
| 237 | + }; |
| 238 | + return r; |
| 239 | +} |
| 240 | + |
| 241 | +export function makeSucceedResponseWithData<T = any>(data: T): HttpResponse<T> { |
| 242 | + const r: HttpResponse<T> = { |
| 243 | + data, |
| 244 | + response: { |
| 245 | + ok: true, |
| 246 | + status: 200 |
| 247 | + } as any |
| 248 | + }; |
| 249 | + return r; |
| 250 | +} |
| 251 | + |
0 commit comments