Skip to content

armgit / spree

Public Imported from GitHub
0

Open Source Platform for DTC, B2B Commerce, Marketplaces & Omnichannel. REST APIs, TypeScript SDKs, and production-ready Next.js storefront. Self-host it. Own your data. No vendor lock-in. Zero platform fees.

ping fixes

Damian Legawiec8f3a7a7122bd
diff --git a/packages/cli/src/api/ping.ts b/packages/cli/src/api/ping.ts
index 1ecc1e2db3..6ff2b97131 100644
--- a/packages/cli/src/api/ping.ts
+++ b/packages/cli/src/api/ping.ts
@@ -40,6 +40,16 @@ export async function pingCredentials(baseUrl: string, apiKey: string): Promise<
}
}
+/**
+ * Whether a ping represents a credential/connectivity failure worth a non-zero
+ * exit — a rejected key or an unreachable host. `forbidden` (valid key, just
+ * lacks read_settings) is NOT a failure. Used by `spree api status` and
+ * `spree auth status` so both can serve as a scriptable health check.
+ */
+export function isPingFailure(ping: PingResult): boolean {
+ return ping.status === 'unauthorized' || ping.status === 'unreachable'
+}
+
/** Human-readable, colorized one-liner for a ping result — shared by `spree api status` and `spree auth status`. */
export function formatPingStatus(ping: PingResult): string {
return {
diff --git a/packages/cli/src/commands/api.ts b/packages/cli/src/commands/api.ts
index 022c2e89a7..c6b8dcb5bd 100644
--- a/packages/cli/src/commands/api.ts
+++ b/packages/cli/src/commands/api.ts
@@ -5,7 +5,7 @@ import pc from 'picocolors'
import { NO_BODY, readBody } from '../api/body.js'
import { handleApiError, type OutputFormat, printResult } from '../api/output.js'
import { buildParams, normalizePath } from '../api/params.js'
-import { formatPingStatus, pingCredentials } from '../api/ping.js'
+import { formatPingStatus, isPingFailure, pingCredentials } from '../api/ping.js'
import { getSchema, listEndpoints, loadBundledSpec } from '../api/spec.js'
import { type ResolvedCredentials, resolveCredentials } from '../config.js'
@@ -229,7 +229,7 @@ export function registerApiCommand(program: Command): void {
)
process.stdout.write(`${lines.join('\n')}\n`)
- if (ping.status === 'unauthorized' || ping.status === 'unreachable') {
+ if (isPingFailure(ping)) {
process.exitCode = 1
}
} catch (error) {
diff --git a/packages/cli/src/commands/auth.ts b/packages/cli/src/commands/auth.ts
index ca6400e945..352f78c29e 100644
--- a/packages/cli/src/commands/auth.ts
+++ b/packages/cli/src/commands/auth.ts
@@ -3,7 +3,7 @@ import type { Command } from 'commander'
import { printTable } from 'console-table-printer'
import pc from 'picocolors'
import { handleApiError } from '../api/output.js'
-import { formatPingStatus, pingCredentials } from '../api/ping.js'
+import { formatPingStatus, isPingFailure, pingCredentials } from '../api/ping.js'
import {
configPath,
projectCredentialsPath,
@@ -146,6 +146,13 @@ export function registerAuthCommand(program: Command): void {
].join('\n'),
'Auth status',
)
+
+ // Exit non-zero on a bad key or unreachable host so scripts/CI can use
+ // `spree auth status` as a credential health check (matches `spree api
+ // status`).
+ if (isPingFailure(ping)) {
+ process.exitCode = 1
+ }
})
auth
diff --git a/packages/cli/tests/ping.test.ts b/packages/cli/tests/ping.test.ts
new file mode 100644
index 0000000000..82043a07ab
--- /dev/null
+++ b/packages/cli/tests/ping.test.ts
@@ -0,0 +1,31 @@
+import { describe, expect, it } from 'vitest'
+import { formatPingStatus, isPingFailure, type PingResult } from '../src/api/ping'
+
+// biome-ignore lint/suspicious/noControlCharactersInRegex: matching ANSI SGR escapes
+const stripAnsi = (s: string) => s.replace(/\x1b\[[0-9;]*m/g, '')
+
+describe('isPingFailure', () => {
+ it('treats a rejected key and an unreachable host as failures', () => {
+ expect(isPingFailure({ status: 'unauthorized' })).toBe(true)
+ expect(isPingFailure({ status: 'unreachable', message: 'ECONNREFUSED' })).toBe(true)
+ })
+
+ it('does NOT treat connected or forbidden as failures', () => {
+ // `forbidden` = valid key that just lacks read_settings — still a healthy credential.
+ expect(isPingFailure({ status: 'connected', storeName: 'Shop' })).toBe(false)
+ expect(isPingFailure({ status: 'forbidden' })).toBe(false)
+ })
+})
+
+describe('formatPingStatus', () => {
+ const cases: Array<[PingResult, string]> = [
+ [{ status: 'connected', storeName: 'Shop' }, 'connected (Shop)'],
+ [{ status: 'forbidden' }, 'connected (key valid; lacks read_settings for store details)'],
+ [{ status: 'unauthorized' }, 'key rejected (401)'],
+ [{ status: 'unreachable', message: 'ECONNREFUSED' }, 'unreachable — ECONNREFUSED'],
+ ]
+
+ it.each(cases)('renders %j', (ping, expected) => {
+ expect(stripAnsi(formatPingStatus(ping))).toBe(expected)
+ })
+})