@trycourier/cli 1.0.1 → 1.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/README.md CHANGED
@@ -35,6 +35,8 @@ You can find your Courier API key in your [Courier Settings](https://app.courier
35
35
  - `courier whoami` – Display the currently authenticated workspace
36
36
  - `courier send` - Send a notification to a user, list, or audience
37
37
  - `courier track` - Send an event to test your Courier Automations
38
+ - `courier digests:flush` - Send an event to test your Courier Automations
39
+ - `courier translations:upload` - Upload .PO files to your Courier workspace
38
40
 
39
41
  For more details, run `courier` to see a list of commands and their arguments & options.
40
42
 
@@ -1,6 +1,7 @@
1
1
  import React, { useEffect, useState } from 'react';
2
2
  import UhOh from '../components/UhOh.js';
3
3
  import Request from '../components/Request.js';
4
+ import Response from '../components/Response.js';
4
5
  import api from '../lib/api.js';
5
6
  export default ({ params }) => {
6
7
  const [resp, setResp] = useState();
@@ -19,5 +20,7 @@ export default ({ params }) => {
19
20
  useEffect(() => {
20
21
  api(request).then(res => setResp(res));
21
22
  }, []);
22
- return React.createElement(Request, { request: request, response: resp });
23
+ return React.createElement(React.Fragment, null,
24
+ React.createElement(Request, { request: request, response: resp }),
25
+ React.createElement(Response, { response: resp }));
23
26
  };
@@ -2,6 +2,7 @@ import React, { useEffect, useState } from 'react';
2
2
  import fs from 'fs';
3
3
  import UhOh from '../components/UhOh.js';
4
4
  import Request from '../components/Request.js';
5
+ import Response from '../components/Response.js';
5
6
  import Elemental from '../components/Elemental.js';
6
7
  import api from '../lib/api.js';
7
8
  import { Box, Text } from 'ink';
@@ -101,10 +102,10 @@ export default ({ params }) => {
101
102
  return (React.createElement(Box, { flexDirection: "column" },
102
103
  params.elemental ? (React.createElement(React.Fragment, null,
103
104
  React.createElement(Box, { borderStyle: "bold", borderColor: "white" },
104
- React.createElement(Text, null,
105
- ' ',
106
- "Elemental")),
105
+ React.createElement(Text, null, " Elemental")),
107
106
  React.createElement(Box, null,
108
107
  React.createElement(Elemental, { elemental: payload.message.content })))) : null,
109
- React.createElement(Request, { request: request, response: resp })));
108
+ React.createElement(React.Fragment, null,
109
+ React.createElement(Request, { request: request, response: resp }),
110
+ React.createElement(Response, { response: resp }))));
110
111
  };
@@ -1,6 +1,7 @@
1
1
  import React, { useEffect, useState } from 'react';
2
2
  import UhOh from '../components/UhOh.js';
3
3
  import Request from '../components/Request.js';
4
+ import Response from '../components/Response.js';
4
5
  import api from '../lib/api.js';
