@sendhoney/cli 0.1.12 → 0.1.14

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.
Files changed (3) hide show
  1. package/README.md +13 -11
  2. package/bin/sendhoney +37 -10
  3. package/package.json +1 -1
package/README.md CHANGED
@@ -11,7 +11,9 @@ SendHoney platform CLI for authenticated API, CRUD, release, and ThunderCompute
11
11
  npm install -g @sendhoney/cli
12
12
 
13
13
  # npm (pinned)
14
- npm install -g @sendhoney/cli@0.1.12
14
+ npm install -g @sendhoney/cli@0.1.14
15
+
16
+ sendhoney --version # should print 0.1.14
15
17
 
16
18
  # Homebrew
17
19
  brew tap sendhoney/tap && brew install sendhoney
@@ -110,7 +112,7 @@ sendhoney widget install --org-id ORG_ID --widget-type both --file ./dist/index.
110
112
 
111
113
  ## Authentication
112
114
 
113
- `sendhoney login` uses Keycloak Authorization Code + PKCE and refreshes expired access tokens automatically. Use `SENDHONEY_TOKEN` for CI / service accounts.
115
+ `sendhoney login` uses Keycloak Authorization Code + PKCE and refreshes expired access tokens automatically. The loopback callback completes immediately after the code exchange; workspace hydration then retries automatically with bounded backoff. On success it prints the authenticated user, resolved `orgId`, and organization data. If the workspace API is unavailable, `auth status` reports `AUTHENTICATED_WORKSPACE_PENDING` without requiring another browser action. Use `SENDHONEY_TOKEN` for CI / service accounts.
114
116
 
115
117
  Credentials are stored in `$XDG_CONFIG_HOME/sendhoney/config.json` (or `~/.config/sendhoney/config.json`) with mode `0600`; tokens are never printed by the CLI. After browser login, the CLI exchanges the PKCE code, hydrates the Keycloak user profile and current organization, and stores the resolved `orgId` in the local session. `sendhoney widget install` can therefore use the saved organization without a second ID lookup.
116
118
 
@@ -131,7 +133,7 @@ The expert interface is the Go program in `apps/sendhoney-tui`. It shares this c
131
133
  ## Publishing
132
134
 
133
135
  Maintained in the `sendhoney-api-monorepo-oatpp-v2` repo under `apps/sendhoney-cli`.
134
- Publish to npmjs using the `staging` dist-tag so `latest` remains unchanged:
136
+ Publish through npm staged packages. Staging does not use a `staging` dist-tag:
135
137
 
136
138
  1. Stage the package (uploaded to npm staging, not yet public):
137
139
  ```bash
@@ -139,19 +141,19 @@ Publish to npmjs using the `staging` dist-tag so `latest` remains unchanged:
139
141
  cd apps/sendhoney-cli
140
142
  ./scripts/publish-sendhoney-cli.sh
141
143
  ```
142
- 2. Verify the staged version:
143
- ```bash
144
- npm view @sendhoney/cli dist-tags versions
145
- ```
146
- 3. Promote the verified version:
144
+ 2. Review and approve the stage at:
145
+ https://www.npmjs.com/settings/sendhoney/staged-packages
146
+
147
+ 3. After approval, install the released version:
147
148
  ```bash
148
- npm dist-tag add @sendhoney/cli@VERSION latest
149
+ npm install -g @sendhoney/cli@VERSION
150
+ npm view @sendhoney/cli version
149
151
  ```
150
152
 
151
- Alternatively, run the publish script which stages and prints the promotion link:
153
+ The repository script stages the package, verifies the returned stage ID, and prints the review link:
152
154
 
153
155
  ```bash
154
156
  ./scripts/publish-sendhoney-cli.sh
155
157
  ```
156
158
 
