@pipedream/unirateapi 0.0.1 → 0.2.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.
- package/actions/convert-currency/convert-currency.mjs +57 -0
- package/actions/get-exchange-rates/get-exchange-rates.mjs +58 -0
- package/actions/list-currency-code-options/list-currency-code-options.mjs +24 -0
- package/package.json +4 -1
- package/sources/new-rate-update/new-rate-update.mjs +95 -0
- package/unirateapi.app.mjs +71 -5
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import app from "../../unirateapi.app.mjs";
|
|
2
|
+
|
|
3
|
+
export default {
|
|
4
|
+
key: "unirateapi-convert-currency",
|
|
5
|
+
name: "Convert Currency",
|
|
6
|
+
version: "0.0.1",
|
|
7
|
+
annotations: {
|
|
8
|
+
destructiveHint: false,
|
|
9
|
+
openWorldHint: true,
|
|
10
|
+
readOnlyHint: true,
|
|
11
|
+
},
|
|
12
|
+
description: "Convert an amount from one currency to another at the latest rate. [See the documentation](https://unirateapi.com/docs).",
|
|
13
|
+
type: "action",
|
|
14
|
+
props: {
|
|
15
|
+
app,
|
|
16
|
+
from: {
|
|
17
|
+
propDefinition: [
|
|
18
|
+
app,
|
|
19
|
+
"currencyCode",
|
|
20
|
+
],
|
|
21
|
+
label: "From",
|
|
22
|
+
description: "The source currency code.",
|
|
23
|
+
},
|
|
24
|
+
to: {
|
|
25
|
+
propDefinition: [
|
|
26
|
+
app,
|
|
27
|
+
"currencyCode",
|
|
28
|
+
],
|
|
29
|
+
label: "To",
|
|
30
|
+
description: "The target currency code.",
|
|
31
|
+
},
|
|
32
|
+
amount: {
|
|
33
|
+
type: "string",
|
|
34
|
+
label: "Amount",
|
|
35
|
+
description: "The amount to convert.",
|
|
36
|
+
default: "1",
|
|
37
|
+
},
|
|
38
|
+
},
|
|
39
|
+
async run({ $ }) {
|
|
40
|
+
const {
|
|
41
|
+
app, from, to, amount,
|
|
42
|
+
} = this;
|
|
43
|
+
|
|
44
|
+
const response = await app.convert({
|
|
45
|
+
$,
|
|
46
|
+
from,
|
|
47
|
+
to,
|
|
48
|
+
amount,
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
$.export(
|
|
52
|
+
"$summary",
|
|
53
|
+
`Converted ${amount} ${from} → ${response?.result ?? "?"} ${to}`,
|
|
54
|
+
);
|
|
55
|
+
return response;
|
|
56
|
+
},
|
|
57
|
+
};
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import app from "../../unirateapi.app.mjs";
|
|
2
|
+
|
|
3
|
+
export default {
|
|
4
|
+
key: "unirateapi-get-exchange-rates",
|
|
5
|
+
name: "Get Exchange Rates",
|
|
6
|
+
version: "0.0.1",
|
|
7
|
+
annotations: {
|
|
8
|
+
destructiveHint: false,
|
|
9
|
+
openWorldHint: true,
|
|
10
|
+
readOnlyHint: true,
|
|
11
|
+
},
|
|
12
|
+
description: "Get the latest exchange rate(s) for a base currency. Returns a single rate when a target is provided, or all rates keyed by currency code when omitted. [See the documentation](https://unirateapi.com/docs).",
|
|
13
|
+
type: "action",
|
|
14
|
+
props: {
|
|
15
|
+
app,
|
|
16
|
+
from: {
|
|
17
|
+
propDefinition: [
|
|
18
|
+
app,
|
|
19
|
+
"currencyCode",
|
|
20
|
+
],
|
|
21
|
+
label: "From",
|
|
22
|
+
description: "The base currency code. Defaults to `USD`.",
|
|
23
|
+
optional: true,
|
|
24
|
+
},
|
|
25
|
+
to: {
|
|
26
|
+
propDefinition: [
|
|
27
|
+
app,
|
|
28
|
+
"currencyCode",
|
|
29
|
+
],
|
|
30
|
+
label: "To",
|
|
31
|
+
description: "Optional target currency code. If omitted, all rates for the base currency are returned.",
|
|
32
|
+
optional: true,
|
|
33
|
+
},
|
|
34
|
+
},
|
|
35
|
+
async run({ $ }) {
|
|
36
|
+
const {
|
|
37
|
+
app, from, to,
|
|
38
|
+
} = this;
|
|
39
|
+
|
|
40
|
+
const response = await app.getRates({
|
|
41
|
+
$,
|
|
42
|
+
from,
|
|
43
|
+
to,
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
const base = from || "USD";
|
|
47
|
+
const rateCount = response?.rates
|
|
48
|
+
? Object.keys(response.rates).length
|
|
49
|
+
: 0;
|
|
50
|
+
$.export(
|
|
51
|
+
"$summary",
|
|
52
|
+
to
|
|
53
|
+
? `Fetched ${base}→${to} rate: ${response?.rate ?? "?"}`
|
|
54
|
+
: `Fetched ${rateCount} rate(s) for ${base}`,
|
|
55
|
+
);
|
|
56
|
+
return response;
|
|
57
|
+
},
|
|
58
|
+
};
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import unirateapi from "../../unirateapi.app.mjs";
|
|
2
|
+
|
|
3
|
+
export default {
|
|
4
|
+
key: "unirateapi-list-currency-code-options",
|
|
5
|
+
name: "List Currency Code Options",
|
|
6
|
+
description: "Retrieves available options for the Currency Code field.",
|
|
7
|
+
version: "0.0.1",
|
|
8
|
+
type: "action",
|
|
9
|
+
annotations: {
|
|
10
|
+
destructiveHint: false,
|
|
11
|
+
openWorldHint: true,
|
|
12
|
+
readOnlyHint: true,
|
|
13
|
+
},
|
|
14
|
+
props: {
|
|
15
|
+
unirateapi,
|
|
16
|
+
},
|
|
17
|
+
async run({ $ }) {
|
|
18
|
+
const options = await unirateapi.propDefinitions.currencyCode.options.call(this.unirateapi);
|
|
19
|
+
$.export("$summary", `Successfully retrieved ${options.length} option${options.length === 1
|
|
20
|
+
? ""
|
|
21
|
+
: "s"}`);
|
|
22
|
+
return options;
|
|
23
|
+
},
|
|
24
|
+
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@pipedream/unirateapi",
|
|
3
|
-
"version": "0.0
|
|
3
|
+
"version": "0.2.0",
|
|
4
4
|
"description": "Pipedream UniRateAPI Components",
|
|
5
5
|
"main": "unirateapi.app.mjs",
|
|
6
6
|
"keywords": [
|
|
@@ -11,5 +11,8 @@
|
|
|
11
11
|
"author": "Pipedream <support@pipedream.com> (https://pipedream.com/)",
|
|
12
12
|
"publishConfig": {
|
|
13
13
|
"access": "public"
|
|
14
|
+
},
|
|
15
|
+
"dependencies": {
|
|
16
|
+
"@pipedream/platform": "^3.4.0"
|
|
14
17
|
}
|
|
15
18
|
}
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
import { DEFAULT_POLLING_SOURCE_TIMER_INTERVAL } from "@pipedream/platform";
|
|
2
|
+
import app from "../../unirateapi.app.mjs";
|
|
3
|
+
|
|
4
|
+
export default {
|
|
5
|
+
key: "unirateapi-new-rate-update",
|
|
6
|
+
name: "New Rate Update",
|
|
7
|
+
version: "0.0.1",
|
|
8
|
+
annotations: {
|
|
9
|
+
destructiveHint: false,
|
|
10
|
+
openWorldHint: true,
|
|
11
|
+
readOnlyHint: true,
|
|
12
|
+
},
|
|
13
|
+
description: "Emit new event when the exchange rate between two currencies changes. [See the documentation](https://unirateapi.com/docs).",
|
|
14
|
+
type: "source",
|
|
15
|
+
dedupe: "unique",
|
|
16
|
+
props: {
|
|
17
|
+
app,
|
|
18
|
+
db: "$.service.db",
|
|
19
|
+
timer: {
|
|
20
|
+
label: "Polling interval",
|
|
21
|
+
description: "Pipedream will poll UniRate on this schedule.",
|
|
22
|
+
type: "$.interface.timer",
|
|
23
|
+
default: {
|
|
24
|
+
intervalSeconds: DEFAULT_POLLING_SOURCE_TIMER_INTERVAL,
|
|
25
|
+
},
|
|
26
|
+
},
|
|
27
|
+
from: {
|
|
28
|
+
propDefinition: [
|
|
29
|
+
app,
|
|
30
|
+
"currencyCode",
|
|
31
|
+
],
|
|
32
|
+
label: "From",
|
|
33
|
+
description: "The base currency code.",
|
|
34
|
+
},
|
|
35
|
+
to: {
|
|
36
|
+
propDefinition: [
|
|
37
|
+
app,
|
|
38
|
+
"currencyCode",
|
|
39
|
+
],
|
|
40
|
+
label: "To",
|
|
41
|
+
description: "The target currency code.",
|
|
42
|
+
},
|
|
43
|
+
},
|
|
44
|
+
methods: {
|
|
45
|
+
_getLastRate() {
|
|
46
|
+
return this.db.get("lastRate");
|
|
47
|
+
},
|
|
48
|
+
_setLastRate(rate) {
|
|
49
|
+
this.db.set("lastRate", rate);
|
|
50
|
+
},
|
|
51
|
+
},
|
|
52
|
+
async run({ $ }) {
|
|
53
|
+
const {
|
|
54
|
+
app, from, to,
|
|
55
|
+
} = this;
|
|
56
|
+
|
|
57
|
+
const response = await app.getRates({
|
|
58
|
+
$,
|
|
59
|
+
from,
|
|
60
|
+
to,
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
const rate = response?.rate;
|
|
64
|
+
if (rate === undefined) {
|
|
65
|
+
return;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
const lastRate = this._getLastRate();
|
|
69
|
+
if (lastRate === rate) {
|
|
70
|
+
return;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
if (lastRate === undefined) {
|
|
74
|
+
this._setLastRate(rate);
|
|
75
|
+
return;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
const ts = Date.now();
|
|
79
|
+
this.$emit(
|
|
80
|
+
{
|
|
81
|
+
from,
|
|
82
|
+
to,
|
|
83
|
+
rate,
|
|
84
|
+
previousRate: lastRate,
|
|
85
|
+
ts,
|
|
86
|
+
},
|
|
87
|
+
{
|
|
88
|
+
id: `${from}-${to}-${rate}`,
|
|
89
|
+
summary: `${from}→${to}: ${lastRate} → ${rate}`,
|
|
90
|
+
ts,
|
|
91
|
+
},
|
|
92
|
+
);
|
|
93
|
+
this._setLastRate(rate);
|
|
94
|
+
},
|
|
95
|
+
};
|
package/unirateapi.app.mjs
CHANGED
|
@@ -1,11 +1,77 @@
|
|
|
1
|
+
import { axios } from "@pipedream/platform";
|
|
2
|
+
|
|
1
3
|
export default {
|
|
2
4
|
type: "app",
|
|
3
5
|
app: "unirateapi",
|
|
4
|
-
propDefinitions: {
|
|
6
|
+
propDefinitions: {
|
|
7
|
+
currencyCode: {
|
|
8
|
+
type: "string",
|
|
9
|
+
label: "Currency Code",
|
|
10
|
+
description: "An ISO 4217 currency code (e.g. `USD`, `EUR`, `GBP`).",
|
|
11
|
+
async options() {
|
|
12
|
+
const { currencies = [] } = await this.listCurrencies();
|
|
13
|
+
return currencies.map((value) => ({
|
|
14
|
+
label: value,
|
|
15
|
+
value,
|
|
16
|
+
}));
|
|
17
|
+
},
|
|
18
|
+
},
|
|
19
|
+
},
|
|
5
20
|
methods: {
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
21
|
+
_apiUrl() {
|
|
22
|
+
return "https://api.unirateapi.com/api";
|
|
23
|
+
},
|
|
24
|
+
_getParams(params) {
|
|
25
|
+
return {
|
|
26
|
+
api_key: this.$auth.api_key,
|
|
27
|
+
...params,
|
|
28
|
+
};
|
|
29
|
+
},
|
|
30
|
+
async _makeRequest({
|
|
31
|
+
$ = this, path, params = {}, headers = {}, ...opts
|
|
32
|
+
}) {
|
|
33
|
+
return axios($, {
|
|
34
|
+
url: `${this._apiUrl()}/${path}`,
|
|
35
|
+
params: this._getParams(params),
|
|
36
|
+
...opts,
|
|
37
|
+
headers: {
|
|
38
|
+
Accept: "application/json",
|
|
39
|
+
...headers,
|
|
40
|
+
},
|
|
41
|
+
});
|
|
42
|
+
},
|
|
43
|
+
listCurrencies(args = {}) {
|
|
44
|
+
return this._makeRequest({
|
|
45
|
+
path: "currencies",
|
|
46
|
+
...args,
|
|
47
|
+
});
|
|
48
|
+
},
|
|
49
|
+
getRates({
|
|
50
|
+
from, to, ...args
|
|
51
|
+
} = {}) {
|
|
52
|
+
return this._makeRequest({
|
|
53
|
+
path: "rates",
|
|
54
|
+
params: {
|
|
55
|
+
from,
|
|
56
|
+
...(to && {
|
|
57
|
+
to,
|
|
58
|
+
}),
|
|
59
|
+
},
|
|
60
|
+
...args,
|
|
61
|
+
});
|
|
62
|
+
},
|
|
63
|
+
convert({
|
|
64
|
+
from, to, amount, ...args
|
|
65
|
+
}) {
|
|
66
|
+
return this._makeRequest({
|
|
67
|
+
path: "convert",
|
|
68
|
+
params: {
|
|
69
|
+
from,
|
|
70
|
+
to,
|
|
71
|
+
amount,
|
|
72
|
+
},
|
|
73
|
+
...args,
|
|
74
|
+
});
|
|
9
75
|
},
|
|
10
76
|
},
|
|
11
|
-
};
|
|
77
|
+
};
|