@trycourier/cli 1.2.1 → 1.4.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/README.md CHANGED
@@ -8,12 +8,13 @@ The Courier CLI is a developer tool to help you build, test, and manage your int
8
8
  - Track events to trigger your Courier Automations
9
9
  - Push & pull industry-standard translation files for internationalizing your content
10
10
 
11
- ## Install the Courier CLI
11
+ ## Installing the Courier CLI
12
12
 
13
- From the command-line, use the following command to install the Courier CLI:
13
+ From the command-line, use the following command to install the Courier CLI and set your API key:
14
14
 
15
15
  ```bash
16
16
  $ npm install -g @trycourier/cli
17
+ $ courier config --apikey <your-api-key>
17
18
  ```
18
19
 
19
20
  ### Requirements
@@ -22,6 +23,12 @@ $ npm install -g @trycourier/cli
22
23
 
23
24
  ## Authenticate the CLI
24
25
 
26
+ The fastest way to get started is to run:
27
+
28
+ ```
29
+ $ courier config --apikey <your-api-key>
30
+ ```
31
+
25
32
  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
33
 
27
34
  - A `.courier` file in the current working directory
@@ -32,6 +39,7 @@ You can find your Courier API key in your [Courier Settings](https://app.courier
32
39
 
33
40
  ## Commands
34
41
 
42
+ - `courier config` – Set your Courier API key in `~/.courier`
35
43
  - `courier whoami` – Display the currently authenticated workspace
36
44
  - `courier send` - Send a notification to a user, list, or audience
37
45
  - `courier track` - Send an event to test your Courier Automations
package/dist/cli.js CHANGED
@@ -8,14 +8,7 @@ const CLI = async () => {
8
8
  const params = args(process.argv);
9
9
  const mappings = (await import('./mappings.js')).default;
10
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
- }
11
+ render(React.createElement(Router, { args: params, mappings: mappings }));
19
12
  };
20
13
  (async () => {
21
14
  await CLI();
@@ -0,0 +1,9 @@
1
+ import React from 'react';
2
+ type Params = {
3
+ apikey?: string;
4
+ overwrite?: boolean;
5
+ };
6
+ declare const _default: ({ params }: {
7
+ params: Params;
8
+ }) => React.JSX.Element;
9
+ export default _default;
@@ -0,0 +1,18 @@
1
+ import React from 'react';
2
+ import UhOh from '../components/UhOh.js';
3
+ import { Text } from 'ink';
4
+ import fs from 'fs';
5
+ export default ({ params }) => {
6
+ const FILE_PATH = `${process.cwd()}/.courier`;
7
+ if (!params?.apikey) {
8
+ return React.createElement(UhOh, { text: "You must specify your API key using --apikey <your-api-key>" });
9
+ }
10
+ if (fs.existsSync(FILE_PATH) && !params.overwrite) {
11
+ return React.createElement(UhOh, { text: `${FILE_PATH} already exists. Consider adding the --overwrite option` });
12
+ }
13
+ fs.writeFileSync(FILE_PATH, `COURIER_API_KEY=${params.apikey}\n`);
14
+ return (React.createElement(Text, { bold: true, color: "green" },
15
+ "Your API key has been saved to ",
16
+ FILE_PATH,
17
+ ". Run \"courier whoami\" to verify API credentials."));
18
+ };
@@ -20,7 +20,7 @@ export default ({ params }) => {
20
20
  useEffect(() => {
21
21
  api(request).then(res => setResp(res));
22
22
  }, []);
23
- return React.createElement(React.Fragment, null,
23
+ return (React.createElement(React.Fragment, null,
24
24
  React.createElement(Request, { request: request, response: resp }),
25
- React.createElement(Response, { response: resp }));
25
+ React.createElement(Response, { response: resp })));
26
26
  };
@@ -2,7 +2,7 @@ import React from 'react';
2
2
  interface IMapping {
3
3
  params?: string;
4
4
  instructions?: string;
5
- example?: string;
5
+ example?: string | string[];
6
6
  options?: {
7
7
  option: string;
8
8
  value?: string;
@@ -39,7 +39,9 @@ export default ({ mappings }) => {
39
39
  " ",
40
40
  o.value)))))
41
41
  : null,
42
- v.example ? (React.createElement(Text, { color: "cyan" },
42
+ v.example && Array.isArray(v.example) ? (v.example.map((e, i) => (React.createElement(Text, { color: "cyan", key: i },
43
+ ' ',
44
+ e)))) : v.example ? (React.createElement(Text, { color: "cyan" },
43
45
  ' ',
44
46
  v.example)) : null));
