Skip to content

Commit b736fc0

Browse files
committed
[Components] pencil_spaces #17478
Actions - Create Space
1 parent 65d964b commit b736fc0

File tree

5 files changed

+237
-7
lines changed

5 files changed

+237
-7
lines changed
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
import { VISIBILITY_OPTIONS } from "../../common/constants.mjs";
2+
import { parseObject } from "../../common/utils.mjs";
3+
import pencilSpaces from "../../pencil_spaces.app.mjs";
4+
5+
export default {
6+
key: "pencil_spaces-create-space",
7+
name: "Create A Space",
8+
description: "Create a new space in Pencil Spaces. [See the documentation](https://api.pencilspaces.com/reference#tag/spaces/POST/spaces/create)",
9+
version: "0.0.1",
10+
type: "action",
11+
props: {
12+
pencilSpaces,
13+
title: {
14+
type: "string",
15+
label: "Title",
16+
description: "The title of the Space. If not provided and `Space To Clone Id` is set, the existing Space name will be used. If not, a random Space name will be generated.",
17+
optional: true,
18+
},
19+
spaceToCloneId: {
20+
propDefinition: [
21+
pencilSpaces,
22+
"spaceToCloneId",
23+
],
24+
optional: true,
25+
},
26+
ownerId: {
27+
propDefinition: [
28+
pencilSpaces,
29+
"ownerId",
30+
],
31+
optional: true,
32+
},
33+
hostIds: {
34+
propDefinition: [
35+
pencilSpaces,
36+
"ownerId",
37+
],
38+
type: "string[]",
39+
label: "Host Ids",
40+
description: "The hosts you wish to invite to the Space. The user associated with your API key will always be added as a host.",
41+
optional: true,
42+
},
43+
participantIds: {
44+
propDefinition: [
45+
pencilSpaces,
46+
"ownerId",
47+
],
48+
type: "string[]",
49+
label: "Participant Ids",
50+
description: "The participants you wish to invite to the Space.",
51+
optional: true,
52+
},
53+
visibility: {
54+
type: "string",
55+
label: "Visibility",
56+
description: "The visibility of the Space.",
57+
options: VISIBILITY_OPTIONS,
58+
optional: true,
59+
},
60+
notifyInvitees: {
61+
type: "boolean",
62+
label: "Notify Invitees",
63+
description: "Whether you want to automatically notify invitees when you create your Space.",
64+
},
65+
siteId: {
66+
propDefinition: [
67+
pencilSpaces,
68+
"siteId",
69+
],
70+
optional: true,
71+
},
72+
},
73+
async run({ $ }) {
74+
const response = await this.pencilSpaces.createSpace({
75+
$,
76+
data: {
77+
title: this.title,
78+
spaceToCloneId: this.spaceToCloneId,
79+
ownerId: {
80+
userId: this.ownerId,
81+
},
82+
hostIds: parseObject(this.hostIds)?.map((hostId) => ({
83+
userId: hostId,
84+
})),
85+
participantIds: parseObject(this.participantIds)?.map((participantId) => ({
86+
userId: participantId,
87+
})),
88+
visibility: this.visibility,
89+
notifyInvitees: this.notifyInvitees,
90+
siteId: this.siteId,
91+
},
92+
});
93+
94+
$.export("$summary", `Successfully created space with ID: "${response.spaceId}"`);
95+
return response;
96+
},
97+
};
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
export const VISIBILITY_OPTIONS = [
2+
{
3+
label: "Public",
4+
value: "public",
5+
},
6+
{
7+
label: "Private",
8+
value: "private",
9+
},
10+
];
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
export const parseObject = (obj) => {
2+
if (!obj) return undefined;
3+
4+
if (Array.isArray(obj)) {
5+
return obj.map((item) => {
6+
if (typeof item === "string") {
7+
try {
8+
return JSON.parse(item);
9+
} catch (e) {
10+
return item;
11+
}
12+
}
13+
return item;
14+
});
15+
}
16+
if (typeof obj === "string") {
17+
try {
18+
return JSON.parse(obj);
19+
} catch (e) {
20+
return obj;
21+
}
22+
}
23+
return obj;
24+
};

