@trycourier/cli 1.0.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.
Files changed (61) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +47 -0
  3. package/dist/Counter.d.ts +3 -0
  4. package/dist/Counter.js +16 -0
  5. package/dist/Error.d.ts +3 -0
  6. package/dist/Error.js +4 -0
  7. package/dist/Help.d.ts +3 -0
  8. package/dist/Help.js +13 -0
  9. package/dist/app.d.ts +6 -0
  10. package/dist/app.js +7 -0
  11. package/dist/cli.d.ts +2 -0
  12. package/dist/cli.js +22 -0
  13. package/dist/commands/DigestFlush.d.ts +5 -0
  14. package/dist/commands/DigestFlush.js +23 -0
  15. package/dist/commands/DigestsFlush.d.ts +5 -0
  16. package/dist/commands/DigestsFlush.js +23 -0
  17. package/dist/commands/EventsTrack.d.ts +5 -0
  18. package/dist/commands/EventsTrack.js +34 -0
  19. package/dist/commands/Help.d.ts +16 -0
  20. package/dist/commands/Help.js +46 -0
  21. package/dist/commands/NotYetImplemented.d.ts +3 -0
  22. package/dist/commands/NotYetImplemented.js +5 -0
  23. package/dist/commands/Send.d.ts +5 -0
  24. package/dist/commands/Send.js +96 -0
  25. package/dist/commands/Track.d.ts +5 -0
  26. package/dist/commands/Track.js +34 -0
  27. package/dist/commands/WhoAmI.d.ts +3 -0
  28. package/dist/commands/WhoAmI.js +22 -0
  29. package/dist/components/Counter.d.ts +3 -0
  30. package/dist/components/Counter.js +16 -0
  31. package/dist/components/Error.d.ts +5 -0
  32. package/dist/components/Error.js +5 -0
  33. package/dist/components/Hello.d.ts +5 -0
  34. package/dist/components/Hello.js +7 -0
  35. package/dist/components/Help.d.ts +9 -0
  36. package/dist/components/Help.js +23 -0
  37. package/dist/components/KVP.d.ts +11 -0
  38. package/dist/components/KVP.js +11 -0
  39. package/dist/components/Request.d.ts +19 -0
  40. package/dist/components/Request.js +24 -0
  41. package/dist/components/Router.d.ts +11 -0
  42. package/dist/components/Router.js +17 -0
  43. package/dist/components/Spinner.d.ts +6 -0
  44. package/dist/components/Spinner.js +41 -0
  45. package/dist/components/UhOh.d.ts +5 -0
  46. package/dist/components/UhOh.js +10 -0
  47. package/dist/components/WhoAmI.d.ts +3 -0
  48. package/dist/components/WhoAmI.js +42 -0
  49. package/dist/constants.d.ts +6 -0
  50. package/dist/constants.js +6 -0
  51. package/dist/lib/api.d.ts +13 -0
  52. package/dist/lib/api.js +32 -0
  53. package/dist/lib/args.d.ts +2 -0
  54. package/dist/lib/args.js +13 -0
  55. package/dist/lib/delay.d.ts +2 -0
  56. package/dist/lib/delay.js +7 -0
  57. package/dist/lib/load-env.d.ts +2 -0
  58. package/dist/lib/load-env.js +16 -0
  59. package/dist/mappings.d.ts +14 -0
  60. package/dist/mappings.js +98 -0
  61. package/package.json +61 -0
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2023 Courier
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,47 @@
1
+ # Courier CLI
2
+
3
+ ### Build, test, and manage your [Courier](https://www.courier.com) integration directly from the command line.
4
+
5
+ The Courier CLI is a developer tool to help you build, test, and manage your integration with Courier directly from the command line. It’s simple to install, works on macOS, Windows, and Linux, and offers a range of functionality to enhance your developer experience with Courier. You can use the Courier CLI to:
6
+
7
+ - Send messages from the command line to users, lists, or audiences
8
+ - Track events to trigger your Courier Automations
9
+ - Push & pull industry-standard translation files for internationalizing your content
10
+
11
+ ## Install the Courier CLI
12
+
13
+ From the command-line, use the following command to install the Courier CLI:
14
+
15
+ ```bash
16
+ $ npm install -g @trycourier/cli
17
+ ```
18
+
19
+ ### Requirements
20
+
21
+ - Courier CLI has only been tested on node.js v16+
22
+
23
+ ## Authenticate the CLI
24
+
25
+ Courier CLI looks for an environment variable named `COURIER_API_KEY`. It will load that key from the first location it finds in the following list:
26
+
27
+ - A `.courier` file in the current working directory
28
+ - `~/.courier` (in your home directory)
29
+ - A `COURIER_API_KEY` value otherwise set in your environment (such as via `~/.profile` or `~/.zshrc`)
30
+
31
+ You can find your Courier API key in your [Courier Settings](https://app.courier.com/settings/api-keys).
32
+
33
+ ## Commands
34
+
35
+ - `courier whoami` – Display the currently authenticated workspace
36
+ - `courier send` - Send a notification to a user, list, or audience
37
+ - `courier track` - Send an event to test your Courier Automations
38
+
39
+ For more details, run `courier` to see a list of commands and their arguments & options.
40
+
41
+ ## License
42
+
43
+ [MIT License](http://www.opensource.org/licenses/mit-license.php)
44
+
45
+ ## Author
46
+
47
+ [Courier](https://github.com/trycourier) ([support@courier.com](mailto:support@courier.com))
@@ -0,0 +1,3 @@
1
+ import React from 'react';
2
+ declare const _default: () => React.JSX.Element;
3
+ export default _default;
@@ -0,0 +1,16 @@
1
+ import React, { useEffect, useState } from 'react';
2
+ import { Text } from 'ink';
3
+ export default () => {
4
+ const [counter, setCounter] = useState(0);
5
+ useEffect(() => {
6
+ const timer = setInterval(() => {
7
+ setCounter(previousCounter => previousCounter + 1);
8
+ }, 100);
9
+ return () => {
10
+ clearInterval(timer);
11
+ };
12
+ }, []);
13
+ return React.createElement(Text, { color: "green" },
14
+ counter,
15
+ " tests passed");
16
+ };
@@ -0,0 +1,3 @@
1
+ import React from 'react';
2
+ declare const _default: () => React.JSX.Element;
3
+ export default _default;
package/dist/Error.js ADDED
@@ -0,0 +1,4 @@
1
+ import React from 'react';
2
+ import { Box, Text } from 'ink';
3
+ export default () => (React.createElement(Box, { flexDirection: "column" },
4
+ React.createElement(Text, { color: "red" }, "ERROR")));
package/dist/Help.d.ts ADDED
@@ -0,0 +1,3 @@
1
+ import React from 'react';
2
+ declare const _default: () => React.JSX.Element;
3
+ export default _default;
package/dist/Help.js ADDED
@@ -0,0 +1,13 @@
1
+ import React from 'react';
2
+ import { Box, Text, Newline } from 'ink';
3
+ export default () => (React.createElement(Box, { flexDirection: "column" },
4
+ React.createElement(Text, null,
5
+ "Usage $ %NAME%",
6
+ React.createElement(Newline, null)),
7
+ React.createElement(Text, null, "Options"),
8
+ React.createElement(Text, null,
9
+ " --name Your name",
10
+ React.createElement(Newline, null)),
11
+ React.createElement(Text, null, "Examples"),
12
+ React.createElement(Text, null, " $ %NAME% --name=Jane"),
13
+ React.createElement(Text, null, " Hello, Jane")));
package/dist/app.d.ts ADDED
@@ -0,0 +1,6 @@
1
+ import React from 'react';
2
+ type Props = {
3
+ name: string | undefined;
4
+ };
5
+ export default function App({ name }: Props): React.JSX.Element;
6
+ export {};
package/dist/app.js ADDED
@@ -0,0 +1,7 @@
1
+ import React from 'react';
2
+ import { Text } from 'ink';
3
+ export default function App({ name = 'Stranger' }) {
4
+ return (React.createElement(Text, null,
5
+ "Hello, ",
6
+ React.createElement(Text, { color: "green" }, name)));
7
+ }
package/dist/cli.d.ts ADDED
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/env node
2
+ export {};
package/dist/cli.js ADDED
@@ -0,0 +1,22 @@
1
+ #!/usr/bin/env node
2
+ import React from 'react';
3
+ import { render } from 'ink';
4
+ import args from './lib/args.js';
5
+ import loadEnv from './lib/load-env.js';
6
+ const CLI = async () => {
7
+ await loadEnv();
8
+ const params = args(process.argv);
9
+ const mappings = (await import('./mappings.js')).default;
10
+ const Router = (await import('./components/Router.js')).default;
11
+ const UhOh = (await import('./components/UhOh.js')).default;
12
+ const apiKey = process.env['COURIER_API_KEY'];
13
+ if (apiKey && apiKey.length) {
14
+ render(React.createElement(Router, { args: params, mappings: mappings }));
15
+ }
16
+ else {
17
+ render(React.createElement(UhOh, { text: "No COURIER_API_KEY specified; add that to your environment or ~/.courier file" }));
18
+ }
19
+ };
20
+ (async () => {
21
+ await CLI();
22
+ })();
@@ -0,0 +1,5 @@
1
+ import React from 'react';
2
+ declare const _default: ({ params }: {
3
+ params: any;
4
+ }) => React.JSX.Element;
5
+ export default _default;
@@ -0,0 +1,23 @@
1
+ import React, { useEffect, useState } from 'react';
2
+ import UhOh from '../components/UhOh.js';
3
+ import Request from '../components/Request.js';
4
+ import api from '../lib/api.js';
5
+ export default ({ params }) => {
6
+ const [resp, setResp] = useState();
7
+ const userId = params?._?.[0];
8
+ if (!userId) {
9
+ return React.createElement(UhOh, { text: "You must specify the ID of the user whose digest you want to flush." });
10
+ }
11
+ const topicId = params?._?.[1];
12
+ if (!topicId) {
13
+ return React.createElement(UhOh, { text: "You must specify the ID of the Digest-enabled Subscription Topic you want to flush for the given user." });
14
+ }
15
+ const request = {
16
+ method: 'POST',
17
+ url: `/users/${userId}/preferences/${topicId}/flush`,
18
+ };
19
+ useEffect(() => {
20
+ api(request).then(res => setResp(res));
21
+ }, []);
22
+ return React.createElement(Request, { request: request, response: resp });
23
+ };
@@ -0,0 +1,5 @@
1
+ import React from 'react';
2
+ declare const _default: ({ params }: {
3
+ params: any;
4
+ }) => React.JSX.Element;
5
+ export default _default;
@@ -0,0 +1,23 @@
1
+ import React, { useEffect, useState } from 'react';
2
+ import UhOh from '../components/UhOh.js';
3
+ import Request from '../components/Request.js';
4
+ import api from '../lib/api.js';
5
+ export default ({ params }) => {
6
+ const [resp, setResp] = useState();
7
+ const userId = params?._?.[0];
8
+ if (!userId) {
9
+ return React.createElement(UhOh, { text: "You must specify the ID of the user whose digest you want to flush." });
10
+ }
11
+ const topicId = params?._?.[1];
12
+ if (!topicId) {
13
+ return React.createElement(UhOh, { text: "You must specify the ID of the Digest Topic you want to flush for the given user." });
14
+ }
15
+ const request = {
16
+ method: 'POST',
17
+ url: `/users/${userId}/topic/${topicId}/flush`,
18
+ };
19
+ useEffect(() => {
20
+ api(request).then(res => setResp(res));
21
+ }, []);
22
+ return React.createElement(Request, { request: request, response: resp });
23
+ };
@@ -0,0 +1,5 @@
1
+ import React from 'react';
2
+ declare const _default: ({ params }: {
3
+ params: any;
4
+ }) => React.JSX.Element;
5
+ export default _default;
@@ -0,0 +1,34 @@
1
+ import React, { useEffect, useState } from 'react';
2
+ import UhOh from '../components/UhOh.js';
3
+ import Request from '../components/Request.js';
4
+ import api from '../lib/api.js';
5
+ export default ({ params }) => {
6
+ const [resp, setResp] = useState();
7
+ const eventId = params?._?.[0];
8
+ if (!eventId) {
9
+ return React.createElement(UhOh, { text: "You must specify an event ID to trigger." });
10
+ }
11
+ const userId = params?._?.[1];
12
+ if (!userId) {
13
+ return React.createElement(UhOh, { text: "You must specify a user ID for the event." });
14
+ }
15
+ const { _, ...properties } = params;
16
+ const payload = {
17
+ type: 'track',
18
+ event: eventId,
19
+ messageId: Math.random().toString(36).substring(2),
20
+ properties: {
21
+ userId,
22
+ ...properties,
23
+ },
24
+ };
25
+ const request = {
26
+ method: 'POST',
27
+ url: '/inbound/courier',
28
+ body: payload,
29
+ };
30
+ useEffect(() => {
31
+ api(request).then(res => setResp(res));
32
+ }, []);
33
+ return React.createElement(Request, { request: request, response: resp });
34
+ };
@@ -0,0 +1,16 @@
1
+ import React from 'react';
2
+ interface IMapping {
3
+ params?: string;
4
+ instructions?: string;
5
+ example?: string;
6
+ options?: {
7
+ option: string;
8
+ value?: string;
9
+ instructions?: string;
10
+ }[];
11
+ component: (params?: any) => React.ReactElement;
12
+ }
13
+ declare const _default: ({ mappings }: {
14
+ mappings: Map<string, IMapping>;
15
+ }) => React.JSX.Element;
16
+ export default _default;
@@ -0,0 +1,46 @@
1
+ import React from 'react';
2
+ import { Box, Text, Newline } from 'ink';
3
+ import constants from '../constants.js';
4
+ export default ({ mappings }) => {
5
+ const keys = [...mappings.keys()]; // convert to array
6
+ return (React.createElement(Box, { flexDirection: "column" },
7
+ React.createElement(Text, { bold: true }, "Usage"),
8
+ React.createElement(Text, null,
9
+ "$ ",
10
+ React.createElement(Text, { color: constants.colors.primary }, "courier"),
11
+ ' ',
12
+ React.createElement(Text, { color: "gray" }, "<command>"),
13
+ React.createElement(Newline, null)),
14
+ React.createElement(Text, { bold: true }, "Commands"),
15
+ keys.map(k => {
16
+ const v = mappings.get(k);
17
+ if (!v || !v.instructions || !v.instructions.length) {
18
+ return null;
19
+ }
20
+ return (React.createElement(React.Fragment, { key: k },
21
+ React.createElement(Box, null,
22
+ React.createElement(Text, null,
23
+ ' • ',
24
+ React.createElement(Text, { color: constants.colors.primary }, k),
25
+ v.params ? React.createElement(Text, { color: "gray" },
26
+ " ",
27
+ v.params) : null)),
28
+ React.createElement(Box, null,
29
+ React.createElement(Text, null,
30
+ ' ',
31
+ v.instructions)),
32
+ v.options
33
+ ? v.options.map(o => (React.createElement(React.Fragment, { key: o.option },
34
+ React.createElement(Box, null,
35
+ React.createElement(Text, { color: "gray" },
36
+ ' ',
37
+ o.option),
38
+ React.createElement(Text, null,
39
+ " ",
40
+ o.value)))))
41
+ : null,
42
+ v.example ? (React.createElement(Text, { color: "cyan" },
43
+ ' ',
44
+ v.example)) : null));
45
+ })));
46
+ };
@@ -0,0 +1,3 @@
1
+ import React from 'react';
2
+ declare const _default: () => React.JSX.Element;
3
+ export default _default;
@@ -0,0 +1,5 @@
1
+ import React from 'react';
2
+ import { Text } from 'ink';
3
+ export default () => {
4
+ return (React.createElement(Text, { bold: true, color: "magenta" }, "Not Yet Implemented \uD83E\uDD37\u200D\u2642\uFE0F"));
5
+ };
@@ -0,0 +1,5 @@
1
+ import React from 'react';
2
+ declare const _default: ({ params }: {
3
+ params: any;
4
+ }) => React.JSX.Element;
5
+ export default _default;
@@ -0,0 +1,96 @@
1
+ import React, { useEffect, useState } from 'react';
2
+ import fs from 'fs';
3
+ import UhOh from '../components/UhOh.js';
4
+ import Request from '../components/Request.js';
5
+ import api from '../lib/api.js';
6
+ const constructPayload = (params) => {
7
+ const to = [];
8
+ if (params.user) {
9
+ to.push({ user_id: params.user });
10
+ }
11
+ if (params.list) {
12
+ to.push({ list_id: params.list });
13
+ }
14
+ if (params.audience) {
15
+ to.push({ audience_id: params.audience });
16
+ }
17
+ if (params.email) {
18
+ to.push({ email: params.email });
19
+ }
20
+ if (params.tel) {
21
+ to.push({ phone_number: params.tel });
22
+ }
23
+ if (params.apn) {
24
+ to.push({ apn: { token: params.apn } });
25
+ }
26
+ if (params.fcm) {
27
+ to.push({ firebaseToken: params.fcm });
28
+ }
29
+ let content = {
30
+ title: undefined,
31
+ body: undefined,
32
+ };
33
+ if (params.title) {
34
+ content.title = params.title;
35
+ }
36
+ if (params.body) {
37
+ content.body = params.body;
38
+ }
39
+ else if (params.message) {
40
+ content.body = params.message;
41
+ }
42
+ let routing = {
43
+ channels: [],
44
+ method: params.all ? 'all' : 'single',
45
+ };
46
+ if (params.channel) {
47
+ routing.channels = params.channel.split(',');
48
+ }
49
+ else if (params.channels) {
50
+ routing.channels = params.channels.split(',');
51
+ }
52
+ if (params.email && params.email.length) {
53
+ routing.channels.push('email');
54
+ }
55
+ if (params.tel && params.tel.length) {
56
+ routing.channels.push('sms');
57
+ }
58
+ if ((params.apn && params.apn.length) || (params.fcm && params.fcm.length)) {
59
+ routing.channels.push('push');
60
+ }
61
+ const { _, user, list, audience, email, tel, apn, fcm, title, body, elemental, ...data } = params;
62
+ return {
63
+ message: {
64
+ to: to.length === 1 ? to[0] : to,
65
+ content,
66
+ routing: routing.channels.length ? routing : undefined,
67
+ data: data ? data : undefined,
68
+ },
69
+ };
70
+ };
71
+ export default ({ params }) => {
72
+ const [resp, setResp] = useState();
73
+ const payload = constructPayload(params);
74
+ if (!payload.message.to) {
75
+ return React.createElement(UhOh, { text: "You must specify a recipient." });
76
+ }
77
+ if (!params.elemental &&
78
+ (!payload.message.content.body || !payload.message.content.body?.length)) {
79
+ return React.createElement(UhOh, { text: "You must specify a body for the message." });
80
+ }
81
+ if (params.elemental) {
82
+ if (!fs.existsSync(params.elemental)) {
83
+ return React.createElement(UhOh, { text: "Invalid file path to Elemental document." });
84
+ }
85
+ payload.message.content = JSON.parse(fs.readFileSync(params.elemental, 'utf8'));
86
+ }
87
+ const request = {
88
+ method: 'POST',
89
+ url: '/send',
90
+ body: payload,
91
+ };
92
+ useEffect(() => {
93
+ api(request).then(res => setResp(res));
94
+ }, []);
95
+ return React.createElement(Request, { request: request, response: resp });
96
+ };
@@ -0,0 +1,5 @@
1
+ import React from 'react';
2
+ declare const _default: ({ params }: {
3
+ params: any;
4
+ }) => React.JSX.Element;
5
+ export default _default;
@@ -0,0 +1,34 @@
1
+ import React, { useEffect, useState } from 'react';
2
+ import UhOh from '../components/UhOh.js';
3
+ import Request from '../components/Request.js';
4
+ import api from '../lib/api.js';
5
+ export default ({ params }) => {
6
+ const [resp, setResp] = useState();
7
+ const eventId = params?._?.[0];
8
+ if (!eventId) {
9
+ return React.createElement(UhOh, { text: "You must specify an event ID to trigger." });
10
+ }
11
+ const userId = params?._?.[1];
12
+ if (!userId) {
13
+ return React.createElement(UhOh, { text: "You must specify a user ID for the event." });
14
+ }
15
+ const { _, ...properties } = params;
16
+ const payload = {
17
+ type: 'track',
18
+ event: eventId,
19
+ messageId: Math.random().toString(36).substring(2),
20
+ properties: {
21
+ userId,
22
+ ...properties,
23
+ },
24
+ };
25
+ const request = {
26
+ method: 'POST',
27
+ url: '/inbound/courier',
28
+ body: payload,
29
+ };
30
+ useEffect(() => {
31
+ api(request).then(res => setResp(res));
32
+ }, []);
33
+ return React.createElement(Request, { request: request, response: resp });
34
+ };
@@ -0,0 +1,3 @@
1
+ import React from 'react';
2
+ declare const _default: () => React.JSX.Element;
3
+ export default _default;
@@ -0,0 +1,22 @@
1
+ import React, { useEffect, useState } from 'react';
2
+ import { Box } from 'ink';
3
+ import KVP from '../components/KVP.js';
4
+ import Request from '../components/Request.js';
5
+ import api from '../lib/api.js';
6
+ export default () => {
7
+ const [resp, setResp] = useState();
8
+ const request = {
9
+ url: '/debug',
10
+ method: 'POST',
11
+ };
12
+ useEffect(() => {
13
+ api(request).then(res => setResp(res));
14
+ }, []);
15
+ return (React.createElement(Box, { flexDirection: "column" },
16
+ React.createElement(Request, { request: request, response: resp, hideResponse: true }),
17
+ resp && resp.json ? (React.createElement(React.Fragment, null,
18
+ React.createElement(KVP, { width: 20, label: "Workspace Name", value: resp.json.tenantName }),
19
+ React.createElement(KVP, { width: 20, label: "Workspace ID", value: resp.json.tenantId }),
20
+ React.createElement(KVP, { width: 20, label: "API Key Environment", value: resp.json.environment }),
21
+ React.createElement(KVP, { width: 20, label: "API Key Scope", value: resp.json.scope }))) : null));
22
+ };
@@ -0,0 +1,3 @@
1
+ import React from 'react';
2
+ declare const _default: () => React.JSX.Element;
3
+ export default _default;
@@ -0,0 +1,16 @@
1
+ import React, { useEffect, useState } from 'react';
2
+ import { Text } from 'ink';
3
+ export default () => {
4
+ const [counter, setCounter] = useState(0);
5
+ useEffect(() => {
6
+ const timer = setInterval(() => {
7
+ setCounter(previousCounter => previousCounter + 1);
8
+ }, 100);
9
+ return () => {
10
+ clearInterval(timer);
11
+ };
12
+ }, []);
13
+ return React.createElement(Text, { color: "green" },
14
+ counter,
15
+ " tests passed");
16
+ };
@@ -0,0 +1,5 @@
1
+ import React from 'react';
2
+ declare const _default: ({ text }: {
3
+ text: string;
4
+ }) => React.JSX.Element;
5
+ export default _default;
@@ -0,0 +1,5 @@
1
+ import React from 'react';
2
+ import { Text } from 'ink';
3
+ export default ({ text }) => {
4
+ return React.createElement(Text, { color: "red" }, text);
5
+ };
@@ -0,0 +1,5 @@
1
+ import React from 'react';
2
+ declare const _default: ({ name }: {
3
+ name?: string | undefined;
4
+ }) => React.JSX.Element;
5
+ export default _default;
@@ -0,0 +1,7 @@
1
+ import React from 'react';
2
+ import { Text } from 'ink';
3
+ export default ({ name = 'Stranger' }) => {
4
+ return (React.createElement(Text, null,
5
+ "Hello, ",
6
+ React.createElement(Text, { color: "green" }, name)));
7
+ };
@@ -0,0 +1,9 @@
1
+ import React from 'react';
2
+ interface IMapping {
3
+ instructions?: string;
4
+ component: (params: (string | undefined)[]) => React.ReactElement;
5
+ }
6
+ declare const _default: ({ mappings }: {
7
+ mappings: Map<string, IMapping>;
8
+ }) => React.JSX.Element;
9
+ export default _default;
@@ -0,0 +1,23 @@
1
+ import React from 'react';
2
+ import { Box, Text, Newline } from 'ink';
3
+ import KVP from './KVP.js';
4
+ import constants from '../constants.js';
5
+ export default ({ mappings }) => {
6
+ const keys = [...mappings.keys()]; // convert to array
7
+ return React.createElement(Box, { flexDirection: "column" },
8
+ React.createElement(Text, { bold: true }, "Usage"),
9
+ React.createElement(Text, null,
10
+ " $ ",
11
+ React.createElement(Text, { color: constants.colors.primary }, "courier"),
12
+ " ",
13
+ React.createElement(Text, { color: "gray" }, "<command>"),
14
+ React.createElement(Newline, null)),
15
+ React.createElement(Text, { bold: true }, "Commands"),
16
+ keys.map((k) => {
17
+ const v = mappings.get(k);
18
+ if (!v || !v.instructions || !v.instructions.length) {
19
+ return null;
20
+ }
21
+ return React.createElement(KVP, { key: k, width: 12, indent: " \u2022 ", label: k, labelColor: constants.colors.primary, labelBold: false, value: v.instructions });
22
+ }));
23
+ };
@@ -0,0 +1,11 @@
1
+ import React from 'react';
2
+ type Props = {
3
+ label?: string;
4
+ value?: string;
5
+ width?: number;
6
+ indent?: string;
7
+ labelColor?: string;
8
+ labelBold?: boolean;
9
+ };
10
+ declare const _default: ({ label, labelColor, labelBold, value, width, indent, }: Props) => React.JSX.Element;
11
+ export default _default;
@@ -0,0 +1,11 @@
1
+ import React from 'react';
2
+ import { Box, Text } from 'ink';
3
+ export default ({ label, labelColor, labelBold, value, width, indent, }) => {
4
+ return (React.createElement(Box, null,
5
+ React.createElement(Box, { width: width },
6
+ React.createElement(Text, null, indent),
7
+ React.createElement(Text, { bold: labelBold === false ? false : true, color: labelColor }, label)),
8
+ React.createElement(Text, null,
9
+ " ",
10
+ value)));
11
+ };
@@ -0,0 +1,19 @@
1
+ import React from 'react';
2
+ interface IRequest {
3
+ url: string;
4
+ method: string;
5
+ body?: object | string;
6
+ }
7
+ interface IResponse {
8
+ res: Response;
9
+ json?: any;
10
+ err?: Error;
11
+ }
12
+ type Props = {
13
+ request: IRequest;
14
+ response?: IResponse;
15
+ hideResponse?: boolean;
16
+ spinnerText?: string;
17
+ };
18
+ declare const _default: (props: Props) => React.JSX.Element;
19
+ export default _default;
@@ -0,0 +1,24 @@
1
+ import React from 'react';
2
+ import { Box, Text } from 'ink';
3
+ import Spinner from './Spinner.js';
4
+ const defaultSpinnerText = 'blipping those bits and blooping those bytes';
5
+ export default (props) => {
6
+ return (React.createElement(Box, { flexDirection: "column" },
7
+ React.createElement(Box, { borderStyle: "bold", borderColor: "blue" },
8
+ React.createElement(Text, null,
9
+ React.createElement(Text, { bold: true }, props.request.method),
10
+ ' ',
11
+ "https://api.courier.com",
12
+ props.request.url)),
13
+ props.request.body && (React.createElement(Text, null, JSON.stringify(props.request.body, undefined, ' '))),
14
+ !props.request.body ? (React.createElement(Text, { color: "gray" }, "No request body")) : null,
15
+ !props.response ? (React.createElement(Spinner, { text: props.spinnerText || defaultSpinnerText })) : null,
16
+ props.response ? (React.createElement(Box, { borderStyle: "bold", borderColor: props.response?.err ? 'red' : 'green' },
17
+ React.createElement(Text, null,
18
+ "HTTP",
19
+ ' ',
20
+ React.createElement(Text, { bold: true }, props.response.err
21
+ ? props.response.err.message
22
+ : props.response?.res?.status)))) : null,
23
+ props.response && !props.hideResponse ? (props.response.json ? (React.createElement(Text, null, JSON.stringify(props.response.json, undefined, ' '))) : (React.createElement(Text, { color: "gray" }, "No response body"))) : null));
24
+ };
@@ -0,0 +1,11 @@
1
+ import React from 'react';
2
+ interface IMapping {
3
+ instructions?: string;
4
+ component: (params?: any) => React.ReactElement;
5
+ }
6
+ type Props = {
7
+ args: string[];
8
+ mappings: Map<string, IMapping>;
9
+ };
10
+ declare const _default: ({ args, mappings }: Props) => React.JSX.Element;
11
+ export default _default;
@@ -0,0 +1,17 @@
1
+ import React from 'react';
2
+ import Help from '../commands/Help.js';
3
+ import yargs from 'yargs-parser';
4
+ export default ({ args, mappings }) => {
5
+ if (!args.length || !args[0]) {
6
+ return React.createElement(Help, { mappings: mappings });
7
+ }
8
+ const mapping = mappings.get(args[0]);
9
+ const [, ...params] = args;
10
+ const parsedParams = params.length ? yargs(params) : undefined;
11
+ if (mapping) {
12
+ return mapping.component(parsedParams);
13
+ }
14
+ else {
15
+ return React.createElement(Help, { mappings: mappings });
16
+ }
17
+ };
@@ -0,0 +1,6 @@
1
+ import React from 'react';
2
+ type Props = {
3
+ text?: string;
4
+ };
5
+ declare const _default: ({ text }: Props) => React.JSX.Element;
6
+ export default _default;
@@ -0,0 +1,41 @@
1
+ import React, { useEffect, useState } from 'react';
2
+ import { Box, Text } from 'ink';
3
+ import spinners from 'cli-spinners';
4
+ import constants from '../constants.js';
5
+ /*
6
+ some of my favorites:
7
+ - spinners.dots
8
+ - spinners.triangle
9
+ - spinners.binary
10
+ - spinners.arc
11
+ - spinners.arrow
12
+ - spinners.bouncingBar
13
+ - spinners.pong
14
+ - spinners.aesthetic
15
+ */
16
+ const spinner = spinners.aesthetic;
17
+ const spinnerColor = constants.colors.primary;
18
+ const textColor = 'white';
19
+ export default ({ text }) => {
20
+ const [index, setIndex] = useState(0);
21
+ useEffect(() => {
22
+ const timer = setInterval(() => {
23
+ setIndex(i => {
24
+ if (i + 1 >= spinner.frames.length) {
25
+ return 0;
26
+ }
27
+ else {
28
+ return i + 1;
29
+ }
30
+ });
31
+ }, spinner.interval);
32
+ return () => {
33
+ clearInterval(timer);
34
+ };
35
+ }, []);
36
+ return (React.createElement(Box, { borderStyle: "classic", borderColor: "gray" },
37
+ React.createElement(Text, { bold: true, color: spinnerColor }, spinner.frames[index]),
38
+ text && React.createElement(Text, { color: textColor },
39
+ " ",
40
+ text)));
41
+ };
@@ -0,0 +1,5 @@
1
+ import React from 'react';
2
+ declare const _default: ({ text }: {
3
+ text: string;
4
+ }) => React.JSX.Element | null;
5
+ export default _default;
@@ -0,0 +1,10 @@
1
+ import React from 'react';
2
+ import { Text } from 'ink';
3
+ export default ({ text }) => {
4
+ if (!text || !text.length) {
5
+ return null;
6
+ }
7
+ else {
8
+ return (React.createElement(Text, { bold: true, color: "red" }, text));
9
+ }
10
+ };
@@ -0,0 +1,3 @@
1
+ import React from 'react';
2
+ declare const _default: () => React.JSX.Element;
3
+ export default _default;
@@ -0,0 +1,42 @@
1
+ import React, { useEffect, useState } from 'react';
2
+ import { Box } from 'ink';
3
+ import Spinner from './Spinner.js';
4
+ import KVP from './KVP.js';
5
+ import UhOh from './UhOh.js';
6
+ import api from '../lib/api.js';
7
+ function delay(ms) {
8
+ if (ms === 0) {
9
+ return Promise.resolve();
10
+ }
11
+ return new Promise(resolve => setTimeout(resolve, ms));
12
+ }
13
+ export default () => {
14
+ const [resp, setResp] = useState();
15
+ const [error, setError] = useState();
16
+ useEffect(() => {
17
+ delay(2000)
18
+ .then(() => api('/debug', 'POST'))
19
+ .then(res => {
20
+ if (res.status > 400) {
21
+ throw new Error(`${res.status}: ${res.statusText}`);
22
+ }
23
+ else {
24
+ return res.json();
25
+ }
26
+ })
27
+ .then(json => setResp(json), (err) => setError(err.message));
28
+ }, []);
29
+ if (error) {
30
+ return React.createElement(UhOh, { text: error });
31
+ }
32
+ else if (resp) {
33
+ return React.createElement(Box, { flexDirection: "column" },
34
+ React.createElement(KVP, { width: 20, label: "Workspace Name", value: resp.tenantName }),
35
+ React.createElement(KVP, { width: 20, label: "Workspace ID", value: resp.tenantId }),
36
+ React.createElement(KVP, { width: 20, label: "API Key Environment", value: resp.environment }),
37
+ React.createElement(KVP, { width: 20, label: "API Key Scope", value: resp.scope }));
38
+ }
39
+ else {
40
+ return React.createElement(Spinner, { text: "pondering your existence..." });
41
+ }
42
+ };
@@ -0,0 +1,6 @@
1
+ declare const constants: {
2
+ colors: {
3
+ primary: string;
4
+ };
5
+ };
6
+ export default constants;
@@ -0,0 +1,6 @@
1
+ const constants = {
2
+ colors: {
3
+ primary: '#FF5E5E',
4
+ },
5
+ };
6
+ export default constants;
@@ -0,0 +1,13 @@
1
+ interface IRequest {
2
+ url: string;
3
+ method: string;
4
+ body?: object | string;
5
+ options?: RequestInit;
6
+ }
7
+ interface IResponse {
8
+ res: Response;
9
+ json?: any;
10
+ err?: Error;
11
+ }
12
+ declare const _default: (request: IRequest) => Promise<IResponse>;
13
+ export default _default;
@@ -0,0 +1,32 @@
1
+ const VERSION = '1.0.0';
2
+ const isString = (s) => {
3
+ return typeof s === 'string' || s instanceof String;
4
+ };
5
+ export default async (request) => {
6
+ const baseUrl = process.env['COURIER_DOMAIN'] || 'https://api.courier.com';
7
+ return fetch(`${baseUrl}${request.url}`, {
8
+ method: request.method,
9
+ headers: {
10
+ Authorization: `Bearer ${process.env['COURIER_API_KEY']}`,
11
+ 'Content-Type': 'application/json',
12
+ 'User-Agent': `courier-cli/${VERSION}`,
13
+ },
14
+ body: request.body && !isString(request.body)
15
+ ? JSON.stringify(request.body)
16
+ : undefined,
17
+ ...request.options,
18
+ }).then(res => {
19
+ if (res.status > 400) {
20
+ return {
21
+ res,
22
+ err: new Error(`${res.status}: ${res.statusText}`),
23
+ };
24
+ }
25
+ else if (res.status === 204) {
26
+ return { res };
27
+ }
28
+ else {
29
+ return res.json().then(json => ({ res, json }));
30
+ }
31
+ });
32
+ };
@@ -0,0 +1,2 @@
1
+ declare const _default: (argv: string[]) => string[];
2
+ export default _default;
@@ -0,0 +1,13 @@
1
+ export default (argv) => {
2
+ if (!argv || !argv.length || !argv[0]) {
3
+ return [];
4
+ }
5
+ else if (argv[0].endsWith('node')) {
6
+ const [, , ...args] = argv;
7
+ return args;
8
+ }
9
+ else {
10
+ const [, ...args] = argv;
11
+ return args;
12
+ }
13
+ };
@@ -0,0 +1,2 @@
1
+ declare function delay(ms: number): Promise<unknown>;
2
+ export default delay;
@@ -0,0 +1,7 @@
1
+ function delay(ms) {
2
+ if (ms === 0) {
3
+ return Promise.resolve();
4
+ }
5
+ return new Promise(resolve => setTimeout(resolve, ms));
6
+ }
7
+ export default delay;
@@ -0,0 +1,2 @@
1
+ declare const _default: () => Promise<boolean>;
2
+ export default _default;
@@ -0,0 +1,16 @@
1
+ import os from 'os';
2
+ import fs from 'fs';
3
+ import dotenv from 'dotenv';
4
+ export default async () => {
5
+ const paths = [
6
+ `${process.cwd()}/.courier`,
7
+ `${os.homedir()}/.courier`, // user home directory
8
+ ];
9
+ for (const path of paths) {
10
+ if (fs.existsSync(path)) {
11
+ dotenv.config({ path });
12
+ return true;
13
+ }
14
+ }
15
+ return false;
16
+ };
@@ -0,0 +1,14 @@
1
+ import React from 'react';
2
+ interface IMapping {
3
+ params?: string;
4
+ instructions?: string;
5
+ example?: string;
6
+ options?: {
7
+ option: string;
8
+ value?: string;
9
+ instructions?: string;
10
+ }[];
11
+ component: (params?: any) => React.ReactElement;
12
+ }
13
+ declare const mappings: Map<string, IMapping>;
14
+ export default mappings;
@@ -0,0 +1,98 @@
1
+ import React from 'react';
2
+ import Help from './commands/Help.js';
3
+ import WhoAmI from './commands/WhoAmI.js';
4
+ import Track from './commands/Track.js';
5
+ import Send from './commands/Send.js';
6
+ import DigestFlush from './commands/DigestFlush.js';
7
+ const mappings = new Map();
8
+ mappings.set('help', {
9
+ component: () => {
10
+ return React.createElement(Help, { mappings: mappings });
11
+ },
12
+ });
13
+ mappings.set('whoami', {
14
+ instructions: 'Display the currently authenticated workspace',
15
+ component: () => {
16
+ return React.createElement(WhoAmI, null);
17
+ },
18
+ });
19
+ mappings.set('send', {
20
+ instructions: 'Send a notification to a user, list, or audience',
21
+ options: [
22
+ {
23
+ option: '--email <email address>',
24
+ value: '',
25
+ },
26
+ {
27
+ option: '--tel <phone number>',
28
+ value: '',
29
+ },
30
+ {
31
+ option: '--apn <Apple push token>',
32
+ value: '',
33
+ },
34
+ {
35
+ option: '--fcm <Firebase push token>',
36
+ value: '',
37
+ },
38
+ {
39
+ option: '--user <user ID>',
40
+ value: 'ID of a Courier User in your workspace',
41
+ },
42
+ {
43
+ option: '--list <list ID>',
44
+ value: 'ID of a Courier List in your workspace',
45
+ },
46
+ {
47
+ option: '--audience <audience ID>',
48
+ value: 'ID of a Courier Audience in your workspace',
49
+ },
50
+ {
51
+ option: '--body <message body>',
52
+ value: '',
53
+ },
54
+ {
55
+ option: '--title <message title/subject>',
56
+ value: '',
57
+ },
58
+ {
59
+ option: '--elemental <filepath>',
60
+ value: 'path to Courier Elemental JSON file',
61
+ },
62
+ {
63
+ option: '--channels <channel>',
64
+ value: 'comma-delimted list of channels to send to',
65
+ },
66
+ {
67
+ option: '--all',
68
+ value: 'send to all channels for each recipient (default is "single")',
69
+ },
70
+ ],
71
+ example: `courier send --tel 555-867-5309 --body "Hey Jenny\\!"`,
72
+ component: params => {
73
+ return React.createElement(Send, { params: params });
74
+ },
75
+ });
76
+ mappings.set('track', {
77
+ params: '<event> <user>',
78
+ instructions: 'Send a track event to trigger a Courier Automations',
79
+ options: [
80
+ {
81
+ option: '--<key> <value>',
82
+ value: 'arbitrary key/value properties for your event',
83
+ },
84
+ ],
85
+ example: `courier track EXAMPLE_EVENT user123 --name "Pip the Pigeon"`,
86
+ component: params => {
87
+ return React.createElement(Track, { params: params });
88
+ },
89
+ });
90
+ mappings.set('digest:flush', {
91
+ params: '<user> <digest>',
92
+ instructions: 'Flush any currently queued events for a given user + digest',
93
+ example: `courier digest:flush user123 MY_DIGEST_TOPIC`,
94
+ component: params => {
95
+ return React.createElement(DigestFlush, { params: params });
96
+ },
97
+ });
98
+ export default mappings;
package/package.json ADDED
@@ -0,0 +1,61 @@
1
+ {
2
+ "name": "@trycourier/cli",
3
+ "version": "1.0.0",
4
+ "license": "MIT",
5
+ "bin": {
6
+ "courier": "dist/cli.js"
7
+ },
8
+ "type": "module",
9
+ "engines": {
10
+ "node": ">=16"
11
+ },
12
+ "scripts": {
13
+ "build": "tsc",
14
+ "dev": "tsc --watch",
15
+ "test": "prettier --check . && xo && ava",
16
+ "format": "prettier --write ."
17
+ },
18
+ "files": [
19
+ "dist"
20
+ ],
21
+ "dependencies": {
22
+ "cli-spinners": "^2.9.0",
23
+ "dotenv": "^16.0.3",
24
+ "ink": "^4.1.0",
25
+ "react": "^18.2.0",
26
+ "yargs-parser": "^21.1.1"
27
+ },
28
+ "devDependencies": {
29
+ "@sindresorhus/tsconfig": "^3.0.1",
30
+ "@types/react": "^18.0.32",
31
+ "@types/yargs-parser": "^21.0.0",
32
+ "@vdemedes/prettier-config": "^2.0.1",
33
+ "ava": "^5.2.0",
34
+ "chalk": "^5.2.0",
35
+ "eslint-config-xo-react": "^0.27.0",
36
+ "eslint-plugin-react": "^7.32.2",
37
+ "eslint-plugin-react-hooks": "^4.6.0",
38
+ "ink-testing-library": "^3.0.0",
39
+ "prettier": "^2.8.7",
40
+ "ts-node": "^10.9.1",
41
+ "typescript": "^5.0.3",
42
+ "xo": "^0.53.1"
43
+ },
44
+ "ava": {
45
+ "extensions": {
46
+ "ts": "module",
47
+ "tsx": "module"
48
+ },
49
+ "nodeArguments": [
50
+ "--loader=ts-node/esm"
51
+ ]
52
+ },
53
+ "xo": {
54
+ "extends": "xo-react",
55
+ "prettier": true,
56
+ "rules": {
57
+ "react/prop-types": "off"
58
+ }
59
+ },
60
+ "prettier": "@vdemedes/prettier-config"
61
+ }