5
6
  export default ({ params }) => {
6
7
  const [resp, setResp] = useState();
@@ -30,5 +31,7 @@ export default ({ params }) => {
30
31
  useEffect(() => {
31
32
  api(request).then(res => setResp(res));
32
33
  }, []);
33
- return React.createElement(Request, { request: request, response: resp });
34
+ return React.createElement(React.Fragment, null,
35
+ React.createElement(Request, { request: request, response: resp }),
36
+ React.createElement(Response, { response: resp }));
34
37
  };
@@ -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,28 @@
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
+ export default ({ params }) => {
7
+ const [resp, setResp] = useState();
8
+ const locale = params?._?.[0];
9
+ if (!locale) {
10
+ return React.createElement(UhOh, { text: "You must specify a locale, e.g. en-US." });
11
+ }
12
+ const filepath = params?._?.[1];
13
+ if (!filepath) {
14
+ return React.createElement(UhOh, { text: "You must specify a path to the .PO file." });
15
+ }
16
+ else if (!fs.existsSync(filepath)) {
17
+ return React.createElement(UhOh, { text: "No .PO file found at given path." });
18
+ }
19
+ const request = {
20
+ method: 'PUT',
21
+ url: `/translations/global/${locale}`,
22
+ body: fs.readFileSync(filepath, 'utf8'),
23
+ };
24
+ useEffect(() => {
25
+ api(request).then(res => setResp(res));
26
+ }, []);
27
+ return React.createElement(Request, { request: request, response: resp });
28
+ };
@@ -0,0 +1,10 @@
1
+ import React from 'react';
2
+ type Params = {
3
+ _?: string[];
4
+ text?: boolean;
5
+ domain?: string;
6
+ };
7
+ declare const _default: ({ params }: {
8
+ params: Params;
9
+ }) => React.JSX.Element;
10
+ export default _default;
@@ -0,0 +1,28 @@
1
+ import React, { useEffect, useState } from 'react';
2
+ import { Box, Text } from 'ink';
3
+ import UhOh from '../components/UhOh.js';
4
+ import Request from '../components/Request.js';
5
+ import Response from '../components/Response.js';
6
+ import api from '../lib/api.js';
7
+ export default ({ params }) => {
8
+ const [resp, setResp] = useState();
9
+ const locale = params?._?.[0];
10
+ if (!locale) {
11
+ return React.createElement(UhOh, { text: "You must specify a locale, e.g. en-US." });
12
+ }
13
+ const isText = params.text || false;
14
+ const request = {
15
+ method: 'GET',
16
+ url: `/translations/${params.domain || 'default'}/${locale}`,
17
+ };
18
+ useEffect(() => {
19
+ api(request).then(res => {
20
+ res.res.headers.get('content-type');
21
+ setResp(res);
22
+ });
23
+ }, []);
24
+ return isText ? (React.createElement(Box, null,
25
+ React.createElement(Text, null, resp?.text))) : (React.createElement(React.Fragment, null,
26
+ React.createElement(Request, { request: request, response: resp }),
27
+ React.createElement(Response, { response: resp })));
28
+ };
@@ -0,0 +1,9 @@
1
+ import React from 'react';
2
+ type Params = {
3
+ _?: string[];
4
+ domain?: string;
5
+ };
6
+ declare const _default: ({ params }: {
7
+ params: Params;
8
+ }) => React.JSX.Element;
9
+ export default _default;
@@ -0,0 +1,35 @@
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 Response from '../components/Response.js';
6
+ import api from '../lib/api.js';
7
+ export default ({ params }) => {
8
+ const [resp, setResp] = useState();
9
+ const locale = params?._?.[0];
10
+ if (!locale) {
11
+ return React.createElement(UhOh, { text: "You must specify a locale, e.g. en-US." });
12
+ }
13
+ const filepath = params?._?.[1];
14
+ if (!filepath) {
15
+ return React.createElement(UhOh, { text: "You must specify a path to the .PO file." });
16
+ }
17
+ else if (!fs.existsSync(filepath)) {
18
+ return React.createElement(UhOh, { text: "No .PO file found at given path." });
19
+ }
20
+ const po = fs.readFileSync(filepath, 'utf8');
21
+ const request = {
22
+ method: 'PUT',
23
+ url: `/translations/${params.domain || 'default'}/${locale}`,
24
+ headers: {
25
+ 'Content-Type': 'text/plain',
26
+ },
27
+ body: po,
28
+ };
29
+ useEffect(() => {
30
+ api(request).then(res => setResp(res));
31
+ }, []);
32
+ return React.createElement(React.Fragment, null,
33
+ React.createElement(Request, { request: request, response: resp }),
34
+ React.createElement(Response, { response: resp }));
35
+ };
@@ -13,7 +13,7 @@ export default () => {
13
13
  api(request).then(res => setResp(res));
14
14
  }, []);
