Skip to content

Commit 13c3708

Browse files
committed
Add version info with update availability to SDK
- Add getVersion() method to REST client (NFCAgentClient) - Update WebSocket version handler to include update availability info - Fix VersionInfo TypeScript type to match actual API response - Document version API in both README files SDK v0.2.0
1 parent 97aef24 commit 13c3708

File tree

6 files changed

+76
-7
lines changed

6 files changed

+76
-7
lines changed

README.md

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,9 +176,26 @@ Configure via environment variables:
176176
| `DELETE` | `/v1/readers/{n}/password` | Remove password |
177177
| `POST` | `/v1/readers/{n}/records` | Write multiple NDEF records |
178178
| `GET` | `/v1/supported-readers` | List supported reader models |
179-
| `GET` | `/v1/version` | Get version info |
179+
| `GET` | `/v1/version` | Get version and update info |
180180
| `GET` | `/v1/health` | Health check |
181181

182+
#### Version Endpoint
183+
184+
The `/v1/version` endpoint returns version information and checks for available updates:
185+
186+
```json
187+
{
188+
"version": "1.2.3",
189+
"buildTime": "2025-01-15T10:30:00Z",
190+
"gitCommit": "abc123def456...",
191+
"updateAvailable": true,
192+
"latestVersion": "1.3.0",
193+
"releaseUrl": "https://github.com/SimplyPrint/nfc-agent/releases/tag/v1.3.0"
194+
}
195+
```
196+
197+
The `updateAvailable`, `latestVersion`, and `releaseUrl` fields are only present when the agent has checked for updates.
198+
182199
### WebSocket
183200

184201
Connect to `ws://127.0.0.1:32145/v1/ws` for real-time card events.
@@ -189,6 +206,7 @@ Connect to `ws://127.0.0.1:32145/v1/ws` for real-time card events.
189206
- `write_card` - Write data to card
190207
- `subscribe` / `unsubscribe` - Real-time card detection
191208
- `erase_card`, `lock_card`, `set_password`, `remove_password`
209+
- `version` - Get version and update info (same response as HTTP endpoint)
192210

193211
**Events:**
194212
- `card_detected` - Card placed on reader

internal/api/websocket.go

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -663,11 +663,25 @@ func (c *WSClient) handleSupportedReaders(id string) {
663663
}
664664

665665
func (c *WSClient) handleVersion(id string) {
666-
c.sendResponse(id, "version", map[string]string{
666+
response := map[string]interface{}{
667667
"version": Version,
668668
"buildTime": BuildTime,
669669
"gitCommit": GitCommit,
670-
})
670+
}
671+
672+
// Include update info if available (for JS SDK / SimplyPrint integration)
673+
if updateChecker != nil {
674+
info := updateChecker.Check(false) // Use cached result
675+
response["updateAvailable"] = info.Available
676+
if info.LatestVersion != "" {
677+
response["latestVersion"] = info.LatestVersion
678+
}
679+
if info.ReleaseURL != "" {
680+
response["releaseUrl"] = info.ReleaseURL
681+
}
682+
}
683+
684+
c.sendResponse(id, "version", response)
671685
}
672686

673687
func (c *WSClient) handleHealth(id string) {

sdk/README.md

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,9 +173,14 @@ await ws.removePassword(0, 'mypassword');
173173
// Lock card permanently (IRREVERSIBLE!)
174174
await ws.lockCard(0);
175175

176-
// Get version info
176+
// Get version info (includes update availability)
177177
const version = await ws.getVersion();
178178
console.log('Agent version:', version.version);
179+
console.log('Build time:', version.buildTime);
180+
if (version.updateAvailable) {
181+
console.log('Update available:', version.latestVersion);
182+
console.log('Download:', version.releaseUrl);
183+
}
179184

180185
// Health check
181186
const health = await ws.health();
@@ -212,6 +217,13 @@ await client.writeCard(0, { data: 'Hello!', dataType: 'text' });
212217

213218
// Get supported readers info
214219
const supported = await client.getSupportedReaders();
220+
221+
// Get version info (includes update availability)
222+
const version = await client.getVersion();
223+
console.log('Version:', version.version);
224+
if (version.updateAvailable) {
225+
console.log('Update available:', version.latestVersion);
226+
}
215227
```
216228

217229
### Card Polling (REST)
@@ -276,6 +288,7 @@ poller.start();
276288
| `readCard(reader)` | Read card data |
277289
| `writeCard(reader, options)` | Write data to card |
278290
| `getSupportedReaders()` | Get supported hardware info |
291+
| `getVersion()` | Get agent version and update info |
279292
| `pollCard(reader, options)` | Create a CardPoller |
280293

281294
### Types
@@ -311,6 +324,15 @@ interface CardDetectedEvent {
311324
interface CardRemovedEvent {
312325
reader: number;
313326
}
327+
328+
interface VersionInfo {
329+
version: string;
330+
buildTime: string;
331+
gitCommit: string;
332+
updateAvailable?: boolean; // true if a newer version exists
333+
latestVersion?: string; // latest available version
334+
releaseUrl?: string; // URL to download the update
335+
}
314336
```
315337

316338
---

sdk/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@simplyprint/nfc-agent",
3-
"version": "0.1.0",
3+
"version": "0.2.0",
44
"description": "JavaScript SDK for NFC Agent - read and write NFC cards via local nfc-agent server",
55
"type": "module",
66
"main": "./dist/index.cjs",

sdk/src/client.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import type {
66
PollOptions,
77
SupportedReadersResponse,
88
APIErrorResponse,
9+
VersionInfo,
910
} from './types.js';
1011
import { ConnectionError, CardError, APIError } from './errors.js';
1112
import { CardPoller } from './poller.js';
@@ -167,6 +168,14 @@ export class NFCAgentClient {
167168
return this.request<SupportedReadersResponse>('/v1/supported-readers');
168169
}
169170

171+
/**
172+
* Get agent version information
173+
* @returns Version info including build details and update availability
174+
*/
175+
async getVersion(): Promise<VersionInfo> {
176+
return this.request<VersionInfo>('/v1/version');
177+
}
178+
170179
/**
171180
* Create a card poller for automatic card detection
172181
* @param readerIndex - Index of the reader to poll

sdk/src/types.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -240,8 +240,14 @@ export interface SubscribePayload {
240240
*/
241241
export interface VersionInfo {
242242
version: string;
243-
build?: string;
244-
platform?: string;
243+
buildTime: string;
244+
gitCommit: string;
245+
/** Whether a newer version is available */
246+
updateAvailable?: boolean;
247+
/** Latest available version (if updateAvailable is true) */
248+
latestVersion?: string;
249+
/** URL to download the latest release */
250+
releaseUrl?: string;
245251
}
246252

247253
/**

0 commit comments

Comments
 (0)