157
- npm disallows republishing an already-published version, so always bump `version` in `package.json` before staging.
159
+ npm disallows republishing an already-published version, so always bump `version` in `package.json` before staging. Do not run `npm publish` or `npm dist-tag add` for staged releases.
package/bin/sendhoney CHANGED
@@ -77,9 +77,14 @@ async function browserLogin(args, config) {
77
77
  const tokenResponse = await fetch(`${authUrl}/realms/${encodeURIComponent(realm)}/protocol/openid-connect/token`, { method: 'POST', headers: { 'Content-Type': 'application/x-www-form-urlencoded' }, body: new URLSearchParams({ grant_type: 'authorization_code', client_id: clientId, code, redirect_uri: callback, code_verifier: verifier }).toString() })
78
78
  const tokens = await tokenResponse.json().catch(() => ({}))
79
79
  if (!tokenResponse.ok || !tokens.access_token) throw new Error(`Keycloak login failed (${tokenResponse.status}): ${tokens.error_description || tokens.error || 'no access token'}`)
80
- const session = await persistLoginSession(config, args, { authUrl, realm, clientId }, tokens)
80
+ const session = await persistLoginSession(config, args, { authUrl, realm, clientId }, tokens, { hydrate: false })
81
81
  res.setHeader('Content-Type', 'text/html; charset=utf-8'); res.end(loginCallbackPage(true, session.organization || {}))
82
- clearTimeout(timeout); server.close(); resolve({ tokens, session })
82
+ clearTimeout(timeout); server.close()
83
+ stdout.write('✓ Keycloak authentication complete. Resolving workspace…\n')
84
+ const hydratedSession = await hydrateWorkspaceWithRetries(config, args, { authUrl, realm, clientId }, tokens, session)
85
+ if (hydratedSession.orgId) stdout.write(`✓ Workspace ready: ${hydratedSession.organization?.name || hydratedSession.orgId}\n`)
86
+ else stdout.write('! Authentication succeeded, but workspace resolution is still pending.\n')
87
+ resolve({ tokens, session: hydratedSession })
83
88
  } catch (failure) {
84
89
  res.setHeader('Content-Type', 'text/html; charset=utf-8'); res.end(loginCallbackPage(false)); clearTimeout(timeout); server.close(); reject(failure)
85
90
  }
@@ -89,7 +94,7 @@ async function browserLogin(args, config) {
89
94
  openBrowser(loginUrl)
90
95
  stdout.write(`Opening Keycloak login in your browser (${callback})\n`)
91
96
  })
92
- return json({ ok: true, authenticated: true, orgId: result.session.orgId || undefined, apiUrl: result.session.apiUrl })
97
+ return json({ ok: true, authenticated: true, user: result.session.user, organization: result.session.organization, orgId: result.session.orgId || undefined, apiUrl: result.session.apiUrl })
93
98
  }
94
99
 
