Skip to content

Commit 22f98d6

Browse files
endinkendink
authored andcommitted
simply http response define
1 parent 4a669eb commit 22f98d6

File tree

3 files changed

+33
-9
lines changed

3 files changed

+33
-9
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "infra-sdk-core",
3-
"version": "1.6.0",
3+
"version": "1.6.1",
44
"description": "A type script sdk for infra java spring server.",
55
"main": "lib/index.js",
66
"scripts": {

src/request/index.ts

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import {
77
RequestResponse
88
} from "umi-request";
99
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";
1111
import { OAuth2Session, ApplicationError } from "../core";
1212
import { clientSession } from "../oauth2/session";
1313
import { isNotNullOrBlankString } from "../utils";
@@ -83,15 +83,15 @@ const handleError = (error: ResponseError, options: RequestOptions, skipNotify?:
8383

8484
export interface ExtendedRequestMethod<R = true> extends RequestMethod<R> {
8585
<T = any>(url: string, options?: ExtendedRequestOptionsInit): R extends true
86-
? Promise<RequestResponse<T & ApplicationError>>
86+
? Promise<HttpResponse<T>>
8787
: Promise<T | ApplicationError>;
8888
login: (
8989
param: LoginParam,
9090
options?: OAuth2RequestOptions
91-
) => Promise<RequestResponse<OAuth2AccessToken & ApplicationError>>;
91+
) => Promise<HttpResponse<OAuth2AccessToken>>;
9292
checkToken: (
9393
options?: OAuth2RequestOptions & { tokenValue?: string }
94-
) => Promise<RequestResponse<CheckTokenResult & ApplicationError>>;
94+
) => Promise<HttpResponse<CheckTokenResult>>;
9595
}
9696

9797
export interface ExtendedRequestOptionsInit extends RequestOptionsInit {
@@ -195,7 +195,7 @@ export function initRequest(options: RequestOptions, session?: OAuth2Session): E
195195

196196
return request<OAuth2AccessToken & ApplicationError>(accessTokenUrl, s);
197197
};
198-
198+
199199
req.checkToken = async (ropt?: OAuth2RequestOptions & { tokenValue?: string }) => {
200200
const { checkTokenUrl, session: current } = requestContext;
201201

@@ -227,3 +227,25 @@ export function initRequest(options: RequestOptions, session?: OAuth2Session): E
227227
}
228228

229229

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+

src/request/types.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { ResponseError } from "umi-request";
2-
import { OAuth2Session, ToastAdapter } from "../core";
1+
import { RequestResponse, ResponseError } from "umi-request";
2+
import { ApplicationError, OAuth2Session, ToastAdapter } from "../core";
33

44
export interface ErrorContext {
55
error: ResponseError;
@@ -23,4 +23,6 @@ export interface RequestOptions {
2323
noneOAuth2?: boolean;
2424
}
2525

26-
export type RequestContext = Pick<RequestOptions, "accessTokenUrl" | "checkTokenUrl"> & { session?: OAuth2Session };
26+
export type RequestContext = Pick<RequestOptions, "accessTokenUrl" | "checkTokenUrl"> & { session?: OAuth2Session };
27+
28+
export type HttpResponse<T> = RequestResponse<T & ApplicationError>;

0 commit comments

Comments
 (0)