feat: API Resources for consistent JSON formatting

NameResource transforms alias data with parsed type, capabilities,
and owner_type (registry/community). ClaimResource formats claim
responses. Lookup endpoint uses NameResource, claim uses ClaimResource
with additional message. Follows CorePHP patterns/building-rest-apis.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
Claude 2026-04-04 11:49:19 +01:00
parent 14f4d1fdc0
commit 6af0d7ea73
No known key found for this signature in database
GPG key ID: AF404715446AEB41
3 changed files with 87 additions and 14 deletions

View file

@ -13,6 +13,7 @@ use Mod\Chain\Services\DaemonRpc;
use Mod\Chain\Services\WalletRpc;
use Mod\Names\Actions;
use Mod\Names\Models\NameClaim;
use Mod\Names\Resources;
/**
* .lthn TLD registrar API.
@ -49,13 +50,9 @@ class NamesController extends Controller
return response()->json(['error' => 'Name not registered'], 404);
}
return response()->json([
'name' => $name,
'fqdn' => "{$name}.lthn",
'address' => $alias['address'] ?? '',
'comment' => $alias['comment'] ?? '',
'registered' => true,
]);
$alias['name'] = $name;
return (new Resources\NameResource($alias))->response();
}
/**
@ -529,13 +526,10 @@ class NamesController extends Controller
{
$claim = Actions\SubmitClaim::run($request->only(['name', 'email']));
return response()->json([
'claim_id' => $claim->claim_id,
'name' => $claim->name,
'fqdn' => "{$claim->name}.lthn",
'status' => $claim->status,
'message' => "Your claim has been submitted. We will notify you at {$claim->email} when approved.",
], 201);
return (new Resources\ClaimResource($claim))
->additional(['message' => "Your claim has been submitted. We will notify you at {$claim->email} when approved."])
->response()
->setStatusCode(201);
}
/**

View file

@ -0,0 +1,28 @@
<?php
declare(strict_types=1);
namespace Mod\Names\Resources;
use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;
/**
* Consistent JSON format for claim responses.
*
* return new ClaimResource($claim);
*/
class ClaimResource extends JsonResource
{
public function toArray(Request $request): array
{
return [
'claim_id' => $this->claim_id,
'name' => $this->name,
'fqdn' => $this->name . '.lthn',
'email' => $this->email,
'status' => $this->status,
'created_at' => $this->created_at?->toIso8601String(),
];
}
}

View file

@ -0,0 +1,51 @@
<?php
declare(strict_types=1);
namespace Mod\Names\Resources;
use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;
/**
* Consistent JSON format for name lookups.
*
* return new NameResource($aliasData);
*/
class NameResource extends JsonResource
{
public function toArray(Request $request): array
{
$address = $this['address'] ?? '';
$comment = $this['comment'] ?? '';
$parsed = $this->parseComment($comment);
$registrarWallets = [
'iTHNHN11yXMeBphpFSuHnDaSJ15QxiSEJXNY59VKbxKq4ype4xAH1PZHd1EKTknkPK9hHTu2G2tBBZzvrcRFaYMF8vWTzFZjGY' => 'registry',
'iTHNUNiuu3VP1yy8xH2y5iQaABKXurdjqZmzFiBiyR4dKG3j6534e9jMriY6SM7PH8NibVwVWW1DWJfQEWnSjS8n3Wgx86pQpY' => 'testnet-registry',
];
return [
'name' => $this['alias'] ?? $this['name'] ?? '',
'fqdn' => ($this['alias'] ?? $this['name'] ?? '') . '.lthn',
'address' => $address,
'owner_type' => isset($registrarWallets[$address]) ? 'registry' : 'community',
'type' => $parsed['type'] ?? 'user',
'capabilities' => ! empty($parsed['cap']) ? explode(',', $parsed['cap']) : [],
'comment' => $comment,
];
}
private function parseComment(string $comment): array
{
$parsed = [];
foreach (explode(';', $comment) as $pair) {
$parts = explode('=', $pair, 2);
if (count($parts) === 2) {
$parsed[$parts[0]] = $parts[1];
}
}
return $parsed;
}
}