Skip to content

Commit

Permalink
fix type for fail (#9)
Browse files Browse the repository at this point in the history
  • Loading branch information
justjavac authored Aug 4, 2022
1 parent da8a645 commit 8b7dd14
Showing 1 changed file with 14 additions and 7 deletions.
21 changes: 14 additions & 7 deletions mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,31 @@ export interface ModuleInfo {
star_count: number;
}

interface ModuleInfoResponse {
success: boolean;
interface SuccessResponse {
success: true;
data: ModuleInfo;
}

interface FailResponse {
success: false;
error: string;
}

type ModuleInfoResponse = SuccessResponse | FailResponse;

export default async function moduleInfo(
mod: string,
): Promise<ModuleInfo | null> {
const response = await fetch(
`https://api.deno.land/modules/${mod}`,
);

const { success, error, data }: ModuleInfoResponse = await response.json();
if (success) {
return data;
const result: ModuleInfoResponse = await response.json();

if (result.success) {
return result.data;
} else {
console.error(`Error: error`);
console.error("Error: %s", result.error);
return null;
}
}

0 comments on commit 8b7dd14

Please sign in to comment.