45
47
  })));
@@ -29,18 +29,18 @@ const constructPayload = (params) => {
29
29
  if (params.fcm) {
30
30
  to.push({ firebaseToken: params.fcm });
31
31
  }
32
- let content = {
32
+ let contentElemental = {
33
33
  title: undefined,
34
34
  body: undefined,
35
35
  };
36
36
  if (params.title) {
37
- content.title = params.title;
37
+ contentElemental.title = params.title;
38
38
  }
39
39
  if (params.body) {
40
- content.body = params.body;
40
+ contentElemental.body = params.body;
41
41
  }
42
42
  else if (params.message) {
43
- content.body = params.message;
43
+ contentElemental.body = params.message;
44
44
  }
45
45
  let routing = {
46
46
  channels: [],
@@ -61,36 +61,56 @@ const constructPayload = (params) => {
61
61
  if ((params.apn && params.apn.length) || (params.fcm && params.fcm.length)) {
62
62
  routing.channels.push('push');
63
63
  }
64
- const { _, user, list, audience, email, tel, apn, fcm, title, body, elemental, mock, ...data } = params;
65
- return {
66
- message: {
67
- to: to.length === 1 ? to[0] : to,
68
- content,
69
- routing: routing.channels.length ? routing : undefined,
70
- data: data ? data : undefined,
71
- },
72
- };
64
+ const { _, user, list, audience, email, tel, apn, fcm, title, body, message, template, channel, channels, all, elemental, mock, ...data } = params;
65
+ if (params.template) {
66
+ return {
67
+ type: 'template',
68
+ message: {
69
+ to: to.length === 1 ? to[0] : to,
70
+ template: params.template,
71
+ routing: routing.channels.length ? routing : undefined,
72
+ data: data ? data : undefined,
73
+ },
74
+ };
75
+ }
76
+ else {
77
+ return {
78
+ type: 'elemental',
79
+ message: {
80
+ to: to.length === 1 ? to[0] : to,
81
+ content: contentElemental,
82
+ routing: routing.channels.length ? routing : undefined,
83
+ data: data ? data : undefined,
84
+ },
85
+ };
86
+ }
73
87
  };
74
88
  export default ({ params }) => {
75
89
  const [resp, setResp] = useState();
76
- const payload = constructPayload(params);
90
+ if (!params.body && !params.template && !params.elemental) {
91
+ return (React.createElement(UhOh, { text: "You must specify a message body, template, or Elemental file path." }));
92
+ }
93
+ let payload = constructPayload(params);
77
94
  if (!payload.message.to) {
78
95
  return React.createElement(UhOh, { text: "You must specify a recipient." });
79
96
  }
80
- if (!params.elemental &&
97
+ if (payload.type === 'elemental' &&
98
+ !params.elemental &&
81
99
  (!payload.message.content.body || !payload.message.content.body?.length)) {
82
- return React.createElement(UhOh, { text: "You must specify a body for the message." });
100
+ throw new Error('You must specify a body for the message.');
83
101
  }
84
- if (params.elemental) {
102
+ if (params.elemental && payload.type === 'elemental') {
85
103
  if (!fs.existsSync(params.elemental)) {
86
- return React.createElement(UhOh, { text: "Invalid file path to Elemental document." });
104
+ throw new Error('Invalid file path to Elemental document.');
87
105
  }
88
106
  payload.message.content = JSON.parse(fs.readFileSync(params.elemental, 'utf8'));
89
107
  }
90
108
  const request = {
91
109
  method: 'POST',
92
110
  url: '/send',
93
- body: payload,
111
+ body: {
112
+ message: payload.message,
113
+ },
94
114
  };
95
115
  useEffect(() => {
96
116
  if (params.mock) {
@@ -103,8 +123,7 @@ export default ({ params }) => {
103
123
  params.elemental ? (React.createElement(React.Fragment, null,
104
124
  React.createElement(Box, { borderStyle: "bold", borderColor: "white" },
105
125
  React.createElement(Text, null, " Elemental")),
106
- React.createElement(Box, null,
107
- React.createElement(Elemental, { elemental: payload.message.content })))) : null,
126
+ React.createElement(Box, null, payload.type === 'elemental' ? (React.createElement(Elemental, { elemental: payload.message.content })) : null))) : null,
108
127
  React.createElement(React.Fragment, null,
109
128
  React.createElement(Request, { request: request, response: resp }),
110
129
  React.createElement(Response, { response: resp }))));
@@ -31,7 +31,7 @@ export default ({ params }) => {
31
31
  useEffect(() => {
32
32
  api(request).then(res => setResp(res));
33
33
  }, []);
34
- return React.createElement(React.Fragment, null,
34
+ return (React.createElement(React.Fragment, null,
35
35
  React.createElement(Request, { request: request, response: resp }),
36
- React.createElement(Response, { response: resp }));
36
+ React.createElement(Response, { response: resp })));
37
37
  };
@@ -29,7 +29,7 @@ export default ({ params }) => {
29
29
  useEffect(() => {
30
30
  api(request).then(res => setResp(res));
31
31
  }, []);
32
- return React.createElement(React.Fragment, null,
32
+ return (React.createElement(React.Fragment, null,
33
33
  React.createElement(Request, { request: request, response: resp }),
34
- React.createElement(Response, { response: resp }));
34
+ React.createElement(Response, { response: resp })));
35
35
  };
@@ -2,6 +2,7 @@ import React, { useEffect, useState } from 'react';
2
2
  import { Box } from 'ink';
3
3
  import KVP from '../components/KVP.js';
4
4
  import Request from '../components/Request.js';
5
+ import Response from '../components/Response.js';
5
6
  import api from '../lib/api.js';
6
7
  export default () => {
7
8
  const [resp, setResp] = useState();
@@ -14,7 +15,7 @@ export default () => {
14
15
  }, []);
15
16
  return (React.createElement(Box, { flexDirection: "column" },
16
17
  React.createElement(Request, { request: request, response: resp }),
17
- resp && resp.json ? (React.createElement(React.Fragment, null,
18
+ resp && resp.err ? (React.createElement(Response, { response: resp })) : resp && resp.json ? (React.createElement(React.Fragment, null,
18
19
  React.createElement(KVP, { width: 20, label: "Workspace Name", value: resp.json.tenantName }),
19
20
  React.createElement(KVP, { width: 20, label: "Workspace ID", value: resp.json.tenantId }),
20
21
  React.createElement(KVP, { width: 20, label: "API Key Environment", value: resp.json.environment }),
@@ -3,14 +3,14 @@ import { Box, Text } from 'ink';
3
3
  import Spinner from './Spinner.js';
4
4
  const defaultSpinnerText = 'blipping those bits and blooping those bytes';
5
5
  export default (props) => {
6
+ const url = `${process.env['COURIER_DOMAIN'] || 'https://api.courier.com'}${props.request.url}`;
6
7
  return (React.createElement(Box, { flexDirection: "column" },
7
8
  React.createElement(Box, { borderStyle: "bold", borderColor: "blue" },
8
9
  React.createElement(Text, null,
9
10
  ' ',
10
11
  React.createElement(Text, { bold: true }, props.request.method),
11
12
  ' ',
12
- "https://api.courier.com",
13
- props.request.url)),
13
+ url)),
14
14
  props.request.body ? (typeof props.request.body === 'string' ? (React.createElement(Text, null, props.request.body)) : (React.createElement(Text, null, JSON.stringify(props.request.body, undefined, ' ')))) : null,
15
15
  !props.request.body ? React.createElement(Text, { color: "gray" }, "No request body") : null,
16
16
  !props.response ? (React.createElement(Spinner, { text: props.spinnerText || defaultSpinnerText })) : null));
@@ -1,7 +1,7 @@
1
1
  import React from 'react';
2
2
  interface IMapping {
3
- instructions?: string;
4
3
  component: (params?: any) => React.ReactElement;
4
+ noApiKeyRequired?: boolean;
5
5
  }
6
6
  type Props = {
7
7
  args: string[];
@@ -1,6 +1,7 @@
1
1
  import React from 'react';
2
- import Help from '../commands/Help.js';
3
2
  import yargs from 'yargs-parser';
3
+ import { Text } from 'ink';
4
+ import Help from '../commands/Help.js';
4
5
  export default ({ args, mappings }) => {
5
6
  if (!args.length || !args[0]) {
6
7
  return React.createElement(Help, { mappings: mappings });
@@ -9,7 +10,18 @@ export default ({ args, mappings }) => {
9
10
  const [, ...params] = args;
10
11
  const parsedParams = params.length ? yargs(params) : undefined;
11
12
  if (mapping) {
12
- return mapping.component(parsedParams);
13
+ const apiKey = process.env['COURIER_API_KEY'];
14
+ if ((apiKey && apiKey.length) || mapping.noApiKeyRequired) {
15
+ return mapping.component(parsedParams);
16
+ }
17
+ else {
18
+ return React.createElement(React.Fragment, null,
19
+ React.createElement(Text, { bold: true, color: "red" }, "No COURIER_API_KEY specified, please set via one of these options:"),
20
+ React.createElement(Text, { bold: true, color: "red" }, "\u2022 running \"courier config --apikey <your-api-key>\""),
21
+ React.createElement(Text, { bold: true, color: "red" }, "\u2022 setting COURIER_API_KEY in your shell via \"export COURIER_API_KEY=<your-api-key>\""),
22
+ React.createElement(Text, { bold: true, color: "red" }, "\u2022 setting COURIER_API_KEY in a \".courier\" file in your current working directory"),
23
+ React.createElement(Text, { bold: true, color: "red" }, "\u2022 setting COURIER_API_KEY in a \".courier\" file in your user's home directory"));
24
+ }
13
25
  }
14
26
  else {
15
27
  return React.createElement(Help, { mappings: mappings });
@@ -1,8 +1,9 @@
1
1
  import React from 'react';
2
2
  interface IMapping {
3
+ noApiKeyRequired?: boolean;
3
4
  params?: string;
4
5
  instructions?: string;
5
- example?: string;
6
+ example?: string | string[];
6
7
  options?: {
7
8
  option: string;
8
9
  value?: string;
package/dist/mappings.js CHANGED
@@ -1,5 +1,6 @@
1
1
  import React from 'react';
2
2
  import Help from './commands/Help.js';
3
+ import Config from './commands/Config.js';
3
4
  import WhoAmI from './commands/WhoAmI.js';
4
5
  import Track from './commands/Track.js';
5
6
  import Send from './commands/Send.js';
@@ -11,6 +12,25 @@ mappings.set('help', {
11
12
  component: () => {
12
13
  return React.createElement(Help, { mappings: mappings });
13
14
  },
15
+ noApiKeyRequired: true
16
+ });
17
+ mappings.set('config', {
18
+ instructions: 'Persist your Courier API key into a .courier file in your current working directory',
19
+ component: params => {
20
+ return React.createElement(Config, { params: params });
21
+ },
22
+ options: [
23
+ {
24
+ option: '--apikey <Courier API Key>',
25
+ value: '',
26
+ },
27
+ {
28
+ option: '--overwrite',
29
+ value: 'overwrite an existing .courier file',
30
+ },
31
+ ],
32
+ example: `courier config --apikey MY_API_KEY`,
33
+ noApiKeyRequired: true
14
34
  });
15
35
  mappings.set('whoami', {
16
36
  instructions: 'Display the currently authenticated workspace',
@@ -19,7 +39,7 @@ mappings.set('whoami', {
19
39
  },
20
40
  });
21
41
  mappings.set('send', {
22
- instructions: 'Send a notification to a user, list, or audience',
42
+ instructions: 'Send a notification to a user, list, or audience. Unrecognized parameters will be sent as message data.',
23
43
  options: [
24
44
  {
25
45
  option: '--email <email address>',
@@ -61,6 +81,10 @@ mappings.set('send', {
61
81
  option: '--elemental <filepath>',
62
82
  value: 'path to Courier Elemental JSON file',
63
83
  },
84
+ {
85
+ option: '--template <template ID or alias>',
86
+ value: 'ID or alias of a template stored in Courier',
87
+ },
64
88
  {
65
89
  option: '--channels <channel>',
66
90
  value: 'comma-delimted list of channels to send to',
@@ -70,7 +94,10 @@ mappings.set('send', {
70
94
  value: 'send to all channels for each recipient (default is "single")',
71
95
  },
72
96
  ],
73
- example: `courier send --tel 555-867-5309 --body "Hey Jenny\\!"`,
97
+ example: [
98
+ `courier send --tel 555-867-5309 --body "Hey Jenny\\!"`,
99
+ `courier send --user user123 --template my-template-id --foo bar`,
100
+ ],
74
101
  component: params => {
75
102
  return React.createElement(Send, { params: params });
76
103
  },
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@trycourier/cli",
3
- "version": "1.2.1",
3
+ "version": "1.4.0",
4
4
  "license": "MIT",
5
5
  "bin": {
6
6
  "courier": "dist/cli.js"