Skip to content

Commit

Permalink
better typing for backend types
Browse files Browse the repository at this point in the history
  • Loading branch information
hanzhi713 committed Jan 13, 2021
1 parent abb2766 commit b84564e
Showing 1 changed file with 16 additions and 5 deletions.
21 changes: 16 additions & 5 deletions src/store/profile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,15 @@ interface BackendBaseResponse {
message: string;
}

interface BackendFailedResponse extends BackendBaseResponse {
success: false;
}

interface BackendListRequest {
name?: string; // the profile name. If omitted, return all the profiles (each profile should be the latest version)
version?: number; // only present if "name" is present. If this field is missing, then the latest profile should be returned
/** the profile name. If omitted, return all the profiles (each profile should be the latest version) */
name?: string;
/** only present if "name" is present. If this field is missing, then the latest profile should be returned */
version?: number;
}

interface ProfileVersion {
Expand All @@ -41,6 +47,7 @@ interface BackendListItem {
}

interface BackendListResponse extends BackendBaseResponse {
success: true;
/** if the name field of the request is missing, this should be a list of all profiles. Otherwise, this should be a list of 1 profile corresponding to the name and version given. */
profiles: BackendListItem[];
}
Expand All @@ -61,6 +68,7 @@ interface BackendUploadRequest {
}

interface BackendUploadResponse extends BackendBaseResponse {
success: true;
/** version information of each newly uploaded profile. If failed, this field is not present */
versions: ProfileVersion[][];
}
Expand All @@ -73,6 +81,7 @@ interface BackendRenameRequest {
}

interface BackendRenameResponse extends BackendBaseResponse {
success: true;
versions: ProfileVersion[];
}

Expand All @@ -82,7 +91,9 @@ interface BackendDeleteRequest {
name: string;
}

interface BackendDeleteResponse extends BackendBaseResponse {}
interface BackendDeleteResponse extends BackendBaseResponse {
success: true;
}

export interface LocalProfileEntry {
/** name of the profile */
Expand Down Expand Up @@ -411,7 +422,7 @@ class Profile {
private async requestBackend<RequestType, ResponseType extends BackendBaseResponse>(
endpoint: string,
request: RequestType
): Promise<ResponseType> {
): Promise<ResponseType | BackendFailedResponse> {
try {
return (
await axios.post<ResponseType>(endpoint, request, {
Expand All @@ -424,7 +435,7 @@ class Profile {
return {
success: false,
message: err.message
} as ResponseType;
};
}
}

Expand Down

0 comments on commit b84564e

Please sign in to comment.