Skip to content

Commit

Permalink
Get package metadata in source component with polling request strategy (
Browse files Browse the repository at this point in the history
  • Loading branch information
jcortes committed Jun 20, 2024
1 parent bc21d53 commit 0dd3f83
Show file tree
Hide file tree
Showing 8 changed files with 171 additions and 58 deletions.
19 changes: 19 additions & 0 deletions components/npm/common/constants.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
const WEBHOOK_ID = "webhookId";
const SECRET = "secret";

const API = {
DEFAULT: {
BASE_URL: "https://api.npmjs.org",
VERSION_PATH: "",
},
REGISTRY: {
BASE_URL: "https://registry.npmjs.org",
VERSION_PATH: "/-/npm/v1",
},
};

export default {
API,
WEBHOOK_ID,
SECRET,
};
4 changes: 0 additions & 4 deletions components/npm/npm.app.js

This file was deleted.

32 changes: 32 additions & 0 deletions components/npm/npm.app.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { axios } from "@pipedream/platform";
import constants from "./common/constants.mjs";

export default {
type: "app",
app: "npm",
methods: {
getUrl(path, api = constants.API.DEFAULT, withVersion = false) {
return `${api.BASE_URL}${withVersion && api.VERSION_PATH || ""}${path}`;
},
makeRequest({
$ = this, path, api, withVersion, ...args
} = {}) {
return axios($, {
...args,
url: this.getUrl(path, api, withVersion),
});
},
getPackageMetadata({
packageName, ...args
} = {}) {
return this.makeRequest({
api: constants.API.REGISTRY,
path: `/${packageName}`,
headers: {
"Accept": "application/vnd.npm.install-v1+json",
},
...args,
});
},
},
};
6 changes: 3 additions & 3 deletions components/npm/package.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"name": "@pipedream/npm",
"version": "0.3.6",
"version": "0.4.0",
"description": "Pipedream Npm Components",
"main": "npm.app.js",
"main": "npm.app.mjs",
"keywords": [
"pipedream",
"npm"
Expand All @@ -14,6 +14,6 @@
"access": "public"
},
"dependencies": {
"@pipedream/platform": "^1.2.0"
"@pipedream/platform": "^3.0.0"
}
}
49 changes: 0 additions & 49 deletions components/npm/sources/download-counts/download-counts.js

This file was deleted.

69 changes: 69 additions & 0 deletions components/npm/sources/download-counts/download-counts.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import app from "../../npm.app.mjs";

export default {
key: "npm-download-counts",
name: "New Download Counts",
description: "Emit new event with the latest count of downloads for an npm package. [See the documentation](https://github.com/npm/registry/blob/main/docs/download-counts.md).",
version: "0.1.0",
type: "source",
props: {
app,
db: "$.service.db",
timer: {
type: "$.interface.timer",
description: "One day interval time is recommended because NPM only update metrics once a day. [See the documentation](https://github.com/npm/registry/blob/main/docs/download-counts.md#data-source).",
default: {
intervalSeconds: 60 * 60 * 24,
},
},
period: {
type: "string",
label: "Period",
description: "Select last-day, last-week or last-month.",
optional: false,
default: "last-day",
options: [
"last-day",
"last-week",
"last-month",
],
},
packageName: {
type: "string",
label: "Package",
description: "Enter an npm package name. Leave blank for all",
optional: true,
},
},
methods: {
getDownloadCounts({
period, packageName, ...args
} = {}) {
const basePath = `/downloads/point/${encodeURIComponent(period)}`;
return this.app.makeRequest({
path: packageName
? `${basePath}/${encodeURIComponent(packageName)}`
: basePath,
...args,
});
},
},
async run({ timestamp: ts }) {
const {
getDownloadCounts,
period,
packageName,
} = this;

const response = await getDownloadCounts({
period,
packageName,
});

this.$emit(response, {
id: ts,
summary: `New Download Count ${response.downloads}`,
ts,
});
},
};
46 changes: 46 additions & 0 deletions components/npm/sources/new-package-version/new-package-version.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { DEFAULT_POLLING_SOURCE_TIMER_INTERVAL } from "@pipedream/platform";
import app from "../../npm.app.mjs";

export default {
key: "npm-new-package-version",
name: "New Package Version",
description: "Emit new event when a new version of an npm package is published. [See the documentation](https://github.com/npm/registry/blob/main/docs/responses/package-metadata.md)",
version: "0.0.1",
type: "source",
dedupe: "unique",
props: {
app,
db: "$.service.db",
timer: {
type: "$.interface.timer",
default: {
intervalSeconds: DEFAULT_POLLING_SOURCE_TIMER_INTERVAL,
},
},
packageName: {
type: "string",
label: "Package",
description: "Enter an npm package name. Leave blank for all",
default: "@pipedream/platform",
},
},
async run() {
const {
app,
packageName,
} = this;

const response = await app.getPackageMetadata({
debug: true,
packageName,
});

const { "dist-tags": { latest: latestVersion } } = response;

this.$emit(response, {
id: latestVersion,
summary: `New Package Version ${latestVersion}`,
ts: Date.parse(response.modified),
});
},
};
4 changes: 2 additions & 2 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 0dd3f83

Please sign in to comment.