Skip to content

Commit 3f5b8bc

Browse files
authored
13332 components neetokb (#19223)
* Implement neetokb API integration with new methods for listing articles and pagination; add new source for detecting published articles; update package version to 0.1.0 and include dependencies. * pnpm update * Update key for new published article source to improve clarity and consistency
1 parent 803a3e2 commit 3f5b8bc

File tree

6 files changed

+148
-5
lines changed

6 files changed

+148
-5
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const LIMIT = 100;

components/neetokb/neetokb.app.mjs

Lines changed: 57 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,65 @@
1+
import { axios } from "@pipedream/platform";
2+
import { LIMIT } from "./common/constants.mjs";
3+
14
export default {
25
type: "app",
36
app: "neetokb",
47
propDefinitions: {},
58
methods: {
6-
// this.$auth contains connected account data
7-
authKeys() {
8-
console.log(Object.keys(this.$auth));
9+
_apiUrl() {
10+
return `https://${this.$auth.organization_name}.neetokb.com/api/v1`;
11+
},
12+
_getHeaders() {
13+
return {
14+
"Content-Type": "application/json",
15+
"X-Api-Key": `${this.$auth.api_key}`,
16+
};
17+
},
18+
_makeRequest({
19+
$ = this, path, ...opts
20+
}) {
21+
return axios($, {
22+
url: `${this._apiUrl()}/${path}`,
23+
headers: this._getHeaders(),
24+
...opts,
25+
});
26+
},
27+
listArticles(args = {}) {
28+
return this._makeRequest({
29+
path: "articles",
30+
...args,
31+
});
32+
},
33+
async *paginate({
34+
fn, params = {}, maxResults = null, ...opts
35+
}) {
36+
let hasMore = false;
37+
let count = 0;
38+
let page = 0;
39+
40+
do {
41+
params.page_number = ++page;
42+
params.page_size = LIMIT;
43+
const {
44+
articles,
45+
pagination: {
46+
total_pages: tPages, current_page_number: cPage,
47+
},
48+
} = await fn({
49+
params,
50+
...opts,
51+
});
52+
for (const d of articles) {
53+
yield d;
54+
55+
if (maxResults && ++count === maxResults) {
56+
return count;
57+
}
58+
}
59+
60+
hasMore = cPage < tPages;
61+
62+
} while (hasMore);
963
},
1064
},
1165
};

components/neetokb/package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@pipedream/neetokb",
3-
"version": "0.0.2",
3+
"version": "0.1.0",
44
"description": "Pipedream neetoKB Components",
55
"main": "neetokb.app.mjs",
66
"keywords": [
@@ -11,5 +11,8 @@
1111
"author": "Pipedream <support@pipedream.com> (https://pipedream.com/)",
1212
"publishConfig": {
1313
"access": "public"
14+
},
15+
"dependencies": {
16+
"@pipedream/platform": "^3.1.1"
1417
}
1518
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
import { DEFAULT_POLLING_SOURCE_TIMER_INTERVAL } from "@pipedream/platform";
2+
import neetokb from "../../neetokb.app.mjs";
3+
import sampleEmit from "./test-event.mjs";
4+
5+
export default {
6+
key: "neetokb-new-published-article",
7+
name: "New Published Article",
8+
description: "Emit new event when a new article is published. [See the documentation](https://neetokb-apis.mintlify.app/api-reference/articles/list-articles).",
9+
version: "0.0.1",
10+
type: "source",
11+
dedupe: "unique",
12+
props: {
13+
neetokb,
14+
db: "$.service.db",
15+
timer: {
16+
type: "$.interface.timer",
17+
default: {
18+
intervalSeconds: DEFAULT_POLLING_SOURCE_TIMER_INTERVAL,
19+
},
20+
},
21+
},
22+
methods: {
23+
_getPublishedIds() {
24+
return this.db.get("publishedIds") || [];
25+
},
26+
_setPublishedIds(publishedIds) {
27+
this.db.set("publishedIds", publishedIds);
28+
},
29+
async emitEvent(maxResults = false) {
30+
const publishedIds = this._getPublishedIds();
31+
const response = this.neetokb.paginate({
32+
fn: this.neetokb.listArticles,
33+
params: {
34+
status: "published",
35+
},
36+
});
37+
38+
const responseArray = [];
39+
for await (const item of response) {
40+
responseArray.push(item);
41+
}
42+
43+
let newArticles = responseArray.filter((article) => !publishedIds.includes(article.id));
44+
45+
if (maxResults && newArticles.length > maxResults) {
46+
newArticles = newArticles.slice(0, maxResults);
47+
}
48+
49+
for (const article of newArticles.reverse()) {
50+
const ts = new Date();
51+
this.$emit(article, {
52+
id: `${article.id}-${ts}`,
53+
summary: `New Published Article: ${article.title}`,
54+
ts,
55+
});
56+
}
57+
this._setPublishedIds(responseArray.map((article) => article.id));
58+
},
59+
},
60+
hooks: {
61+
async deploy() {
62+
await this.emitEvent(25);
63+
},
64+
},
65+
async run() {
66+
await this.emitEvent();
67+
},
68+
sampleEmit,
69+
};
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
export default {
2+
"id": "0c8d8101-62cb-4227-9dd0-953fbf88e74d",
3+
"slug": "article-title",
4+
"title": "Article Title",
5+
"state": "published",
6+
"unique_views_count": 0,
7+
"is_archived": false,
8+
"category": {
9+
"id": "ea665eb3-23a1-4cbd-9dff-0a585d6c8c53",
10+
"name": "Getting Started"
11+
}
12+
}

pnpm-lock.yaml

Lines changed: 5 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)