diff --git a/components/slab/actions/get-posts/get-posts.mjs b/components/slab/actions/get-posts/get-posts.mjs new file mode 100644 index 0000000000000..2091c568784f4 --- /dev/null +++ b/components/slab/actions/get-posts/get-posts.mjs @@ -0,0 +1,72 @@ +import slab from "../../slab.app.mjs"; + +export default { + key: "slab-get-posts", + name: "Get Posts", + description: "Get posts by ID. See [documentation](https://studio.apollographql.com/public/Slab/variant/current/home), [schema link](https://studio.apollographql.com/public/Slab/variant/current/explorer?searchQuery=RootQueryType.posts)", + version: "0.0.1", + type: "action", + annotations: { + destructiveHint: false, + openWorldHint: true, + readOnlyHint: true, + }, + props: { + slab, + postIds: { + type: "string[]", + label: "Post IDs", + description: "Select one or more posts to retrieve", + options: async function({ prevContext }) { + return this.slab.listPostsForOptions({ + cursor: prevContext?.cursor, + }); + }, + }, + }, + async run({ $ }) { + const query = ` + query GetPosts($ids: [ID!]!) { + posts(ids: $ids) { + id + title + linkAccess + archivedAt + publishedAt + insertedAt + updatedAt + version + content + banner { + original + thumb + preset + } + owner { + id + name + email + } + topics { + id + name + } + } + } + `; + const variables = { + ids: this.postIds, + }; + const response = await this.slab._makeRequest({ + $, + data: { + query, + variables, + }, + }); + const posts = response.posts || []; + $.export("$summary", `Successfully retrieved ${posts.length} post(s)`); + return posts; + }, +}; + diff --git a/components/slab/actions/list-posts/list-posts.mjs b/components/slab/actions/list-posts/list-posts.mjs new file mode 100644 index 0000000000000..07c53e8ad7ab9 --- /dev/null +++ b/components/slab/actions/list-posts/list-posts.mjs @@ -0,0 +1,79 @@ +import slab from "../../slab.app.mjs"; +import { LIST_POSTS_QUERY } from "../../common/queries.mjs"; + +export default { + key: "slab-list-posts", + name: "List Posts", + description: "List posts in the organization. See [documentation](https://studio.apollographql.com/public/Slab/variant/current/home), [schema link](https://studio.apollographql.com/public/Slab/variant/current/explorer?searchQuery=RootQueryType.search)", + version: "0.0.1", + annotations: { + destructiveHint: false, + openWorldHint: true, + readOnlyHint: true, + }, + type: "action", + props: { + slab, + first: { + propDefinition: [ + slab, + "first", + ], + }, + after: { + propDefinition: [ + slab, + "after", + ], + }, + last: { + propDefinition: [ + slab, + "last", + ], + }, + before: { + propDefinition: [ + slab, + "before", + ], + }, + }, + async run({ $ }) { + const query = LIST_POSTS_QUERY; + const variables = { + query: "", + ...(this.first && { + first: parseInt(this.first), + }), + ...(this.after && { + after: this.after, + }), + ...(this.last && { + last: parseInt(this.last), + }), + ...(this.before && { + before: this.before, + }), + }; + const response = await this.slab._makeRequest({ + $, + data: { + query, + variables, + }, + }); + const edges = response.search?.edges || []; + const posts = edges + .map((edge) => edge.node?.post) + .filter(Boolean); + const pageInfo = response.search?.pageInfo || {}; + $.export("$summary", `Successfully retrieved ${posts.length} post(s)`); + return { + posts, + pageInfo, + edges, + }; + }, +}; + diff --git a/components/slab/actions/search-posts/search-posts.mjs b/components/slab/actions/search-posts/search-posts.mjs new file mode 100644 index 0000000000000..190395f689092 --- /dev/null +++ b/components/slab/actions/search-posts/search-posts.mjs @@ -0,0 +1,84 @@ +import slab from "../../slab.app.mjs"; +import { SEARCH_POSTS_QUERY } from "../../common/queries.mjs"; + +export default { + key: "slab-search-posts", + name: "Search Posts", + description: "Search for posts based on query. See [documentation](https://studio.apollographql.com/public/Slab/variant/current/home), [schema link](https://studio.apollographql.com/public/Slab/variant/current/explorer?searchQuery=RootQueryType.search)", + version: "0.0.1", + annotations: { + destructiveHint: false, + openWorldHint: true, + readOnlyHint: true, + }, + type: "action", + props: { + slab, + query: { + type: "string", + label: "Query", + description: "Search query string", + }, + first: { + propDefinition: [ + slab, + "first", + ], + }, + after: { + propDefinition: [ + slab, + "after", + ], + }, + last: { + propDefinition: [ + slab, + "last", + ], + }, + before: { + propDefinition: [ + slab, + "before", + ], + }, + }, + async run({ $ }) { + const query = SEARCH_POSTS_QUERY; + const variables = { + query: this.query, + ...(this.first && { + first: parseInt(this.first), + }), + ...(this.after && { + after: this.after, + }), + ...(this.last && { + last: parseInt(this.last), + }), + ...(this.before && { + before: this.before, + }), + }; + const response = await this.slab._makeRequest({ + $, + data: { + query, + variables, + }, + }); + const edges = response.search?.edges || []; + const posts = edges + .map((edge) => edge.node?.post) + .filter(Boolean); + const pageInfo = response.search?.pageInfo || {}; + $.export("$summary", `Successfully found ${posts.length} post(s) matching "${this.query}"`); + return { + posts, + pageInfo, + edges, + }; + }, +}; + diff --git a/components/slab/common/queries.mjs b/components/slab/common/queries.mjs new file mode 100644 index 0000000000000..89d4a7dfa144c --- /dev/null +++ b/components/slab/common/queries.mjs @@ -0,0 +1,96 @@ +export const SEARCH_POSTS_QUERY = ` + query SearchPosts($query: String!, $first: Int, $after: String, $last: Int, $before: String) { + search(query: $query, types: [POST], first: $first, after: $after, last: $last, before: $before) { + edges { + node { + ... on PostSearchResult { + title + highlight + content + post { + id + title + linkAccess + archivedAt + publishedAt + insertedAt + updatedAt + version + content + banner { + original + thumb + preset + } + owner { + id + name + email + } + topics { + id + name + } + } + } + } + cursor + } + pageInfo { + hasNextPage + hasPreviousPage + startCursor + endCursor + } + } + } +`; + +export const LIST_POSTS_QUERY = ` + query ListPosts($query: String!, $first: Int, $after: String, $last: Int, $before: String) { + search(query: $query, types: [POST], first: $first, after: $after, last: $last, before: $before) { + edges { + node { + ... on PostSearchResult { + title + highlight + content + post { + id + title + linkAccess + archivedAt + publishedAt + insertedAt + updatedAt + version + content + banner { + original + thumb + preset + } + owner { + id + name + email + } + topics { + id + name + } + } + } + } + cursor + } + pageInfo { + hasNextPage + hasPreviousPage + startCursor + endCursor + } + } + } +`; + diff --git a/components/slab/package.json b/components/slab/package.json index 9f5cd3a7f748a..84dcd679ca10b 100644 --- a/components/slab/package.json +++ b/components/slab/package.json @@ -1,6 +1,6 @@ { "name": "@pipedream/slab", - "version": "0.0.1", + "version": "0.1.0", "description": "Pipedream Slab Components", "main": "slab.app.mjs", "keywords": [ @@ -11,5 +11,8 @@ "author": "Pipedream (https://pipedream.com/)", "publishConfig": { "access": "public" + }, + "dependencies": { + "@pipedream/platform": "^3.1.0" } -} \ No newline at end of file +} diff --git a/components/slab/slab.app.mjs b/components/slab/slab.app.mjs index 6c0ed067b2546..60b509efcf539 100644 --- a/components/slab/slab.app.mjs +++ b/components/slab/slab.app.mjs @@ -1,11 +1,110 @@ +import { axios } from "@pipedream/platform"; + export default { type: "app", app: "slab", - propDefinitions: {}, + propDefinitions: { + first: { + type: "string", + label: "First", + description: "Number of items to retrieve when paginating forwards", + optional: true, + }, + after: { + type: "string", + label: "After", + description: "Cursor to continue pagination forwards", + optional: true, + }, + last: { + type: "string", + label: "Last", + description: "Number of items to retrieve when paginating backwards", + optional: true, + }, + before: { + type: "string", + label: "Before", + description: "Cursor to continue pagination backwards", + optional: true, + }, + }, methods: { - // this.$auth contains connected account data - authKeys() { - console.log(Object.keys(this.$auth)); + _baseUrl() { + return "https://api.slab.com/v1/graphql"; + }, + _getHeaders() { + return { + "Content-Type": "application/json", + "authorization": `${this.$auth.api_token}`, + }; + }, + async _makeRequest({ + $ = this, + ...opts + }) { + const config = { + method: "POST", + url: this._baseUrl(), + headers: this._getHeaders(), + ...opts, + }; + const response = await axios($, config); + if (response.data?.errors?.length) { + throw new Error(`GraphQL Error: ${response.data.errors[0].message}`); + } + return response.data || response; + }, + async listPostsForOptions({ cursor }) { + const query = ` + query ListPosts($after: String, $first: Int) { + search(query: "", types: [POST], after: $after, first: $first) { + edges { + node { + ... on PostSearchResult { + post { + id + title + } + } + } + } + pageInfo { + hasNextPage + endCursor + } + } + } + `; + const variables = { + first: 20, + ...(cursor && { + after: cursor, + }), + }; + const response = await this._makeRequest({ + data: { + query, + variables, + }, + }); + const edges = response.search?.edges || []; + const options = edges + .map((edge) => edge.node?.post) + .filter(Boolean) + .map((post) => ({ + label: post.title, + value: post.id, + })); + const pageInfo = response.search?.pageInfo || {}; + return { + options, + context: { + cursor: pageInfo.hasNextPage + ? pageInfo.endCursor + : undefined, + }, + }; }, }, -}; \ No newline at end of file +}; diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 06bf388df5ecd..eb0aeb2d3c925 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -13851,7 +13851,11 @@ importers: specifier: ^3.1.1 version: 3.1.1 - components/slab: {} + components/slab: + dependencies: + '@pipedream/platform': + specifier: ^3.1.0 + version: 3.1.1 components/slack: dependencies: