smartnpm/ts/smartnpm.classes.npmregistry.ts

73 lines
2.4 KiB
TypeScript
Raw Normal View History

2017-08-14 15:50:48 +00:00
import * as plugins from './smartnpm.plugins'
2017-08-15 15:03:21 +00:00
// interfaces
import { ISearchObject } from './smartnpm.interfaces'
2017-08-14 15:50:48 +00:00
2017-08-15 15:03:21 +00:00
// classes
import { NpmPackage } from './smartnpm.classes.npmpackage'
2017-08-14 15:50:48 +00:00
export class NpmRegistry {
2017-08-15 12:20:33 +00:00
private searchDomain = 'https://api.npms.io/v2/search?q='
2017-08-15 15:03:21 +00:00
async search (searchObjectArg: ISearchObject) {
2017-08-15 12:20:33 +00:00
let searchString = ''
let addToSearchString = (addStringArg: string) => {
2017-08-15 15:03:21 +00:00
searchString = `${searchString}+${addStringArg}`
2017-08-15 12:20:33 +00:00
}
2017-08-15 15:03:21 +00:00
// name
if (searchObjectArg.name) { searchString = `${searchObjectArg.name}` }
2017-08-15 12:20:33 +00:00
// metadata
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) {
addToSearchString(`is:deprecated`)
} else {
addToSearchString(`not:deprecated`)
}
}
if (searchObjectArg.unstable) {
if (searchObjectArg.unstable === true) {
addToSearchString(`is:unstable`)
} else {
addToSearchString(`not:unstable`)
}
}
if (searchObjectArg.insecure) {
if (searchObjectArg.insecure === true) {
addToSearchString(`is:insecure`)
} else {
addToSearchString(`not:insecure`)
}
}
2017-08-15 15:03:21 +00:00
2017-08-15 12:20:33 +00:00
// search behaviour
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
if (searchObjectArg.qualityWeight) { addToSearchString(`author:${searchObjectArg.qualityWeight}`) }
if (searchObjectArg.popularityWeight) { addToSearchString(`author:${searchObjectArg.popularityWeight}`) }
if (searchObjectArg.maintenanceWeight) { addToSearchString(`author:${searchObjectArg.maintenanceWeight}`) }
2017-08-15 15:03:21 +00:00
plugins.beautylog.log(`Search for "${searchString}" on npm`)
let response = (await plugins.smartrequest.get(this.searchDomain + searchString, {}))
let body: any = response.body
// lets create the response
let packageArray: NpmPackage[] = []
for (let packageArg of body.results) {
let localPackage = new NpmPackage(packageArg.package)
packageArray.push(localPackage)
}
return packageArray
2017-08-14 15:50:48 +00:00
}
}