@jinshuju/cli 0.3.0 → 0.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +20 -17
- package/dist/auth.d.ts +5 -2
- package/dist/auth.js +17 -14
- package/dist/cli.js +1 -5
- package/dist/commands/account.js +35 -0
- package/dist/commands/entry.js +31 -5
- package/dist/commands/index.js +12 -3
- package/dist/commands/local.js +18 -17
- package/dist/commands/sms.d.ts +2 -0
- package/dist/commands/sms.js +9 -0
- package/dist/commands/trade.d.ts +2 -0
- package/dist/commands/trade.js +185 -0
- package/dist/config.d.ts +16 -13
- package/dist/config.js +13 -26
- package/dist/http.js +1 -5
- package/dist/local.d.ts +1 -3
- package/dist/local.js +48 -57
- package/dist/options.d.ts +1 -1
- package/dist/options.js +3 -5
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -29,30 +29,27 @@ its execute bit — `npm run build` restores it.
|
|
|
29
29
|
|
|
30
30
|
## Authentication
|
|
31
31
|
|
|
32
|
-
|
|
33
|
-
an
|
|
34
|
-
|
|
35
|
-
Write them to the config file (`~/.jinshuju/config.json`, mode 600):
|
|
32
|
+
Two kinds of credential are supported: an interactive browser login (OAuth),
|
|
33
|
+
and an access token (personal or account). Both go in through `auth login`:
|
|
36
34
|
|
|
37
35
|
```bash
|
|
38
|
-
jinshuju
|
|
39
|
-
jinshuju
|
|
40
|
-
jinshuju config set api_secret xxx
|
|
36
|
+
jinshuju auth login # browser login
|
|
37
|
+
jinshuju auth login --access-token xxx # access token, checked before it is stored
|
|
41
38
|
```
|
|
42
39
|
|
|
43
|
-
|
|
40
|
+
Either one is written to the config file (`~/.jinshuju/config.json`, mode 600)
|
|
41
|
+
and replaces whatever credential was stored before. `jinshuju auth logout`
|
|
42
|
+
forgets it (revoking it first, for a browser session).
|
|
43
|
+
|
|
44
|
+
For CI and scripts, an environment variable works without touching the file:
|
|
44
45
|
|
|
45
46
|
```bash
|
|
46
47
|
export JINSHUJU_ACCESS_TOKEN=xxx
|
|
47
|
-
# or
|
|
48
|
-
export JINSHUJU_API_KEY=xxx
|
|
49
|
-
export JINSHUJU_API_SECRET=xxx
|
|
50
48
|
```
|
|
51
49
|
|
|
52
|
-
The **precedence** is
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
login is a surprise nobody wants. `jinshuju auth status` reports which
|
|
50
|
+
The **precedence** is `JINSHUJU_ACCESS_TOKEN`, then the stored credential. A
|
|
51
|
+
token set for this run always beats a stored login: acting as last week's
|
|
52
|
+
login instead is a surprise nobody wants. `jinshuju auth status` reports which
|
|
56
53
|
credential is in use and where it came from:
|
|
57
54
|
|
|
58
55
|
```
|
|
@@ -60,8 +57,8 @@ $ jinshuju auth status
|
|
|
60
57
|
Authenticated with an access token (from env).
|
|
61
58
|
```
|
|
62
59
|
|
|
63
|
-
|
|
64
|
-
|
|
60
|
+
`config set` holds only `host`, `auth_host` and `client_id`; credentials are
|
|
61
|
+
not config.
|
|
65
62
|
|
|
66
63
|
## Creating forms
|
|
67
64
|
|
|
@@ -186,6 +183,10 @@ jinshuju entry create --form Kp7mQ2 --json @entry.json
|
|
|
186
183
|
cat entry.json | jinshuju entry create --form Kp7mQ2 --json -
|
|
187
184
|
```
|
|
188
185
|
|
|
186
|
+
Creating, updating or getting one entry answers with a `url`: where that entry
|
|
187
|
+
opens in the Jinshuju data page, for whoever wrote it to check it there. A
|
|
188
|
+
`--batch` write answers without one.
|
|
189
|
+
|
|
189
190
|
## Writing
|
|
190
191
|
|
|
191
192
|
Apart from the settings-block dispatch described above, every write command
|
|
@@ -238,6 +239,8 @@ jinshuju entry search repair --scope-filter 'entries_count gt 100' # picks whi
|
|
|
238
239
|
|
|
239
240
|
jinshuju entry stats --from 2026-09-01 # how much each form received in the period
|
|
240
241
|
jinshuju entry stats --from 2026-09-01 --to 2026-09-07 --kind form --limit 10
|
|
242
|
+
jinshuju entry stats --from 2026-09-28 --by day # the same, split into each day, in one call
|
|
243
|
+
jinshuju entry stats --from 2026-01-01 --by month # or by week / month: at most 92 days, 53 weeks, 24 months
|
|
241
244
|
```
|
|
242
245
|
|
|
243
246
|
A form that could not be searched stays in the result **with the reason**,
|
package/dist/auth.d.ts
CHANGED
|
@@ -11,7 +11,6 @@ export type OAuthLoginOptions = LoadConfigOptions & {
|
|
|
11
11
|
authHost?: string;
|
|
12
12
|
clientId?: string;
|
|
13
13
|
scopes?: string;
|
|
14
|
-
openBrowser?: boolean;
|
|
15
14
|
timeoutMs?: number;
|
|
16
15
|
port?: number;
|
|
17
16
|
};
|
|
@@ -35,5 +34,9 @@ export declare function buildAuthorizationUrl(params: {
|
|
|
35
34
|
}): string;
|
|
36
35
|
export declare function loginWithOAuth(options?: OAuthLoginOptions, opener?: BrowserOpener): Promise<OAuthLoginResult>;
|
|
37
36
|
export declare function refreshOAuthToken(config: LoadedConfig): Promise<OAuthConfig>;
|
|
38
|
-
|
|
37
|
+
/**
|
|
38
|
+
* Revokes a stored OAuth session, then forgets whatever credential is stored.
|
|
39
|
+
* An access token has no revoke endpoint here; it is only forgotten.
|
|
40
|
+
*/
|
|
41
|
+
export declare function logout(config: LoadedConfig): Promise<void>;
|
|
39
42
|
export declare function shouldRefresh(auth: OAuthConfig, skewMs?: number): boolean;
|
package/dist/auth.js
CHANGED
|
@@ -2,7 +2,7 @@ import { createHash, randomBytes, timingSafeEqual } from 'node:crypto';
|
|
|
2
2
|
import { createServer } from 'node:http';
|
|
3
3
|
import { execFile } from 'node:child_process';
|
|
4
4
|
import { AuthError } from './errors.js';
|
|
5
|
-
import {
|
|
5
|
+
import { clearCredential, defaultScopes, loadConfig, saveOAuthConfig } from './config.js';
|
|
6
6
|
function base64Url(bytes) {
|
|
7
7
|
return bytes.toString('base64').replace(/=/g, '').replace(/\+/g, '-').replace(/\//g, '_');
|
|
8
8
|
}
|
|
@@ -47,8 +47,7 @@ export async function loginWithOAuth(options = {}, opener = openUrl) {
|
|
|
47
47
|
// state, a refusal — and a rejection nobody is awaiting yet is reported as
|
|
48
48
|
// unhandled. It is awaited right below; this only says so in the meantime.
|
|
49
49
|
callback.code.catch(() => undefined);
|
|
50
|
-
|
|
51
|
-
await opener(authorizeUrl);
|
|
50
|
+
await opener(authorizeUrl);
|
|
52
51
|
const code = await callback.code;
|
|
53
52
|
const token = await exchangeAuthorizationCode(config.authHost, clientId, callback.redirectUri, code, verifier);
|
|
54
53
|
const auth = toOAuthConfig(config, clientId, token);
|
|
@@ -67,17 +66,21 @@ export async function refreshOAuthToken(config) {
|
|
|
67
66
|
saveOAuthConfig(config.configPath, auth);
|
|
68
67
|
return auth;
|
|
69
68
|
}
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
69
|
+
/**
|
|
70
|
+
* Revokes a stored OAuth session, then forgets whatever credential is stored.
|
|
71
|
+
* An access token has no revoke endpoint here; it is only forgotten.
|
|
72
|
+
*/
|
|
73
|
+
export async function logout(config) {
|
|
74
|
+
if (config.auth?.access_token) {
|
|
75
|
+
const url = new URL('/oauth/revoke', config.auth.auth_host);
|
|
76
|
+
await fetch(url, {
|
|
77
|
+
method: 'POST',
|
|
78
|
+
headers: { 'Content-Type': 'application/x-www-form-urlencoded', Accept: 'application/json' },
|
|
79
|
+
body: new URLSearchParams({ client_id: config.auth.client_id, token: config.auth.access_token }),
|
|
80
|
+
signal: AbortSignal.timeout(config.timeoutMs ?? TOKEN_TIMEOUT_MS)
|
|
81
|
+
}).catch(() => undefined);
|
|
82
|
+
}
|
|
83
|
+
clearCredential(config.configPath);
|
|
81
84
|
}
|
|
82
85
|
export function shouldRefresh(auth, skewMs = 60_000) {
|
|
83
86
|
if (!auth.expires_at)
|
package/dist/cli.js
CHANGED
|
@@ -46,11 +46,7 @@ async function runRemote(command, words, flags, runtime, stdin) {
|
|
|
46
46
|
new JinshujuHttpClient(loadConfig({
|
|
47
47
|
configPath: options.config ?? defaultConfigPath,
|
|
48
48
|
env: runtime.env,
|
|
49
|
-
cli: {
|
|
50
|
-
apiKey: options.api_key,
|
|
51
|
-
apiSecret: options.api_secret,
|
|
52
|
-
host: options.host
|
|
53
|
-
}
|
|
49
|
+
cli: { host: options.host }
|
|
54
50
|
}));
|
|
55
51
|
// A command that needs more than one round trip, or has to decide how many,
|
|
56
52
|
// runs itself and answers with what should be printed.
|
package/dist/commands/account.js
CHANGED
|
@@ -16,5 +16,40 @@ export const ACCOUNT = [
|
|
|
16
16
|
summary: 'List account members',
|
|
17
17
|
options: [LIMIT_OPTION],
|
|
18
18
|
request: (input) => ({ method: 'GET', path: `${API}/billing_account/users`, query: paging(input) })
|
|
19
|
+
},
|
|
20
|
+
{
|
|
21
|
+
path: ['account', 'invoice', 'list'],
|
|
22
|
+
summary: 'List e-invoices, and the amount not invoiced yet',
|
|
23
|
+
description: 'Account admins only, as in the web app; a member is refused with 403.',
|
|
24
|
+
request: () => ({ method: 'GET', path: `${API}/billing_account/invoices` })
|
|
25
|
+
},
|
|
26
|
+
{
|
|
27
|
+
path: ['account', 'payment', 'list'],
|
|
28
|
+
summary: 'List what the account spent or topped up on 金数据: SMS, AI points, plans, recharges',
|
|
29
|
+
description: 'Not what its payment forms collected from customers: that is transaction list. Newest first. ' +
|
|
30
|
+
'Account admins only, as in the web app; a member is refused with 403.',
|
|
31
|
+
options: [
|
|
32
|
+
{ name: '--from', type: 'string', placeholder: '<YYYY-MM-DD>', description: 'First day' },
|
|
33
|
+
{ name: '--to', type: 'string', placeholder: '<YYYY-MM-DD>', description: 'Last day, inclusive' },
|
|
34
|
+
{
|
|
35
|
+
name: '--verb',
|
|
36
|
+
type: 'string',
|
|
37
|
+
placeholder: '<verb>',
|
|
38
|
+
description: 'Only this kind of record, e.g. sms_charge'
|
|
39
|
+
},
|
|
40
|
+
{ ...LIMIT_OPTION, description: 'Records to return (default 50, up to 1000)' },
|
|
41
|
+
{ name: '--with-balance', type: 'boolean', description: 'Also return the current balance and quotas' }
|
|
42
|
+
],
|
|
43
|
+
request: (input) => ({
|
|
44
|
+
method: 'GET',
|
|
45
|
+
path: `${API}/billing_account/payment_histories`,
|
|
46
|
+
query: {
|
|
47
|
+
start_date: input.options.from,
|
|
48
|
+
end_date: input.options.to,
|
|
49
|
+
verb: input.options.verb,
|
|
50
|
+
limit: input.options.limit === undefined ? undefined : String(input.options.limit),
|
|
51
|
+
include_balance: input.options.with_balance ? 'true' : undefined
|
|
52
|
+
}
|
|
53
|
+
})
|
|
19
54
|
}
|
|
20
55
|
];
|
package/dist/commands/entry.js
CHANGED
|
@@ -15,6 +15,12 @@ function list(value) {
|
|
|
15
15
|
function labels(input) {
|
|
16
16
|
return input.options.labels ? 'true' : undefined;
|
|
17
17
|
}
|
|
18
|
+
/**
|
|
19
|
+
* The web host is not the API host on every environment, so the link is the
|
|
20
|
+
* server's to spell; all a command can do is say it is there and what it is for.
|
|
21
|
+
*/
|
|
22
|
+
const DETAIL_URL = 'One entry comes back with `url`, where it opens in the Jinshuju data page: hand it to the user ' +
|
|
23
|
+
'so they can check it.';
|
|
18
24
|
const LABELS_OPTION = {
|
|
19
25
|
name: '--labels',
|
|
20
26
|
type: 'boolean',
|
|
@@ -406,10 +412,13 @@ export const ENTRY = [
|
|
|
406
412
|
{
|
|
407
413
|
path: ['entry', 'stats'],
|
|
408
414
|
summary: 'Count submissions per form over a date range',
|
|
415
|
+
// Split, each bucket's forms are the answer; a table of bucket totals drops them.
|
|
416
|
+
text: { essentialLists: ['data'] },
|
|
409
417
|
description: 'Not the question `entry count` answers. These are submissions as they happened: an import ' +
|
|
410
418
|
'lands on the day it ran whatever dates its rows carry, and deletions are never subtracted, ' +
|
|
411
419
|
'so this is how much arrived rather than how much is still there. A whole day is the ' +
|
|
412
|
-
'smallest window; both ends are inclusive and days are cut in the reported time zone.'
|
|
420
|
+
'smallest window; both ends are inclusive and days are cut in the reported time zone. For a ' +
|
|
421
|
+
'day-by-day, weekly or monthly answer pass --by once rather than one call per period.',
|
|
413
422
|
options: [
|
|
414
423
|
{ name: '--from', type: 'string', placeholder: '<YYYY-MM-DD>', description: 'First day to count, inclusive' },
|
|
415
424
|
{
|
|
@@ -429,7 +438,16 @@ export const ENTRY = [
|
|
|
429
438
|
name: '--limit',
|
|
430
439
|
type: 'integer',
|
|
431
440
|
placeholder: '<n>',
|
|
432
|
-
description: 'How many forms to list, most submissions first (default 100, max 100)'
|
|
441
|
+
description: 'How many forms to list, most submissions first (default 100, or 10 per bucket with --by; max 100)'
|
|
442
|
+
},
|
|
443
|
+
{
|
|
444
|
+
name: '--by',
|
|
445
|
+
type: 'string',
|
|
446
|
+
choices: ['day', 'week', 'month'],
|
|
447
|
+
placeholder: '<unit>',
|
|
448
|
+
description: 'Also split the range into buckets: every one, empty ones with total 0, --limit per bucket. ' +
|
|
449
|
+
'Weeks start on Monday; the first and last are clipped to the range. ' +
|
|
450
|
+
'At most 92 days, 53 weeks or 24 months'
|
|
433
451
|
}
|
|
434
452
|
],
|
|
435
453
|
request: (input) => ({
|
|
@@ -439,12 +457,15 @@ export const ENTRY = [
|
|
|
439
457
|
from: requiredOption(input, 'from'),
|
|
440
458
|
to: input.options.to,
|
|
441
459
|
kind: input.options.kind,
|
|
442
|
-
limit: input.options.limit === undefined ? undefined : String(input.options.limit)
|
|
460
|
+
limit: input.options.limit === undefined ? undefined : String(input.options.limit),
|
|
461
|
+
by: input.options.by
|
|
443
462
|
}
|
|
444
463
|
}),
|
|
445
464
|
examples: [
|
|
446
465
|
'jinshuju entry stats --from 2026-09-01',
|
|
447
|
-
'jinshuju entry stats --from 2026-09-01 --to 2026-09-07 --kind form --limit 10'
|
|
466
|
+
'jinshuju entry stats --from 2026-09-01 --to 2026-09-07 --kind form --limit 10',
|
|
467
|
+
'jinshuju entry stats --from 2026-09-28 --by day',
|
|
468
|
+
'jinshuju entry stats --from 2026-01-01 --by month'
|
|
448
469
|
]
|
|
449
470
|
},
|
|
450
471
|
{
|
|
@@ -537,7 +558,9 @@ export const ENTRY = [
|
|
|
537
558
|
path: ['entry', 'create'],
|
|
538
559
|
summary: 'Create entries',
|
|
539
560
|
description: 'The payload is keyed by field api_code, not by field label. --batch takes a list of them and ' +
|
|
540
|
-
'writes them in one request. ' +
|
|
561
|
+
'writes them in one request, and answers without a `url` for each. ' +
|
|
562
|
+
DETAIL_URL +
|
|
563
|
+
' ' +
|
|
541
564
|
VALUE_SHAPES,
|
|
542
565
|
options: [...CONTAINER_OPTIONS, JSON_OPTION, BATCH_OPTION, ATTACH_OPTION],
|
|
543
566
|
run: async (input, client) => {
|
|
@@ -566,6 +589,8 @@ export const ENTRY = [
|
|
|
566
589
|
description: 'The payload merges onto the entry, leaving the fields it does not name alone; --replace ' +
|
|
567
590
|
'writes the entry as given, clearing the rest. --batch takes [{serial_number, entry}] and ' +
|
|
568
591
|
'always merges. ' +
|
|
592
|
+
DETAIL_URL +
|
|
593
|
+
' ' +
|
|
569
594
|
VALUE_SHAPES,
|
|
570
595
|
args: [{ name: 'serial', required: false, description: 'Entry serial number; leave out with --batch' }],
|
|
571
596
|
options: [
|
|
@@ -708,6 +733,7 @@ export const ENTRY = [
|
|
|
708
733
|
{
|
|
709
734
|
path: ['entry', 'get'],
|
|
710
735
|
summary: 'Show one entry',
|
|
736
|
+
description: DETAIL_URL,
|
|
711
737
|
args: [{ name: 'serial', required: true, description: 'Entry serial number' }],
|
|
712
738
|
options: [
|
|
713
739
|
...CONTAINER_OPTIONS,
|
package/dist/commands/index.js
CHANGED
|
@@ -15,17 +15,19 @@ import { FOLDER } from './folder.js';
|
|
|
15
15
|
import { FORM } from './form.js';
|
|
16
16
|
import { LOCAL } from './local.js';
|
|
17
17
|
import { OPENSEARCH } from './opensearch.js';
|
|
18
|
+
import { SMS } from './sms.js';
|
|
18
19
|
import { TABLE } from './table.js';
|
|
20
|
+
import { TRADE } from './trade.js';
|
|
19
21
|
import { VIEW } from './view.js';
|
|
20
22
|
/** Resource order in the root help, and the one-liner each gets. */
|
|
21
23
|
export const RESOURCES = [
|
|
22
24
|
{
|
|
23
25
|
name: 'auth',
|
|
24
26
|
summary: 'Manage authentication',
|
|
25
|
-
note: 'login is the browser flow. To use a token instead: `jinshuju
|
|
27
|
+
note: 'login is the browser flow. To use a token instead: `jinshuju auth login --access-token <token>`, ' +
|
|
26
28
|
'or set JINSHUJU_ACCESS_TOKEN. `auth status --verify` says which account the credential belongs to.'
|
|
27
29
|
},
|
|
28
|
-
{ name: 'account', summary: 'Account and
|
|
30
|
+
{ name: 'account', summary: 'Account, members, invoices and what the account paid' },
|
|
29
31
|
{ name: 'folder', summary: 'Manage folders' },
|
|
30
32
|
{
|
|
31
33
|
name: 'form',
|
|
@@ -39,6 +41,11 @@ export const RESOURCES = [
|
|
|
39
41
|
{ name: 'entry', summary: 'Manage entries' },
|
|
40
42
|
{ name: 'comment', summary: 'Manage entry comments' },
|
|
41
43
|
{ name: 'opensearch', summary: 'Manage public queries' },
|
|
44
|
+
{ name: 'merchant', summary: '小金商户: onboarding, balance and settlement' },
|
|
45
|
+
{ name: 'transaction', summary: 'Payment orders placed on payment forms' },
|
|
46
|
+
{ name: 'refund', summary: 'Refunds of payment orders' },
|
|
47
|
+
{ name: 'payout', summary: 'Payouts to the settlement account' },
|
|
48
|
+
{ name: 'sms', summary: 'SMS signature and template review' },
|
|
42
49
|
{ name: 'config', summary: 'Manage CLI configuration' }
|
|
43
50
|
];
|
|
44
51
|
export const COMMANDS = [
|
|
@@ -51,7 +58,9 @@ export const COMMANDS = [
|
|
|
51
58
|
...VIEW,
|
|
52
59
|
...ENTRY,
|
|
53
60
|
...COMMENT,
|
|
54
|
-
...OPENSEARCH
|
|
61
|
+
...OPENSEARCH,
|
|
62
|
+
...TRADE,
|
|
63
|
+
...SMS
|
|
55
64
|
];
|
|
56
65
|
/** The command whose path the words begin with, longest match first. */
|
|
57
66
|
export function findCommand(words, commands = COMMANDS) {
|
package/dist/commands/local.js
CHANGED
|
@@ -20,20 +20,21 @@ function local(...names) {
|
|
|
20
20
|
export const LOCAL = [
|
|
21
21
|
{
|
|
22
22
|
path: ['auth', 'login'],
|
|
23
|
-
summary: 'Log in through the browser and store the
|
|
24
|
-
description: '
|
|
25
|
-
'session to the config file.
|
|
26
|
-
'
|
|
27
|
-
|
|
28
|
-
|
|
23
|
+
summary: 'Log in through the browser, or with an access token, and store the credential',
|
|
24
|
+
description: 'Without --access-token: opens the authorization page, waits for the redirect on a loopback ' +
|
|
25
|
+
'port, and writes the session to the config file. With it: checks the token with one call, ' +
|
|
26
|
+
'then stores it; the OAuth options are ignored. Either replaces whatever credential was stored ' +
|
|
27
|
+
'before. JINSHUJU_ACCESS_TOKEN, if set, still outranks what this stores.',
|
|
28
|
+
options: local('--access-token', '--auth-host', '--client-id', '--scopes', '--no-open', '--host'),
|
|
29
|
+
examples: ['jinshuju auth login', 'jinshuju auth login --no-open', 'jinshuju auth login --access-token <token>']
|
|
29
30
|
},
|
|
30
31
|
{
|
|
31
32
|
path: ['auth', 'status'],
|
|
32
33
|
summary: 'Show which credential is in use, and where it came from',
|
|
33
|
-
description: 'The precedence is
|
|
34
|
+
description: 'The precedence is JINSHUJU_ACCESS_TOKEN, then the credential `auth login` stored. ' +
|
|
34
35
|
'--verify spends one call to confirm the credential still works, and reports the account it ' +
|
|
35
36
|
'belongs to — with more than one configured, nothing else here says which is in play.',
|
|
36
|
-
options: local('--verify', '--
|
|
37
|
+
options: local('--verify', '--host', '--auth-host', '--client-id'),
|
|
37
38
|
examples: ['jinshuju auth status', 'jinshuju auth status --verify']
|
|
38
39
|
},
|
|
39
40
|
{
|
|
@@ -44,33 +45,33 @@ export const LOCAL = [
|
|
|
44
45
|
},
|
|
45
46
|
{
|
|
46
47
|
path: ['auth', 'logout'],
|
|
47
|
-
summary: '
|
|
48
|
-
description:
|
|
48
|
+
summary: 'Forget the stored credential, revoking it first if it is a browser session',
|
|
49
|
+
description: 'An access token cannot be revoked from here, only forgotten. JINSHUJU_ACCESS_TOKEN is not ' +
|
|
50
|
+
"this command's to drop.",
|
|
49
51
|
options: local('--auth-host', '--client-id')
|
|
50
52
|
},
|
|
51
53
|
{
|
|
52
54
|
path: ['config', 'get'],
|
|
53
55
|
summary: 'Read one configuration value',
|
|
54
|
-
description: `Keys: ${CONFIG_KEYS.join(', ')}.
|
|
56
|
+
description: `Keys: ${CONFIG_KEYS.join(', ')}. Credentials are not config: \`auth login\` stores them.`,
|
|
55
57
|
args: [{ name: 'key', required: true, description: `One of ${CONFIG_KEYS.join(', ')}` }],
|
|
56
|
-
|
|
57
|
-
examples: ['jinshuju config get api_key', 'jinshuju config get access_token --show-secret']
|
|
58
|
+
examples: ['jinshuju config get host']
|
|
58
59
|
},
|
|
59
60
|
{
|
|
60
61
|
path: ['config', 'set'],
|
|
61
62
|
summary: 'Write one configuration value',
|
|
62
|
-
description: `Keys: ${CONFIG_KEYS.join(', ')}. The file is written with mode 600.
|
|
63
|
-
'
|
|
63
|
+
description: `Keys: ${CONFIG_KEYS.join(', ')}. The file is written with mode 600. JINSHUJU_HOST, ` +
|
|
64
|
+
'JINSHUJU_AUTH_HOST and JINSHUJU_OAUTH_CLIENT_ID outrank whatever is stored here.',
|
|
64
65
|
args: [
|
|
65
66
|
{ name: 'key', required: true, description: `One of ${CONFIG_KEYS.join(', ')}` },
|
|
66
67
|
{ name: 'value', required: true, description: 'The value to store' }
|
|
67
68
|
],
|
|
68
|
-
examples: ['jinshuju config set
|
|
69
|
+
examples: ['jinshuju config set host https://jinshuju.net']
|
|
69
70
|
},
|
|
70
71
|
{
|
|
71
72
|
path: ['config', 'unset'],
|
|
72
73
|
summary: 'Remove one configuration value',
|
|
73
74
|
args: [{ name: 'key', required: true, description: `One of ${CONFIG_KEYS.join(', ')}` }],
|
|
74
|
-
examples: ['jinshuju config unset
|
|
75
|
+
examples: ['jinshuju config unset host']
|
|
75
76
|
}
|
|
76
77
|
];
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { API } from './shared.js';
|
|
2
|
+
export const SMS = [
|
|
3
|
+
{
|
|
4
|
+
path: ['sms', 'status'],
|
|
5
|
+
summary: 'List SMS signatures and templates with their review status and why a review failed',
|
|
6
|
+
description: 'Your own signatures and templates, not those of the rest of the account.',
|
|
7
|
+
request: () => ({ method: 'GET', path: `${API}/sms/audit_statuses` })
|
|
8
|
+
}
|
|
9
|
+
];
|
|
@@ -0,0 +1,185 @@
|
|
|
1
|
+
import { PAGINATION_OPTIONS } from '../options.js';
|
|
2
|
+
import { API, paging } from './shared.js';
|
|
3
|
+
/**
|
|
4
|
+
* The account's 小金商户: what its payment forms collected, refunded and paid
|
|
5
|
+
* out. An account collecting through its own bound WeChat / Alipay merchant
|
|
6
|
+
* (直连) has none of this in 金数据, and the reads below say so rather than
|
|
7
|
+
* answering with zeroes.
|
|
8
|
+
*/
|
|
9
|
+
const PAID_STATUSES = ['paid', 'unpaid', 'refunded', 'refunding', 'partial_refunded'];
|
|
10
|
+
const RECORD_STATUSES = ['processing', 'success', 'error'];
|
|
11
|
+
const DATE_OPTIONS = [
|
|
12
|
+
{
|
|
13
|
+
name: '--from',
|
|
14
|
+
type: 'string',
|
|
15
|
+
placeholder: '<YYYY-MM-DD>',
|
|
16
|
+
description: 'First day, Asia/Shanghai; give it with --to'
|
|
17
|
+
},
|
|
18
|
+
{ name: '--to', type: 'string', placeholder: '<YYYY-MM-DD>', description: 'Last day, inclusive; give it with --from' }
|
|
19
|
+
];
|
|
20
|
+
/** The CLI spells a dimension as its flag is spelled; the API names it by its parameter. */
|
|
21
|
+
const GROUP_BY = { time: 'time', 'paid-status': 'paid_status', channel: 'payment_channel' };
|
|
22
|
+
const ORDER_FILTER_OPTIONS = [
|
|
23
|
+
{
|
|
24
|
+
name: '--status',
|
|
25
|
+
type: 'string',
|
|
26
|
+
repeatable: true,
|
|
27
|
+
choices: PAID_STATUSES,
|
|
28
|
+
placeholder: '<status>',
|
|
29
|
+
description: 'Keep only these payment statuses, repeatable. Omitted, every status, unpaid included'
|
|
30
|
+
},
|
|
31
|
+
{
|
|
32
|
+
name: '--channel',
|
|
33
|
+
type: 'string',
|
|
34
|
+
choices: ['wxpay', 'alipay'],
|
|
35
|
+
placeholder: '<channel>',
|
|
36
|
+
description: 'Keep only orders paid through this channel: wxpay (微信) or alipay (支付宝)'
|
|
37
|
+
},
|
|
38
|
+
{ name: '--form', type: 'string', placeholder: '<token>', description: 'Keep only orders of this payment form' }
|
|
39
|
+
];
|
|
40
|
+
const INCLUDE_OPTION = {
|
|
41
|
+
name: '--include',
|
|
42
|
+
type: 'string',
|
|
43
|
+
choices: ['both', 'stats', 'records'],
|
|
44
|
+
placeholder: '<what>',
|
|
45
|
+
description: 'both (default), stats for the statistics alone, or records for the rows alone'
|
|
46
|
+
};
|
|
47
|
+
const DETAIL_LIMITS = 'Rows are capped at 90 days and 200 matching records per query; past either, no rows come back, ' +
|
|
48
|
+
'only detail_limit and the stats for the whole range. Narrow the range rather than splitting it.';
|
|
49
|
+
function dates(input) {
|
|
50
|
+
return {
|
|
51
|
+
start_date: input.options.from,
|
|
52
|
+
end_date: input.options.to
|
|
53
|
+
};
|
|
54
|
+
}
|
|
55
|
+
function orderFilters(input) {
|
|
56
|
+
const statuses = input.options.status ?? [];
|
|
57
|
+
return {
|
|
58
|
+
paid_status: statuses.length > 0 ? statuses : undefined,
|
|
59
|
+
payment_channel: input.options.channel,
|
|
60
|
+
form_token: input.options.form
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
export const TRADE = [
|
|
64
|
+
{
|
|
65
|
+
path: ['merchant', 'get'],
|
|
66
|
+
summary: 'Show the 小金商户: where onboarding stands, balance, rate and settlement account',
|
|
67
|
+
description: 'Answers without a merchant too: merchant_stage says where the application stands. Bank card, phone, ' +
|
|
68
|
+
'email and ID numbers come back masked, as the product shows them.',
|
|
69
|
+
request: () => ({ method: 'GET', path: `${API}/trade/merchant` })
|
|
70
|
+
},
|
|
71
|
+
{
|
|
72
|
+
path: ['transaction', 'list'],
|
|
73
|
+
summary: 'List payment orders customers placed on payment forms',
|
|
74
|
+
description: 'Defaults to the last 90 days; --order-no alone searches all time. ' +
|
|
75
|
+
DETAIL_LIMITS +
|
|
76
|
+
' For totals or a breakdown, use transaction aggregate instead of adding up rows.',
|
|
77
|
+
options: [
|
|
78
|
+
...DATE_OPTIONS,
|
|
79
|
+
...ORDER_FILTER_OPTIONS,
|
|
80
|
+
{
|
|
81
|
+
name: '--order-no',
|
|
82
|
+
type: 'string',
|
|
83
|
+
placeholder: '<no>',
|
|
84
|
+
description: 'Exact match on 交易单号, 商户订单号 or 金数据单号'
|
|
85
|
+
},
|
|
86
|
+
...PAGINATION_OPTIONS
|
|
87
|
+
],
|
|
88
|
+
request: (input) => ({
|
|
89
|
+
method: 'GET',
|
|
90
|
+
path: `${API}/trade/transactions`,
|
|
91
|
+
query: {
|
|
92
|
+
...dates(input),
|
|
93
|
+
...orderFilters(input),
|
|
94
|
+
order_no: input.options.order_no,
|
|
95
|
+
...paging(input)
|
|
96
|
+
}
|
|
97
|
+
}),
|
|
98
|
+
paginate: { items: 'transactions', cursor: 'next' },
|
|
99
|
+
examples: [
|
|
100
|
+
'jinshuju transaction list --from 2026-09-01 --to 2026-09-28',
|
|
101
|
+
'jinshuju transaction list --order-no 1234567890',
|
|
102
|
+
'jinshuju transaction list --form Kp7mQ2 --status refunded --status partial_refunded --all'
|
|
103
|
+
]
|
|
104
|
+
},
|
|
105
|
+
{
|
|
106
|
+
path: ['transaction', 'aggregate'],
|
|
107
|
+
summary: 'Total the payment orders: amount, paid count and order count, optionally grouped',
|
|
108
|
+
description: 'Defaults to the last 30 days, up to 366. The totals cover the whole range whatever the paging of a listing.',
|
|
109
|
+
options: [
|
|
110
|
+
...DATE_OPTIONS,
|
|
111
|
+
{
|
|
112
|
+
name: '--group-by',
|
|
113
|
+
type: 'string',
|
|
114
|
+
choices: Object.keys(GROUP_BY),
|
|
115
|
+
placeholder: '<dimension>',
|
|
116
|
+
description: 'Break the totals down by one dimension; time picks day, week or month from the span'
|
|
117
|
+
},
|
|
118
|
+
...ORDER_FILTER_OPTIONS
|
|
119
|
+
],
|
|
120
|
+
request: (input) => ({
|
|
121
|
+
method: 'GET',
|
|
122
|
+
path: `${API}/trade/transactions/aggregate`,
|
|
123
|
+
query: { ...dates(input), group_by: GROUP_BY[input.options.group_by], ...orderFilters(input) }
|
|
124
|
+
}),
|
|
125
|
+
examples: ['jinshuju transaction aggregate --from 2026-09-01 --to 2026-09-28 --group-by channel']
|
|
126
|
+
},
|
|
127
|
+
{
|
|
128
|
+
path: ['refund', 'list'],
|
|
129
|
+
summary: 'List refunds, with their statistics',
|
|
130
|
+
description: 'Defaults to the last 90 days; --refund-no alone searches all time. ' + DETAIL_LIMITS,
|
|
131
|
+
options: [
|
|
132
|
+
...DATE_OPTIONS,
|
|
133
|
+
{
|
|
134
|
+
name: '--status',
|
|
135
|
+
type: 'string',
|
|
136
|
+
choices: RECORD_STATUSES,
|
|
137
|
+
placeholder: '<status>',
|
|
138
|
+
description: 'Keep only refunds in this state'
|
|
139
|
+
},
|
|
140
|
+
{ name: '--refund-no', type: 'string', placeholder: '<no>', description: 'Exact match on 退款流水号' },
|
|
141
|
+
INCLUDE_OPTION,
|
|
142
|
+
...PAGINATION_OPTIONS
|
|
143
|
+
],
|
|
144
|
+
request: (input) => ({
|
|
145
|
+
method: 'GET',
|
|
146
|
+
path: `${API}/trade/refunds`,
|
|
147
|
+
query: {
|
|
148
|
+
...dates(input),
|
|
149
|
+
status: input.options.status,
|
|
150
|
+
refund_no: input.options.refund_no,
|
|
151
|
+
include: input.options.include,
|
|
152
|
+
...paging(input)
|
|
153
|
+
}
|
|
154
|
+
}),
|
|
155
|
+
paginate: { items: 'refunds', cursor: 'next' }
|
|
156
|
+
},
|
|
157
|
+
{
|
|
158
|
+
path: ['payout', 'list'],
|
|
159
|
+
summary: 'List payouts (提现) to the settlement account, with their statistics',
|
|
160
|
+
description: 'Defaults to the last 90 days. ' + DETAIL_LIMITS,
|
|
161
|
+
options: [
|
|
162
|
+
...DATE_OPTIONS,
|
|
163
|
+
{
|
|
164
|
+
name: '--status',
|
|
165
|
+
type: 'string',
|
|
166
|
+
choices: RECORD_STATUSES,
|
|
167
|
+
placeholder: '<status>',
|
|
168
|
+
description: 'Keep only payouts in this state'
|
|
169
|
+
},
|
|
170
|
+
INCLUDE_OPTION,
|
|
171
|
+
...PAGINATION_OPTIONS
|
|
172
|
+
],
|
|
173
|
+
request: (input) => ({
|
|
174
|
+
method: 'GET',
|
|
175
|
+
path: `${API}/trade/payouts`,
|
|
176
|
+
query: {
|
|
177
|
+
...dates(input),
|
|
178
|
+
status: input.options.status,
|
|
179
|
+
include: input.options.include,
|
|
180
|
+
...paging(input)
|
|
181
|
+
}
|
|
182
|
+
}),
|
|
183
|
+
paginate: { items: 'payouts', cursor: 'next' }
|
|
184
|
+
}
|
|
185
|
+
];
|
package/dist/config.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
export type ConfigKey = '
|
|
1
|
+
export type ConfigKey = 'host' | 'auth_host' | 'client_id';
|
|
2
2
|
export type ConfigSource = 'cli' | 'env' | 'file' | 'missing';
|
|
3
3
|
export type OAuthConfig = {
|
|
4
4
|
type: 'oauth';
|
|
@@ -9,14 +9,17 @@ export type OAuthConfig = {
|
|
|
9
9
|
expires_at?: string;
|
|
10
10
|
scope?: string;
|
|
11
11
|
};
|
|
12
|
+
/** An access token stored by `auth login --access-token`. It takes the same slot as an OAuth session. */
|
|
13
|
+
export type TokenConfig = {
|
|
14
|
+
type: 'access_token';
|
|
15
|
+
access_token: string;
|
|
16
|
+
};
|
|
12
17
|
export type LoadedConfig = {
|
|
13
18
|
/**
|
|
14
|
-
* A personal or account access token, sent as a bearer
|
|
15
|
-
*
|
|
19
|
+
* A personal or account access token, sent as a bearer: JINSHUJU_ACCESS_TOKEN,
|
|
20
|
+
* or the one `auth login --access-token` stored.
|
|
16
21
|
*/
|
|
17
22
|
accessToken?: string;
|
|
18
|
-
apiKey?: string;
|
|
19
|
-
apiSecret?: string;
|
|
20
23
|
host: string;
|
|
21
24
|
authHost: string;
|
|
22
25
|
clientId?: string;
|
|
@@ -26,8 +29,6 @@ export type LoadedConfig = {
|
|
|
26
29
|
configPath: string;
|
|
27
30
|
sources: {
|
|
28
31
|
accessToken: ConfigSource;
|
|
29
|
-
apiKey: ConfigSource;
|
|
30
|
-
apiSecret: ConfigSource;
|
|
31
32
|
host: ConfigSource;
|
|
32
33
|
authHost: ConfigSource;
|
|
33
34
|
clientId: ConfigSource;
|
|
@@ -38,9 +39,6 @@ export type LoadConfigOptions = {
|
|
|
38
39
|
configPath?: string;
|
|
39
40
|
env?: NodeJS.ProcessEnv | Record<string, string | undefined>;
|
|
40
41
|
cli?: {
|
|
41
|
-
accessToken?: string;
|
|
42
|
-
apiKey?: string;
|
|
43
|
-
apiSecret?: string;
|
|
44
42
|
host?: string;
|
|
45
43
|
authHost?: string;
|
|
46
44
|
clientId?: string;
|
|
@@ -51,16 +49,21 @@ export declare const defaultHost = "https://jinshuju.net";
|
|
|
51
49
|
export declare const defaultAuthHost = "https://account.jinshuju.net";
|
|
52
50
|
export declare const defaultOAuthClientId = "jinshuju_cli_public";
|
|
53
51
|
export declare const defaultScopes = "public forms read_entries write_entries form_setting read_contacts users";
|
|
52
|
+
/**
|
|
53
|
+
* One credential at a time: a login of either kind replaces whatever the last
|
|
54
|
+
* one stored, so the file never holds two that disagree about who is calling.
|
|
55
|
+
*/
|
|
54
56
|
export type RawConfig = Partial<Record<ConfigKey, string>> & {
|
|
55
|
-
auth?: OAuthConfig;
|
|
57
|
+
auth?: OAuthConfig | TokenConfig;
|
|
56
58
|
};
|
|
57
59
|
export declare function loadConfig(options?: LoadConfigOptions): LoadedConfig;
|
|
58
60
|
export declare function setConfigValue(configPath: string, key: ConfigKey, value: string): void;
|
|
59
61
|
export declare function unsetConfigValue(configPath: string, key: ConfigKey): void;
|
|
60
62
|
export declare function saveOAuthConfig(configPath: string, auth: OAuthConfig): void;
|
|
61
|
-
export declare function
|
|
63
|
+
export declare function saveAccessToken(configPath: string, accessToken: string): void;
|
|
64
|
+
/** Forgets the stored credential, whichever kind it is. */
|
|
65
|
+
export declare function clearCredential(configPath: string): void;
|
|
62
66
|
export declare function getConfig(configPath: string): RawConfig;
|
|
63
|
-
export declare function maskSecret(value: string | undefined): string | undefined;
|
|
64
67
|
/** Every key the config file holds, in the order help should list them. */
|
|
65
68
|
export declare const CONFIG_KEYS: readonly ConfigKey[];
|
|
66
69
|
export declare function assertConfigKey(value: string): asserts value is ConfigKey;
|
package/dist/config.js
CHANGED
|
@@ -53,19 +53,16 @@ export function loadConfig(options = {}) {
|
|
|
53
53
|
const configPath = options.configPath ?? defaultConfigPath;
|
|
54
54
|
const env = options.env ?? process.env;
|
|
55
55
|
const file = readConfigFile(configPath);
|
|
56
|
-
const
|
|
57
|
-
const
|
|
58
|
-
const
|
|
56
|
+
const auth = file.auth?.type === 'oauth' ? file.auth : undefined;
|
|
57
|
+
const storedToken = file.auth?.type === 'access_token' ? file.auth.access_token : undefined;
|
|
58
|
+
const accessToken = pickValue(undefined, env.JINSHUJU_ACCESS_TOKEN, storedToken);
|
|
59
59
|
const host = pickValue(options.cli?.host, env.JINSHUJU_HOST, file.host, defaultHost);
|
|
60
|
-
const authHost = pickValue(options.cli?.authHost, env.JINSHUJU_AUTH_HOST, file.auth_host ??
|
|
60
|
+
const authHost = pickValue(options.cli?.authHost, env.JINSHUJU_AUTH_HOST, file.auth_host ?? auth?.auth_host, defaultAuthHost);
|
|
61
61
|
const defaultClientId = authHost.value === defaultAuthHost ? defaultOAuthClientId : undefined;
|
|
62
|
-
const clientId = pickValue(options.cli?.clientId, env.JINSHUJU_OAUTH_CLIENT_ID, file.client_id ??
|
|
63
|
-
const auth = file.auth?.type === 'oauth' ? file.auth : undefined;
|
|
62
|
+
const clientId = pickValue(options.cli?.clientId, env.JINSHUJU_OAUTH_CLIENT_ID, file.client_id ?? auth?.client_id, defaultClientId);
|
|
64
63
|
const timeoutMs = parseTimeout(env.JINSHUJU_TIMEOUT_MS);
|
|
65
64
|
return {
|
|
66
65
|
accessToken: accessToken.value,
|
|
67
|
-
apiKey: apiKey.value,
|
|
68
|
-
apiSecret: apiSecret.value,
|
|
69
66
|
host: host.value,
|
|
70
67
|
authHost: authHost.value,
|
|
71
68
|
clientId: clientId.value,
|
|
@@ -74,8 +71,6 @@ export function loadConfig(options = {}) {
|
|
|
74
71
|
configPath,
|
|
75
72
|
sources: {
|
|
76
73
|
accessToken: accessToken.source,
|
|
77
|
-
apiKey: apiKey.source,
|
|
78
|
-
apiSecret: apiSecret.source,
|
|
79
74
|
host: host.source,
|
|
80
75
|
authHost: authHost.source,
|
|
81
76
|
clientId: clientId.source,
|
|
@@ -108,7 +103,13 @@ export function saveOAuthConfig(configPath, auth) {
|
|
|
108
103
|
config.client_id = auth.client_id;
|
|
109
104
|
writeConfigFile(configPath, config);
|
|
110
105
|
}
|
|
111
|
-
export function
|
|
106
|
+
export function saveAccessToken(configPath, accessToken) {
|
|
107
|
+
const config = readConfigFile(configPath);
|
|
108
|
+
config.auth = { type: 'access_token', access_token: accessToken };
|
|
109
|
+
writeConfigFile(configPath, config);
|
|
110
|
+
}
|
|
111
|
+
/** Forgets the stored credential, whichever kind it is. */
|
|
112
|
+
export function clearCredential(configPath) {
|
|
112
113
|
const config = readConfigFile(configPath);
|
|
113
114
|
delete config.auth;
|
|
114
115
|
writeConfigFile(configPath, config);
|
|
@@ -116,22 +117,8 @@ export function clearOAuthConfig(configPath) {
|
|
|
116
117
|
export function getConfig(configPath) {
|
|
117
118
|
return readConfigFile(configPath);
|
|
118
119
|
}
|
|
119
|
-
export function maskSecret(value) {
|
|
120
|
-
if (!value)
|
|
121
|
-
return undefined;
|
|
122
|
-
if (value.length <= 8)
|
|
123
|
-
return '••••';
|
|
124
|
-
return `${value.slice(0, 4)}…${value.slice(-4)}`;
|
|
125
|
-
}
|
|
126
120
|
/** Every key the config file holds, in the order help should list them. */
|
|
127
|
-
export const CONFIG_KEYS = [
|
|
128
|
-
'access_token',
|
|
129
|
-
'api_key',
|
|
130
|
-
'api_secret',
|
|
131
|
-
'host',
|
|
132
|
-
'auth_host',
|
|
133
|
-
'client_id'
|
|
134
|
-
];
|
|
121
|
+
export const CONFIG_KEYS = ['host', 'auth_host', 'client_id'];
|
|
135
122
|
export function assertConfigKey(value) {
|
|
136
123
|
if (!CONFIG_KEYS.includes(value)) {
|
|
137
124
|
throw new UsageError(`Config key must be one of ${CONFIG_KEYS.join(', ')}`);
|
package/dist/http.js
CHANGED
|
@@ -217,16 +217,12 @@ export class JinshujuHttpClient {
|
|
|
217
217
|
async authorizationHeader() {
|
|
218
218
|
if (this.config.accessToken)
|
|
219
219
|
return `Bearer ${this.config.accessToken}`;
|
|
220
|
-
if (this.config.apiKey && this.config.apiSecret) {
|
|
221
|
-
const credentials = Buffer.from(`${this.config.apiKey}:${this.config.apiSecret}`).toString('base64');
|
|
222
|
-
return `Basic ${credentials}`;
|
|
223
|
-
}
|
|
224
220
|
if (this.config.auth?.access_token) {
|
|
225
221
|
if (shouldRefresh(this.config.auth) && this.config.auth.refresh_token) {
|
|
226
222
|
this.config.auth = await refreshOAuthToken(this.config);
|
|
227
223
|
}
|
|
228
224
|
return `Bearer ${this.config.auth.access_token}`;
|
|
229
225
|
}
|
|
230
|
-
throw new AuthError('Missing authentication. Run `jinshuju auth login
|
|
226
|
+
throw new AuthError('Missing authentication. Run `jinshuju auth login` or `jinshuju auth login --access-token <token>`, or set JINSHUJU_ACCESS_TOKEN.');
|
|
231
227
|
}
|
|
232
228
|
}
|
package/dist/local.d.ts
CHANGED
|
@@ -9,15 +9,13 @@ import { type CliResult, type CliRuntime } from './result.js';
|
|
|
9
9
|
type LocalOptions = {
|
|
10
10
|
output: OutputFormat;
|
|
11
11
|
configPath: string;
|
|
12
|
-
|
|
13
|
-
apiSecret?: string;
|
|
12
|
+
accessToken?: string;
|
|
14
13
|
host?: string;
|
|
15
14
|
authHost?: string;
|
|
16
15
|
clientId?: string;
|
|
17
16
|
scopes?: string;
|
|
18
17
|
noOpen: boolean;
|
|
19
18
|
verify: boolean;
|
|
20
|
-
showSecret: boolean;
|
|
21
19
|
};
|
|
22
20
|
export declare function localOptions(flags: Record<string, unknown>, stdin: () => string): LocalOptions;
|
|
23
21
|
export declare function runLocal(words: readonly string[], flags: Record<string, unknown>, runtime: CliRuntime, stdin: () => string): Promise<CliResult>;
|
package/dist/local.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
|
-
import { loginWithOAuth,
|
|
1
|
+
import { loginWithOAuth, logout, refreshOAuthToken } from './auth.js';
|
|
2
2
|
import { bindOptions } from './args.js';
|
|
3
|
-
import { assertConfigKey, defaultConfigPath, getConfig, loadConfig,
|
|
3
|
+
import { assertConfigKey, CONFIG_KEYS, defaultConfigPath, getConfig, loadConfig, saveAccessToken, setConfigValue, unsetConfigValue } from './config.js';
|
|
4
|
+
import { AuthError } from './errors.js';
|
|
4
5
|
import { JinshujuHttpClient } from './http.js';
|
|
5
6
|
import { GLOBAL_OPTIONS, LOCAL_OPTIONS, UsageError } from './options.js';
|
|
6
7
|
import { format } from './render.js';
|
|
@@ -11,15 +12,13 @@ export function localOptions(flags, stdin) {
|
|
|
11
12
|
return {
|
|
12
13
|
output: bound.output ?? 'text',
|
|
13
14
|
configPath: bound.config ?? defaultConfigPath,
|
|
14
|
-
|
|
15
|
-
apiSecret: bound.api_secret,
|
|
15
|
+
accessToken: bound.access_token,
|
|
16
16
|
host: bound.host,
|
|
17
17
|
authHost: bound.auth_host,
|
|
18
18
|
clientId: bound.client_id,
|
|
19
19
|
scopes: bound.scopes,
|
|
20
20
|
noOpen: Boolean(bound.no_open),
|
|
21
|
-
verify: Boolean(bound.verify)
|
|
22
|
-
showSecret: Boolean(bound.show_secret)
|
|
21
|
+
verify: Boolean(bound.verify)
|
|
23
22
|
};
|
|
24
23
|
}
|
|
25
24
|
export async function runLocal(words, flags, runtime, stdin) {
|
|
@@ -49,24 +48,24 @@ function requireArg(value, name) {
|
|
|
49
48
|
throw new UsageError(`Missing argument <${name}>`);
|
|
50
49
|
return value;
|
|
51
50
|
}
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
})));
|
|
51
|
+
/** A client that sends `accessToken` when one is given, and the configured credential otherwise. */
|
|
52
|
+
function createClient(options, runtime, accessToken) {
|
|
53
|
+
if (runtime.client)
|
|
54
|
+
return runtime.client;
|
|
55
|
+
const config = loadConfig({ configPath: options.configPath, env: runtime.env, cli: { host: options.host } });
|
|
56
|
+
return new JinshujuHttpClient(accessToken ? { ...config, accessToken } : config);
|
|
59
57
|
}
|
|
60
58
|
async function authLogin(options, runtime) {
|
|
59
|
+
if (options.accessToken !== undefined)
|
|
60
|
+
return await tokenLogin(options.accessToken, options, runtime);
|
|
61
61
|
const result = await loginWithOAuth({
|
|
62
62
|
configPath: options.configPath,
|
|
63
63
|
env: runtime.env,
|
|
64
64
|
host: options.host,
|
|
65
65
|
authHost: options.authHost,
|
|
66
66
|
clientId: options.clientId,
|
|
67
|
-
scopes: options.scopes
|
|
68
|
-
|
|
69
|
-
});
|
|
67
|
+
scopes: options.scopes
|
|
68
|
+
}, options.noOpen ? printLoginUrl : undefined);
|
|
70
69
|
const payload = {
|
|
71
70
|
authenticated: true,
|
|
72
71
|
mode: 'oauth',
|
|
@@ -79,39 +78,39 @@ async function authLogin(options, runtime) {
|
|
|
79
78
|
return ok(format(payload, options.output, 0));
|
|
80
79
|
return ok(`Authenticated with OAuth.\nConfig: ${options.configPath}`);
|
|
81
80
|
}
|
|
81
|
+
/**
|
|
82
|
+
* A token is checked before it is stored: one pasted wrong would otherwise sit
|
|
83
|
+
* in the config until the next command failed with it, far from where it was
|
|
84
|
+
* typed. The same call says whose token it is.
|
|
85
|
+
*/
|
|
86
|
+
/** --no-open: the URL goes to stderr, so stdout still carries only the result. */
|
|
87
|
+
async function printLoginUrl(url) {
|
|
88
|
+
process.stderr.write(`Open this URL in a browser to log in:\n${url}\n`);
|
|
89
|
+
}
|
|
90
|
+
async function tokenLogin(accessToken, options, runtime) {
|
|
91
|
+
if (!accessToken)
|
|
92
|
+
throw new UsageError('--access-token needs a token');
|
|
93
|
+
const account = await identify(createClient(options, runtime, accessToken));
|
|
94
|
+
saveAccessToken(options.configPath, accessToken);
|
|
95
|
+
if (options.output !== 'text')
|
|
96
|
+
return ok(format({ authenticated: true, mode: 'access_token', account }, options.output, 0));
|
|
97
|
+
return ok(`Authenticated with an access token.${describeAccount(account)}\nConfig: ${options.configPath}`);
|
|
98
|
+
}
|
|
82
99
|
async function authStatus(options, runtime) {
|
|
83
100
|
const config = loadConfig({
|
|
84
101
|
configPath: options.configPath,
|
|
85
102
|
env: runtime.env,
|
|
86
|
-
cli: {
|
|
87
|
-
apiKey: options.apiKey,
|
|
88
|
-
apiSecret: options.apiSecret,
|
|
89
|
-
host: options.host,
|
|
90
|
-
authHost: options.authHost,
|
|
91
|
-
clientId: options.clientId
|
|
92
|
-
}
|
|
103
|
+
cli: { host: options.host, authHost: options.authHost, clientId: options.clientId }
|
|
93
104
|
});
|
|
94
|
-
const authenticated = Boolean(config.accessToken ||
|
|
95
|
-
const mode = config.accessToken
|
|
96
|
-
? 'access_token'
|
|
97
|
-
: config.apiKey && config.apiSecret
|
|
98
|
-
? 'api_key_secret'
|
|
99
|
-
: config.auth?.access_token
|
|
100
|
-
? 'oauth'
|
|
101
|
-
: 'none';
|
|
105
|
+
const authenticated = Boolean(config.accessToken || config.auth?.access_token);
|
|
106
|
+
const mode = config.accessToken ? 'access_token' : config.auth?.access_token ? 'oauth' : 'none';
|
|
102
107
|
const payload = {
|
|
103
108
|
authenticated,
|
|
104
109
|
mode,
|
|
105
110
|
host: config.host,
|
|
106
111
|
auth_host: config.authHost,
|
|
107
112
|
sources: config.sources,
|
|
108
|
-
source: mode === 'access_token'
|
|
109
|
-
? config.sources.accessToken
|
|
110
|
-
: mode === 'api_key_secret'
|
|
111
|
-
? config.sources.apiKey
|
|
112
|
-
: mode === 'oauth'
|
|
113
|
-
? config.sources.auth
|
|
114
|
-
: 'missing',
|
|
113
|
+
source: mode === 'access_token' ? config.sources.accessToken : mode === 'oauth' ? config.sources.auth : 'missing',
|
|
115
114
|
oauth: config.auth
|
|
116
115
|
? {
|
|
117
116
|
client_id: config.auth.client_id,
|
|
@@ -129,14 +128,16 @@ async function authStatus(options, runtime) {
|
|
|
129
128
|
const full = { ...payload, ...(account ? { account } : {}) };
|
|
130
129
|
if (options.output !== 'text')
|
|
131
130
|
return ok(format(full, options.output, 0));
|
|
132
|
-
const whose =
|
|
131
|
+
const whose = describeAccount(account);
|
|
133
132
|
if (mode === 'access_token')
|
|
134
133
|
return ok(`Authenticated with an access token (from ${config.sources.accessToken}).${whose}`);
|
|
135
134
|
if (mode === 'oauth')
|
|
136
135
|
return ok(`Authenticated with OAuth.${whose}`);
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
136
|
+
return ok('Missing authentication. Run `jinshuju auth login` or `jinshuju auth login --access-token <token>`, ' +
|
|
137
|
+
'or set JINSHUJU_ACCESS_TOKEN.');
|
|
138
|
+
}
|
|
139
|
+
function describeAccount(account) {
|
|
140
|
+
return account ? ` Account: ${account.name}${account.plan ? ` (${account.plan})` : ''}.` : '';
|
|
140
141
|
}
|
|
141
142
|
/**
|
|
142
143
|
* Who the credential belongs to. Reading the account proves the credential works
|
|
@@ -158,6 +159,8 @@ async function authRefresh(options, runtime) {
|
|
|
158
159
|
env: runtime.env,
|
|
159
160
|
cli: { host: options.host, authHost: options.authHost, clientId: options.clientId }
|
|
160
161
|
});
|
|
162
|
+
if (!config.auth && config.accessToken)
|
|
163
|
+
throw new AuthError('An access token cannot be refreshed; log in again with a new one if it stopped working.');
|
|
161
164
|
const auth = await refreshOAuthToken(config);
|
|
162
165
|
if (options.output !== 'text')
|
|
163
166
|
return ok(format({ authenticated: true, mode: 'oauth', expires_at: auth.expires_at, scope: auth.scope }, options.output, 0));
|
|
@@ -165,7 +168,7 @@ async function authRefresh(options, runtime) {
|
|
|
165
168
|
}
|
|
166
169
|
async function authLogout(options, runtime) {
|
|
167
170
|
const config = loadConfig({ configPath: options.configPath, env: runtime.env });
|
|
168
|
-
await
|
|
171
|
+
await logout(config);
|
|
169
172
|
return ok(options.output !== 'text' ? format({ authenticated: false }, options.output, 0) : 'Logged out.');
|
|
170
173
|
}
|
|
171
174
|
function configGet(positionals, options) {
|
|
@@ -173,20 +176,8 @@ function configGet(positionals, options) {
|
|
|
173
176
|
if (requestedKey)
|
|
174
177
|
assertConfigKey(requestedKey);
|
|
175
178
|
const config = getConfig(options.configPath);
|
|
176
|
-
const
|
|
177
|
-
const
|
|
178
|
-
let payload;
|
|
179
|
-
if (requestedKey) {
|
|
180
|
-
const configKey = requestedKey;
|
|
181
|
-
payload = { [configKey]: renderValue(configKey, config[configKey]) };
|
|
182
|
-
}
|
|
183
|
-
else {
|
|
184
|
-
payload = {
|
|
185
|
-
access_token: renderValue('access_token', config.access_token),
|
|
186
|
-
api_key: renderValue('api_key', config.api_key),
|
|
187
|
-
api_secret: renderValue('api_secret', config.api_secret)
|
|
188
|
-
};
|
|
189
|
-
}
|
|
179
|
+
const keys = requestedKey ? [requestedKey] : CONFIG_KEYS;
|
|
180
|
+
const payload = Object.fromEntries(keys.map((key) => [key, config[key]]));
|
|
190
181
|
if (options.output !== 'text')
|
|
191
182
|
return ok(format(payload, options.output, 0));
|
|
192
183
|
return ok(Object.entries(payload)
|
package/dist/options.d.ts
CHANGED
|
@@ -42,7 +42,7 @@ export declare const PAGINATION_OPTIONS: readonly OptionSpec[];
|
|
|
42
42
|
export declare const JSON_OPTION: OptionSpec;
|
|
43
43
|
export declare const MINE_OPTION: OptionSpec;
|
|
44
44
|
export { UsageError } from './errors.js';
|
|
45
|
-
/** `--
|
|
45
|
+
/** `--auth-host` reads back as `auth_host`. */
|
|
46
46
|
export declare function optionKey(spec: OptionSpec): string;
|
|
47
47
|
export interface FilterCondition {
|
|
48
48
|
readonly field: string;
|
package/dist/options.js
CHANGED
|
@@ -24,15 +24,13 @@ export const GLOBAL_OPTIONS = [
|
|
|
24
24
|
* in it is a flag nobody knows about.
|
|
25
25
|
*/
|
|
26
26
|
export const LOCAL_OPTIONS = [
|
|
27
|
-
{ name: '--
|
|
28
|
-
{ name: '--api-secret', type: 'string', placeholder: '<secret>', description: 'Override API secret' },
|
|
27
|
+
{ name: '--access-token', type: 'string', placeholder: '<token>', description: 'Log in with an access token' },
|
|
29
28
|
{ name: '--host', type: 'string', placeholder: '<url>', description: 'API host' },
|
|
30
29
|
{ name: '--auth-host', type: 'string', placeholder: '<url>', description: 'OAuth host' },
|
|
31
30
|
{ name: '--client-id', type: 'string', placeholder: '<id>', description: 'OAuth public client id' },
|
|
32
31
|
{ name: '--scopes', type: 'string', placeholder: '<scopes>', description: 'Space-separated OAuth scopes' },
|
|
33
32
|
{ name: '--no-open', type: 'boolean', description: 'Print the login URL instead of opening a browser' },
|
|
34
|
-
{ name: '--verify', type: 'boolean', description: 'Verify the credentials with a lightweight call' }
|
|
35
|
-
{ name: '--show-secret', type: 'boolean', description: 'Show secrets unmasked' }
|
|
33
|
+
{ name: '--verify', type: 'boolean', description: 'Verify the credentials with a lightweight call' }
|
|
36
34
|
];
|
|
37
35
|
/** The data container a command acts on. Mutually exclusive; both map to form_token. */
|
|
38
36
|
export const CONTAINER_OPTIONS = [
|
|
@@ -120,7 +118,7 @@ export const MINE_OPTION = {
|
|
|
120
118
|
description: 'Switch to what the current user submitted'
|
|
121
119
|
};
|
|
122
120
|
export { UsageError } from './errors.js';
|
|
123
|
-
/** `--
|
|
121
|
+
/** `--auth-host` reads back as `auth_host`. */
|
|
124
122
|
export function optionKey(spec) {
|
|
125
123
|
return spec.name.replace(/^--/, '').replace(/-/g, '_');
|
|
126
124
|
}
|