95
100
  async function refreshToken(config) {
@@ -118,12 +123,16 @@ function organizationIdFrom(value) {
118
123
  return String(value.orgId || value.org_id || value.organizationId || value.tenantId || value.organization?.id || value.organization?.orgId || value.workspace?.organizationId || value.organizations?.[0]?.id || '').trim()
119
124
  }
120
125
 
121
- async function persistLoginSession(config, args, auth, tokens) {
126
+ const workspaceRetryDelays = [0, 1000, 2000, 4000, 8000]
127
+
128
+ function sleep(milliseconds) { return new Promise((resolve) => setTimeout(resolve, milliseconds)) }
129
+
130
+ async function persistLoginSession(config, args, auth, tokens, { hydrate = true } = {}) {
122
131
  const apiUrl = args['api-url'] || config.apiUrl || 'https://api.sendhoney.app'
123
132
  const headers = { Accept: 'application/json', Authorization: `Bearer ${tokens.access_token}` }
124
133
  const [userResponse, orgResponse] = await Promise.all([
125
134
  fetch(`${auth.authUrl}/realms/${encodeURIComponent(auth.realm)}/protocol/openid-connect/userinfo`, { headers }).catch(() => null),
126
- fetch(`${apiUrl.replace(/\/$/, '')}/api/v1/org/me`, { headers }).catch(() => null),
135
+ hydrate ? fetch(`${apiUrl.replace(/\/$/, '')}/api/v1/org/me`, { headers }).catch(() => null) : Promise.resolve(null),
127
136
  ])
128
137
  const user = userResponse ? await userResponse.json().catch(() => ({})) : {}
129
138
  let organization = orgResponse ? await orgResponse.json().catch(() => ({})) : {}
@@ -138,20 +147,37 @@ async function persistLoginSession(config, args, auth, tokens) {
138
147
  organization = { ...onboarding, ...fullOrganization, orgId }
139
148
  }
140
149
  }
141
- const session = { authenticated: true, authenticatedAt: new Date().toISOString(), user, organization, orgId: orgId || undefined }
150
+ const session = {
151
+ authenticated: true,
152
+ authenticatedAt: new Date().toISOString(),
153
+ workspaceState: orgId ? 'AUTHENTICATED_WORKSPACE_READY' : 'AUTHENTICATED_WORKSPACE_PENDING',
154
+ user,
155
+ organization,
156
+ orgId: orgId || undefined,
157
+ }
142
158
  saveConfig({
143
159
  ...config, apiUrl, authUrl: auth.authUrl, realm: auth.realm, clientId: auth.clientId,
144
160
  accessToken: tokens.access_token, refreshToken: tokens.refresh_token || config.refreshToken,
145
161
  idToken: tokens.id_token, expiresAt: Date.now() + (tokens.expires_in || 300) * 1000,
146
162
  ...(orgId ? { orgId } : {}), session,
147
163
  })
148
- return { apiUrl, orgId, user, organization }
164
+ return { apiUrl, orgId, user, organization, workspaceState: session.workspaceState }
165
+ }
166
+
167
+ async function hydrateWorkspaceWithRetries(config, args, auth, tokens, initialSession) {
168
+ let session = initialSession
169
+ for (const delay of workspaceRetryDelays) {
170
+ if (delay) await sleep(delay)
171
+ session = await persistLoginSession(config, args, auth, tokens)
172
+ if (session.orgId) return session
173
+ }
174
+ return session
149
175
  }
150
176
 
151
177
  function authStatus() {
152
178
  const config = loadConfig()
153
179
  const expiresAt = Number(config.expiresAt || 0)
154
- json({ authenticated: Boolean(config.accessToken) && (!expiresAt || expiresAt > Date.now()), expiresAt: expiresAt ? new Date(expiresAt).toISOString() : undefined, orgId: config.orgId || config.session?.orgId, user: config.session?.user, organization: config.session?.organization, configFile })
180
+ json({ authenticated: Boolean(config.accessToken) && (!expiresAt || expiresAt > Date.now()), workspaceState: config.session?.workspaceState || (config.orgId ? 'AUTHENTICATED_WORKSPACE_READY' : 'AUTHENTICATED_WORKSPACE_PENDING'), expiresAt: expiresAt ? new Date(expiresAt).toISOString() : undefined, orgId: config.orgId || config.session?.orgId, user: config.session?.user, organization: config.session?.organization, configFile })
155
181
  }
156
182
  function redactCredential(value) {
157
183
  if (Array.isArray(value)) return value.map(redactCredential)
@@ -413,8 +439,9 @@ async function login(args) {
413
439
  })
414
440
  const body = await response.json().catch(() => ({}))
415
441
  if (!response.ok || !body.access_token) die(`login failed (${response.status}): ${body.error_description || body.error || 'no access token'}`)
416
- const session = await persistLoginSession(config, args, { authUrl, realm, clientId }, body)
417
- json({ ok: true, username, authenticated: true, orgId: session.orgId || undefined, apiUrl: session.apiUrl })
442
+ const initialSession = await persistLoginSession(config, args, { authUrl, realm, clientId }, body, { hydrate: false })
443
+ const session = await hydrateWorkspaceWithRetries(config, args, { authUrl, realm, clientId }, body, initialSession)
444
+ json({ ok: true, username, authenticated: true, user: session.user, organization: session.organization, orgId: session.orgId || undefined, apiUrl: session.apiUrl })
418
445
  }
419
446
 
420
447
  async function main() {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sendhoney/cli",
3
- "version": "0.1.12",
3
+ "version": "0.1.14",
4
4
  "description": "SendHoney platform CLI for authenticated API, CRUD, release, and ThunderCompute operations.",
5
5
  "private": false,
6
6
  "bin": { "sendhoney": "bin/sendhoney" },