@isnap/cli 0.0.0-stage → 0.1.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/LICENSE +21 -0
- package/README.md +119 -2
- package/dist/bin.js +16 -0
- package/dist/bin.js.map +1 -0
- package/dist/cli.js +107 -0
- package/dist/cli.js.map +1 -0
- package/dist/commands/lines.js +38 -0
- package/dist/commands/lines.js.map +1 -0
- package/dist/commands/listen.js +306 -0
- package/dist/commands/listen.js.map +1 -0
- package/dist/commands/login.js +38 -0
- package/dist/commands/login.js.map +1 -0
- package/dist/commands/logout.js +23 -0
- package/dist/commands/logout.js.map +1 -0
- package/dist/commands/messages.js +61 -0
- package/dist/commands/messages.js.map +1 -0
- package/dist/commands/send.js +41 -0
- package/dist/commands/send.js.map +1 -0
- package/dist/config.js +120 -0
- package/dist/config.js.map +1 -0
- package/dist/context.js +10 -0
- package/dist/context.js.map +1 -0
- package/dist/output.js +18 -0
- package/dist/output.js.map +1 -0
- package/dist/prompt.js +50 -0
- package/dist/prompt.js.map +1 -0
- package/package.json +48 -4
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 iSnap
|
|
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
CHANGED
|
@@ -1,3 +1,120 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @isnap/cli
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
The iSnap command line. Sign in with an API key, send a message, list your lines and messages, and receive your events on a local URL, from a terminal or a script. It is built on [`@isnap/sdk`](https://www.npmjs.com/package/@isnap/sdk).
|
|
4
|
+
|
|
5
|
+
The full documentation is at [docs.isnap.ai](https://docs.isnap.ai). For help, write to [support@isnap.ai](mailto:support@isnap.ai).
|
|
6
|
+
|
|
7
|
+
## Install
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
npm install --global @isnap/cli
|
|
11
|
+
isnap --help
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
Or run it without installing: `npx @isnap/cli --help`.
|
|
15
|
+
|
|
16
|
+
Node.js 20 or later is required.
|
|
17
|
+
|
|
18
|
+
## Sign in
|
|
19
|
+
|
|
20
|
+
Create an API key in the dashboard, then:
|
|
21
|
+
|
|
22
|
+
```bash
|
|
23
|
+
isnap login
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
`isnap login` asks for the key with no echo, checks it with the API, and stores it. Nothing is stored when the API refuses the key: the command exits with a non-zero code and prints the error code the API returned.
|
|
27
|
+
|
|
28
|
+
In a script, give the key on stdin: `echo "$ISNAP_KEY" | isnap login`.
|
|
29
|
+
|
|
30
|
+
An API key is a secret. The CLI never prints it, logs it or shows it in an error.
|
|
31
|
+
|
|
32
|
+
`isnap logout` deletes the stored key.
|
|
33
|
+
|
|
34
|
+
## Commands
|
|
35
|
+
|
|
36
|
+
| Command | What it does |
|
|
37
|
+
| --------------------------------------------------------- | ------------------------------------------------------------------- |
|
|
38
|
+
| `isnap login [--api-url <url>]` | Checks an API key and stores it |
|
|
39
|
+
| `isnap logout` | Deletes the stored key |
|
|
40
|
+
| `isnap lines list` | Lists your lines that are active or reserved |
|
|
41
|
+
| `isnap send --from <line_id> --to <handle> --text <text>` | Sends a message from one of your lines and prints its id and status |
|
|
42
|
+
| `isnap messages list [--line <line_id>] [--limit <n>]` | Lists your messages, newest first, 25 by default |
|
|
43
|
+
| `isnap listen --forward-to <url>` | Sends your live events to a local URL, signed like a webhook |
|
|
44
|
+
|
|
45
|
+
`isnap send` sends a real message. `--from` is a line id as `isnap lines list` shows it, and `--to` is a phone number such as `+14155550123` or an email address.
|
|
46
|
+
|
|
47
|
+
iSnap keeps a message for a short time only, so `isnap messages list` shows the messages it still holds.
|
|
48
|
+
|
|
49
|
+
```console
|
|
50
|
+
$ isnap lines list
|
|
51
|
+
ID NUMBER KIND STATUS
|
|
52
|
+
line_8Xk2mQ4vTn7Bw9Rz3Lc6Yp1Hd +14155550123 rental_iphone active
|
|
53
|
+
|
|
54
|
+
$ isnap send --from line_8Xk2mQ4vTn7Bw9Rz3Lc6Yp1Hd --to +14155550199 --text "Hello from the terminal"
|
|
55
|
+
Message msg_3fK9: queued
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
## Receive events locally
|
|
59
|
+
|
|
60
|
+
`isnap listen` opens your account's live event stream and sends each event to a URL on your machine, so you can build and test a webhook receiver without a public URL.
|
|
61
|
+
|
|
62
|
+
```bash
|
|
63
|
+
isnap listen --forward-to http://localhost:3000/webhooks [--events <type>] [--line <line_id>]
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
```console
|
|
67
|
+
$ isnap listen --forward-to http://localhost:3000/webhooks
|
|
68
|
+
Forwarding events to http://localhost:3000/webhooks
|
|
69
|
+
Signing secret for this run: whsec_4c1f...
|
|
70
|
+
Stop with Ctrl+C.
|
|
71
|
+
message.received evt_8f2a 200
|
|
72
|
+
line.active evt_91bc failed: ECONNREFUSED
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
- Each event is a `POST` whose body is the event exactly as the stream sent it, signed as a webhook delivery is (the `webhook-id`, `webhook-timestamp` and `webhook-signature` headers of Standard Webhooks). Pass the signing secret printed at start to `verifyWebhook` from `@isnap/sdk`. The secret is new on each run and belongs to that run only.
|
|
76
|
+
- `--events` keeps one event type: an exact type such as `message.received`, a family such as `line.*`, or `*`. `--line` keeps the events tied to one line. They filter the stream as they filter `GET /v1/webhooks/events`.
|
|
77
|
+
- Each event prints one line: its type, its event id and the status your receiver answered, or the connection error. A receiver that is down or answers an error is reported for that event, and the event is not sent again.
|
|
78
|
+
- `isnap listen` creates no webhook subscription. Stop it with Ctrl+C.
|
|
79
|
+
- The stream is live only. When it drops, or iSnap asks for a new connection, `isnap listen` reconnects with a delay that grows from 1 to 30 seconds and says that the events of the gap are not replayed: read them with `GET /v1/webhooks/events`. An account has a limited number of open streams: when the API answers `event_stream_limit_exceeded`, `isnap listen` prints it and tries again.
|
|
80
|
+
- When the API key is revoked, or the API refuses it, `isnap listen` prints the error and exits with 1, without trying again.
|
|
81
|
+
- With `--json`, stdout holds one JSON object per line: first `{ "type": "listening", "forward_to": "...", "signing_secret": "..." }`, then `{ "type": "<event type>", "event_id": "...", "status": 200 }` for each event, or `"error": "ECONNREFUSED"` in place of `status`. Reconnection notices go to stderr.
|
|
82
|
+
|
|
83
|
+
An event can hold personal data, such as message text and handles: it goes to the URL you give and nowhere else.
|
|
84
|
+
|
|
85
|
+
## JSON output
|
|
86
|
+
|
|
87
|
+
Every command accepts `--json`. The command then prints JSON on stdout, for scripts and agents:
|
|
88
|
+
|
|
89
|
+
- `lines list` prints the API's response, `{ "lines": [...] }`.
|
|
90
|
+
- `send` prints the message the API returned.
|
|
91
|
+
- `messages list` prints an array of messages.
|
|
92
|
+
- `listen` prints the lines described above.
|
|
93
|
+
- `login` prints `{ "logged_in": true, "api_url": "..." }` and `logout` prints `{ "logged_out": true }`.
|
|
94
|
+
|
|
95
|
+
## Errors and exit codes
|
|
96
|
+
|
|
97
|
+
When the API refuses a call, the command prints `Error <code>: <message>` on stderr, with the error code the API documents, and exits with 1. A command that is misused (a missing option, an unknown command) exits with 2.
|
|
98
|
+
|
|
99
|
+
```console
|
|
100
|
+
$ isnap send --from line_8Xk2mQ4vTn7Bw9Rz3Lc6Yp1Hd --to +14155550199 --text "Hi"
|
|
101
|
+
Error outbound_first_forbidden: ...
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
## Where the key is stored
|
|
105
|
+
|
|
106
|
+
`isnap login` writes `$XDG_CONFIG_HOME/isnap/config.json`, or `~/.config/isnap/config.json` when `XDG_CONFIG_HOME` is not set. Only you can read the file (mode 0600). It holds the key and the API URL the key was checked against, and later commands call that same API.
|
|
107
|
+
|
|
108
|
+
## Environment variables and options
|
|
109
|
+
|
|
110
|
+
| Setting | Effect |
|
|
111
|
+
| ----------------- | ------------------------------------------------------------------------------------------------- |
|
|
112
|
+
| `ISNAP_API_KEY` | Used instead of the stored key, by every command. A script can set it and never run `isnap login` |
|
|
113
|
+
| `ISNAP_API_URL` | Used instead of the stored API URL |
|
|
114
|
+
| `--api-url <url>` | Used instead of `ISNAP_API_URL` and the stored API URL |
|
|
115
|
+
|
|
116
|
+
The API URL is `https://api.isnap.ai` unless one of these says otherwise. To target another environment, run `isnap login --api-url <url>`: the URL is stored with the key.
|
|
117
|
+
|
|
118
|
+
## License
|
|
119
|
+
|
|
120
|
+
MIT
|
package/dist/bin.js
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import { run } from './cli.js';
|
|
3
|
+
import { readSecret } from './prompt.js';
|
|
4
|
+
// Ctrl+C or a termination signal ends a command that runs until it is stopped.
|
|
5
|
+
const stop = new AbortController();
|
|
6
|
+
process.once('SIGINT', () => stop.abort());
|
|
7
|
+
process.once('SIGTERM', () => stop.abort());
|
|
8
|
+
const exitCode = await run(process.argv.slice(2), {
|
|
9
|
+
env: process.env,
|
|
10
|
+
signal: stop.signal,
|
|
11
|
+
stdout: (text) => process.stdout.write(text),
|
|
12
|
+
stderr: (text) => process.stderr.write(text),
|
|
13
|
+
readSecret: (prompt) => readSecret(prompt, process.stdin, process.stderr)
|
|
14
|
+
});
|
|
15
|
+
process.exitCode = exitCode;
|
|
16
|
+
//# sourceMappingURL=bin.js.map
|
package/dist/bin.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"bin.js","sourceRoot":"","sources":["../src/bin.ts"],"names":[],"mappings":";AACA,OAAO,EAAE,GAAG,EAAE,MAAM,UAAU,CAAA;AAC9B,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAA;AAExC,+EAA+E;AAC/E,MAAM,IAAI,GAAG,IAAI,eAAe,EAAE,CAAA;AAClC,OAAO,CAAC,IAAI,CAAC,QAAQ,EAAE,GAAG,EAAE,CAAC,IAAI,CAAC,KAAK,EAAE,CAAC,CAAA;AAC1C,OAAO,CAAC,IAAI,CAAC,SAAS,EAAE,GAAG,EAAE,CAAC,IAAI,CAAC,KAAK,EAAE,CAAC,CAAA;AAE3C,MAAM,QAAQ,GAAG,MAAM,GAAG,CAAC,OAAO,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,CAAC,EAAE;IAChD,GAAG,EAAE,OAAO,CAAC,GAAG;IAChB,MAAM,EAAE,IAAI,CAAC,MAAM;IACnB,MAAM,EAAE,CAAC,IAAI,EAAE,EAAE,CAAC,OAAO,CAAC,MAAM,CAAC,KAAK,CAAC,IAAI,CAAC;IAC5C,MAAM,EAAE,CAAC,IAAI,EAAE,EAAE,CAAC,OAAO,CAAC,MAAM,CAAC,KAAK,CAAC,IAAI,CAAC;IAC5C,UAAU,EAAE,CAAC,MAAM,EAAE,EAAE,CAAC,UAAU,CAAC,MAAM,EAAE,OAAO,CAAC,KAAK,EAAE,OAAO,CAAC,MAAM,CAAC;CAC1E,CAAC,CAAA;AACF,OAAO,CAAC,QAAQ,GAAG,QAAQ,CAAA"}
|
package/dist/cli.js
ADDED
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
import { createRequire } from 'node:module';
|
|
2
|
+
import { APIError } from '@isnap/sdk';
|
|
3
|
+
import { LINES_HELP, linesList } from './commands/lines.js';
|
|
4
|
+
import { LISTEN_HELP, listen } from './commands/listen.js';
|
|
5
|
+
import { LOGIN_HELP, login } from './commands/login.js';
|
|
6
|
+
import { LOGOUT_HELP, logout } from './commands/logout.js';
|
|
7
|
+
import { MESSAGES_HELP, messagesList } from './commands/messages.js';
|
|
8
|
+
import { SEND_HELP, send } from './commands/send.js';
|
|
9
|
+
import { CliError } from './config.js';
|
|
10
|
+
const HELP = `Usage: isnap <command> [options]
|
|
11
|
+
|
|
12
|
+
Commands:
|
|
13
|
+
login Sign in with an API key
|
|
14
|
+
logout Delete the stored API key
|
|
15
|
+
lines list List your lines
|
|
16
|
+
send Send a message from one of your lines
|
|
17
|
+
messages list List your messages
|
|
18
|
+
listen Forward the live event stream to a local URL, signed like a webhook
|
|
19
|
+
|
|
20
|
+
Every command accepts --json to print JSON on stdout, and --help.
|
|
21
|
+
ISNAP_API_KEY overrides the stored key, and ISNAP_API_URL or --api-url the stored API URL.
|
|
22
|
+
|
|
23
|
+
Documentation: https://docs.isnap.ai
|
|
24
|
+
`;
|
|
25
|
+
const COMMANDS = new Map([
|
|
26
|
+
['login', { handler: login, help: LOGIN_HELP }],
|
|
27
|
+
['logout', { handler: logout, help: LOGOUT_HELP }],
|
|
28
|
+
['send', { handler: send, help: SEND_HELP }],
|
|
29
|
+
['lines list', { handler: linesList, help: LINES_HELP }],
|
|
30
|
+
['messages list', { handler: messagesList, help: MESSAGES_HELP }],
|
|
31
|
+
['listen', { handler: listen, help: LISTEN_HELP }]
|
|
32
|
+
]);
|
|
33
|
+
function version() {
|
|
34
|
+
const manifest = createRequire(import.meta.url)('../package.json');
|
|
35
|
+
if (typeof manifest === 'object' && manifest !== null && 'version' in manifest) {
|
|
36
|
+
return String(manifest.version);
|
|
37
|
+
}
|
|
38
|
+
return 'unknown';
|
|
39
|
+
}
|
|
40
|
+
/** The text of a failure, as a person reads it: the API's documented code first. */
|
|
41
|
+
function describe(error) {
|
|
42
|
+
if (error instanceof APIError) {
|
|
43
|
+
return { text: `Error ${error.code}: ${error.message}`, exitCode: 1 };
|
|
44
|
+
}
|
|
45
|
+
if (error instanceof CliError)
|
|
46
|
+
return { text: `Error: ${error.message}`, exitCode: error.exitCode };
|
|
47
|
+
if (error instanceof Error &&
|
|
48
|
+
'code' in error &&
|
|
49
|
+
String(error.code).startsWith('ERR_PARSE_ARGS')) {
|
|
50
|
+
return { text: `Error: ${error.message}. See \`isnap --help\`.`, exitCode: 2 };
|
|
51
|
+
}
|
|
52
|
+
const message = error instanceof Error ? error.message : 'Something went wrong.';
|
|
53
|
+
return { text: `Error: ${message}`, exitCode: 1 };
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* Runs one `isnap` command and resolves with its exit code. Every write goes
|
|
57
|
+
* through a filter that replaces any API key the run has seen, so no output
|
|
58
|
+
* and no error can show one.
|
|
59
|
+
*/
|
|
60
|
+
export async function run(argv, io) {
|
|
61
|
+
const secrets = new Set();
|
|
62
|
+
const hidden = (text) => {
|
|
63
|
+
let result = text;
|
|
64
|
+
for (const secret of secrets)
|
|
65
|
+
result = result.split(secret).join('[redacted]');
|
|
66
|
+
return result;
|
|
67
|
+
};
|
|
68
|
+
const context = {
|
|
69
|
+
...io,
|
|
70
|
+
stdout: (text) => io.stdout(hidden(text)),
|
|
71
|
+
stderr: (text) => io.stderr(hidden(text)),
|
|
72
|
+
hide: (secret) => {
|
|
73
|
+
if (secret !== '')
|
|
74
|
+
secrets.add(secret);
|
|
75
|
+
}
|
|
76
|
+
};
|
|
77
|
+
const [first, second, ...rest] = argv;
|
|
78
|
+
if (first === undefined || first === '--help' || first === '-h' || first === 'help') {
|
|
79
|
+
context.stdout(HELP);
|
|
80
|
+
return 0;
|
|
81
|
+
}
|
|
82
|
+
if (first === '--version' || first === '-v') {
|
|
83
|
+
context.stdout(`${version()}\n`);
|
|
84
|
+
return 0;
|
|
85
|
+
}
|
|
86
|
+
const named = second === undefined ? undefined : COMMANDS.get(`${first} ${second}`);
|
|
87
|
+
const command = named ?? COMMANDS.get(first);
|
|
88
|
+
if (command === undefined) {
|
|
89
|
+
context.stderr(`Unknown command: ${[first, second].filter((word) => word !== undefined).join(' ')}. See \`isnap --help\`.\n`);
|
|
90
|
+
return 2;
|
|
91
|
+
}
|
|
92
|
+
const args = named === undefined ? argv.slice(1) : rest;
|
|
93
|
+
if (args.includes('--help') || args.includes('-h')) {
|
|
94
|
+
context.stdout(command.help);
|
|
95
|
+
return 0;
|
|
96
|
+
}
|
|
97
|
+
try {
|
|
98
|
+
await command.handler(args, context);
|
|
99
|
+
return 0;
|
|
100
|
+
}
|
|
101
|
+
catch (error) {
|
|
102
|
+
const { text, exitCode } = describe(error);
|
|
103
|
+
context.stderr(`${text}\n`);
|
|
104
|
+
return exitCode;
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
//# sourceMappingURL=cli.js.map
|
package/dist/cli.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"cli.js","sourceRoot":"","sources":["../src/cli.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,MAAM,aAAa,CAAA;AAC3C,OAAO,EAAE,QAAQ,EAAE,MAAM,YAAY,CAAA;AACrC,OAAO,EAAE,UAAU,EAAE,SAAS,EAAE,MAAM,qBAAqB,CAAA;AAC3D,OAAO,EAAE,WAAW,EAAE,MAAM,EAAE,MAAM,sBAAsB,CAAA;AAC1D,OAAO,EAAE,UAAU,EAAE,KAAK,EAAE,MAAM,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QACD,OAAO,CAAC,CAAA;IACV,CAAC;IACD,MAAM,IAAI,GAAG,KAAK,KAAK,SAAS,CAAC,CAAC,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,IAAI,CAAA;IACvD,IAAI,IAAI,CAAC,QAAQ,CAAC,QAAQ,CAAC,IAAI,IAAI,CAAC,QAAQ,CAAC,IAAI,CAAC,EAAE,CAAC;QACnD,OAAO,CAAC,MAAM,CAAC,OAAO,CAAC,IAAI,CAAC,CAAA;QAC5B,OAAO,CAAC,CAAA;IACV,CAAC;IAED,IAAI,CAAC;QACH,MAAM,OAAO,CAAC,OAAO,CAAC,IAAI,EAAE,OAAO,CAAC,CAAA;QACpC,OAAO,CAAC,CAAA;IACV,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACf,MAAM,EAAE,IAAI,EAAE,QAAQ,EAAE,GAAG,QAAQ,CAAC,KAAK,CAAC,CAAA;QAC1C,OAAO,CAAC,MAAM,CAAC,GAAG,IAAI,IAAI,CAAC,CAAA;QAC3B,OAAO,QAAQ,CAAA;IACjB,CAAC;AACH,CAAC"}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { parseArgs } from 'node:util';
|
|
2
|
+
import { resolveCredentials } from '../config.js';
|
|
3
|
+
import { clientFor } from '../context.js';
|
|
4
|
+
import { json, table } from '../output.js';
|
|
5
|
+
export const LINES_HELP = `Usage: isnap lines list [--api-url <url>] [--json]
|
|
6
|
+
|
|
7
|
+
Lists your lines that are active or reserved. A key limited to some lines lists
|
|
8
|
+
only those.
|
|
9
|
+
|
|
10
|
+
Options:
|
|
11
|
+
--api-url <url> Another API, such as a staging environment (or ISNAP_API_URL)
|
|
12
|
+
--json Print the API's response as JSON
|
|
13
|
+
`;
|
|
14
|
+
export async function linesList(args, context) {
|
|
15
|
+
const { values } = parseArgs({
|
|
16
|
+
args,
|
|
17
|
+
options: { 'api-url': { type: 'string' }, json: { type: 'boolean' } },
|
|
18
|
+
allowPositionals: false
|
|
19
|
+
});
|
|
20
|
+
const credentials = await resolveCredentials(values['api-url'], context.env);
|
|
21
|
+
const data = await clientFor(context, credentials).lines.listMine();
|
|
22
|
+
if (values.json === true) {
|
|
23
|
+
context.stdout(`${json(data)}\n`);
|
|
24
|
+
}
|
|
25
|
+
else if (data.lines.length === 0) {
|
|
26
|
+
context.stdout('No lines.\n');
|
|
27
|
+
}
|
|
28
|
+
else {
|
|
29
|
+
const rows = data.lines.map((line) => [
|
|
30
|
+
line.id,
|
|
31
|
+
line.phone_number,
|
|
32
|
+
line.billing_tier,
|
|
33
|
+
line.status
|
|
34
|
+
]);
|
|
35
|
+
context.stdout(`${table(['ID', 'NUMBER', 'KIND', 'STATUS'], rows)}\n`);
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
//# sourceMappingURL=lines.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"lines.js","sourceRoot":"","sources":["../../src/commands/lines.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,WAAW,CAAA;AACrC,OAAO,EAAE,kBAAkB,EAAE,MAAM,cAAc,CAAA;AACjD,OAAO,EAAE,SAAS,EAAgB,MAAM,eAAe,CAAA;AACvD,OAAO,EAAE,IAAI,EAAE,KAAK,EAAE,MAAM,cAAc,CAAA;AAE1C,MAAM,CAAC,MAAM,UAAU,GAAG;;;;;;;;CAQzB,CAAA;AAED,MAAM,CAAC,KAAK,UAAU,SAAS,CAAC,IAAc,EAAE,OAAgB;IAC9D,MAAM,EAAE,MAAM,EAAE,GAAG,SAAS,CAAC;QAC3B,IAAI;QACJ,OAAO,EAAE,EAAE,SAAS,EAAE,EAAE,IAAI,EAAE,QAAQ,EAAE,EAAE,IAAI,EAAE,EAAE,IAAI,EAAE,SAAS,EAAE,EAAE;QACrE,gBAAgB,EAAE,KAAK;KACxB,CAAC,CAAA;IACF,MAAM,WAAW,GAAG,MAAM,kBAAkB,CAAC,MAAM,CAAC,SAAS,CAAC,EAAE,OAAO,CAAC,GAAG,CAAC,CAAA;IAC5E,MAAM,IAAI,GAAG,MAAM,SAAS,CAAC,OAAO,EAAE,WAAW,CAAC,CAAC,KAAK,CAAC,QAAQ,EAAE,CAAA;IAEnE,IAAI,MAAM,CAAC,IAAI,KAAK,IAAI,EAAE,CAAC;QACzB,OAAO,CAAC,MAAM,CAAC,GAAG,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,CAAA;IACnC,CAAC;SAAM,IAAI,IAAI,CAAC,KAAK,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;QACnC,OAAO,CAAC,MAAM,CAAC,aAAa,CAAC,CAAA;IAC/B,CAAC;SAAM,CAAC;QACN,MAAM,IAAI,GAAG,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC;YACpC,IAAI,CAAC,EAAE;YACP,IAAI,CAAC,YAAY;YACjB,IAAI,CAAC,YAAY;YACjB,IAAI,CAAC,MAAM;SACZ,CAAC,CAAA;QACF,OAAO,CAAC,MAAM,CAAC,GAAG,KAAK,CAAC,CAAC,IAAI,EAAE,QAAQ,EAAE,MAAM,EAAE,QAAQ,CAAC,EAAE,IAAI,CAAC,IAAI,CAAC,CAAA;IACxE,CAAC;AACH,CAAC"}
|
|
@@ -0,0 +1,306 @@
|
|
|
1
|
+
import { createHmac, randomBytes } from 'node:crypto';
|
|
2
|
+
import { parseArgs } from 'node:util';
|
|
3
|
+
import { APIError } from '@isnap/sdk';
|
|
4
|
+
import { CliError, resolveCredentials } from '../config.js';
|
|
5
|
+
export const LISTEN_HELP = `Usage: isnap listen --forward-to <url> [--events <type>] [--line <line_id>] [--api-url <url>] [--json]
|
|
6
|
+
|
|
7
|
+
Opens the live event stream of your account and sends each event to a URL on
|
|
8
|
+
your machine as a POST, signed like a webhook delivery, so the code that
|
|
9
|
+
verifies your webhooks can be tried locally. It creates no webhook
|
|
10
|
+
subscription.
|
|
11
|
+
|
|
12
|
+
At start it prints a signing secret for this run. Pass it to verifyWebhook
|
|
13
|
+
from @isnap/sdk (or any Standard Webhooks library) in the receiver.
|
|
14
|
+
|
|
15
|
+
The stream is live only. When it drops, listen reconnects with a growing delay,
|
|
16
|
+
and the events of the gap are not replayed: read them with GET /v1/webhooks/events.
|
|
17
|
+
A receiver that is down or answers an error is reported for that event, and
|
|
18
|
+
the event is not sent again.
|
|
19
|
+
|
|
20
|
+
Options:
|
|
21
|
+
--forward-to <url> Where to POST each event, such as http://localhost:3000/webhooks
|
|
22
|
+
--events <type> Only this event type: exact (message.received), a family (line.*) or *
|
|
23
|
+
--line <line_id> Only the events tied to this line
|
|
24
|
+
--api-url <url> Another API, such as a staging environment (or ISNAP_API_URL)
|
|
25
|
+
--json Print one JSON object per line on stdout
|
|
26
|
+
`;
|
|
27
|
+
const STREAM_PATH = '/v1/webhooks/events/stream';
|
|
28
|
+
const FORWARD_TIMEOUT_MS = 10_000;
|
|
29
|
+
const FIRST_DELAY_MS = 1_000;
|
|
30
|
+
const MAX_DELAY_MS = 30_000;
|
|
31
|
+
/** Splits a server-sent events body into frames, as the SSE specification reads it. */
|
|
32
|
+
async function* readFrames(body) {
|
|
33
|
+
const decoder = new TextDecoder();
|
|
34
|
+
const reader = body.getReader();
|
|
35
|
+
let buffer = '';
|
|
36
|
+
let id;
|
|
37
|
+
let event = '';
|
|
38
|
+
let data = [];
|
|
39
|
+
const field = (line) => {
|
|
40
|
+
if (line === '') {
|
|
41
|
+
const frame = data.length > 0 || event !== ''
|
|
42
|
+
? { id, event: event || 'message', data: data.join('\n') }
|
|
43
|
+
: null;
|
|
44
|
+
id = undefined;
|
|
45
|
+
event = '';
|
|
46
|
+
data = [];
|
|
47
|
+
return frame;
|
|
48
|
+
}
|
|
49
|
+
if (line.startsWith(':'))
|
|
50
|
+
return null;
|
|
51
|
+
const colon = line.indexOf(':');
|
|
52
|
+
const name = colon === -1 ? line : line.slice(0, colon);
|
|
53
|
+
const raw = colon === -1 ? '' : line.slice(colon + 1);
|
|
54
|
+
const value = raw.startsWith(' ') ? raw.slice(1) : raw;
|
|
55
|
+
if (name === 'id')
|
|
56
|
+
id = value;
|
|
57
|
+
else if (name === 'event')
|
|
58
|
+
event = value;
|
|
59
|
+
else if (name === 'data')
|
|
60
|
+
data.push(value);
|
|
61
|
+
return null;
|
|
62
|
+
};
|
|
63
|
+
try {
|
|
64
|
+
for (;;) {
|
|
65
|
+
const { done, value } = await reader.read();
|
|
66
|
+
buffer += decoder.decode(value, { stream: !done });
|
|
67
|
+
for (;;) {
|
|
68
|
+
const end = /\r\n|\n|\r/.exec(buffer);
|
|
69
|
+
// A \r at the very end may be the first half of a \r\n.
|
|
70
|
+
if (end === null || (!done && end[0] === '\r' && end.index === buffer.length - 1))
|
|
71
|
+
break;
|
|
72
|
+
const frame = field(buffer.slice(0, end.index));
|
|
73
|
+
buffer = buffer.slice(end.index + end[0].length);
|
|
74
|
+
if (frame !== null)
|
|
75
|
+
yield frame;
|
|
76
|
+
}
|
|
77
|
+
if (done)
|
|
78
|
+
return;
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
finally {
|
|
82
|
+
reader.releaseLock();
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
/** The Standard Webhooks signature: `v1,` and the base64 HMAC-SHA256 of `<id>.<timestamp>.<body>`. */
|
|
86
|
+
function sign(secret, id, timestamp, body) {
|
|
87
|
+
const key = Buffer.from(secret.slice('whsec_'.length), 'base64');
|
|
88
|
+
return `v1,${createHmac('sha256', key).update(`${id}.${timestamp}.${body}`).digest('base64')}`;
|
|
89
|
+
}
|
|
90
|
+
function reason(error) {
|
|
91
|
+
if (error instanceof Error) {
|
|
92
|
+
const cause = error.cause;
|
|
93
|
+
if (cause instanceof Error && 'code' in cause && typeof cause.code === 'string') {
|
|
94
|
+
return cause.code;
|
|
95
|
+
}
|
|
96
|
+
return error.name === 'TimeoutError' ? 'timed out' : error.message;
|
|
97
|
+
}
|
|
98
|
+
return 'unknown error';
|
|
99
|
+
}
|
|
100
|
+
/** The error the API documents in its envelope, or one built from the status. */
|
|
101
|
+
async function apiErrorOf(response) {
|
|
102
|
+
let code = `http_${response.status}`;
|
|
103
|
+
let message = `The API answered ${response.status}.`;
|
|
104
|
+
try {
|
|
105
|
+
const body = await response.json();
|
|
106
|
+
if (typeof body === 'object' && body !== null && 'error' in body) {
|
|
107
|
+
const error = body.error;
|
|
108
|
+
if (typeof error === 'object' && error !== null) {
|
|
109
|
+
if ('code' in error && typeof error.code === 'string')
|
|
110
|
+
code = error.code;
|
|
111
|
+
if ('message' in error && typeof error.message === 'string')
|
|
112
|
+
message = error.message;
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
catch {
|
|
117
|
+
// The status alone says what happened.
|
|
118
|
+
}
|
|
119
|
+
return new APIError({ status: response.status, code, message, traceId: '', requestId: '' });
|
|
120
|
+
}
|
|
121
|
+
function keyRevoked(data) {
|
|
122
|
+
let code = 'api_key_revoked';
|
|
123
|
+
try {
|
|
124
|
+
const body = JSON.parse(data);
|
|
125
|
+
if (typeof body === 'object' &&
|
|
126
|
+
body !== null &&
|
|
127
|
+
'code' in body &&
|
|
128
|
+
typeof body.code === 'string') {
|
|
129
|
+
code = body.code;
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
catch {
|
|
133
|
+
// The documented code stands.
|
|
134
|
+
}
|
|
135
|
+
return new APIError({
|
|
136
|
+
status: 401,
|
|
137
|
+
code,
|
|
138
|
+
message: 'This API key was revoked.',
|
|
139
|
+
traceId: '',
|
|
140
|
+
requestId: ''
|
|
141
|
+
});
|
|
142
|
+
}
|
|
143
|
+
function defaultSleep(ms, signal) {
|
|
144
|
+
return new Promise((resolve) => {
|
|
145
|
+
const timer = setTimeout(resolve, ms);
|
|
146
|
+
signal?.addEventListener('abort', () => {
|
|
147
|
+
clearTimeout(timer);
|
|
148
|
+
resolve();
|
|
149
|
+
}, { once: true });
|
|
150
|
+
});
|
|
151
|
+
}
|
|
152
|
+
export async function listen(args, context) {
|
|
153
|
+
const { values } = parseArgs({
|
|
154
|
+
args,
|
|
155
|
+
options: {
|
|
156
|
+
'forward-to': { type: 'string' },
|
|
157
|
+
events: { type: 'string' },
|
|
158
|
+
line: { type: 'string' },
|
|
159
|
+
'api-url': { type: 'string' },
|
|
160
|
+
json: { type: 'boolean' }
|
|
161
|
+
},
|
|
162
|
+
allowPositionals: false
|
|
163
|
+
});
|
|
164
|
+
const target = values['forward-to'];
|
|
165
|
+
if (target === undefined) {
|
|
166
|
+
throw new CliError('`isnap listen` needs --forward-to. See `isnap listen --help`.', 2);
|
|
167
|
+
}
|
|
168
|
+
let forwardUrl;
|
|
169
|
+
try {
|
|
170
|
+
forwardUrl = new URL(target);
|
|
171
|
+
}
|
|
172
|
+
catch {
|
|
173
|
+
throw new CliError('--forward-to is not a URL, for example http://localhost:3000/webhooks.', 2);
|
|
174
|
+
}
|
|
175
|
+
if (forwardUrl.protocol !== 'https:' && forwardUrl.protocol !== 'http:') {
|
|
176
|
+
throw new CliError('--forward-to must start with http:// or https://.', 2);
|
|
177
|
+
}
|
|
178
|
+
const credentials = await resolveCredentials(values['api-url'], context.env);
|
|
179
|
+
context.hide(credentials.apiKey);
|
|
180
|
+
const streamUrl = new URL(`${credentials.apiUrl}${STREAM_PATH}`);
|
|
181
|
+
if (values.events !== undefined)
|
|
182
|
+
streamUrl.searchParams.set('event', values.events);
|
|
183
|
+
if (values.line !== undefined)
|
|
184
|
+
streamUrl.searchParams.set('line_id', values.line);
|
|
185
|
+
const asJson = values.json === true;
|
|
186
|
+
const secret = `whsec_${randomBytes(32).toString('hex')}`;
|
|
187
|
+
const { signal } = context;
|
|
188
|
+
const stopped = () => signal?.aborted === true;
|
|
189
|
+
const sleep = context.sleep ?? defaultSleep;
|
|
190
|
+
const connect = context.fetch ?? fetch;
|
|
191
|
+
const notice = (text) => context.stderr(`${text}\n`);
|
|
192
|
+
context.stdout(asJson
|
|
193
|
+
? `${JSON.stringify({ type: 'listening', forward_to: forwardUrl.href, signing_secret: secret })}\n`
|
|
194
|
+
: `Forwarding events to ${forwardUrl.href}\nSigning secret for this run: ${secret}\nStop with Ctrl+C.\n`);
|
|
195
|
+
async function forward(frame) {
|
|
196
|
+
let eventId = frame.id;
|
|
197
|
+
if (eventId === undefined) {
|
|
198
|
+
try {
|
|
199
|
+
const body = JSON.parse(frame.data);
|
|
200
|
+
if (typeof body === 'object' && body !== null && 'event_id' in body) {
|
|
201
|
+
if (typeof body.event_id === 'string')
|
|
202
|
+
eventId = body.event_id;
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
catch {
|
|
206
|
+
// Handled below with the frame without an id.
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
if (eventId === undefined) {
|
|
210
|
+
notice(`Skipped a ${frame.event} frame that carries no event id.`);
|
|
211
|
+
return;
|
|
212
|
+
}
|
|
213
|
+
const timestamp = String(Math.floor(Date.now() / 1000));
|
|
214
|
+
let outcome;
|
|
215
|
+
try {
|
|
216
|
+
const response = await fetch(forwardUrl, {
|
|
217
|
+
method: 'POST',
|
|
218
|
+
headers: {
|
|
219
|
+
'content-type': 'application/json',
|
|
220
|
+
'webhook-id': eventId,
|
|
221
|
+
'webhook-timestamp': timestamp,
|
|
222
|
+
'webhook-signature': sign(secret, eventId, timestamp, frame.data)
|
|
223
|
+
},
|
|
224
|
+
body: frame.data,
|
|
225
|
+
redirect: 'manual',
|
|
226
|
+
signal: AbortSignal.timeout(FORWARD_TIMEOUT_MS)
|
|
227
|
+
});
|
|
228
|
+
await response.body?.cancel();
|
|
229
|
+
outcome = { status: response.status };
|
|
230
|
+
}
|
|
231
|
+
catch (error) {
|
|
232
|
+
outcome = { error: reason(error) };
|
|
233
|
+
}
|
|
234
|
+
if (asJson) {
|
|
235
|
+
context.stdout(`${JSON.stringify({ type: frame.event, event_id: eventId, ...outcome })}\n`);
|
|
236
|
+
}
|
|
237
|
+
else {
|
|
238
|
+
const result = 'status' in outcome ? String(outcome.status) : `failed: ${outcome.error}`;
|
|
239
|
+
context.stdout(`${frame.event} ${eventId} ${result}\n`);
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
/** Reads one stream until it ends; throws what stops `listen` for good. */
|
|
243
|
+
async function consume(onOpen) {
|
|
244
|
+
let response;
|
|
245
|
+
try {
|
|
246
|
+
response = await connect(streamUrl, {
|
|
247
|
+
headers: { authorization: `Bearer ${credentials.apiKey}`, accept: 'text/event-stream' },
|
|
248
|
+
signal
|
|
249
|
+
});
|
|
250
|
+
}
|
|
251
|
+
catch (error) {
|
|
252
|
+
return { message: `Could not connect: ${reason(error)}.`, gap: false };
|
|
253
|
+
}
|
|
254
|
+
if (response.status === 429) {
|
|
255
|
+
const error = await apiErrorOf(response);
|
|
256
|
+
return { message: `Error ${error.code}: ${error.message}`, gap: false };
|
|
257
|
+
}
|
|
258
|
+
if (response.status >= 500) {
|
|
259
|
+
await response.body?.cancel();
|
|
260
|
+
return { message: `The API answered ${response.status}.`, gap: false };
|
|
261
|
+
}
|
|
262
|
+
if (!response.ok || response.body === null)
|
|
263
|
+
throw await apiErrorOf(response);
|
|
264
|
+
onOpen();
|
|
265
|
+
const { body } = response;
|
|
266
|
+
try {
|
|
267
|
+
for await (const frame of readFrames(body)) {
|
|
268
|
+
if (frame.event === 'heartbeat')
|
|
269
|
+
continue;
|
|
270
|
+
if (frame.event === 'key_revoked')
|
|
271
|
+
throw keyRevoked(frame.data);
|
|
272
|
+
if (frame.event === 'reconnect_required') {
|
|
273
|
+
return { message: 'iSnap asked for a new connection.', gap: true };
|
|
274
|
+
}
|
|
275
|
+
await forward(frame);
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
catch (error) {
|
|
279
|
+
if (error instanceof APIError)
|
|
280
|
+
throw error;
|
|
281
|
+
if (stopped())
|
|
282
|
+
return { message: 'Stopped.', gap: false };
|
|
283
|
+
return { message: `The stream was interrupted: ${reason(error)}.`, gap: true };
|
|
284
|
+
}
|
|
285
|
+
finally {
|
|
286
|
+
await body.cancel().catch(() => undefined);
|
|
287
|
+
}
|
|
288
|
+
return { message: 'The stream closed.', gap: true };
|
|
289
|
+
}
|
|
290
|
+
let delay = FIRST_DELAY_MS;
|
|
291
|
+
while (!stopped()) {
|
|
292
|
+
const retry = await consume(() => {
|
|
293
|
+
delay = FIRST_DELAY_MS;
|
|
294
|
+
});
|
|
295
|
+
if (stopped())
|
|
296
|
+
return;
|
|
297
|
+
notice(retry.message);
|
|
298
|
+
if (retry.gap) {
|
|
299
|
+
notice('Events during the gap are not replayed. Read them with GET /v1/webhooks/events.');
|
|
300
|
+
}
|
|
301
|
+
notice(`Reconnecting in ${delay / 1000}s.`);
|
|
302
|
+
await sleep(delay, signal);
|
|
303
|
+
delay = Math.min(delay * 2, MAX_DELAY_MS);
|
|
304
|
+
}
|
|
305
|
+
}
|
|
306
|
+
//# sourceMappingURL=listen.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"listen.js","sourceRoot":"","sources":["../../src/commands/listen.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,WAAW,EAAE,MAAM,aAAa,CAAA;AACrD,OAAO,EAAE,SAAS,EAAE,MAAM,WAAW,CAAA;AACrC,OAAO,EAAE,QAAQ,EAAE,MAAM,YAAY,CAAA;AACrC,OAAO,EAAE,QAAQ,EAAE,kBAAkB,EAAE,MAAM,cAAc,CAAA;AAG3D,MAAM,CAAC,MAAM,WAAW,GAAG;;;;;;;;;;;;;;;;;;;;;CAqB1B,CAAA;AAED,MAAM,WAAW,GAAG,4BAA4B,CAAA;AAChD,MAAM,kBAAkB,GAAG,MAAM,CAAA;AACjC,MAAM,cAAc,GAAG,KAAK,CAAA;AAC5B,MAAM,YAAY,GAAG,MAAM,CAAA;AAQ3B,uFAAuF;AACvF,KAAK,SAAS,CAAC,CAAC,UAAU,CAAC,IAAgC;IACzD,MAAM,OAAO,GAAG,IAAI,WAAW,EAAE,CAAA;IACjC,MA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AAK,CAAC,GAAG,EAAE,GAAG,EAAE,IAAI,EAAE,CAAA;QAChF,CAAC;gBAAS,CAAC;YACT,MAAM,IAAI,CAAC,MAAM,EAAE,CAAC,KAAK,CAAC,GAAG,EAAE,CAAC,SAAS,CAAC,CAAA;QAC5C,CAAC;QACD,OAAO,EAAE,OAAO,EAAE,oBAAoB,EAAE,GAAG,EAAE,IAAI,EAAE,CAAA;IACrD,CAAC;IAED,IAAI,KAAK,GAAG,cAAc,CAAA;IAC1B,OAAO,CAAC,OAAO,EAAE,EAAE,CAAC;QAClB,MAAM,KAAK,GAAG,MAAM,OAAO,CAAC,GAAG,EAAE;YAC/B,KAAK,GAAG,cAAc,CAAA;QACxB,CAAC,CAAC,CAAA;QACF,IAAI,OAAO,EAAE;YAAE,OAAM;QACrB,MAAM,CAAC,KAAK,CAAC,OAAO,CAAC,CAAA;QACrB,IAAI,KAAK,CAAC,GAAG,EAAE,CAAC;YACd,MAAM,CAAC,iFAAiF,CAAC,CAAA;QAC3F,CAAC;QACD,MAAM,CAAC,mBAAmB,KAAK,GAAG,IAAI,IAAI,CAAC,CAAA;QAC3C,MAAM,KAAK,CAAC,KAAK,EAAE,MAAM,CAAC,CAAA;QAC1B,KAAK,GAAG,IAAI,CAAC,GAAG,CAAC,KAAK,GAAG,CAAC,EAAE,YAAY,CAAC,CAAA;IAC3C,CAAC;AACH,CAAC"}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { parseArgs } from 'node:util';
|
|
2
|
+
import { CliError, resolveApiUrl, writeStoredConfig } from '../config.js';
|
|
3
|
+
import { clientFor } from '../context.js';
|
|
4
|
+
import { json } from '../output.js';
|
|
5
|
+
export const LOGIN_HELP = `Usage: isnap login [--api-url <url>] [--json]
|
|
6
|
+
|
|
7
|
+
Asks for an API key from the dashboard, checks it against the API and stores
|
|
8
|
+
it, with the API URL it was checked against, in your user configuration
|
|
9
|
+
directory ($XDG_CONFIG_HOME/isnap/config.json, or ~/.config/isnap/config.json),
|
|
10
|
+
readable by you only. Nothing is stored when the API refuses the key.
|
|
11
|
+
|
|
12
|
+
The key is read with no echo. When stdin is not a terminal, the first line of
|
|
13
|
+
stdin is the key: echo "$KEY" | isnap login
|
|
14
|
+
|
|
15
|
+
Options:
|
|
16
|
+
--api-url <url> Another API, such as a staging environment (or ISNAP_API_URL)
|
|
17
|
+
--json Print the result as JSON
|
|
18
|
+
`;
|
|
19
|
+
export async function login(args, context) {
|
|
20
|
+
const { values } = parseArgs({
|
|
21
|
+
args,
|
|
22
|
+
options: { 'api-url': { type: 'string' }, json: { type: 'boolean' } },
|
|
23
|
+
allowPositionals: false
|
|
24
|
+
});
|
|
25
|
+
const apiUrl = resolveApiUrl(values['api-url'], context.env, null);
|
|
26
|
+
const apiKey = await context.readSecret('API key: ');
|
|
27
|
+
if (apiKey === '')
|
|
28
|
+
throw new CliError('No API key entered.', 2);
|
|
29
|
+
context.hide(apiKey);
|
|
30
|
+
// The key is checked with the call every later command makes, `GET /v1/lines/my`
|
|
31
|
+
// (`GET /v1/lines` lists the marketplace and answers without a key).
|
|
32
|
+
await clientFor(context, { apiKey, apiUrl }).lines.listMine();
|
|
33
|
+
await writeStoredConfig(context.env, { api_key: apiKey, api_url: apiUrl });
|
|
34
|
+
context.stdout(values.json === true
|
|
35
|
+
? `${json({ logged_in: true, api_url: apiUrl })}\n`
|
|
36
|
+
: `Signed in to ${apiUrl}.\n`);
|
|
37
|
+
}
|
|
38
|
+
//# sourceMappingURL=login.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"login.js","sourceRoot":"","sources":["../../src/commands/login.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,WAAW,CAAA;AACrC,OAAO,EAAE,QAAQ,EAAE,aAAa,EAAE,iBAAiB,EAAE,MAAM,cAAc,CAAA;AACzE,OAAO,EAAE,SAAS,EAAgB,MAAM,eAAe,CAAA;AACvD,OAAO,EAAE,IAAI,EAAE,MAAM,cAAc,C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+
import { deleteStoredConfig } from '../config.js';
|
|
3
|
+
import { json } from '../output.js';
|
|
4
|
+
export const LOGOUT_HELP = `Usage: isnap logout [--json]
|
|
5
|
+
|
|
6
|
+
Deletes the API key and the API URL that \`isnap login\` stored.
|
|
7
|
+
|
|
8
|
+
Options:
|
|
9
|
+
--json Print the result as JSON
|
|
10
|
+
`;
|
|
11
|
+
export async function logout(args, context) {
|
|
12
|
+
const { values } = parseArgs({
|
|
13
|
+
args,
|
|
14
|
+
options: { json: { type: 'boolean' } },
|
|
15
|
+
allowPositionals: false
|
|
16
|
+
});
|
|
17
|
+
const removed = await deleteStoredConfig(context.env);
|
|
18
|
+
if (values.json === true)
|
|
19
|
+
context.stdout(`${json({ logged_out: removed })}\n`);
|
|
20
|
+
else
|
|
21
|
+
context.stdout(removed ? 'Signed out.\n' : 'Not signed in.\n');
|
|
22
|
+
}
|
|
23
|
+
//# sourceMappingURL=logout.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"logout.js","sourceRoot":"","sources":["../../src/commands/logout.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,WAAW,CAAA;AACrC,OAAO,EAAE,kBAAkB,EAAE,MAAM,cAAc,CAAA;AAEjD,OAAO,EAAE,IAAI,EAAE,MAAM,cAAc,CAAA;AAEnC,MAAM,CAAC,MAAM,WAAW,GAAG;;;;;;CAM1B,CAAA;AAED,MAAM,CAAC,KAAK,UAAU,MAAM,CAAC,IAAc,EAAE,OAAgB;IAC3D,MAAM,EAAE,MAAM,EAAE,GAAG,SAAS,CAAC;QAC3B,IAAI;QACJ,OAAO,EAAE,EAAE,IAAI,EAAE,EAAE,IAAI,EAAE,SAAS,EAAE,EAAE;QACtC,gBAAgB,EAAE,KAAK;KACxB,CAAC,CAAA;IACF,MAAM,OAAO,GAAG,MAAM,kBAAkB,CAAC,OAAO,CAAC,GAAG,CAAC,CAAA;IACrD,IAAI,MAAM,CAAC,IAAI,KAAK,IAAI;QAAE,OAAO,CAAC,MAAM,CAAC,GAAG,IAAI,CAAC,EAAE,UAAU,EAAE,OAAO,EAAE,CAAC,IAAI,CAAC,CAAA;;QACzE,OAAO,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC,CAAC,eAAe,CAAC,CAAC,CAAC,kBAAkB,CAAC,CAAA;AACrE,CAAC"}
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import { parseArgs } from 'node:util';
|
|
2
|
+
import { CliError, resolveCredentials } from '../config.js';
|
|
3
|
+
import { clientFor } from '../context.js';
|
|
4
|
+
import { json, preview, table } from '../output.js';
|
|
5
|
+
const DEFAULT_LIMIT = 25;
|
|
6
|
+
const MAX_PAGE_SIZE = 100;
|
|
7
|
+
export const MESSAGES_HELP = `Usage: isnap messages list [--line <line_id>] [--limit <n>] [--api-url <url>] [--json]
|
|
8
|
+
|
|
9
|
+
Lists your messages, newest first. iSnap keeps a message for a short time only.
|
|
10
|
+
|
|
11
|
+
Options:
|
|
12
|
+
--line <line_id> Only the messages of this line
|
|
13
|
+
--limit <n> At most this many messages (default ${DEFAULT_LIMIT})
|
|
14
|
+
--api-url <url> Another API, such as a staging environment (or ISNAP_API_URL)
|
|
15
|
+
--json Print the messages as a JSON array
|
|
16
|
+
`;
|
|
17
|
+
export async function messagesList(args, context) {
|
|
18
|
+
const { values } = parseArgs({
|
|
19
|
+
args,
|
|
20
|
+
options: {
|
|
21
|
+
line: { type: 'string' },
|
|
22
|
+
limit: { type: 'string' },
|
|
23
|
+
'api-url': { type: 'string' },
|
|
24
|
+
json: { type: 'boolean' }
|
|
25
|
+
},
|
|
26
|
+
allowPositionals: false
|
|
27
|
+
});
|
|
28
|
+
const limit = values.limit === undefined ? DEFAULT_LIMIT : Number(values.limit);
|
|
29
|
+
if (!Number.isInteger(limit) || limit < 1) {
|
|
30
|
+
throw new CliError('--limit must be a whole number of 1 or more.', 2);
|
|
31
|
+
}
|
|
32
|
+
const credentials = await resolveCredentials(values['api-url'], context.env);
|
|
33
|
+
const client = clientFor(context, credentials);
|
|
34
|
+
const messages = [];
|
|
35
|
+
for await (const message of client.messages.list({
|
|
36
|
+
line_id: values.line,
|
|
37
|
+
limit: Math.min(limit, MAX_PAGE_SIZE)
|
|
38
|
+
})) {
|
|
39
|
+
messages.push(message);
|
|
40
|
+
if (messages.length >= limit)
|
|
41
|
+
break;
|
|
42
|
+
}
|
|
43
|
+
if (values.json === true) {
|
|
44
|
+
context.stdout(`${json(messages)}\n`);
|
|
45
|
+
}
|
|
46
|
+
else if (messages.length === 0) {
|
|
47
|
+
context.stdout('No messages.\n');
|
|
48
|
+
}
|
|
49
|
+
else {
|
|
50
|
+
const rows = messages.map((message) => [
|
|
51
|
+
message.id,
|
|
52
|
+
message.direction,
|
|
53
|
+
message.from,
|
|
54
|
+
message.to,
|
|
55
|
+
message.status,
|
|
56
|
+
preview(message.body, 40)
|
|
57
|
+
]);
|
|
58
|
+
context.stdout(`${table(['ID', 'DIRECTION', 'FROM', 'TO', 'STATUS', 'TEXT'], rows)}\n`);
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
//# sourceMappingURL=messages.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"messages.js","sourceRoot":"","sources":["../../src/commands/messages.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,WAAW,CAAA;AAErC,OAAO,EAAE,QAAQ,EAAE,kBAAkB,EAAE,MAAM,cAAc,CAAA;AAC3D,OAAO,EAAE,SAAS,EAAgB,MAAM,eAAe,CAAA;AACvD,OAAO,EAAE,IAAI,EAAE,OAAO,EAAE,KAAK,EAAE,MAAM,cAAc,CAAA;AAEnD,MAAM,aAAa,GAAG,EAAE,CAAA;AACxB,MAAM,aAAa,GAAG,GAAG,CAAA;AAEzB,MAAM,CAAC,MAAM,aAAa,GAAG;;;;;;0DAM6B,aAAa;;;CAGtE,CAAA;AAED,MAAM,CAAC,KAAK,UAAU,YAAY,CAAC,IAA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;QACzB,OAAO,CAAC,MAAM,CAAC,GAAG,IAAI,CAAC,QAAQ,CAAC,IAAI,CAAC,CAAA;IACvC,CAAC;SAAM,IAAI,QAAQ,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;QACjC,OAAO,CAAC,MAAM,CAAC,gBAAgB,CAAC,CAAA;IAClC,CAAC;SAAM,CAAC;QACN,MAAM,IAAI,GAAG,QAAQ,CAAC,GAAG,CAAC,CAAC,OAAO,EAAE,EAAE,CAAC;YACrC,OAAO,CAAC,EAAE;YACV,OAAO,CAAC,SAAS;YACjB,OAAO,CAAC,IAAI;YACZ,OAAO,CAAC,EAAE;YACV,OAAO,CAAC,MAAM;YACd,OAAO,CAAC,OAAO,CAAC,IAAI,EAAE,EAAE,CAAC;SAC1B,CAAC,CAAA;QACF,OAAO,CAAC,MAAM,CAAC,GAAG,KAAK,CAAC,CAAC,IAAI,EAAE,WAAW,EAAE,MAAM,EAAE,IAAI,EAAE,QAAQ,EAAE,MAAM,CAAC,EAAE,IAAI,CAAC,IAAI,CAAC,CAAA;IACzF,CAAC;AACH,CAAC"}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { parseArgs } from 'node:util';
|
|
2
|
+
import { CliError, resolveCredentials } from '../config.js';
|
|
3
|
+
import { clientFor } from '../context.js';
|
|
4
|
+
import { json } from '../output.js';
|
|
5
|
+
export const SEND_HELP = `Usage: isnap send --from <line_id> --to <handle> --text <text> [--api-url <url>] [--json]
|
|
6
|
+
|
|
7
|
+
Sends a message from one of your lines and prints its id and status. This sends
|
|
8
|
+
a real message.
|
|
9
|
+
|
|
10
|
+
Options:
|
|
11
|
+
--from <line_id> The line to send from, as \`isnap lines list\` shows it
|
|
12
|
+
--to <handle> The recipient: a phone number such as +14155550123, or an email
|
|
13
|
+
--text <text> The message text
|
|
14
|
+
--api-url <url> Another API, such as a staging environment (or ISNAP_API_URL)
|
|
15
|
+
--json Print the API's response as JSON
|
|
16
|
+
`;
|
|
17
|
+
export async function send(args, context) {
|
|
18
|
+
const { values } = parseArgs({
|
|
19
|
+
args,
|
|
20
|
+
options: {
|
|
21
|
+
from: { type: 'string' },
|
|
22
|
+
to: { type: 'string' },
|
|
23
|
+
text: { type: 'string' },
|
|
24
|
+
'api-url': { type: 'string' },
|
|
25
|
+
json: { type: 'boolean' }
|
|
26
|
+
},
|
|
27
|
+
allowPositionals: false
|
|
28
|
+
});
|
|
29
|
+
const { from, to, text } = values;
|
|
30
|
+
if (from === undefined || to === undefined || text === undefined) {
|
|
31
|
+
throw new CliError('`isnap send` needs --from, --to and --text. See `isnap send --help`.', 2);
|
|
32
|
+
}
|
|
33
|
+
const credentials = await resolveCredentials(values['api-url'], context.env);
|
|
34
|
+
const message = await clientFor(context, credentials).messages.send({
|
|
35
|
+
line_id: from,
|
|
36
|
+
to,
|
|
37
|
+
body: text
|
|
38
|
+
});
|
|
39
|
+
context.stdout(values.json === true ? `${json(message)}\n` : `Message ${message.id}: ${message.status}\n`);
|
|
40
|
+
}
|
|
41
|
+
//# sourceMappingURL=send.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"send.js","sourceRoot":"","sources":["../../src/commands/send.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,WAAW,CAAA;AACrC,OAAO,EAAE,QAAQ,EAAE,kBAAkB,EAAE,MAAM,cAAc,CAAA;AAC3D,OAAO,EAAE,SAAS,EAAgB,MAAM,eAAe,CAAA;AACvD,OAAO,EAAE,IAAI,EAAE,MAAM,cAAc,CAAA;AAEnC,MAAM,CAAC,MAAM,SAAS,GAAG;;;;;;;;;;;CAWxB,CAAA;AAED,MAAM,CAAC,KAAK,UAAU,IAAI,CAAC,IAAc,EAAE,OAAgB;IACzD,MAAM,EAAE,MAAM,EAAE,GAAG,SAAS,CAAC;QAC3B,IAAI;QACJ,OAAO,EAAE;YACP,IAAI,EAAE,EAAE,IAAI,EAAE,QAAQ,EAAE;YACxB,EAAE,EAAE,EAAE,IAAI,EAAE,QAAQ,EAAE;YACtB,IAAI,EAAE,EAAE,IAAI,EAAE,QAAQ,EAAE;YACxB,SAAS,EAAE,EAAE,IAAI,EAAE,QAAQ,EAAE;YAC7B,IAAI,EAAE,EAAE,IAAI,EAAE,SAAS,EAAE;SAC1B;QACD,gBAAgB,EAAE,KAAK;KACxB,CAAC,CAAA;IACF,MAAM,EAAE,IAAI,EAAE,EAAE,EAAE,IAAI,EAAE,GAAG,MAAM,CAAA;IACjC,IAAI,IAAI,KAAK,SAAS,IAAI,EAAE,KAAK,SAAS,IAAI,IAAI,KAAK,SAAS,EAAE,CAAC;QACjE,MAAM,IAAI,QAAQ,CAAC,sEAAsE,EAAE,CAAC,CAAC,CAAA;IAC/F,CAAC;IACD,MAAM,WAAW,GAAG,MAAM,kBAAkB,CAAC,MAAM,CAAC,SAAS,CAAC,EAAE,OAAO,CAAC,GAAG,CAAC,CAAA;IAC5E,MAAM,OAAO,GAAG,MAAM,SAAS,CAAC,OAAO,EAAE,WAAW,CAAC,CAAC,QAAQ,CAAC,IAAI,CAAC;QAClE,OAAO,EAAE,IAAI;QACb,EAAE;QACF,IAAI,EAAE,IAAI;KACX,CAAC,CAAA;IAEF,OAAO,CAAC,MAAM,CACZ,MAAM,CAAC,IAAI,KAAK,IAAI,CAAC,CAAC,CAAC,GAAG,IAAI,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,WAAW,OAAO,CAAC,EAAE,KAAK,OAAO,CAAC,MAAM,IAAI,CAC3F,CAAA;AACH,CAAC"}
|
package/dist/config.js
ADDED
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
import { chmod, mkdir, readFile, rename, rm, writeFile } from 'node:fs/promises';
|
|
2
|
+
import { homedir } from 'node:os';
|
|
3
|
+
import { dirname, join } from 'node:path';
|
|
4
|
+
export const DEFAULT_API_URL = 'https://api.isnap.ai';
|
|
5
|
+
/** A refusal the user can act on: its message is safe to print as it is. */
|
|
6
|
+
export class CliError extends Error {
|
|
7
|
+
exitCode;
|
|
8
|
+
constructor(message, exitCode = 1) {
|
|
9
|
+
super(message);
|
|
10
|
+
this.name = 'CliError';
|
|
11
|
+
this.exitCode = exitCode;
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
const present = (value) => value !== undefined && value.trim() !== '' ? value.trim() : undefined;
|
|
15
|
+
/** `$XDG_CONFIG_HOME/isnap/config.json`, or `~/.config/isnap/config.json`. */
|
|
16
|
+
export function configPath(env) {
|
|
17
|
+
const base = present(env.XDG_CONFIG_HOME) ?? join(present(env.HOME) ?? homedir(), '.config');
|
|
18
|
+
return join(base, 'isnap', 'config.json');
|
|
19
|
+
}
|
|
20
|
+
function isNotFound(error) {
|
|
21
|
+
return error instanceof Error && 'code' in error && error.code === 'ENOENT';
|
|
22
|
+
}
|
|
23
|
+
/** The stored key and URL, or `null` when nobody has logged in. */
|
|
24
|
+
export async function readStoredConfig(env) {
|
|
25
|
+
const path = configPath(env);
|
|
26
|
+
let text;
|
|
27
|
+
try {
|
|
28
|
+
text = await readFile(path, 'utf8');
|
|
29
|
+
}
|
|
30
|
+
catch (error) {
|
|
31
|
+
if (isNotFound(error))
|
|
32
|
+
return null;
|
|
33
|
+
throw new CliError(`Cannot read ${path}. Run \`isnap login\` again.`);
|
|
34
|
+
}
|
|
35
|
+
let parsed;
|
|
36
|
+
try {
|
|
37
|
+
parsed = JSON.parse(text);
|
|
38
|
+
}
|
|
39
|
+
catch {
|
|
40
|
+
parsed = null;
|
|
41
|
+
}
|
|
42
|
+
if (typeof parsed === 'object' && parsed !== null && 'api_key' in parsed && 'api_url' in parsed) {
|
|
43
|
+
const { api_key: apiKey, api_url: apiUrl } = parsed;
|
|
44
|
+
if (typeof apiKey === 'string' && typeof apiUrl === 'string') {
|
|
45
|
+
return { api_key: apiKey, api_url: apiUrl };
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
throw new CliError(`${path} is not a valid iSnap configuration file. Run \`isnap login\` again.`);
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* Stores the key and the URL in a file only its owner can read. The file is
|
|
52
|
+
* written under a temporary name with its final mode and renamed over the old
|
|
53
|
+
* one, so the key is never in a file others can read, even for an instant.
|
|
54
|
+
*/
|
|
55
|
+
export async function writeStoredConfig(env, config) {
|
|
56
|
+
const path = configPath(env);
|
|
57
|
+
const directory = dirname(path);
|
|
58
|
+
await mkdir(directory, { recursive: true, mode: 0o700 });
|
|
59
|
+
const temporary = join(directory, `.config.${process.pid}.${Date.now()}.tmp`);
|
|
60
|
+
try {
|
|
61
|
+
await writeFile(temporary, `${JSON.stringify(config, null, 2)}\n`, {
|
|
62
|
+
mode: 0o600,
|
|
63
|
+
flag: 'wx'
|
|
64
|
+
});
|
|
65
|
+
// The process's umask can only remove permissions; this makes the mode certain.
|
|
66
|
+
await chmod(temporary, 0o600);
|
|
67
|
+
await rename(temporary, path);
|
|
68
|
+
}
|
|
69
|
+
catch (error) {
|
|
70
|
+
await rm(temporary, { force: true });
|
|
71
|
+
throw error;
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
/** Deletes the stored key and URL; resolves `false` when there was none. */
|
|
75
|
+
export async function deleteStoredConfig(env) {
|
|
76
|
+
try {
|
|
77
|
+
await rm(configPath(env));
|
|
78
|
+
return true;
|
|
79
|
+
}
|
|
80
|
+
catch (error) {
|
|
81
|
+
if (isNotFound(error))
|
|
82
|
+
return false;
|
|
83
|
+
throw new CliError(`Cannot delete ${configPath(env)}.`);
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
/** A URL a person typed, checked, without trailing slashes. */
|
|
87
|
+
export function parseApiUrl(value) {
|
|
88
|
+
let url;
|
|
89
|
+
try {
|
|
90
|
+
url = new URL(value);
|
|
91
|
+
}
|
|
92
|
+
catch {
|
|
93
|
+
throw new CliError('The API URL is not a URL, for example https://api.isnap.ai.', 2);
|
|
94
|
+
}
|
|
95
|
+
if (url.protocol !== 'https:' && url.protocol !== 'http:') {
|
|
96
|
+
throw new CliError('The API URL must start with https:// or http://.', 2);
|
|
97
|
+
}
|
|
98
|
+
return value.replace(/\/+$/, '');
|
|
99
|
+
}
|
|
100
|
+
/**
|
|
101
|
+
* The URL a command calls: `--api-url`, then `ISNAP_API_URL`, then the one
|
|
102
|
+
* stored at login, then the production API.
|
|
103
|
+
*/
|
|
104
|
+
export function resolveApiUrl(flag, env, stored) {
|
|
105
|
+
return parseApiUrl(present(flag) ?? present(env.ISNAP_API_URL) ?? stored?.api_url ?? DEFAULT_API_URL);
|
|
106
|
+
}
|
|
107
|
+
/** The key and the URL for a command that calls the API. */
|
|
108
|
+
export async function resolveCredentials(flagApiUrl, env) {
|
|
109
|
+
const envKey = present(env.ISNAP_API_KEY);
|
|
110
|
+
const urlOverride = present(flagApiUrl) ?? present(env.ISNAP_API_URL);
|
|
111
|
+
// Everything is overridden: the stored file is not read, so a damaged one
|
|
112
|
+
// does not stop a script that sets its own key and URL.
|
|
113
|
+
const stored = envKey !== undefined && urlOverride !== undefined ? null : await readStoredConfig(env);
|
|
114
|
+
const apiKey = envKey ?? stored?.api_key;
|
|
115
|
+
if (apiKey === undefined) {
|
|
116
|
+
throw new CliError('Not signed in. Run `isnap login`, or set ISNAP_API_KEY.');
|
|
117
|
+
}
|
|
118
|
+
return { apiKey, apiUrl: resolveApiUrl(flagApiUrl, env, stored) };
|
|
119
|
+
}
|
|
120
|
+
//# sourceMappingURL=config.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"config.js","sourceRoot":"","sources":["../src/config.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAE,KAAK,EAAE,QAAQ,EAAE,MAAM,EAAE,EAAE,EAAE,SAAS,EAAE,MAAM,kBAAkB,CAAA;AAChF,OAAO,EAAE,OAAO,EAAE,MAAM,SAAS,CAAA;AACjC,OAAO,EAAE,OAAO,EAAE,IAAI,EAAE,MAAM,WAAW,CAAA;AAEzC,MAAM,CAAC,MAAM,eAAe,GAAG,sBAAsB,CAA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ACH,MAAM,UAAU,aAAa,CAC3B,IAAwB,EACxB,GAAQ,EACR,MAA2B;IAE3B,OAAO,WAAW,CAChB,OAAO,CAAC,IAAI,CAAC,IAAI,OAAO,CAAC,GAAG,CAAC,aAAa,CAAC,IAAI,MAAM,EAAE,OAAO,IAAI,eAAe,CAClF,CAAA;AACH,CAAC;AAED,4DAA4D;AAC5D,MAAM,CAAC,KAAK,UAAU,kBAAkB,CACtC,UAA8B,EAC9B,GAAQ;IAER,MAAM,MAAM,GAAG,OAAO,CAAC,GAAG,CAAC,aAAa,CAAC,CAAA;IACzC,MAAM,WAAW,GAAG,OAAO,CAAC,UAAU,CAAC,IAAI,OAAO,CAAC,GAAG,CAAC,aAAa,CAAC,CAAA;IACrE,0EAA0E;IAC1E,wDAAwD;IACxD,MAAM,MAAM,GACV,MAAM,KAAK,SAAS,IAAI,WAAW,KAAK,SAAS,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,MAAM,gBAAgB,CAAC,GAAG,CAAC,CAAA;IACxF,MAAM,MAAM,GAAG,MAAM,IAAI,MAAM,EAAE,OAAO,CAAA;IACxC,IAAI,MAAM,KAAK,SAAS,EAAE,CAAC;QACzB,MAAM,IAAI,QAAQ,CAAC,yDAAyD,CAAC,CAAA;IAC/E,CAAC;IACD,OAAO,EAAE,MAAM,EAAE,MAAM,EAAE,aAAa,CAAC,UAAU,EAAE,GAAG,EAAE,MAAM,CAAC,EAAE,CAAA;AACnE,CAAC"}
|
package/dist/context.js
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { ISnapClient } from '@isnap/sdk';
|
|
2
|
+
export function clientFor(context, credentials) {
|
|
3
|
+
context.hide(credentials.apiKey);
|
|
4
|
+
return new ISnapClient({
|
|
5
|
+
apiKey: credentials.apiKey,
|
|
6
|
+
baseUrl: credentials.apiUrl,
|
|
7
|
+
fetch: context.fetch
|
|
8
|
+
});
|
|
9
|
+
}
|
|
10
|
+
//# sourceMappingURL=context.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"context.js","sourceRoot":"","sources":["../src/context.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,YAAY,CAAA;AAsBxC,MAAM,UAAU,SAAS,CAAC,OAAgB,EAAE,WAAwB;IAClE,OAAO,CAAC,IAAI,CAAC,WAAW,CAAC,MAAM,CAAC,CAAA;IAChC,OAAO,IAAI,WAAW,CAAC;QACrB,MAAM,EAAE,WAAW,CAAC,MAAM;QAC1B,OAAO,EAAE,WAAW,CAAC,MAAM;QAC3B,KAAK,EAAE,OAAO,CAAC,KAAK;KACrB,CAAC,CAAA;AACJ,CAAC"}
|
package/dist/output.js
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/** Columns left-aligned and separated by two spaces, with no trailing space. */
|
|
2
|
+
export function table(headers, rows) {
|
|
3
|
+
const widths = headers.map((header, column) => Math.max(header.length, ...rows.map((row) => row[column]?.length ?? 0)));
|
|
4
|
+
const line = (cells) => cells
|
|
5
|
+
.map((cell, column) => cell.padEnd(widths[column] ?? 0))
|
|
6
|
+
.join(' ')
|
|
7
|
+
.trimEnd();
|
|
8
|
+
return [line(headers), ...rows.map(line)].join('\n');
|
|
9
|
+
}
|
|
10
|
+
/** One line of at most `max` characters: line breaks become spaces. */
|
|
11
|
+
export function preview(text, max) {
|
|
12
|
+
const oneLine = (text ?? '').replace(/\s+/g, ' ').trim();
|
|
13
|
+
return oneLine.length > max ? `${oneLine.slice(0, max - 3)}...` : oneLine;
|
|
14
|
+
}
|
|
15
|
+
export function json(value) {
|
|
16
|
+
return JSON.stringify(value, null, 2);
|
|
17
|
+
}
|
|
18
|
+
//# sourceMappingURL=output.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"output.js","sourceRoot":"","sources":["../src/output.ts"],"names":[],"mappings":"AAAA,gFAAgF;AAChF,MAAM,UAAU,KAAK,CAAC,OAAiB,EAAE,IAAgB;IACvD,MAAM,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|
|
3
|
+
const BACKSPACE = ['\u007f', '\b'];
|
|
4
|
+
/**
|
|
5
|
+
* Reads one secret line. On a terminal the prompt is shown and nothing the
|
|
6
|
+
* user types is echoed. When stdin is a pipe, the first line is read and no
|
|
7
|
+
* prompt is shown, so `echo "$KEY" | isnap login` works in a script.
|
|
8
|
+
*/
|
|
9
|
+
export function readSecret(prompt, input, output) {
|
|
10
|
+
const interactive = input.isTTY === true && input.setRawMode !== undefined;
|
|
11
|
+
return new Promise((resolve, reject) => {
|
|
12
|
+
let value = '';
|
|
13
|
+
input.setEncoding('utf8');
|
|
14
|
+
const finish = (result) => {
|
|
15
|
+
input.removeListener('data', onData);
|
|
16
|
+
input.removeListener('end', onEnd);
|
|
17
|
+
if (interactive) {
|
|
18
|
+
input.setRawMode?.(false);
|
|
19
|
+
output.write('\n');
|
|
20
|
+
}
|
|
21
|
+
input.pause();
|
|
22
|
+
result();
|
|
23
|
+
};
|
|
24
|
+
const onEnd = () => finish(() => resolve(value.trim()));
|
|
25
|
+
const onData = (chunk) => {
|
|
26
|
+
// A key such as an arrow sends an escape sequence: it is not part of the secret.
|
|
27
|
+
if (interactive && chunk.startsWith('\u001b'))
|
|
28
|
+
return;
|
|
29
|
+
for (const char of chunk) {
|
|
30
|
+
if (char === '\n' || char === '\r')
|
|
31
|
+
return finish(() => resolve(value.trim()));
|
|
32
|
+
if (interactive && char === ETX) {
|
|
33
|
+
return finish(() => reject(new CliError('Cancelled.', 130)));
|
|
34
|
+
}
|
|
35
|
+
if (interactive && BACKSPACE.includes(char))
|
|
36
|
+
value = value.slice(0, -1);
|
|
37
|
+
else
|
|
38
|
+
value += char;
|
|
39
|
+
}
|
|
40
|
+
};
|
|
41
|
+
if (interactive) {
|
|
42
|
+
output.write(prompt);
|
|
43
|
+
input.setRawMode?.(true);
|
|
44
|
+
}
|
|
45
|
+
input.on('data', onData);
|
|
46
|
+
input.on('end', onEnd);
|
|
47
|
+
input.resume();
|
|
48
|
+
});
|
|
49
|
+
}
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,CAAC,QAAQ,CAAC;gBAAE,OAAM;YACrD,KAAK,MAAM,IAAI,IAAI,KAAK,EAAE,CAAC;gBACzB,IAAI,IAAI,KAAK,IAAI,IAAI,IAAI,KAAK,IAAI;oBAAE,OAAO,MAAM,CAAC,GAAG,EAAE,CAAC,OAAO,CAAC,KAAK,CAAC,IAAI,EAAE,CAAC,CAAC,CAAA;gBAC9E,IAAI,WAAW,IAAI,IAAI,KAAK,GAAG,EAAE,CAAC;oBAChC,OAAO,MAAM,CAAC,GAAG,EAAE,CAAC,MAAM,CAAC,IAAI,QAAQ,CAAC,YAAY,EAAE,GAAG,CAAC,CAAC,CAAC,CAAA;gBAC9D,CAAC;gBACD,IAAI,WAAW,IAAI,SAAS,CAAC,QAAQ,CAAC,IAAI,CAAC;oBAAE,KAAK,GAAG,KAAK,CAAC,KAAK,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,CAAA;;oBAClE,KAAK,IAAI,IAAI,CAAA;YACpB,CAAC;QACH,CAAC,CAAA;QAED,IAAI,WAAW,EAAE,CAAC;YAChB,MAAM,CAAC,KAAK,CAAC,MAAM,CAAC,CAAA;YACpB,KAAK,CAAC,UAAU,EAAE,CAAC,IAAI,CAAC,CAAA;QAC1B,CAAC;QACD,KAAK,CAAC,EAAE,CAAC,MAAM,EAAE,MAAM,CAAC,CAAA;QACxB,KAAK,CAAC,EAAE,CAAC,KAAK,EAAE,KAAK,CAAC,CAAA;QACtB,KAAK,CAAC,MAAM,EAAE,CAAA;IAChB,CAAC,CAAC,CAAA;AACJ,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,50 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@isnap/cli",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "The iSnap command line: sign in with an API key, send a message, list lines and messages",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"bin": {
|
|
8
|
+
"isnap": "./dist/bin.js"
|
|
9
|
+
},
|
|
10
|
+
"files": [
|
|
11
|
+
"dist",
|
|
12
|
+
"README.md",
|
|
13
|
+
"LICENSE"
|
|
14
|
+
],
|
|
15
|
+
"keywords": [
|
|
16
|
+
"isnap",
|
|
17
|
+
"imessage",
|
|
18
|
+
"sms",
|
|
19
|
+
"rcs",
|
|
20
|
+
"cli"
|
|
21
|
+
],
|
|
22
|
+
"homepage": "https://docs.isnap.ai",
|
|
23
|
+
"bugs": {
|
|
24
|
+
"email": "support@isnap.ai"
|
|
25
|
+
},
|
|
26
|
+
"scripts": {
|
|
27
|
+
"build": "tsc -p tsconfig.build.json && bun scripts/make-executable.ts",
|
|
28
|
+
"lint": "eslint src/ scripts/",
|
|
29
|
+
"typecheck": "tsc --noEmit",
|
|
30
|
+
"test": "vitest run",
|
|
31
|
+
"test:watch": "vitest"
|
|
32
|
+
},
|
|
33
|
+
"engines": {
|
|
34
|
+
"node": ">=20"
|
|
35
|
+
},
|
|
36
|
+
"publishConfig": {
|
|
37
|
+
"access": "public"
|
|
38
|
+
},
|
|
39
|
+
"dependencies": {
|
|
40
|
+
"@isnap/sdk": "2.4.0"
|
|
41
|
+
},
|
|
42
|
+
"devDependencies": {
|
|
43
|
+
"@eslint/js": "^10.0.1",
|
|
44
|
+
"@types/node": "^22.0.0",
|
|
45
|
+
"eslint": "^9.0.0",
|
|
46
|
+
"typescript": "^5.7.0",
|
|
47
|
+
"typescript-eslint": "^8.57.1",
|
|
48
|
+
"vitest": "^3.0.0"
|
|
49
|
+
}
|
|
50
|
+
}
|