Skip to content

Commit

Permalink
chore(types) add return type to all api functions
Browse files Browse the repository at this point in the history
Signed-off-by: David Edler <[email protected]>
  • Loading branch information
edlerd committed Feb 15, 2024
1 parent a5ea2e7 commit df91172
Show file tree
Hide file tree
Showing 10 changed files with 73 additions and 51 deletions.
2 changes: 1 addition & 1 deletion src/api/certificates.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export const fetchCertificates = (): Promise<LxdCertificate[]> => {
});
};

export const addCertificate = (token: string) => {
export const addCertificate = (token: string): Promise<void> => {
return new Promise((resolve, reject) => {
fetch(`/1.0/certificates`, {
method: "POST",
Expand Down
4 changes: 2 additions & 2 deletions src/api/cluster.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -83,13 +83,13 @@ export const createClusterGroup = (
});
};

export const deleteClusterGroup = (group: string) => {
export const deleteClusterGroup = (group: string): Promise<void> => {
return new Promise((resolve, reject) => {
fetch(`/1.0/cluster/groups/${group}`, {
method: "DELETE",
})
.then(handleResponse)
.then((data) => resolve(data))
.then(resolve)
.catch(reject);
});
};
22 changes: 17 additions & 5 deletions src/api/instances.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -104,23 +104,35 @@ export const migrateInstance = (
});
};

export const startInstance = (instance: LxdInstance) => {
export const startInstance = (
instance: LxdInstance,
): Promise<LxdOperationResponse> => {
return putInstanceAction(instance.name, instance.project, "start");
};

export const stopInstance = (instance: LxdInstance, isForce: boolean) => {
export const stopInstance = (
instance: LxdInstance,
isForce: boolean,
): Promise<LxdOperationResponse> => {
return putInstanceAction(instance.name, instance.project, "stop", isForce);
};

export const freezeInstance = (instance: LxdInstance) => {
export const freezeInstance = (
instance: LxdInstance,
): Promise<LxdOperationResponse> => {
return putInstanceAction(instance.name, instance.project, "freeze");
};

export const unfreezeInstance = (instance: LxdInstance) => {
export const unfreezeInstance = (
instance: LxdInstance,
): Promise<LxdOperationResponse> => {
return putInstanceAction(instance.name, instance.project, "unfreeze");
};

export const restartInstance = (instance: LxdInstance, isForce: boolean) => {
export const restartInstance = (
instance: LxdInstance,
isForce: boolean,
): Promise<LxdOperationResponse> => {
return putInstanceAction(instance.name, instance.project, "restart", isForce);
};

Expand Down
4 changes: 2 additions & 2 deletions src/api/network-forwards.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export const createNetworkForward = (
network: string,
forward: Partial<LxdNetworkForward>,
project: string,
) => {
): Promise<void> => {
return new Promise((resolve, reject) => {
fetch(`/1.0/networks/${network}/forwards?project=${project}`, {
method: "POST",
Expand All @@ -51,7 +51,7 @@ export const updateNetworkForward = (
network: string,
forward: Partial<LxdNetworkForward>,
project: string,
) => {
): Promise<void> => {
return new Promise((resolve, reject) => {
fetch(
`/1.0/networks/${network}/forwards/${forward.listen_address}?project=${project}`,
Expand Down
18 changes: 9 additions & 9 deletions src/api/networks.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export const createClusterBridge = (
network: Partial<LxdNetwork>,
project: string,
clusterMembers: LxdClusterMember[],
) => {
): Promise<void> => {
return new Promise((resolve, reject) => {
const memberNetwork = {
name: network.name,
Expand Down Expand Up @@ -75,22 +75,22 @@ export const createNetwork = (
network: Partial<LxdNetwork>,
project: string,
target?: string,
) => {
): Promise<void> => {
return new Promise((resolve, reject) => {
const targetParam = target ? `&target=${target}` : "";
fetch(`/1.0/networks?project=${project}${targetParam}`, {
method: "POST",
body: JSON.stringify(network),
})
.then(handleResponse)
.then((data) => resolve(data))
.then(resolve)
.catch(async (e: Error) => {
// when creating a network on localhost the request will get cancelled
// check manually if creation was successful
if (e.message === "Failed to fetch") {
const newNetwork = await fetchNetwork(network.name ?? "", project);
if (newNetwork) {
resolve(newNetwork);
resolve();
}
}
reject(e);
Expand All @@ -101,7 +101,7 @@ export const createNetwork = (
export const updateNetwork = (
network: Partial<LxdNetwork> & Required<Pick<LxdNetwork, "config">>,
project: string,
) => {
): Promise<void> => {
return new Promise((resolve, reject) => {
fetch(`/1.0/networks/${network.name ?? ""}?project=${project}`, {
method: "PUT",
Expand All @@ -111,14 +111,14 @@ export const updateNetwork = (
},
})
.then(handleResponse)
.then((data) => resolve(data))
.then(resolve)
.catch(async (e: Error) => {
// when updating a network on localhost the request will get cancelled
// check manually if the edit was successful
if (e.message === "Failed to fetch") {
const newNetwork = await fetchNetwork(network.name ?? "", project);
if (areNetworksEqual(network, newNetwork)) {
resolve(newNetwork);
resolve();
}
}
reject(e);
Expand All @@ -130,7 +130,7 @@ export const renameNetwork = (
oldName: string,
newName: string,
project: string,
) => {
): Promise<void> => {
return new Promise((resolve, reject) => {
fetch(`/1.0/networks/${oldName}?project=${project}`, {
method: "POST",
Expand All @@ -146,7 +146,7 @@ export const renameNetwork = (
if (e.message === "Failed to fetch") {
const renamedNetwork = await fetchNetwork(newName, project);
if (renamedNetwork) {
resolve(renamedNetwork);
resolve();
}
}
reject(e);
Expand Down
15 changes: 9 additions & 6 deletions src/api/profiles.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,22 @@ export const fetchProfiles = (project: string): Promise<LxdProfile[]> => {
});
};

export const createProfile = (body: string, project: string) => {
export const createProfile = (body: string, project: string): Promise<void> => {
return new Promise((resolve, reject) => {
fetch(`/1.0/profiles?project=${project}`, {
method: "POST",
body: body,
})
.then(handleResponse)
.then((data) => resolve(data))
.then(resolve)
.catch(reject);
});
};

export const updateProfile = (profile: LxdProfile, project: string) => {
export const updateProfile = (
profile: LxdProfile,
project: string,
): Promise<void> => {
return new Promise((resolve, reject) => {
fetch(`/1.0/profiles/${profile.name}?project=${project}`, {
method: "PUT",
Expand All @@ -54,7 +57,7 @@ export const renameProfile = (
oldName: string,
newName: string,
project: string,
) => {
): Promise<void> => {
return new Promise((resolve, reject) => {
fetch(`/1.0/profiles/${oldName}?project=${project}`, {
method: "POST",
Expand All @@ -68,13 +71,13 @@ export const renameProfile = (
});
};

export const deleteProfile = (name: string, project: string) => {
export const deleteProfile = (name: string, project: string): Promise<void> => {
return new Promise((resolve, reject) => {
fetch(`/1.0/profiles/${name}?project=${project}`, {
method: "DELETE",
})
.then(handleResponse)
.then((data) => resolve(data))
.then(resolve)
.catch(reject);
});
};
12 changes: 6 additions & 6 deletions src/api/projects.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,19 @@ export const fetchProject = (name: string): Promise<LxdProject> => {
});
};

export const createProject = (body: string) => {
export const createProject = (body: string): Promise<void> => {
return new Promise((resolve, reject) => {
fetch(`/1.0/projects`, {
method: "POST",
body: body,
})
.then(handleResponse)
.then((data) => resolve(data))
.then(resolve)
.catch(reject);
});
};

export const updateProject = (project: LxdProject) => {
export const updateProject = (project: LxdProject): Promise<void> => {
return new Promise((resolve, reject) => {
fetch(`/1.0/projects/${project.name}`, {
method: "PUT",
Expand All @@ -43,7 +43,7 @@ export const updateProject = (project: LxdProject) => {
},
})
.then(handleResponse)
.then((data) => resolve(data))
.then(resolve)
.catch(reject);
});
};
Expand All @@ -65,13 +65,13 @@ export const renameProject = (
});
};

export const deleteProject = (project: LxdProject) => {
export const deleteProject = (project: LxdProject): Promise<void> => {
return new Promise((resolve, reject) => {
fetch(`/1.0/projects/${project.name}`, {
method: "DELETE",
})
.then(handleResponse)
.then((data) => resolve(data))
.then(resolve)
.catch(reject);
});
};
4 changes: 2 additions & 2 deletions src/api/server.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export const fetchSettings = (): Promise<LxdSettings> => {
});
};

export const updateSettings = (config: LxdConfigPair) => {
export const updateSettings = (config: LxdConfigPair): Promise<void> => {
return new Promise((resolve, reject) => {
fetch("/1.0", {
method: "PATCH",
Expand All @@ -22,7 +22,7 @@ export const updateSettings = (config: LxdConfigPair) => {
}),
})
.then(handleResponse)
.then((data) => resolve(data))
.then(resolve)
.catch(reject);
});
};
Expand Down
Loading

0 comments on commit df91172

Please sign in to comment.