@pipedream/postman 0.0.1 → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,86 @@
1
+ import postman from "../../postman.app.mjs";
2
+
3
+ export default {
4
+ key: "postman-create-environment",
5
+ name: "Create Environment",
6
+ description: "Creates a new environment in Postman. [See the documentation](https://learning.postman.com/docs/developer/postman-api/intro-api/)",
7
+ version: "0.0.1",
8
+ type: "action",
9
+ props: {
10
+ postman,
11
+ workspaceId: {
12
+ propDefinition: [
13
+ postman,
14
+ "workspaceId",
15
+ ],
16
+ optional: true,
17
+ },
18
+ environmentName: {
19
+ type: "string",
20
+ label: "New Environment Name",
21
+ description: "The name for the new environment",
22
+ },
23
+ variablesCount: {
24
+ type: "integer",
25
+ label: "Variables Quantity",
26
+ description: "The quantity of variables you want to create into the environment.",
27
+ reloadProps: true,
28
+ optional: true,
29
+ },
30
+ },
31
+ async additionalProps() {
32
+ const props = {};
33
+ if (this.variablesCount) {
34
+ for (var i = 1; i <= this.variablesCount; i++) {
35
+ props[`variableKey_${i}`] = {
36
+ type: "string",
37
+ label: `Variable Name ${i}`,
38
+ description: `The name for the variable ${i}`,
39
+ };
40
+ props[`variableValue_${i}`] = {
41
+ type: "string",
42
+ label: `Variable Value ${i}`,
43
+ description: `The value for the variable ${i}`,
44
+ };
45
+ props[`variableEnabled_${i}`] = {
46
+ type: "boolean",
47
+ label: `Variable Enabled ${i}`,
48
+ description: `Whether the variable ${i} is enabled or not.`,
49
+ };
50
+ props[`variableType_${i}`] = {
51
+ type: "string",
52
+ label: `Variable Type ${i}`,
53
+ description: `The type of the variable ${i}`,
54
+ options: [
55
+ "secret",
56
+ "default",
57
+ ],
58
+ };
59
+ }
60
+ }
61
+ return props;
62
+ },
63
+ async run({ $ }) {
64
+ const variables = [];
65
+ for (var i = 1; i <= this.variablesCount; i++) {
66
+ variables.push({
67
+ key: this[`variableKey_${i}`],
68
+ value: this[`variableValue_${i}`],
69
+ enabled: this[`variableEnabled_${i}`],
70
+ type: this[`variableType_${i}`],
71
+ });
72
+ }
73
+
74
+ const response = await this.postman.createEnvironment({
75
+ $,
76
+ data: {
77
+ environment: {
78
+ name: this.environmentName,
79
+ values: variables,
80
+ },
81
+ },
82
+ });
83
+ $.export("$summary", `Successfully created a new environment with ID: ${response.environment?.id}`);
84
+ return response;
85
+ },
86
+ };
@@ -0,0 +1,27 @@
1
+ import postman from "../../postman.app.mjs";
2
+
3
+ export default {
4
+ key: "postman-run-monitor",
5
+ name: "Run Monitor",
6
+ description: "Run a specific monitor in Postman. [See the documentation](https://learning.postman.com/docs/developer/postman-api/intro-api/)",
7
+ version: "0.0.1",
8
+ type: "action",
9
+ props: {
10
+ postman,
11
+ monitorId: {
12
+ propDefinition: [
13
+ postman,
14
+ "monitorId",
15
+ ],
16
+ },
17
+ },
18
+ async run({ $ }) {
19
+ const response = await this.postman.runMonitor({
20
+ $,
21
+ monitorId: this.monitorId,
22
+ });
23
+
24
+ $.export("$summary", `Successfully executed monitor with ID: ${this.monitorId}`);
25
+ return response;
26
+ },
27
+ };
@@ -0,0 +1,104 @@
1
+ import postman from "../../postman.app.mjs";
2
+
3
+ export default {
4
+ key: "postman-update-variable",
5
+ name: "Update Environment Variable",
6
+ description: "Updates a specific environment variable in Postman. [See the documentation](https://learning.postman.com/docs/developer/postman-api/intro-api/)",
7
+ version: "0.0.1",
8
+ type: "action",
9
+ props: {
10
+ postman,
11
+ workspaceId: {
12
+ propDefinition: [
13
+ postman,
14
+ "workspaceId",
15
+ ],
16
+ optional: true,
17
+ },
18
+ environmentId: {
19
+ propDefinition: [
20
+ postman,
21
+ "environmentId",
22
+ ({ workspaceId }) => ({
23
+ workspaceId,
24
+ }),
25
+ ],
26
+ },
27
+ variable: {
28
+ propDefinition: [
29
+ postman,
30
+ "variable",
31
+ ({ environmentId }) => ({
32
+ environmentId,
33
+ }),
34
+ ],
35
+ reloadProps: true,
36
+ },
37
+ },
38
+ async additionalProps() {
39
+ const props = {};
40
+ if (this.variable) {
41
+ const { environment: { values: variables } } = await this.postman.getEnvironment({
42
+ environmentId: this.environmentId,
43
+ });
44
+ const indexVar = variables.findIndex((variable) => variable.key === this.variable);
45
+ const variable = variables[indexVar];
46
+
47
+ props.variableValue = {
48
+ type: "string",
49
+ label: "Variable Value",
50
+ description: "The value for the variable",
51
+ };
52
+ props.variableEnabled = {
53
+ type: "boolean",
54
+ label: "Variable Enabled",
55
+ description: "Whether the variable is enabled or not.",
56
+ default: variable.enabled,
57
+ };
58
+ }
59
+ return props;
60
+ },
61
+ async run({ $ }) {
62
+ const {
63
+ postman,
64
+ environmentId,
65
+ } = this;
66
+
67
+ const { environment } = await postman.getEnvironment({
68
+ $,
69
+ environmentId,
70
+ });
71
+
72
+ const indexVar = environment.values.findIndex((variable) => variable.key === this.variable);
73
+ environment.values[indexVar] = {
74
+ ...environment.values[indexVar],
75
+ value: this.variableValue,
76
+ enabled: this.variableEnabled,
77
+ };
78
+
79
+ // This step is to delete all variables and create them again.
80
+ // Without this step, the postman will only update the variables` initial value.
81
+ await postman.updateEnvironment({
82
+ $,
83
+ environmentId,
84
+ data: {
85
+ environment: {
86
+ values: [],
87
+ },
88
+ },
89
+ });
90
+
91
+ const response = await postman.updateEnvironment({
92
+ $,
93
+ environmentId,
94
+ data: {
95
+ environment: {
96
+ values: environment.values,
97
+ },
98
+ },
99
+ });
100
+
101
+ $.export("$summary", `Successfully updated the variable "${this.variable}" in environment ID ${this.environmentId}`);
102
+ return response;
103
+ },
104
+ };
package/package.json CHANGED
@@ -1,18 +1,18 @@
1
1
  {
2
2
  "name": "@pipedream/postman",
3
- "version": "0.0.1",
3
+ "version": "0.1.0",
4
4
  "description": "Pipedream Postman Components",
5
- "main": "dist/app/postman.app.mjs",
5
+ "main": "postman.app.mjs",
6
6
  "keywords": [
7
7
  "pipedream",
8
8
  "postman"
9
9
  ],
10
- "files": [
11
- "dist"
12
- ],
13
10
  "homepage": "https://pipedream.com/apps/postman",
14
11
  "author": "Pipedream <support@pipedream.com> (https://pipedream.com/)",
15
12
  "publishConfig": {
16
13
  "access": "public"
14
+ },
15
+ "dependencies": {
16
+ "@pipedream/platform": "^1.5.1"
17
17
  }
18
18
  }
