@agonyz/packagist-api-client
Version:
API Client for the Packagist API
82 lines (81 loc) • 2.5 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const AbstractPackagistApi_1 = __importDefault(require("./AbstractPackagistApi"));
class PackagistApi extends AbstractPackagistApi_1.default {
constructor(userAgent) {
super(userAgent);
}
async processResponse(response) {
return (await response.json());
}
/**
* Reference: https://packagist.org/apidoc#get-package-data
* @param packageName
*/
async getPackageInfo(packageName) {
const endpoint = `packages/${packageName}.json`;
return this.get(endpoint);
}
/**
* Reference: https://packagist.org/apidoc#list-packages-by-type
* @param type
*/
async getPackagesByType(type) {
const endpoint = `packages/list.json?type=${type}`;
return this.get(endpoint);
}
/**
* Reference: https://packagist.org/apidoc#list-popular-packages
* @param perPage
*/
async getPopularPackages(perPage) {
let endpoint = `explore/popular.json`;
if (perPage) {
endpoint = `explore/popular.json?per_page=${perPage}`;
}
return this.get(endpoint);
}
/**
* Reference: https://packagist.org/apidoc#list-packages-by-organization
* @param vendor
*/
async getPackagesByOrganization(vendor) {
const endpoint = `packages/list.json?vendor=${vendor}`;
return this.get(endpoint);
}
/**
* Reference: https://packagist.org/apidoc#get-package-stats
* @param packageName
*/
async getPackageDownloadStats(packageName) {
const endpoint = `packages/${packageName}/stats.json`;
return this.get(endpoint);
}
/**
* Reference: https://packagist.org/apidoc#search-packages
* @param query
* @param tag
* @param type
* @param perPage
*/
async searchPackages(query, tag, type, perPage) {
let endpoint = `search.json?`;
if (query) {
endpoint = endpoint + `q=${query}`;
}
if (tag) {
endpoint = endpoint + `&tags=${tag}`;
}
if (type) {
endpoint = endpoint + `&type=${type}`;
}
if (perPage) {
endpoint = endpoint + `&per_page=${perPage}`;
}
return this.get(endpoint);
}
}
exports.default = PackagistApi;