|
| 1 | +import { defineApp } from "@pipedream/types"; |
| 2 | +import { axios } from "@pipedream/platform"; |
| 3 | + |
| 4 | +export default defineApp({ |
| 5 | + type: "app", |
| 6 | + app: "serveravatar", |
| 7 | + propDefinitions: {}, |
| 8 | + methods: { |
| 9 | + _getBaseUrl() { |
| 10 | + return "https://api.serveravatar.com"; |
| 11 | + }, |
| 12 | + _getHeaders() { |
| 13 | + return { |
| 14 | + "content-type": "application/json", |
| 15 | + "authorization": `${this.$auth.api_key}`, |
| 16 | + }; |
| 17 | + }, |
| 18 | + _getRequestParams(opts: any) { |
| 19 | + return { |
| 20 | + ...opts, |
| 21 | + url: this._getBaseUrl() + opts.path, |
| 22 | + headers: this._getHeaders(), |
| 23 | + }; |
| 24 | + }, |
| 25 | + async createApplicationDomain(ctx = this, newAppDomainData: any) { |
| 26 | + return await axios(ctx, this._getRequestParams({ |
| 27 | + method: "POST", |
| 28 | + path: "/domains", |
| 29 | + data: newAppDomainData, |
| 30 | + })); |
| 31 | + }, |
| 32 | + async listTeams() { |
| 33 | + const teamsResponse = await axios(this, this._getRequestParams({ |
| 34 | + method: "GET", |
| 35 | + path: "/teams", |
| 36 | + })); |
| 37 | + return teamsResponse.teams; |
| 38 | + }, |
| 39 | + async listServers(teams: any[]) { |
| 40 | + const teamServersPromise = teams.map((team: any) => { |
| 41 | + return axios(this, this._getRequestParams({ |
| 42 | + method: "GET", |
| 43 | + path: `/teams/${team.id}/servers`, |
| 44 | + })); |
| 45 | + }); |
| 46 | + const teamsServers = await Promise.all(teamServersPromise); |
| 47 | + return teamsServers |
| 48 | + .map((teamServers: any) => (teamServers.servers)) |
| 49 | + .flat() |
| 50 | + .filter((server: any) => server.status === "1"); |
| 51 | + }, |
| 52 | + async listAllServersOptions() { |
| 53 | + const teams = await this.listTeams(); |
| 54 | + const servers = await this.listServers(teams); |
| 55 | + return servers.map((server: any) => ({ |
| 56 | + label: `${server.name} - ${server.ip}`, |
| 57 | + value: server.id, |
| 58 | + })); |
| 59 | + }, |
| 60 | + async listApplications(serverId: number, page: number) { |
| 61 | + const serverApplications = await axios(this, this._getRequestParams({ |
| 62 | + method: "GET", |
| 63 | + path: `/servers/${serverId}/applications?page=${page}`, |
| 64 | + })); |
| 65 | + return serverApplications?.applications; |
| 66 | + }, |
| 67 | + async listApplicationsOptions(serverId: number, prevContext: any) { |
| 68 | + if (!serverId) { |
| 69 | + return []; |
| 70 | + } |
| 71 | + const page = prevContext.page |
| 72 | + ? prevContext.page + 1 |
| 73 | + : 1; |
| 74 | + const applications = await this.listApplications(serverId, page); |
| 75 | + const options = applications.data.map((application: any) => ({ |
| 76 | + label: `${application.name} - ${application.framework}`, |
| 77 | + value: application.id, |
| 78 | + })); |
| 79 | + return { |
| 80 | + options, |
| 81 | + context: { |
| 82 | + page: applications.current_page, |
| 83 | + }, |
| 84 | + }; |
| 85 | + }, |
| 86 | + }, |
| 87 | +}); |
0 commit comments