Skip to content
This repository has been archived by the owner on Mar 26, 2023. It is now read-only.

Commit

Permalink
feat: refactor d.ts
Browse files Browse the repository at this point in the history
  • Loading branch information
lizheming committed Apr 30, 2022
1 parent 6d70128 commit 2b2fe6d
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 16 deletions.
15 changes: 8 additions & 7 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { CommentId, GetAdminListRequest, GetAdminListResponse, GetCountRequest, GetCountResponse, GetListRequest, GetListResponse, GetRecentCommentRequest, GetRecentCommentResponse, GetUserResponse, LoginRequest, PostCommentRequest, PostCommentResponse, RegisterUserRequest, UpdateCommentRequest, UpdateUserRequest, WalineAPIOptions } from "./types";
import { BasicResponse, CommentId, GetAdminListRequest, GetAdminListResponse, GetCountRequest, GetCountResponse, GetListRequest, GetListResponse, GetRecentCommentRequest, GetRecentCommentResponse, GetUserResponse, LoginRequest, PostCommentRequest, PostCommentResponse, RegisterUserRequest, UpdateCommentRequest, UpdateUserRequest, WalineAPIOptions } from "./types";
import { fetchFactory, qs } from "./util";

type BaseResponse = Required<BasicResponse>;
export default function(options: WalineAPIOptions) {
const request = fetchFactory(options);
return {
Expand All @@ -27,7 +28,7 @@ export default function(options: WalineAPIOptions) {
});
},
update(data: UpdateCommentRequest) {
return request('/comment', {
return request<BaseResponse>('/comment', {
method: 'PUT',
headers: {
'content-type': 'application/json',
Expand All @@ -36,15 +37,15 @@ export default function(options: WalineAPIOptions) {
});
},
delete(commentId: CommentId) {
return request('/comment/' + commentId, { method: 'DELETE' });
return request<BaseResponse>('/comment/' + commentId, { method: 'DELETE' });
}
},
user: {
getInfo() {
return request<GetUserResponse>('/token');
},
register(params: RegisterUserRequest) {
return request('/user', {
return request<BaseResponse>('/user', {
method: 'POST',
headers: {
'content-type': 'application/json',
Expand All @@ -53,7 +54,7 @@ export default function(options: WalineAPIOptions) {
});
},
update(params: UpdateUserRequest) {
return request('/user', {
return request<BaseResponse>('/user', {
method: 'PUT',
headers: {
'content-type': 'application/json',
Expand All @@ -62,7 +63,7 @@ export default function(options: WalineAPIOptions) {
});
},
login(params: LoginRequest) {
return request('/token', {
return request<BaseResponse>('/token', {
method: 'POST',
headers: {
'content-type': 'application/json',
Expand All @@ -71,7 +72,7 @@ export default function(options: WalineAPIOptions) {
});
},
logout() {
return request('/token', { method: 'DELETE' });
return request<BaseResponse>('/token', { method: 'DELETE' });
}
}
}
Expand Down
6 changes: 2 additions & 4 deletions src/types/comment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,7 @@ export interface GetAdminListRequest {
page?: number;
}

export interface GetAdminListResponse {
errno: number;
errmsg: string;
export interface GetAdminListResponse extends Required<BasicResponse> {
data: AdminList;
}

Expand Down Expand Up @@ -92,7 +90,7 @@ export interface PostCommentRequest {
at?: string;
}

export interface PostCommentResponse extends BasicResponse {
export interface PostCommentResponse extends Required<BasicResponse> {
data: Omit<Comment, 'children'>
}

Expand Down
2 changes: 1 addition & 1 deletion src/types/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,6 @@ export interface LoginRequest {
password: string;
}

export interface GetUserResponse extends BasicResponse{
export interface GetUserResponse extends Required<BasicResponse> {
data: User;
}
6 changes: 3 additions & 3 deletions src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export function fetchFactory(factoryOpt: WalineAPIOptions) {
baseUrl = baseUrl.replace(/\/+$/, '');
}

return async function request<T extends BasicResponse>(input: RequestInfo, init?: RequestInit) {
return async function request<T extends BasicResponse>(input: RequestInfo, init?: RequestInit): Promise<T extends { errno: number; } ? T["data"] : T> {
input = baseUrl + input;

if (factoryOpt.token) {
Expand All @@ -21,11 +21,11 @@ export function fetchFactory(factoryOpt: WalineAPIOptions) {
}

const response = await fetch(input, init);
const resp: T = await response.json();
const resp = await response.json();
if (!resp.hasOwnProperty('errno')) {
return resp;
} else if (resp.errno) {
throw new Error(resp.errmsg);
throw new Error(typeof resp.errmsg === 'string' ? resp.errmsg : JSON.stringify(resp.errmsg));
}

return resp.data;
Expand Down
3 changes: 2 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"forceConsistentCasingInFileNames": true,
"strict": true,
"noImplicitAny": true,
"skipLibCheck": true
"skipLibCheck": true,
"moduleResolution": "node"
}
}

0 comments on commit 2b2fe6d

Please sign in to comment.