diff --git a/components/npm/common/constants.mjs b/components/npm/common/constants.mjs new file mode 100644 index 0000000000000..296b1a61c57bd --- /dev/null +++ b/components/npm/common/constants.mjs @@ -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, +}; diff --git a/components/npm/npm.app.js b/components/npm/npm.app.js deleted file mode 100644 index 135c4ffc3f6f0..0000000000000 --- a/components/npm/npm.app.js +++ /dev/null @@ -1,4 +0,0 @@ -module.exports = { - type: "app", - app: "npm", -}; diff --git a/components/npm/npm.app.mjs b/components/npm/npm.app.mjs new file mode 100644 index 0000000000000..d10043864d0bc --- /dev/null +++ b/components/npm/npm.app.mjs @@ -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, + }); + }, + }, +}; diff --git a/components/npm/package.json b/components/npm/package.json index 9bb5a9032dc90..3d7976d3a461a 100644 --- a/components/npm/package.json +++ b/components/npm/package.json @@ -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" @@ -14,6 +14,6 @@ "access": "public" }, "dependencies": { - "@pipedream/platform": "^1.2.0" + "@pipedream/platform": "^3.0.0" } } diff --git a/components/npm/sources/download-counts/download-counts.js b/components/npm/sources/download-counts/download-counts.js deleted file mode 100644 index 1e11440b1f86c..0000000000000 --- a/components/npm/sources/download-counts/download-counts.js +++ /dev/null @@ -1,49 +0,0 @@ -const npm = require("../../npm.app.js"); -const { DEFAULT_POLLING_SOURCE_TIMER_INTERVAL } = require("@pipedream/platform"); -const axios = require("axios"); - -module.exports = { - key: "npm-download-counts", - name: "npm Download Counts", - description: "Emit an event with the latest count of downloads for an npm package", - version: "0.0.2", - type: "source", - props: { - db: "$.service.db", - timer: { - type: "$.interface.timer", - default: { - intervalSeconds: DEFAULT_POLLING_SOURCE_TIMER_INTERVAL, - }, - }, - 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", - ], - }, - package: { - type: "string", - label: "Package", - description: "Enter an npm package name. Leave blank for all", - optional: true, - default: "@pipedreamhq/platform", - }, - npm, - }, - async run() { - const npm_event = (await axios({ - method: "get", - url: `https://api.npmjs.org/downloads/point/${encodeURIComponent(this.period)}/${encodeURIComponent(this.package)}`, - })).data; - this.$emit(npm_event, { - summary: "" + npm_event.downloads, - }); - }, -}; diff --git a/components/npm/sources/download-counts/download-counts.mjs b/components/npm/sources/download-counts/download-counts.mjs new file mode 100644 index 0000000000000..84232ccafb1be --- /dev/null +++ b/components/npm/sources/download-counts/download-counts.mjs @@ -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, + }); + }, +}; diff --git a/components/npm/sources/new-package-version/new-package-version.mjs b/components/npm/sources/new-package-version/new-package-version.mjs new file mode 100644 index 0000000000000..cb11b00186d80 --- /dev/null +++ b/components/npm/sources/new-package-version/new-package-version.mjs @@ -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), + }); + }, +}; diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index c786cb1177a69..d8baf80fe9480 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -5885,9 +5885,9 @@ importers: components/npm: specifiers: - '@pipedream/platform': ^1.2.0 + '@pipedream/platform': ^3.0.0 dependencies: - '@pipedream/platform': 1.5.1 + '@pipedream/platform': 3.0.0 components/nudgify: specifiers: