smartnpm/ts/smartnpm.classes.npmregistry.ts

103 lines
2.8 KiB
TypeScript
Raw Normal View History

2018-09-01 14:40:42 +00:00
import * as plugins from './smartnpm.plugins';
2017-08-14 15:50:48 +00:00
2017-08-15 15:03:21 +00:00
// interfaces
2018-09-01 14:40:42 +00:00
import { ISearchObject } from './smartnpm.interfaces';
2017-08-14 15:50:48 +00:00
2017-08-15 15:03:21 +00:00
// classes
2018-09-01 14:40:42 +00:00
import { NpmPackage } from './smartnpm.classes.npmpackage';
2017-08-14 15:50:48 +00:00
export class NpmRegistry {
2018-09-01 14:40:42 +00:00
private searchDomain = 'https://api.npms.io/v2/search?q=';
async search(searchObjectArg: ISearchObject) {
let searchString = '';
2017-08-15 12:20:33 +00:00
let addToSearchString = (addStringArg: string) => {
2018-09-01 14:40:42 +00:00
searchString = `${searchString}+${addStringArg}`;
};
2017-08-15 12:20:33 +00:00
2017-08-15 15:03:21 +00:00
// name
2018-09-01 14:40:42 +00:00
if (searchObjectArg.name) {
searchString = `${searchObjectArg.name}`;
}
2017-08-15 15:03:21 +00:00
2017-08-15 12:20:33 +00:00
// metadata
2018-09-01 14:40:42 +00:00
if (searchObjectArg.author) {
addToSearchString(`author:${searchObjectArg.author}`);
}
if (searchObjectArg.maintainer) {
addToSearchString(`maintainer:${searchObjectArg.maintainer}`);
}
if (searchObjectArg.scope) {
addToSearchString(`scope:${searchObjectArg.scope}`);
}
2017-08-15 15:03:21 +00:00
2017-08-15 12:20:33 +00:00
// status
if (searchObjectArg.deprecated) {
if (searchObjectArg.deprecated === true) {
2018-09-01 14:40:42 +00:00
addToSearchString(`is:deprecated`);
2017-08-15 12:20:33 +00:00
} else {
2018-09-01 14:40:42 +00:00
addToSearchString(`not:deprecated`);
2017-08-15 12:20:33 +00:00
}
}
if (searchObjectArg.unstable) {
if (searchObjectArg.unstable === true) {
2018-09-01 14:40:42 +00:00
addToSearchString(`is:unstable`);
2017-08-15 12:20:33 +00:00
} else {
2018-09-01 14:40:42 +00:00
addToSearchString(`not:unstable`);
2017-08-15 12:20:33 +00:00
}
}
if (searchObjectArg.insecure) {
if (searchObjectArg.insecure === true) {
2018-09-01 14:40:42 +00:00
addToSearchString(`is:insecure`);
2017-08-15 12:20:33 +00:00
} else {
2018-09-01 14:40:42 +00:00
addToSearchString(`not:insecure`);
2017-08-15 12:20:33 +00:00
}
}
2017-08-15 15:03:21 +00:00
2017-08-15 12:20:33 +00:00
// search behaviour
2018-09-01 14:40:42 +00:00
if (searchObjectArg.boostExact) {
addToSearchString(`boost-exact:${searchObjectArg.boostExact}`);
}
if (searchObjectArg.scoreEffect) {
addToSearchString(`score-effect:${searchObjectArg.scoreEffect}`);
}
2017-08-15 15:03:21 +00:00
2017-08-15 12:20:33 +00:00
// analytics
2018-09-01 14:40:42 +00:00
if (searchObjectArg.qualityWeight) {
addToSearchString(`author:${searchObjectArg.qualityWeight}`);
}
if (searchObjectArg.popularityWeight) {
addToSearchString(`author:${searchObjectArg.popularityWeight}`);
}
if (searchObjectArg.maintenanceWeight) {
addToSearchString(`author:${searchObjectArg.maintenanceWeight}`);
}
2017-08-15 12:20:33 +00:00
2018-09-01 14:40:42 +00:00
plugins.smartlog.defaultLogger.info(
`Search on npm for ${plugins.consolecolor.coloredString(searchString, 'pink')}`
);
2017-08-15 15:03:21 +00:00
2018-02-14 22:51:08 +00:00
let body: any;
try {
2018-09-01 14:40:42 +00:00
let response = await plugins.smartrequest.getJson(this.searchDomain + searchString, {});
body = response.body;
2018-02-14 22:51:08 +00:00
} catch {
// we do nothing
}
// lets create the packageArray
2018-09-01 14:40:42 +00:00
let packageArray: NpmPackage[] = [];
2018-02-14 22:51:08 +00:00
// if request failed just return it empty
if (!body || typeof body === 'string') {
2018-09-01 14:40:42 +00:00
return packageArray;
2017-09-13 15:33:07 +00:00
}
2018-02-14 22:51:08 +00:00
2017-08-15 15:03:21 +00:00
for (let packageArg of body.results) {
2018-09-01 14:40:42 +00:00
let localPackage = new NpmPackage(packageArg.package);
packageArray.push(localPackage);
2017-08-15 15:03:21 +00:00
}
2018-09-01 14:40:42 +00:00
return packageArray;
2017-08-14 15:50:48 +00:00
}
}