15
15
  return (React.createElement(Box, { flexDirection: "column" },
16
- React.createElement(Request, { request: request, response: resp, hideResponse: true }),
16
+ React.createElement(Request, { request: request, response: resp }),
17
17
  resp && resp.json ? (React.createElement(React.Fragment, null,
18
18
  React.createElement(KVP, { width: 20, label: "Workspace Name", value: resp.json.tenantName }),
19
19
  React.createElement(KVP, { width: 20, label: "Workspace ID", value: resp.json.tenantId }),
@@ -1,24 +1,23 @@
1
1
  import React from 'react';
2
- import { Box, Text, Spacer } from 'ink';
2
+ import { Box, Text } from 'ink';
3
3
  export default ({ elemental }) => {
4
4
  const buckets = [];
5
- elemental.elements.map((element) => {
5
+ elemental.elements.map(element => {
6
6
  const { type, ...rest } = element;
7
7
  buckets.push({
8
8
  type: element.type,
9
- properties: Object.keys(rest).map((key) => ({
9
+ properties: Object.keys(rest).map(key => ({
10
10
  name: key,
11
- value: rest[key]
12
- }))
11
+ value: rest[key],
12
+ })),
13
13
  });
14
14
  });
15
- return React.createElement(Box, { flexDirection: 'column' }, buckets.map((bucket) => (bucket.properties.map((property) => (React.createElement(Box, null,
15
+ return (React.createElement(Box, { flexDirection: "column" }, buckets.map(bucket => bucket.properties.map(property => (React.createElement(Box, null,
16
16
  React.createElement(Box, { width: 16, paddingTop: 1 },
17
17
  React.createElement(Text, null,
18
18
  bucket.type,
19
19
  ".",
20
- property.name),
21
- React.createElement(Spacer, null)),
22
- React.createElement(Box, { borderStyle: 'round', flexDirection: 'column', paddingX: 1 },
20
+ property.name)),
21
+ React.createElement(Box, { borderStyle: "round", flexDirection: "column", paddingX: 1 },
23
22
  React.createElement(Text, null, property.value))))))));
24
23
  };
@@ -12,11 +12,11 @@ interface IResponse {
12
12
  interface IHttpResponse {
13
13
  status: number;
14
14
  statusText: string;
15
+ headers?: Headers;
15
16
  }
16
17
  type Props = {
17
18
  request: IRequest;
18
19
  response?: IResponse;
19
- hideResponse?: boolean;
20
20
  spinnerText?: string;
21
21
  };
22
22
  declare const _default: (props: Props) => React.JSX.Element;
@@ -11,21 +11,7 @@ export default (props) => {
11
11
  ' ',
12
12
  "https://api.courier.com",
13
13
  props.request.url)),
14
- props.request.body && (React.createElement(Text, null, JSON.stringify(props.request.body, undefined, ' '))),
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
- !props.response ? (React.createElement(Spinner, { text: props.spinnerText || defaultSpinnerText })) : null,
17
- props.response ? (React.createElement(Box, { borderStyle: "bold", borderColor: props.response?.err ? 'red' : 'green' },
18
- React.createElement(Text, null,
19
- ' ',
20
- "HTTP",
21
- ' ',
22
- React.createElement(Text, { bold: true }, props.response.err
23
- ? props.response.err.message
24
- : props.response?.res?.status),
25
- ' ',
26
- !props.response.err ? (React.createElement(React.Fragment, null,
27
- React.createElement(Text, null,
28
- "\u2022 ",
29
- props.response.res.statusText))) : null))) : null,
30
- 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));
16
+ !props.response ? (React.createElement(Spinner, { text: props.spinnerText || defaultSpinnerText })) : null));
31
17
  };
@@ -0,0 +1,16 @@
1
+ import React from 'react';
2
+ interface IResponse {
3
+ res: IHttpResponse;
4
+ json?: any;
5
+ err?: Error;
6
+ }
7
+ interface IHttpResponse {
8
+ status: number;
9
+ statusText: string;
10
+ headers?: Headers;
11
+ }
12
+ type Props = {
13
+ response?: IResponse;
14
+ };
15
+ declare const _default: (props: Props) => React.JSX.Element | null;
16
+ export default _default;
@@ -0,0 +1,29 @@
1
+ import React from 'react';
2
+ import { Box, Text } from 'ink';
3
+ function getContentType(res) {
4
+ return res.res.headers?.get('content-type') || 'none';
5
+ }
6
+ export default (props) => {
7
+ if (!props.response)
8
+ return null;
9
+ return (React.createElement(Box, { flexDirection: "column" },
10
+ React.createElement(Box, { flexDirection: "column", borderStyle: "bold", borderColor: props.response?.err ? 'red' : 'green' },
11
+ React.createElement(Text, null,
12
+ ' ',
13
+ "HTTP",
14
+ ' ',
15
+ React.createElement(Text, { bold: true }, props.response.err
16
+ ? props.response.err.message
17
+ : props.response?.res?.status),
18
+ ' ',
19
+ !props.response.err ? (React.createElement(React.Fragment, null,
20
+ React.createElement(Text, null,
21
+ "\u2022 ",
22
+ props.response.res.statusText))) : null),
23
+ !props.response.err &&
24
+ getContentType(props.response) !== 'application/json' ? (React.createElement(Box, null,
25
+ React.createElement(Text, null,
26
+ " Content-Type: ",
27
+ getContentType(props.response)))) : null),
28
+ props.response.json ? (React.createElement(Text, null, JSON.stringify(props.response.json, undefined, ' '))) : (React.createElement(Text, { color: "gray" }, "No response body"))));
29
+ };
package/dist/lib/api.d.ts CHANGED
@@ -2,11 +2,15 @@ interface IRequest {
2
2
  url: string;
3
3
  method: string;
4
4
  body?: object | string;
5
+ headers?: {
6
+ [key: string]: string;
7
+ };
5
8
  options?: RequestInit;
6
9
  }
7
10
  interface IResponse {
8
11
  res: Response;
9
12
  json?: any;
13
+ text?: string;
10
14
  err?: Error;
11
15
  }
12
16
  declare const _default: (request: IRequest) => Promise<IResponse>;
package/dist/lib/api.js CHANGED
@@ -4,18 +4,20 @@ const isString = (s) => {
4
4
  };
5
5
  export default async (request) => {
6
6
  const baseUrl = process.env['COURIER_DOMAIN'] || 'https://api.courier.com';
7
- return fetch(`${baseUrl}${request.url}`, {
7
+ const req = {
8
8
  method: request.method,
9
9
  headers: {
10
10
  Authorization: `Bearer ${process.env['COURIER_API_KEY']}`,
11
11
  'Content-Type': 'application/json',
12
12
  'User-Agent': `courier-cli/${VERSION}`,
13
+ ...request.headers,
13
14
  },
14
15
  body: request.body && !isString(request.body)
15
16
  ? JSON.stringify(request.body)
16
- : undefined,
17
+ : request.body?.toString(),
17
18
  ...request.options,
18
- }).then(res => {
19
+ };
20
+ return fetch(`${baseUrl}${request.url}`, req).then(res => {
19
21
  if (res.status > 400) {
20
22
  return {
21
23
  res,
@@ -26,7 +28,12 @@ export default async (request) => {
26
28
  return { res };
27
29
  }
28
30
  else {
29
- return res.json().then(json => ({ res, json }));
31
+ if (res.headers.get('Content-Type')?.includes('application/json')) {
32
+ return res.json().then(json => ({ res, json }));
33
+ }
34
+ else {
35
+ return res.text().then(text => ({ res, text }));
36
+ }
30
37
  }
31
38
  });
32
39
  };
package/dist/mappings.js CHANGED
@@ -4,6 +4,8 @@ import WhoAmI from './commands/WhoAmI.js';
4
4
  import Track from './commands/Track.js';
5
5
  import Send from './commands/Send.js';
6
6
  import DigestFlush from './commands/DigestFlush.js';
7
+ import TranslationsDownload from './commands/TranslationsDownload.js';
8
+ import TranslationsUpload from './commands/TranslationsUpload.js';
7
9
  const mappings = new Map();
8
10
  mappings.set('help', {
9
11
  component: () => {
@@ -87,12 +89,44 @@ mappings.set('track', {
87
89
  return React.createElement(Track, { params: params });
88
90
  },
89
91
  });
90
- mappings.set('digest:flush', {
92
+ mappings.set('digests:flush', {
91
93
  params: '<user> <digest>',
92
94
  instructions: 'Flush any currently queued events for a given user + digest',
93
- example: `courier digest:flush user123 MY_DIGEST_TOPIC`,
95
+ example: `courier digests:flush user123 MY_DIGEST_TOPIC`,
94
96
  component: params => {
95
97
  return React.createElement(DigestFlush, { params: params });
96
98
  },
97
99
  });
100
+ mappings.set('translations:upload', {
101
+ params: '<locale> <filepath>',
102
+ instructions: 'Upload a .PO file to Courier for a given locale',
103
+ options: [
104
+ {
105
+ option: '--domain <domain>',
106
+ value: 'a custom language domain (default is "default")',
107
+ },
108
+ ],
109
+ example: `courier translations:upload en-US ./translations/en-US.po`,
110
+ component: params => {
111
+ return React.createElement(TranslationsUpload, { params: params });
112
+ },
113
+ });
114
+ mappings.set('translations:download', {
115
+ params: '<locale>',
116
+ instructions: 'Download a .PO file to Courier for a given locale',
117
+ options: [
118
+ {
119
+ option: '--domain <domain>',
120
+ value: 'a custom language domain (default is "default")',
121
+ },
122
+ {
123
+ option: '--text',
124
+ value: 'only return plain text (e.g. for piping into a file)',
125
+ },
126
+ ],
127
+ example: `courier translations:download en-US --text > example.en-US.po`,
128
+ component: params => {
129
+ return React.createElement(TranslationsDownload, { params: params });
130
+ },
131
+ });
98
132
  export default mappings;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@trycourier/cli",
3
- "version": "1.0.1",
3
+ "version": "1.2.0",
4
4
  "license": "MIT",
5
5
  "bin": {
6
6
  "courier": "dist/cli.js"