@trycourier/cli 1.5.0 → 2.0.1
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 +57 -21
- package/dist/cli.js +38 -2
- package/dist/commands/Config.d.ts +2 -7
- package/dist/commands/Config.js +36 -9
- package/dist/commands/DigestFlush.js +3 -1
- package/dist/commands/Help.js +12 -16
- package/dist/commands/Send.js +45 -31
- package/dist/commands/Track.js +3 -1
- package/dist/commands/TranslationsDownload.js +3 -1
- package/dist/commands/TranslationsUpload.js +3 -1
- package/dist/commands/Upgrade.js +36 -0
- package/dist/commands/UsersBulk.js +159 -0
- package/dist/commands/UsersGet.js +3 -1
- package/dist/commands/UsersPreferences.js +41 -0
- package/dist/commands/UsersSet.js +3 -1
- package/dist/commands/WhoAmI.js +5 -2
- package/dist/components/Context.d.ts +43 -0
- package/dist/components/Context.js +75 -0
- package/dist/components/Router.d.ts +2 -9
- package/dist/components/Router.js +51 -20
- package/dist/components/SdkResponse.d.ts +8 -0
- package/dist/components/SdkResponse.js +20 -0
- package/dist/components/Table.d.ts +24 -0
- package/dist/components/Table.js +62 -0
- package/dist/constants.d.ts +1 -0
- package/dist/constants.js +1 -0
- package/dist/lib/api.d.ts +1 -1
- package/dist/lib/api.js +4 -4
- package/dist/lib/courier.d.ts +3 -0
- package/dist/lib/courier.js +7 -0
- package/dist/lib/load-env.js +1 -1
- package/dist/mappings.d.ts +4 -13
- package/dist/mappings.js +118 -15
- package/dist/version.d.ts +2 -0
- package/dist/version.js +2 -0
- package/package.json +24 -7
- package/dist/Counter.js +0 -16
- package/dist/Error.js +0 -4
- package/dist/Help.js +0 -13
- package/dist/app.d.ts +0 -6
- package/dist/app.js +0 -7
- package/dist/commands/DigestsFlush.d.ts +0 -5
- package/dist/commands/DigestsFlush.js +0 -23
- package/dist/commands/EventsTrack.d.ts +0 -5
- package/dist/commands/EventsTrack.js +0 -34
- package/dist/commands/TranslationUpload.d.ts +0 -5
- package/dist/commands/TranslationUpload.js +0 -28
- package/dist/components/Counter.d.ts +0 -3
- package/dist/components/Counter.js +0 -16
- package/dist/components/Error.d.ts +0 -5
- package/dist/components/Error.js +0 -5
- package/dist/components/Hello.d.ts +0 -5
- package/dist/components/Hello.js +0 -7
- package/dist/components/Help.d.ts +0 -9
- package/dist/components/Help.js +0 -23
- package/dist/components/WhoAmI.d.ts +0 -3
- package/dist/components/WhoAmI.js +0 -42
- package/dist/lib/render-elemental.d.ts +0 -2
- package/dist/lib/render-elemental.js +0 -1
- /package/dist/{Counter.d.ts → commands/Upgrade.d.ts} +0 -0
- /package/dist/{Error.d.ts → commands/UsersBulk.d.ts} +0 -0
- /package/dist/{Help.d.ts → commands/UsersPreferences.d.ts} +0 -0
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
import duckdb from 'duckdb';
|
|
2
|
+
import { Box, Text } from 'ink';
|
|
3
|
+
import React, { useEffect, useState } from 'react';
|
|
4
|
+
import { useBoolean, useCounter } from 'usehooks-ts';
|
|
5
|
+
import { useCliContext } from '../components/Context.js';
|
|
6
|
+
import Spinner from '../components/Spinner.js';
|
|
7
|
+
import UhOh from '../components/UhOh.js';
|
|
8
|
+
import _ from 'lodash';
|
|
9
|
+
import delay from '../lib/delay.js';
|
|
10
|
+
import { ProgressBar } from '@inkjs/ui';
|
|
11
|
+
const installExtension = (db, type) => {
|
|
12
|
+
if (['json', 'parquet'].includes(type || '')) {
|
|
13
|
+
db.exec(`
|
|
14
|
+
INSTALL ${type};
|
|
15
|
+
LOAD ${type};`);
|
|
16
|
+
}
|
|
17
|
+
};
|
|
18
|
+
const getFrom = (filename, type) => {
|
|
19
|
+
switch (type) {
|
|
20
|
+
case 'csv':
|
|
21
|
+
return `read_csv(['${filename}'], union_by_name = true)`;
|
|
22
|
+
case 'json':
|
|
23
|
+
return `read_json_auto(['${filename}'])`;
|
|
24
|
+
case 'parquet':
|
|
25
|
+
return `read_parquet(['${filename}'])`;
|
|
26
|
+
}
|
|
27
|
+
};
|
|
28
|
+
const getFileType = (filename) => {
|
|
29
|
+
if (filename.endsWith('.csv')) {
|
|
30
|
+
return 'csv';
|
|
31
|
+
}
|
|
32
|
+
else if (filename.endsWith('.json') || filename.endsWith('.jsonl')) {
|
|
33
|
+
return 'json';
|
|
34
|
+
}
|
|
35
|
+
else if (filename.endsWith('.parquet') ||
|
|
36
|
+
filename.endsWith('.pq') ||
|
|
37
|
+
filename.endsWith('.parq')) {
|
|
38
|
+
return 'parquet';
|
|
39
|
+
}
|
|
40
|
+
else {
|
|
41
|
+
return undefined;
|
|
42
|
+
}
|
|
43
|
+
};
|
|
44
|
+
export default () => {
|
|
45
|
+
const { parsedParams, courier } = useCliContext();
|
|
46
|
+
const [error, setError] = useState();
|
|
47
|
+
const processing = useBoolean(false);
|
|
48
|
+
const [data, setData] = useState();
|
|
49
|
+
const [data_errors, setDataErrors] = useState([]);
|
|
50
|
+
const counter = useCounter(0);
|
|
51
|
+
// const [resp, setResp] = useState<IResponse | undefined>();
|
|
52
|
+
const filename = String(_.get(parsedParams, ['_', 0], ''));
|
|
53
|
+
const filetype = getFileType(filename);
|
|
54
|
+
const db = new duckdb.Database(':memory:'); // or a file name for a persistent DB
|
|
55
|
+
installExtension(db, filetype);
|
|
56
|
+
const keep_flat = Boolean(parsedParams['keep-flat']);
|
|
57
|
+
const remove_nulls = Boolean(parsedParams['remove-nulls']);
|
|
58
|
+
const replace = Boolean(parsedParams['replace']);
|
|
59
|
+
const lists = String(_.get(parsedParams, ['list'], ''))
|
|
60
|
+
.split(',')
|
|
61
|
+
.map(l => l.trim())
|
|
62
|
+
.filter(l => l.length > 0);
|
|
63
|
+
const tenants = String(_.get(parsedParams, ['tenant'], ''))
|
|
64
|
+
.split(',')
|
|
65
|
+
.map(l => l.trim())
|
|
66
|
+
.filter(t => t.length > 0);
|
|
67
|
+
useEffect(() => {
|
|
68
|
+
if (filetype) {
|
|
69
|
+
getData(filetype);
|
|
70
|
+
}
|
|
71
|
+
else {
|
|
72
|
+
setError('File type not supported.');
|
|
73
|
+
}
|
|
74
|
+
}, []);
|
|
75
|
+
useEffect(() => {
|
|
76
|
+
if (data) {
|
|
77
|
+
processData();
|
|
78
|
+
}
|
|
79
|
+
}, [data]);
|
|
80
|
+
const getData = (type) => {
|
|
81
|
+
processing.setTrue();
|
|
82
|
+
const sql = `SELECT * FROM ${getFrom(filename, type)} ;`;
|
|
83
|
+
db.all(sql, (err, result) => {
|
|
84
|
+
if (err) {
|
|
85
|
+
setError(err.message);
|
|
86
|
+
}
|
|
87
|
+
else {
|
|
88
|
+
setData(result);
|
|
89
|
+
}
|
|
90
|
+
});
|
|
91
|
+
};
|
|
92
|
+
const processData = async () => {
|
|
93
|
+
if (data?.length) {
|
|
94
|
+
for (let i = 0; i < data.length; i++) {
|
|
95
|
+
let { user_id, ...profile } = data[i] || {};
|
|
96
|
+
let userId = user_id ? String(user_id) : undefined;
|
|
97
|
+
if (!userId) {
|
|
98
|
+
setDataErrors(p => [...p, `user_id not found in index ${i}`]);
|
|
99
|
+
}
|
|
100
|
+
else {
|
|
101
|
+
try {
|
|
102
|
+
Object.entries(profile).forEach(([key, value]) => {
|
|
103
|
+
if (filetype === 'csv' && !keep_flat) {
|
|
104
|
+
_.unset(profile, key);
|
|
105
|
+
_.set(profile, key, value);
|
|
106
|
+
}
|
|
107
|
+
if (value === null && remove_nulls) {
|
|
108
|
+
_.unset(profile, key);
|
|
109
|
+
}
|
|
110
|
+
});
|
|
111
|
+
if (replace) {
|
|
112
|
+
await courier.profiles.replace(userId, { profile });
|
|
113
|
+
}
|
|
114
|
+
else {
|
|
115
|
+
await courier.profiles.create(userId, { profile });
|
|
116
|
+
}
|
|
117
|
+
if (lists.length) {
|
|
118
|
+
await courier.profiles.subscribeToLists(userId, {
|
|
119
|
+
lists: lists.map(l => ({ listId: l })),
|
|
120
|
+
});
|
|
121
|
+
}
|
|
122
|
+
if (tenants.length) {
|
|
123
|
+
await courier.users.tenants.addMultple(userId, {
|
|
124
|
+
tenants: tenants.map(t => ({ tenant_id: t })),
|
|
125
|
+
});
|
|
126
|
+
}
|
|
127
|
+
counter.increment();
|
|
128
|
+
delay(10000);
|
|
129
|
+
}
|
|
130
|
+
catch (err) {
|
|
131
|
+
setDataErrors(p => [
|
|
132
|
+
...p,
|
|
133
|
+
`user_id (${user_id}) failed to update in index ${i}: ${String(err)}`,
|
|
134
|
+
]);
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
processing.setFalse();
|
|
140
|
+
};
|
|
141
|
+
if (!filename?.length) {
|
|
142
|
+
return React.createElement(UhOh, { text: "You must specify a filename." });
|
|
143
|
+
}
|
|
144
|
+
else if (error?.length) {
|
|
145
|
+
return React.createElement(UhOh, { text: error });
|
|
146
|
+
}
|
|
147
|
+
else if (data && processing.value) {
|
|
148
|
+
return (React.createElement(React.Fragment, null,
|
|
149
|
+
React.createElement(ProgressBar, { value: Math.floor((counter.count + 1 / data.length) * 100) }),
|
|
150
|
+
React.createElement(Spinner, { text: `Completed Rows: ${counter.count} / ${data.length}` })));
|
|
151
|
+
}
|
|
152
|
+
else {
|
|
153
|
+
return (React.createElement(Box, { flexDirection: "column", marginY: 1 },
|
|
154
|
+
React.createElement(Text, { color: 'green' }, `Completed Rows: ${counter.count} / ${data?.length || 0}`),
|
|
155
|
+
data_errors.map((err, i) => {
|
|
156
|
+
return React.createElement(UhOh, { key: i, text: err });
|
|
157
|
+
})));
|
|
158
|
+
}
|
|
159
|
+
};
|
|
@@ -3,7 +3,9 @@ import UhOh from '../components/UhOh.js';
|
|
|
3
3
|
import Request from '../components/Request.js';
|
|
4
4
|
import Response from '../components/Response.js';
|
|
5
5
|
import api from '../lib/api.js';
|
|
6
|
+
import { useCliContext } from '../components/Context.js';
|
|
6
7
|
export default ({ params }) => {
|
|
8
|
+
const { apikey, url } = useCliContext();
|
|
7
9
|
const [resp, setResp] = useState();
|
|
8
10
|
const userId = params?._?.[0];
|
|
9
11
|
if (!userId) {
|
|
@@ -14,7 +16,7 @@ export default ({ params }) => {
|
|
|
14
16
|
url: `/profiles/${userId}`,
|
|
15
17
|
};
|
|
16
18
|
useEffect(() => {
|
|
17
|
-
api(request).then(res => setResp(res));
|
|
19
|
+
api(request, url, apikey).then(res => setResp(res));
|
|
18
20
|
}, []);
|
|
19
21
|
return (React.createElement(React.Fragment, null,
|
|
20
22
|
React.createElement(Request, { request: request, response: resp }),
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { Box, Text } from 'ink';
|
|
2
|
+
import lodash from 'lodash';
|
|
3
|
+
import React, { useEffect, useState } from 'react';
|
|
4
|
+
import { useCliContext } from '../components/Context.js';
|
|
5
|
+
import SdkResponse from '../components/SdkResponse.js';
|
|
6
|
+
import UhOh from '../components/UhOh.js';
|
|
7
|
+
import Spinner from '../components/Spinner.js';
|
|
8
|
+
export default () => {
|
|
9
|
+
const { courier, parsedParams } = useCliContext();
|
|
10
|
+
const params = parsedParams;
|
|
11
|
+
const [resp, setResp] = useState();
|
|
12
|
+
const [err, setErr] = useState();
|
|
13
|
+
const userId = lodash.get(params, ['_', 0]);
|
|
14
|
+
const verbose = lodash.get(params, ['verbose'], false);
|
|
15
|
+
useEffect(() => {
|
|
16
|
+
getPreferences();
|
|
17
|
+
}, []);
|
|
18
|
+
const getPreferences = async () => {
|
|
19
|
+
try {
|
|
20
|
+
const user = await courier.users.preferences.list(userId);
|
|
21
|
+
setResp(user);
|
|
22
|
+
}
|
|
23
|
+
catch (e) {
|
|
24
|
+
setErr(e);
|
|
25
|
+
}
|
|
26
|
+
};
|
|
27
|
+
if (!userId) {
|
|
28
|
+
return React.createElement(UhOh, { text: "You must specify a user ID." });
|
|
29
|
+
}
|
|
30
|
+
else {
|
|
31
|
+
const short_preferences = lodash.map(lodash.get(resp, ['items']), item => {
|
|
32
|
+
return `${item.topic_name}: ${item.status}`;
|
|
33
|
+
});
|
|
34
|
+
return (React.createElement(Box, { marginY: 1, flexDirection: "column" }, !(resp || err) ? (React.createElement(Spinner, { text: `Fetching user preferences for ${userId}...` })) : (React.createElement(React.Fragment, null,
|
|
35
|
+
React.createElement(Text, { color: "green" },
|
|
36
|
+
"User preferences for ",
|
|
37
|
+
userId,
|
|
38
|
+
":"),
|
|
39
|
+
React.createElement(SdkResponse, { response: verbose ? resp : short_preferences, error: err })))));
|
|
40
|
+
}
|
|
41
|
+
};
|
|
@@ -3,7 +3,9 @@ import UhOh from '../components/UhOh.js';
|
|
|
3
3
|
import Request from '../components/Request.js';
|
|
4
4
|
import Response from '../components/Response.js';
|
|
5
5
|
import api from '../lib/api.js';
|
|
6
|
+
import { useCliContext } from '../components/Context.js';
|
|
6
7
|
export default ({ params }) => {
|
|
8
|
+
const { apikey, url } = useCliContext();
|
|
7
9
|
const [resp, setResp] = useState();
|
|
8
10
|
const userId = params?._?.[0];
|
|
9
11
|
if (!userId) {
|
|
@@ -24,7 +26,7 @@ export default ({ params }) => {
|
|
|
24
26
|
},
|
|
25
27
|
};
|
|
26
28
|
useEffect(() => {
|
|
27
|
-
api(request).then(res => setResp(res));
|
|
29
|
+
api(request, url, apikey).then(res => setResp(res));
|
|
28
30
|
}, []);
|
|
29
31
|
return (React.createElement(React.Fragment, null,
|
|
30
32
|
React.createElement(Request, { request: request, response: resp }),
|
package/dist/commands/WhoAmI.js
CHANGED
|
@@ -4,14 +4,16 @@ import KVP from '../components/KVP.js';
|
|
|
4
4
|
import Request from '../components/Request.js';
|
|
5
5
|
import Response from '../components/Response.js';
|
|
6
6
|
import api from '../lib/api.js';
|
|
7
|
+
import { useCliContext } from '../components/Context.js';
|
|
7
8
|
export default () => {
|
|
9
|
+
const { apikey, url } = useCliContext();
|
|
8
10
|
const [resp, setResp] = useState();
|
|
9
11
|
const request = {
|
|
10
12
|
url: '/debug',
|
|
11
13
|
method: 'POST',
|
|
12
14
|
};
|
|
13
15
|
useEffect(() => {
|
|
14
|
-
api(request).then(res => setResp(res));
|
|
16
|
+
api(request, url, apikey).then(res => setResp(res));
|
|
15
17
|
}, []);
|
|
16
18
|
return (React.createElement(Box, { flexDirection: "column" },
|
|
17
19
|
React.createElement(Request, { request: request, response: resp }),
|
|
@@ -19,5 +21,6 @@ export default () => {
|
|
|
19
21
|
React.createElement(KVP, { width: 20, label: "Workspace Name", value: resp.json.tenantName }),
|
|
20
22
|
React.createElement(KVP, { width: 20, label: "Workspace ID", value: resp.json.tenantId }),
|
|
21
23
|
React.createElement(KVP, { width: 20, label: "API Key Environment", value: resp.json.environment }),
|
|
22
|
-
React.createElement(KVP, { width: 20, label: "API Key Scope", value: resp.json.scope })
|
|
24
|
+
React.createElement(KVP, { width: 20, label: "API Key Scope", value: resp.json.scope }),
|
|
25
|
+
resp.json.mock && (React.createElement(KVP, { width: 20, label: "API Key Simulated (Mock)", value: resp.json.scope })))) : null));
|
|
23
26
|
};
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { CourierClient } from '@trycourier/courier';
|
|
2
|
+
import React from 'react';
|
|
3
|
+
import yargsParser from 'yargs-parser';
|
|
4
|
+
export type TEnvironment = 'test' | 'production';
|
|
5
|
+
export type TAPIRouting = 'normal' | 'simulated';
|
|
6
|
+
export type TDocumentScope = 'Published' | 'Draft' | 'Submitted';
|
|
7
|
+
interface ICliContextProvider {
|
|
8
|
+
args: string[];
|
|
9
|
+
mappings: Map<string, IMapping>;
|
|
10
|
+
current?: string;
|
|
11
|
+
latest?: string;
|
|
12
|
+
children: JSX.Element[] | JSX.Element;
|
|
13
|
+
}
|
|
14
|
+
interface ICliContext {
|
|
15
|
+
map?: string;
|
|
16
|
+
env_var: string;
|
|
17
|
+
apikey?: string;
|
|
18
|
+
environment: TEnvironment;
|
|
19
|
+
routing: TAPIRouting;
|
|
20
|
+
document_scope: TDocumentScope;
|
|
21
|
+
mappings: Map<string, IMapping>;
|
|
22
|
+
mapping?: IMapping;
|
|
23
|
+
parsedParams: yargsParser.Arguments;
|
|
24
|
+
args: string[];
|
|
25
|
+
url: string;
|
|
26
|
+
courier: CourierClient;
|
|
27
|
+
current?: string;
|
|
28
|
+
latest?: string;
|
|
29
|
+
}
|
|
30
|
+
type IUseCliContext = () => ICliContext;
|
|
31
|
+
export declare const useCliContext: IUseCliContext;
|
|
32
|
+
export declare const CliContextProvider: ({ args, mappings, current, latest, children, }: ICliContextProvider) => React.JSX.Element;
|
|
33
|
+
export declare const getApiKeyFlags: ({ environment, routing, document_scope, }: {
|
|
34
|
+
environment: TEnvironment;
|
|
35
|
+
routing: TAPIRouting;
|
|
36
|
+
document_scope: TDocumentScope;
|
|
37
|
+
}) => string;
|
|
38
|
+
export declare const getApiKeyVariable: ({ environment, routing, document_scope, }: {
|
|
39
|
+
environment: TEnvironment;
|
|
40
|
+
routing: TAPIRouting;
|
|
41
|
+
document_scope: TDocumentScope;
|
|
42
|
+
}) => string;
|
|
43
|
+
export {};
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import { CourierClient } from '@trycourier/courier';
|
|
2
|
+
import React, { createContext, useContext } from 'react';
|
|
3
|
+
import yargsParser from 'yargs-parser';
|
|
4
|
+
const CliContext = createContext({});
|
|
5
|
+
const PREFIX = 'COURIER_AUTH_TOKEN';
|
|
6
|
+
export const useCliContext = () => useContext(CliContext);
|
|
7
|
+
export const CliContextProvider = ({ args, mappings, current, latest, children, }) => {
|
|
8
|
+
const [map, ...params] = args;
|
|
9
|
+
const mapping = mappings.get(map || '');
|
|
10
|
+
const parsedParams = yargsParser(params);
|
|
11
|
+
const environment = parsedParams['production'] || parsedParams['P'] ? 'production' : 'test';
|
|
12
|
+
const routing = parsedParams['mock'] || parsedParams['M'] ? 'simulated' : 'normal';
|
|
13
|
+
let document_scope = 'Published';
|
|
14
|
+
if (parsedParams['draft'] || parsedParams['D']) {
|
|
15
|
+
document_scope = 'Draft';
|
|
16
|
+
}
|
|
17
|
+
else if (parsedParams['submitted'] || parsedParams['S']) {
|
|
18
|
+
document_scope = 'Submitted';
|
|
19
|
+
}
|
|
20
|
+
const env_var = getApiKeyVariable({ environment, routing, document_scope });
|
|
21
|
+
const apikey = parsedParams['apikey'] ?? process.env[env_var];
|
|
22
|
+
const url = parsedParams['apiurl'] ??
|
|
23
|
+
process.env['COURIER_API_URL'] ??
|
|
24
|
+
'https://api.courier.com';
|
|
25
|
+
const courier = new CourierClient({
|
|
26
|
+
authorizationToken: apikey,
|
|
27
|
+
environment: url,
|
|
28
|
+
});
|
|
29
|
+
const context = {
|
|
30
|
+
apikey,
|
|
31
|
+
mappings,
|
|
32
|
+
mapping,
|
|
33
|
+
parsedParams,
|
|
34
|
+
args,
|
|
35
|
+
environment,
|
|
36
|
+
routing,
|
|
37
|
+
document_scope,
|
|
38
|
+
env_var,
|
|
39
|
+
courier,
|
|
40
|
+
url,
|
|
41
|
+
map,
|
|
42
|
+
current,
|
|
43
|
+
latest,
|
|
44
|
+
};
|
|
45
|
+
return React.createElement(CliContext.Provider, { value: context }, children);
|
|
46
|
+
};
|
|
47
|
+
export const getApiKeyFlags = ({ environment, routing, document_scope, }) => {
|
|
48
|
+
let extra_flags = [];
|
|
49
|
+
if (environment === 'production') {
|
|
50
|
+
extra_flags.push('-P');
|
|
51
|
+
}
|
|
52
|
+
if (routing === 'simulated') {
|
|
53
|
+
extra_flags.push('-M');
|
|
54
|
+
}
|
|
55
|
+
if (document_scope === 'Draft') {
|
|
56
|
+
extra_flags.push('-D');
|
|
57
|
+
}
|
|
58
|
+
else if (document_scope === 'Submitted') {
|
|
59
|
+
extra_flags.push('-S');
|
|
60
|
+
}
|
|
61
|
+
return extra_flags.join(' ');
|
|
62
|
+
};
|
|
63
|
+
export const getApiKeyVariable = ({ environment, routing, document_scope, }) => {
|
|
64
|
+
let var_name = [PREFIX];
|
|
65
|
+
if (environment === 'test') {
|
|
66
|
+
var_name.push('TEST');
|
|
67
|
+
}
|
|
68
|
+
if (routing === 'simulated') {
|
|
69
|
+
var_name.push('MOCK');
|
|
70
|
+
}
|
|
71
|
+
if (document_scope !== 'Published') {
|
|
72
|
+
var_name.push(document_scope.toUpperCase());
|
|
73
|
+
}
|
|
74
|
+
return var_name.join('_').toUpperCase();
|
|
75
|
+
};
|
|
@@ -1,11 +1,4 @@
|
|
|
1
1
|
import React from 'react';
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
noApiKeyRequired?: boolean;
|
|
5
|
-
}
|
|
6
|
-
type Props = {
|
|
7
|
-
args: string[];
|
|
8
|
-
mappings: Map<string, IMapping>;
|
|
9
|
-
};
|
|
10
|
-
declare const _default: ({ args, mappings }: Props) => React.JSX.Element;
|
|
2
|
+
type Props = {};
|
|
3
|
+
declare const _default: ({}: Props) => React.JSX.Element;
|
|
11
4
|
export default _default;
|
|
@@ -1,29 +1,60 @@
|
|
|
1
|
-
import React from 'react';
|
|
2
|
-
import yargs from 'yargs-parser';
|
|
3
1
|
import { Text } from 'ink';
|
|
2
|
+
import React from 'react';
|
|
4
3
|
import Help from '../commands/Help.js';
|
|
5
|
-
|
|
6
|
-
|
|
4
|
+
import { getApiKeyFlags, useCliContext } from './Context.js';
|
|
5
|
+
import VERSION from '../version.js';
|
|
6
|
+
export default ({}) => {
|
|
7
|
+
const { args, mapping, parsedParams, mappings, apikey, env_var, environment, routing, document_scope, map, } = useCliContext();
|
|
8
|
+
if (args.filter(a => ['--version', '-V'].includes(a)).length) {
|
|
9
|
+
return React.createElement(Text, null,
|
|
10
|
+
"courier v",
|
|
11
|
+
VERSION);
|
|
12
|
+
}
|
|
13
|
+
else if (!args.length || !args[0]) {
|
|
7
14
|
return React.createElement(Help, { mappings: mappings });
|
|
8
15
|
}
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
+
else {
|
|
17
|
+
if (mapping) {
|
|
18
|
+
if (parsedParams['help']) {
|
|
19
|
+
return React.createElement(Help, { mappings: new Map([[map, mapping]]) });
|
|
20
|
+
}
|
|
21
|
+
else if ((apikey && apikey.length) || mapping.noApiKeyRequired) {
|
|
22
|
+
return mapping.component(parsedParams);
|
|
23
|
+
}
|
|
24
|
+
else {
|
|
25
|
+
const extra_flags = getApiKeyFlags({
|
|
26
|
+
environment,
|
|
27
|
+
routing,
|
|
28
|
+
document_scope,
|
|
29
|
+
});
|
|
30
|
+
return (React.createElement(React.Fragment, null,
|
|
31
|
+
React.createElement(Text, { bold: true, color: "red" },
|
|
32
|
+
"No ",
|
|
33
|
+
env_var,
|
|
34
|
+
" specified, please set via one of these options:"),
|
|
35
|
+
React.createElement(Text, { bold: true, color: "red" },
|
|
36
|
+
"\u2022 running \"courier config --apikey <your-api-key>",
|
|
37
|
+
' ',
|
|
38
|
+
extra_flags,
|
|
39
|
+
"\""),
|
|
40
|
+
React.createElement(Text, { bold: true, color: "red" },
|
|
41
|
+
"\u2022 setting ",
|
|
42
|
+
env_var,
|
|
43
|
+
" in your shell via \"export ",
|
|
44
|
+
env_var,
|
|
45
|
+
"=<your-api-key>\""),
|
|
46
|
+
React.createElement(Text, { bold: true, color: "red" },
|
|
47
|
+
"\u2022 setting ",
|
|
48
|
+
env_var,
|
|
49
|
+
" in a \".courier\" file in your current working directory"),
|
|
50
|
+
React.createElement(Text, { bold: true, color: "red" },
|
|
51
|
+
"\u2022 setting ",
|
|
52
|
+
env_var,
|
|
53
|
+
" in a \".courier\" file in your user's home directory")));
|
|
54
|
+
}
|
|
16
55
|
}
|
|
17
56
|
else {
|
|
18
|
-
return
|
|
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")));
|
|
57
|
+
return React.createElement(Help, { mappings: mappings });
|
|
24
58
|
}
|
|
25
59
|
}
|
|
26
|
-
else {
|
|
27
|
-
return React.createElement(Help, { mappings: mappings });
|
|
28
|
-
}
|
|
29
60
|
};
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import { CourierError } from '@trycourier/courier';
|
|
3
|
+
type IProps<T> = {
|
|
4
|
+
error?: CourierError;
|
|
5
|
+
response?: T;
|
|
6
|
+
};
|
|
7
|
+
declare const SdkResponse: <T extends object>({ error, response }: IProps<T>) => React.JSX.Element;
|
|
8
|
+
export default SdkResponse;
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import { Box, Text } from 'ink';
|
|
3
|
+
const Spacing = () => React.createElement(Text, null, " ");
|
|
4
|
+
const SdkResponse = ({ error, response }) => {
|
|
5
|
+
const status_code = error ? error.statusCode : '200';
|
|
6
|
+
return (React.createElement(Box, { flexDirection: "column" },
|
|
7
|
+
React.createElement(Box, { flexDirection: "column", borderStyle: "bold", borderColor: error ? 'red' : 'green' },
|
|
8
|
+
React.createElement(Text, null,
|
|
9
|
+
React.createElement(Spacing, null),
|
|
10
|
+
"HTTP",
|
|
11
|
+
React.createElement(Spacing, null),
|
|
12
|
+
React.createElement(Text, { bold: true }, status_code)),
|
|
13
|
+
error?.message?.length && (React.createElement(React.Fragment, null,
|
|
14
|
+
React.createElement(Text, { color: 'red' }, error.message),
|
|
15
|
+
React.createElement(Text, null, error.stack))),
|
|
16
|
+
response && Object.keys(response).length > 0 ? (React.createElement(React.Fragment, null,
|
|
17
|
+
React.createElement(Text, null, JSON.stringify(response, null, 2)))) : (React.createElement(React.Fragment, null,
|
|
18
|
+
React.createElement(Text, { color: "grey" }, "No response body"))))));
|
|
19
|
+
};
|
|
20
|
+
export default SdkResponse;
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
type Scalar = string | number | boolean | null | undefined;
|
|
3
|
+
type ScalarDict = {
|
|
4
|
+
[key: string]: Scalar;
|
|
5
|
+
};
|
|
6
|
+
type TableProps = {
|
|
7
|
+
data: ScalarDict[];
|
|
8
|
+
showHeaders?: boolean;
|
|
9
|
+
headerLabels?: {
|
|
10
|
+
[key: string]: string;
|
|
11
|
+
};
|
|
12
|
+
headerStyles?: {
|
|
13
|
+
color?: string;
|
|
14
|
+
backgroundColor?: string;
|
|
15
|
+
bold?: boolean;
|
|
16
|
+
italic?: boolean;
|
|
17
|
+
underline?: boolean;
|
|
18
|
+
inverse?: boolean;
|
|
19
|
+
strikethrough?: boolean;
|
|
20
|
+
dimColor?: boolean;
|
|
21
|
+
};
|
|
22
|
+
};
|
|
23
|
+
declare const Table: ({ data, showHeaders, headerStyles, headerLabels, }: TableProps) => React.JSX.Element;
|
|
24
|
+
export default Table;
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
// Table.tsx
|
|
2
|
+
import React from 'react';
|
|
3
|
+
import { Box, Text } from 'ink';
|
|
4
|
+
const Table = ({ data, showHeaders = true, headerStyles, headerLabels, }) => {
|
|
5
|
+
// Determine columns and their widths
|
|
6
|
+
const columns = getColumns(data, headerLabels);
|
|
7
|
+
return (React.createElement(Box, { flexDirection: "column", width: "100%" },
|
|
8
|
+
renderHeaderSeparators(columns),
|
|
9
|
+
showHeaders && (React.createElement(React.Fragment, null,
|
|
10
|
+
renderRow(columns.reduce((p, v) => {
|
|
11
|
+
p[v.key] = v.label || v.key;
|
|
12
|
+
return p;
|
|
13
|
+
}, {}), columns, {
|
|
14
|
+
color: 'blue',
|
|
15
|
+
bold: true,
|
|
16
|
+
...headerStyles,
|
|
17
|
+
}),
|
|
18
|
+
renderRowSeparators(columns))),
|
|
19
|
+
data.map((row, index) => (React.createElement(React.Fragment, { key: `row-${index}` },
|
|
20
|
+
index !== 0 && renderRowSeparators(columns),
|
|
21
|
+
renderRow(row, columns, { wrap: 'wrap' })))),
|
|
22
|
+
renderFooterSeparators(columns)));
|
|
23
|
+
};
|
|
24
|
+
// Helper function to determine columns and their widths
|
|
25
|
+
function getColumns(data, headerLabels) {
|
|
26
|
+
let columnWidths = {};
|
|
27
|
+
data.forEach(row => {
|
|
28
|
+
Object.keys(row).forEach(key => {
|
|
29
|
+
const valueLength = row[key]?.toString().length || 0;
|
|
30
|
+
columnWidths[key] = Math.max(columnWidths[key] || key.length, valueLength);
|
|
31
|
+
});
|
|
32
|
+
});
|
|
33
|
+
return Object.keys(columnWidths).map(key => ({
|
|
34
|
+
key: key,
|
|
35
|
+
label: headerLabels?.[key] || key,
|
|
36
|
+
width: (columnWidths[key] ?? 0) + 2, // adding padding
|
|
37
|
+
}));
|
|
38
|
+
}
|
|
39
|
+
// Helper function to render a row with separators
|
|
40
|
+
function renderRow(row, columns, textStyles) {
|
|
41
|
+
return (React.createElement(Box, { flexDirection: "row" },
|
|
42
|
+
React.createElement(Text, null, "\u2502"),
|
|
43
|
+
columns.map((column, index) => (React.createElement(React.Fragment, { key: column.key },
|
|
44
|
+
index !== 0 && React.createElement(Text, null, "\u2502"),
|
|
45
|
+
React.createElement(Box, { width: column.width, justifyContent: "flex-start" },
|
|
46
|
+
React.createElement(Text, { ...textStyles }, row[column.key]?.toString() || ''))))),
|
|
47
|
+
React.createElement(Text, null, "\u2502")));
|
|
48
|
+
}
|
|
49
|
+
function renderHeaderSeparators(columns) {
|
|
50
|
+
return renderRowSeparators(columns, '┌', '┬', '┐');
|
|
51
|
+
}
|
|
52
|
+
function renderFooterSeparators(columns) {
|
|
53
|
+
return renderRowSeparators(columns, '└', '┴', '┘');
|
|
54
|
+
}
|
|
55
|
+
function renderRowSeparators(columns, leftChar = '├', midChar = '┼', rightChar = '┤') {
|
|
56
|
+
return (React.createElement(Box, { flexDirection: "row" },
|
|
57
|
+
React.createElement(Text, null, leftChar),
|
|
58
|
+
columns.map((column, index) => (React.createElement(React.Fragment, { key: column.key },
|
|
59
|
+
React.createElement(Text, null, '─'.repeat(column.width)),
|
|
60
|
+
index < columns.length - 1 ? (React.createElement(Text, null, midChar)) : (React.createElement(Text, null, rightChar)))))));
|
|
61
|
+
}
|
|
62
|
+
export default Table;
|
package/dist/constants.d.ts
CHANGED
package/dist/constants.js
CHANGED
package/dist/lib/api.d.ts
CHANGED
package/dist/lib/api.js
CHANGED
|
@@ -1,15 +1,15 @@
|
|
|
1
|
-
|
|
1
|
+
import VERSION from "../version.js";
|
|
2
2
|
const isString = (s) => {
|
|
3
3
|
return typeof s === 'string' || s instanceof String;
|
|
4
4
|
};
|
|
5
|
-
export default async (request) => {
|
|
6
|
-
const baseUrl = process.env['COURIER_DOMAIN'] || 'https://api.courier.com';
|
|
5
|
+
export default async (request, baseUrl, apikey) => {
|
|
7
6
|
const req = {
|
|
8
7
|
method: request.method,
|
|
9
8
|
headers: {
|
|
10
|
-
Authorization: `Bearer ${
|
|
9
|
+
Authorization: `Bearer ${apikey}`,
|
|
11
10
|
'Content-Type': 'application/json',
|
|
12
11
|
'User-Agent': `courier-cli/${VERSION}`,
|
|
12
|
+
'X-Courier-CLI-Version': VERSION,
|
|
13
13
|
...request.headers,
|
|
14
14
|
},
|
|
15
15
|
body: request.body && !isString(request.body)
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { CourierClient } from "@trycourier/courier";
|
|
2
|
+
const getClient = (environment = 'production') => {
|
|
3
|
+
return new CourierClient({
|
|
4
|
+
authorizationToken: environment ? process.env['COURIER_AUTH_TOKEN'] : process.env['COURIER_AUTH_TOKEN_TEST'],
|
|
5
|
+
});
|
|
6
|
+
};
|
|
7
|
+
export default getClient;
|