@@ -0,0 +1,139 @@
1
+ import { axios } from "@pipedream/platform";
2
+
3
+ export default {
4
+ type: "app",
5
+ app: "postman",
6
+ propDefinitions: {
7
+ environmentId: {
8
+ type: "string",
9
+ label: "Environment ID",
10
+ description: "The ID of the environment to be used.",
11
+ async options({ workspaceId }) {
12
+ const { environments } = await this.listEnvironments({
13
+ params: {
14
+ workspace: workspaceId,
15
+ },
16
+ });
17
+ return environments.map(({
18
+ id: value, name: label,
19
+ }) => ({
20
+ label,
21
+ value,
22
+ }));
23
+ },
24
+ },
25
+ monitorId: {
26
+ type: "string",
27
+ label: "Monitor ID",
28
+ description: "The ID of the monitor to run.",
29
+ async options() {
30
+ const { monitors } = await this.listMonitors();
31
+ return monitors.map(({
32
+ id: value, name: label,
33
+ }) => ({
34
+ label,
35
+ value,
36
+ }));
37
+ },
38
+ },
39
+ variable: {
40
+ type: "string",
41
+ label: "Variable",
42
+ description: "The varriable to be updated.",
43
+ async options({ environmentId }) {
44
+ const { environment: { values: variables } } = await this.getEnvironment({
45
+ environmentId,
46
+ });
47
+
48
+ return variables.map(({ key }) => key);
49
+ },
50
+ },
51
+ workspaceId: {
52
+ type: "string",
53
+ label: "Workspace ID",
54
+ description: "The ID of the workspace to be used.",
55
+ async options() {
56
+ const { workspaces } = await this.listWorkspaces();
57
+ return workspaces.map(({
58
+ id: value, name: label,
59
+ }) => ({
60
+ label,
61
+ value,
62
+ }));
63
+ },
64
+ },
65
+ },
66
+ methods: {
67
+ _baseUrl() {
68
+ return "https://api.getpostman.com";
69
+ },
70
+ _headers() {
71
+ return {
72
+ "X-Api-Key": `${this.$auth.api_key}`,
73
+ "Accept": "application/vnd.api.v10+json",
74
+ };
75
+ },
76
+ _makeRequest({
77
+ $ = this, path, ...opts
78
+ }) {
79
+ return axios($, {
80
+ url: this._baseUrl() + path,
81
+ headers: this._headers(),
82
+ ...opts,
83
+ });
84
+ },
85
+ getEnvironment({
86
+ environmentId, ...opts
87
+ }) {
88
+ return this._makeRequest({
89
+ path: `/environments/${environmentId}`,
90
+ ...opts,
91
+ });
92
+ },
93
+ getMonitor({ monitorId }) {
94
+ return this._makeRequest({
95
+ path: `/monitors/${monitorId}`,
96
+ });
97
+ },
98
+ listEnvironments() {
99
+ return this._makeRequest({
100
+ path: "/environments",
101
+ });
102
+ },
103
+ listMonitors() {
104
+ return this._makeRequest({
105
+ path: "/monitors",
106
+ });
107
+ },
108
+ listWorkspaces() {
109
+ return this._makeRequest({
110
+ path: "/workspaces",
111
+ });
112
+ },
113
+ createEnvironment(opts = {}) {
114
+ return this._makeRequest({
115
+ method: "POST",
116
+ path: "/environments",
117
+ ...opts,
118
+ });
119
+ },
120
+ updateEnvironment({
121
+ environmentId, ...opts
122
+ }) {
123
+ return this._makeRequest({
124
+ method: "PUT",
125
+ path: `/environments/${environmentId}`,
126
+ ...opts,
127
+ });
128
+ },
129
+ runMonitor({
130
+ monitorId, ...opts
131
+ }) {
132
+ return this._makeRequest({
133
+ method: "POST",
134
+ path: `/monitors/${monitorId}/run`,
135
+ ...opts,
136
+ });
137
+ },
138
+ },
139
+ };
@@ -0,0 +1,58 @@
1
+ import postman from "../../postman.app.mjs";
2
+ import sampleEmit from "./test-event.mjs";
3
+
4
+ export default {
5
+ key: "postman-monitor-run-completed",
6
+ name: "New Monitor Run Completed",
7
+ description: "Emit new event when a monitor run is completed. [See the documentation](https://learning.postman.com/docs/monitoring-your-api/intro-monitors/)",
8
+ version: "0.0.1",
9
+ type: "source",
10
+ dedupe: "unique",
11
+ props: {
12
+ postman,
13
+ db: "$.service.db",
14
+ timer: {
15
+ type: "$.interface.timer",
16
+ default: {
17
+ intervalSeconds: 60,
18
+ },
19
+ },
20
+ monitorId: {
21
+ propDefinition: [
22
+ postman,
23
+ "monitorId",
24
+ ],
25
+ },
26
+ },
27
+ methods: {
28
+ _getLastDate() {
29
+ return this.db.get("lastDate") || new Date("1970-01-01");
30
+ },
31
+ _setLastDate(lastDate) {
32
+ this.db.set("lastDate", lastDate);
33
+ },
34
+ async startEvent() {
35
+ const lastDate = this._getLastDate();
36
+
37
+ const { monitor } = await this.postman.getMonitor({
38
+ monitorId: this.monitorId,
39
+ });
40
+
41
+ if (monitor.lastRun?.status === "success" && new Date(monitor.lastRun.startedAt) > new Date(lastDate)) {
42
+ this._setLastDate(monitor.lastRun.startedAt);
43
+ this.emitEvent(monitor);
44
+ }
45
+ },
46
+ emitEvent(monitor) {
47
+ this.$emit(monitor, {
48
+ id: monitor.id + monitor.lastRun.startedAt,
49
+ summary: `Monitor Run Completed: ${monitor.name}`,
50
+ ts: Date.parse(monitor.lastRun.finishedAt),
51
+ });
52
+ },
53
+ },
54
+ async run() {
55
+ await this.startEvent();
56
+ },
57
+ sampleEmit,
58
+ };
@@ -0,0 +1,46 @@
1
+ export default {
2
+ "id": "1e6b6cc1-c760-48e0-968f-4bfaeeae9af1",
3
+ "name": "Test Monitor",
4
+ "uid": "12345678-1e6b6cc1-c760-48e0-968f-4bfaeeae9af1",
5
+ "owner": 12345678,
6
+ "collectionUid": "12345678-12ece9e1-2abf-4edc-8e34-de66e74114d2",
7
+ "environmentUid": "12345678-5daabc50-8451-43f6-922d-96b403b4f28e",
8
+ "options": {
9
+ "strictSSL": true,
10
+ "followRedirects": true,
11
+ "requestTimeout": 3000,
12
+ "requestDelay": 0
13
+ },
14
+ "notifications": {
15
+ "onError": [
16
+ {
17
+ "email": "user@example.com"
18
+ }
19
+ ],
20
+ "onFailure": [
21
+ {
22
+ "email": "user@example.com"
23
+ }
24
+ ]
25
+ },
26
+ "distribution": [],
27
+ "schedule": {
28
+ "cron": "0 0 * * * *",
29
+ "timezone": "America/Chicago",
30
+ "nextRun": "2022-06-18T05:00:00.000Z"
31
+ },
32
+ "lastRun": {
33
+ "status": "failed",
34
+ "startedAt": "2022-06-17T18:39:52.852Z",
35
+ "finishedAt": "2022-06-17T18:39:53.707Z"
36
+ },
37
+ "stats": {
38
+ "assertions": {
39
+ "total": 8,
40
+ "failed": 1
41
+ },
42
+ "requests": {
43
+ "total": 4
44
+ }
45
+ }
46
+ }