Skip to content

Commit

Permalink
implement functionality to skip bundles
Browse files Browse the repository at this point in the history
  • Loading branch information
agonyz committed Jul 15, 2023
1 parent c63bc28 commit 7998f7c
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 3 deletions.
16 changes: 15 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
- [By vendor](#by-vendor)
- [By maintainer](#by-maintainer)
- [Themes](#themes)
- [Add own themes](#add-own-themes)
- [Add own themes](#add-own-themes)
- [Skip Bundles](#skip-bundles)
- [Caching](#caching)

## Top bundles
Expand Down Expand Up @@ -64,5 +65,18 @@ To add your own themes, do the following:
- Place your styles inside the theme file and export your theme
- Create a pull request e.g. `feature/add-theme-{theme-name}`

## Skip Bundles
You can skip bundles you don't want to display
- How to use:
```markdown
/api/packagist/card?vendor={your-packagist-user}&skip={bundle-name1, bundle-name2, ..}
```
- Example:
```markdown
![Packagist Top Bundles](https://github-readme-packagist-stats.vercel.app/api/packagist/card?vendor=agonyz&skip=contao-countdown-bundle,contao-pagespeed-insights-bundle)
```
![Packagist Top Bundles](https://github-readme-packagist-stats.vercel.app/api/packagist/card?vendor=agonyz&skip=contao-countdown-bundle,contao-pagespeed-insights-bundle)


## Caching
- In order to not overuse the packagist api a cache time of `12 hours` was implemented.
4 changes: 3 additions & 1 deletion src/routes/routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class MainRouter {
const maintainer: string | null =
(req.query.maintainer as string) ?? null;
const theme: string | null = (req.query.theme as string) ?? null;
const skip: string | null = (req.query.skip as string) ?? null;

// throw error if vendor is missing
if (!vendor) {
Expand All @@ -38,7 +39,8 @@ class MainRouter {

const packagistData = await this.packagistService.getPackagistData(
vendor,
maintainer
maintainer,
skip
);

if (!packagistData) {
Expand Down
25 changes: 25 additions & 0 deletions src/services/bundle.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { PackagesByOrganization } from '@agonyz/packagist-api-client/lib/interfaces';

export class BundleService {
static skipBundles(
skip: string,
packages: PackagesByOrganization,
vendor: string
): PackagesByOrganization | null {
const bundlesToSkip = new Set(
skip.split(',').map((bundle) => bundle.trim())
);

packages.packageNames = packages.packageNames
.filter((bundle) => {
const bundleName = bundle.replace(vendor + '/', '');
return !bundlesToSkip.has(bundleName);
})
.map((bundle) => vendor + '/' + bundle.replace(vendor + '/', ''));

if (packages.packageNames.length <= 0) {
return null;
}
return packages;
}
}
13 changes: 12 additions & 1 deletion src/services/packagist.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
} from '@agonyz/packagist-api-client/lib/interfaces';
import { SortedPackage } from '../interfaces/packagist.interface';
import { LoggerService } from './logger.service';
import { BundleService } from './bundle.service';

export class PackagistService {
private client: PackagistApi;
Expand All @@ -23,10 +24,12 @@ export class PackagistService {
*
* @param vendor
* @param maintainer
* @param skip
*/
async getPackagistData(
vendor: string,
maintainer: string | null
maintainer: string | null,
skip: string | null
): Promise<Package[] | null> {
// get packages by vendor
let packages;
Expand All @@ -40,6 +43,14 @@ export class PackagistService {
return null;
}

// skip bundles if skip is set
if (skip) {
packages = BundleService.skipBundles(skip, packages, vendor);
if (!packages) {
return null;
}
}

// get maintainer top bundles if maintainer is set
if (maintainer) {
const maintainerBundles: Package[] | null =
Expand Down

0 comments on commit 7998f7c

Please sign in to comment.