-
Notifications
You must be signed in to change notification settings - Fork 5.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* new component * pnpm-lock.yaml * Update components/formcarry/package.json Co-authored-by: Luan Cazarine <[email protected]> --------- Co-authored-by: Luan Cazarine <[email protected]>
- Loading branch information
1 parent
653b81d
commit 792d115
Showing
4 changed files
with
162 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,63 @@ | ||
import { axios } from "@pipedream/platform"; | ||
|
||
export default { | ||
type: "app", | ||
app: "formcarry", | ||
propDefinitions: {}, | ||
methods: { | ||
// this.$auth contains connected account data | ||
authKeys() { | ||
console.log(Object.keys(this.$auth)); | ||
_baseUrl() { | ||
return "https://formcarry.com/api"; | ||
}, | ||
_headers() { | ||
return { | ||
api_key: `${this.$auth.api_key}`, | ||
}; | ||
}, | ||
_makeRequest({ | ||
$ = this, | ||
path, | ||
...opts | ||
}) { | ||
return axios($, { | ||
url: `${this._baseUrl()}${path}`, | ||
headers: this._headers(), | ||
...opts, | ||
}); | ||
}, | ||
listSubmissions({ | ||
formId, ...opts | ||
}) { | ||
return this._makeRequest({ | ||
path: `/form/${formId}/submissions`, | ||
...opts, | ||
}); | ||
}, | ||
async *paginate({ | ||
fn, | ||
args = {}, | ||
resourceKey, | ||
max, | ||
}) { | ||
args = { | ||
...args, | ||
params: { | ||
...args?.params, | ||
limit: 50, | ||
page: 1, | ||
}, | ||
}; | ||
let nextPage, count = 0; | ||
do { | ||
const response = await fn(args); | ||
const items = response[resourceKey]; | ||
for (const item of items) { | ||
yield item; | ||
if (max && ++count >= max) { | ||
return; | ||
} | ||
} | ||
nextPage = response.pagination.next_page; | ||
args.params.page = nextPage; | ||
} while (nextPage); | ||
}, | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
{ | ||
"name": "@pipedream/formcarry", | ||
"version": "0.1.0", | ||
"description": "Pipedream Formcarry Components", | ||
"main": "formcarry.app.mjs", | ||
"keywords": [ | ||
"pipedream", | ||
"formcarry" | ||
], | ||
"homepage": "https://pipedream.com/apps/formcarry", | ||
"author": "Pipedream <[email protected]> (https://pipedream.com/)", | ||
"publishConfig": { | ||
"access": "public" | ||
}, | ||
"dependencies": { | ||
"@pipedream/platform": "^3.0.3" | ||
} | ||
} |
82 changes: 82 additions & 0 deletions
82
components/formcarry/sources/new-form-submission/new-form-submission.mjs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
import formcarry from "../../formcarry.app.mjs"; | ||
import { DEFAULT_POLLING_SOURCE_TIMER_INTERVAL } from "@pipedream/platform"; | ||
|
||
export default { | ||
key: "formcarry-new-form-submission", | ||
name: "New Form Submission", | ||
description: "Emit new event when the specified form receives a new submission. [See the documentation](https://formcarry.com/docs/formcarry-api/submissions-api#cc7f3010897b4c938c8829db46b18656)", | ||
version: "0.0.1", | ||
type: "source", | ||
dedupe: "unique", | ||
props: { | ||
formcarry, | ||
db: "$.service.db", | ||
timer: { | ||
type: "$.interface.timer", | ||
default: { | ||
intervalSeconds: DEFAULT_POLLING_SOURCE_TIMER_INTERVAL, | ||
}, | ||
}, | ||
formId: { | ||
type: "string", | ||
label: "Form ID", | ||
description: "The ID of the form to watch for new submissions", | ||
}, | ||
}, | ||
hooks: { | ||
async deploy() { | ||
await this.processEvent(25); | ||
}, | ||
}, | ||
methods: { | ||
_getLastTs() { | ||
return this.db.get("lastTs") || 0; | ||
}, | ||
_setLastTs(lastTs) { | ||
this.db.set("lastTs", lastTs); | ||
}, | ||
generateMeta(submission) { | ||
return { | ||
id: submission._id, | ||
summary: `New Form Submission ID: ${submission._id}`, | ||
ts: Date.parse(submission.createdAt), | ||
}; | ||
}, | ||
async processEvent(max) { | ||
const lastTs = this._getLastTs(); | ||
|
||
const results = this.formcarry.paginate({ | ||
fn: this.formcarry.listSubmissions, | ||
args: { | ||
formId: this.formId, | ||
}, | ||
resourceKey: "submissions", | ||
max, | ||
}); | ||
|
||
const submissions = []; | ||
for await (const item of results) { | ||
const ts = Date.parse(item.createdAt); | ||
if (ts >= lastTs) { | ||
submissions.push(item); | ||
} else { | ||
break; | ||
} | ||
} | ||
|
||
if (!submissions.length) { | ||
return; | ||
} | ||
|
||
this._setLastTs(Date.parse(submissions[0].createdAt)); | ||
|
||
submissions.forEach((submission) => { | ||
const meta = this.generateMeta(submission); | ||
this.$emit(submission, meta); | ||
}); | ||
}, | ||
}, | ||
async run() { | ||
await this.processEvent(); | ||
}, | ||
}; |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.