Skip to content

Commit

Permalink
add support for users:get/set
Browse files Browse the repository at this point in the history
  • Loading branch information
troygoode committed Aug 9, 2023
1 parent fef84b8 commit af54b80
Show file tree
Hide file tree
Showing 8 changed files with 164 additions and 29 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ You can find your Courier API key in your [Courier Settings](https://app.courier
- `courier whoami` – Display the currently authenticated workspace
- `courier send` - Send a notification to a user, list, or audience
- `courier track` - Send a track event to trigger a Courier Automations
- `courier users:get` - Fetch the data for a given user ID
- `courier users:set` - Overwrite a user's profile with the provided data
- `courier digests:flush` – Flush any currently queued events for a given user + digest
- `courier translations:upload` - Upload .PO files to your Courier workspace
- `courier translations:download` - Download .PO files from your Courier workspace
Expand All @@ -60,6 +62,9 @@ $ courier send --user user123 --elemental my-template.json --foo bar
$ courier track my-event user123 --foo bar
$ courier users:get user123
$ courier users:set user123 --email [email protected] --tel 555-867-5309
$ courier digests:flush user123 my-digest-id
$ courier translations:upload en-US ./translations/en-US.po
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@trycourier/cli",
"version": "1.4.1",
"version": "1.5.0",
"license": "MIT",
"bin": {
"courier": "dist/cli.js"
Expand Down
17 changes: 12 additions & 5 deletions source/commands/Config.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,28 @@ type Params = {
};

export default ({params}: {params: Params}) => {
const FILE_PATH = `${process.cwd()}/.courier`;
const FILE_PATH = `${process.cwd()}/.courier`;

if (!params?.apikey) {
return <UhOh text="You must specify your API key using --apikey <your-api-key>" />;
return (
<UhOh text="You must specify your API key using --apikey <your-api-key>" />
);
}

if (fs.existsSync(FILE_PATH) && !params.overwrite) {
return <UhOh text={`${FILE_PATH} already exists. Consider adding the --overwrite option`} />;
return (
<UhOh
text={`${FILE_PATH} already exists. Consider adding the --overwrite option`}
/>
);
}

fs.writeFileSync(FILE_PATH, `COURIER_API_KEY=${params.apikey}\n`);

return (
<Text bold={true} color="green">
Your API key has been saved to {FILE_PATH}. Run "courier whoami" to verify API credentials.
Your API key has been saved to {FILE_PATH}. Run "courier whoami" to verify
API credentials.
</Text>
);
};
36 changes: 36 additions & 0 deletions source/commands/UsersGet.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import React, {useEffect, useState} from 'react';
import UhOh from '../components/UhOh.js';
import Request from '../components/Request.js';
import Response from '../components/Response.js';
import api from '../lib/api.js';

interface IResponse {
res: Response;
json?: any;
err?: Error;
}

export default ({params}: {params: any}) => {
const [resp, setResp] = useState<IResponse | undefined>();

const userId = params?._?.[0];
if (!userId) {
return <UhOh text="You must specify a user ID." />;
}

const request = {
method: 'GET',
url: `/profiles/${userId}`,
};

useEffect(() => {
api(request).then(res => setResp(res));
}, []);

return (
<>
<Request request={request} response={resp} />
<Response response={resp} />
</>
);
};
48 changes: 48 additions & 0 deletions source/commands/UsersSet.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import React, {useEffect, useState} from 'react';
import UhOh from '../components/UhOh.js';
import Request from '../components/Request.js';
import Response from '../components/Response.js';
import api from '../lib/api.js';

interface IResponse {
res: Response;
json?: any;
err?: Error;
}

export default ({params}: {params: any}) => {
const [resp, setResp] = useState<IResponse | undefined>();

const userId = params?._?.[0];
if (!userId) {
return <UhOh text="You must specify a user ID." />;
}

const {_, ...properties} = params;

if (properties.tel) {
properties.phone_number = properties.tel;
delete properties.tel;
}

const request = {
method: 'PUT',
url: `/profiles/${userId}`,
body: {
profile: {
...properties,
},
},
};

useEffect(() => {
api(request).then(res => setResp(res));
}, []);

return (
<>
<Request request={request} response={resp} />
<Response response={resp} />
</>
);
};
7 changes: 4 additions & 3 deletions source/components/Request.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,15 @@ type Props = {
};

export default (props: Props) => {
const url = `${process.env['COURIER_DOMAIN'] || 'https://api.courier.com'}${props.request.url}`;
const url = `${process.env['COURIER_DOMAIN'] || 'https://api.courier.com'}${
props.request.url
}`;
return (
<Box flexDirection="column">
<Box borderStyle="bold" borderColor="blue">
<Text>
{' '}
<Text bold={true}>{props.request.method}</Text>{' '}
{url}
<Text bold={true}>{props.request.method}</Text> {url}
</Text>
</Box>
{props.request.body ? (
Expand Down
39 changes: 22 additions & 17 deletions source/components/Router.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,23 +27,28 @@ export default ({args, mappings}: Props) => {
if ((apiKey && apiKey.length) || mapping.noApiKeyRequired) {
return mapping.component(parsedParams);
} else {
return <>
<Text bold={true} color="red">
No COURIER_API_KEY specified, please set via one of these options:
</Text>
<Text bold={true} color="red">
• running "courier config --apikey &lt;your-api-key&gt;"
</Text>
<Text bold={true} color="red">
• setting COURIER_API_KEY in your shell via "export COURIER_API_KEY=&lt;your-api-key&gt;"
</Text>
<Text bold={true} color="red">
• setting COURIER_API_KEY in a ".courier" file in your current working directory
</Text>
<Text bold={true} color="red">
• setting COURIER_API_KEY in a ".courier" file in your user's home directory
</Text>
</>
return (
<>
<Text bold={true} color="red">
No COURIER_API_KEY specified, please set via one of these options:
</Text>
<Text bold={true} color="red">
• running "courier config --apikey &lt;your-api-key&gt;"
</Text>
<Text bold={true} color="red">
• setting COURIER_API_KEY in your shell via "export
COURIER_API_KEY=&lt;your-api-key&gt;"
</Text>
<Text bold={true} color="red">
• setting COURIER_API_KEY in a ".courier" file in your current
working directory
</Text>
<Text bold={true} color="red">
• setting COURIER_API_KEY in a ".courier" file in your user's home
directory
</Text>
</>
);
}
} else {
return <Help mappings={mappings} />;
Expand Down
39 changes: 36 additions & 3 deletions source/mappings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import Help from './commands/Help.js';
import Config from './commands/Config.js';
import WhoAmI from './commands/WhoAmI.js';
import Track from './commands/Track.js';
import UsersGet from './commands/UsersGet.js';
import UsersSet from './commands/UsersSet.js';
import Send from './commands/Send.js';
import DigestFlush from './commands/DigestFlush.js';
import TranslationsDownload from './commands/TranslationsDownload.js';
Expand All @@ -27,10 +29,11 @@ mappings.set('help', {
component: () => {
return <Help mappings={mappings} />;
},
noApiKeyRequired: true
noApiKeyRequired: true,
});
mappings.set('config', {
instructions: 'Persist your Courier API key into a .courier file in your current working directory',
instructions:
'Persist your Courier API key into a .courier file in your current working directory',
component: params => {
return <Config params={params} />;
},
Expand All @@ -45,7 +48,7 @@ mappings.set('config', {
},
],
example: `courier config --apikey MY_API_KEY`,
noApiKeyRequired: true
noApiKeyRequired: true,
});
mappings.set('whoami', {
instructions: 'Display the currently authenticated workspace',
Expand Down Expand Up @@ -132,6 +135,36 @@ mappings.set('track', {
return <Track params={params} />;
},
});
mappings.set('users:get', {
params: '<user>',
instructions: 'Fetch the data for a given user ID',
example: `courier users:get user123`,
component: params => {
return <UsersGet params={params} />;
},
});
mappings.set('users:set', {
params: '<user>',
instructions: "Overwrite a user's profile with the provided data",
options: [
{
option: '--email <email address>',
value: '',
},
{
option: '--tel <phone number>',
value: '',
},
{
option: '--<key> <value>',
value: 'arbitrary key/value properties for your user',
},
],
example: `courier users:set user123 --email [email protected]`,
component: params => {
return <UsersSet params={params} />;
},
});
mappings.set('digests:flush', {
params: '<user> <digest>',
instructions: 'Flush any currently queued events for a given user + digest',
Expand Down

0 comments on commit af54b80

Please sign in to comment.