components/pencil_spaces/package.json

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@pipedream/pencil_spaces",
3-
"version": "0.0.1",
3+
"version": "0.1.0",
44
"description": "Pipedream Pencil Spaces Components",
55
"main": "pencil_spaces.app.mjs",
66
"keywords": [
@@ -11,5 +11,8 @@
1111
"author": "Pipedream <[email protected]> (https://pipedream.com/)",
1212
"publishConfig": {
1313
"access": "public"
14+
},
15+
"dependencies": {
16+
"@pipedream/platform": "^3.1.0"
1417
}
15-
}
18+
}
Lines changed: 101 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,107 @@
1+
import { axios } from "@pipedream/platform";
2+
13
export default {
24
type: "app",
35
app: "pencil_spaces",
4-
propDefinitions: {},
6+
propDefinitions: {
7+
spaceToCloneId: {
8+
type: "string",
9+
label: "Space To Clone Id",
10+
description: "The space ID of the Space you want to clone. You may only clone Spaces that are templates, Spaces for which you are a host, and Spaces which you can access as an admin due to your institution settings.",
11+
async options({ page }) {
12+
const { results } = await this.listSpaces({
13+
params: {
14+
pageNumber: page + 1,
15+
},
16+
});
17+
18+
return results.map(({
19+
spaceId: value, title: label,
20+
}) => ({
21+
label,
22+
value,
23+
}));
24+
},
25+
},
26+
ownerId: {
27+
type: "string",
28+
label: "Owner Id",
29+
description: "The ID of the user who will be the owner of the space. If not provided, the current user will be the owner.",
30+
async options({ page }) {
31+
const { results } = await this.listUsers({
32+
params: {
33+
pageNumber: page + 1,
34+
},
35+
});
36+
37+
return results.map(({
38+
userId: value, email: label,
39+
}) => ({
40+
label,
41+
value,
42+
}));
43+
},
44+
},
45+
siteId: {
46+
type: "string",
47+
label: "Site Id",
48+
description: "The ID of the site you want to create the Space in.",
49+
async options() {
50+
const { results } = await this.listSites();
51+
52+
return results.map(({
53+
id: value, name: label,
54+
}) => ({
55+
label,
56+
value,
57+
}));
58+
},
59+
},
60+
},
561
methods: {
6-
// this.$auth contains connected account data
7-
authKeys() {
8-
console.log(Object.keys(this.$auth));
62+
_baseUrl() {
63+
return this.$auth.api_url;
64+
},
65+
_headers(headers = {}) {
66+
return {
67+
...headers,
68+
"Authorization": `Bearer ${this.$auth.api_key}`,
69+
"Content-Type": "application/json",
70+
};
71+
},
72+
async _makeRequest({
73+
$ = this, path, headers, ...opts
74+
}) {
75+
return axios($, {
76+
url: `${this._baseUrl()}${path}`,
77+
headers: this._headers(headers),
78+
...opts,
79+
});
80+
},
81+
listSpaces(opts = {}) {
82+
return this._makeRequest({
83+
path: "spaces",
84+
...opts,
85+
});
86+
},
87+
listUsers(opts = {}) {
88+
return this._makeRequest({
89+
path: "users",
90+
...opts,
91+
});
92+
},
93+
listSites(opts = {}) {
94+
return this._makeRequest({
95+
path: "sites",
96+
...opts,
97+
});
98+
},
99+
createSpace(opts = {}) {
100+
return this._makeRequest({
101+
method: "POST",
102+
path: "spaces/create",
103+
...opts,
104+
});
9105
},
10106
},
11-
};
107+
};

0 commit comments

Comments
 (0)