Skip to content

Commit

Permalink
fix: add user-friendly validation when an invalid input object is pro…
Browse files Browse the repository at this point in the history
…vided (#89)
  • Loading branch information
MarshallOfSound authored and malept committed May 23, 2019
1 parent 0fefeea commit eb17013
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 2 deletions.
6 changes: 6 additions & 0 deletions src/artifact-utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { ElectronArtifactDetails, MirrorOptions } from './types';
import { ensureIsTruthyString } from './utils';

const BASE_URL = 'https://github.com/electron/electron/releases/download/';
const NIGHTLY_BASE_URL = 'https://github.com/electron/nightlies/releases/download/';
Expand All @@ -12,6 +13,9 @@ export function getArtifactFileName(
details: ElectronArtifactDetails,
usage: FileNameUse = FileNameUse.LOCAL,
) {
ensureIsTruthyString(details, 'artifactName');
ensureIsTruthyString(details, 'version');

if (details.isGeneric) {
// When downloading we have to use the artifact name directly as that it was is stored in the release on GitHub
if (usage === FileNameUse.REMOTE) {
Expand All @@ -21,6 +25,8 @@ export function getArtifactFileName(
return `${details.version}-${details.artifactName}`;
}

ensureIsTruthyString(details, 'platform');
ensureIsTruthyString(details, 'arch');
return `${[details.artifactName, details.version, details.platform, details.arch].join('-')}.zip`;
}

Expand Down
5 changes: 3 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { getArtifactFileName, getArtifactRemoteURL, FileNameUse } from './artifa
import { ElectronArtifactDetails, ElectronDownloadRequestOptions } from './types';
import { Cache } from './Cache';
import { getDownloaderForSystem } from './downloader-resolver';
import { withTempDirectory, normalizeVersion, getHostArch } from './utils';
import { withTempDirectory, normalizeVersion, getHostArch, ensureIsTruthyString } from './utils';

export { getHostArch } from './utils';

Expand Down Expand Up @@ -36,9 +36,10 @@ export function download(
* @param artifactDetails The information required to download the artifact
*/
export async function downloadArtifact(_artifactDetails: ElectronArtifactDetails): Promise<string> {
const artifactDetails = {
const artifactDetails: ElectronArtifactDetails = {
..._artifactDetails,
};
ensureIsTruthyString(artifactDetails, 'version');
artifactDetails.version = normalizeVersion(artifactDetails.version);

const fileName = getArtifactFileName(artifactDetails);
Expand Down
6 changes: 6 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,9 @@ export function getNodeArch(arch: string) {

return arch;
}

export function ensureIsTruthyString<T, K extends keyof T>(obj: T, key: K) {
if (!obj[key] || typeof obj[key] !== 'string') {
throw new Error(`Expected property "${key}" to be provided as a string but it was not`);
}
}

0 comments on commit eb17013

Please sign in to comment.