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:
parent
14f4d1fdc0
commit
6af0d7ea73
3 changed files with 87 additions and 14 deletions
|
|
@ -13,6 +13,7 @@ use Mod\Chain\Services\DaemonRpc;
|
||||||
use Mod\Chain\Services\WalletRpc;
|
use Mod\Chain\Services\WalletRpc;
|
||||||
use Mod\Names\Actions;
|
use Mod\Names\Actions;
|
||||||
use Mod\Names\Models\NameClaim;
|
use Mod\Names\Models\NameClaim;
|
||||||
|
use Mod\Names\Resources;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* .lthn TLD registrar API.
|
* .lthn TLD registrar API.
|
||||||
|
|
@ -49,13 +50,9 @@ class NamesController extends Controller
|
||||||
return response()->json(['error' => 'Name not registered'], 404);
|
return response()->json(['error' => 'Name not registered'], 404);
|
||||||
}
|
}
|
||||||
|
|
||||||
return response()->json([
|
$alias['name'] = $name;
|
||||||
'name' => $name,
|
|
||||||
'fqdn' => "{$name}.lthn",
|
return (new Resources\NameResource($alias))->response();
|
||||||
'address' => $alias['address'] ?? '',
|
|
||||||
'comment' => $alias['comment'] ?? '',
|
|
||||||
'registered' => true,
|
|
||||||
]);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -529,13 +526,10 @@ class NamesController extends Controller
|
||||||
{
|
{
|
||||||
$claim = Actions\SubmitClaim::run($request->only(['name', 'email']));
|
$claim = Actions\SubmitClaim::run($request->only(['name', 'email']));
|
||||||
|
|
||||||
return response()->json([
|
return (new Resources\ClaimResource($claim))
|
||||||
'claim_id' => $claim->claim_id,
|
->additional(['message' => "Your claim has been submitted. We will notify you at {$claim->email} when approved."])
|
||||||
'name' => $claim->name,
|
->response()
|
||||||
'fqdn' => "{$claim->name}.lthn",
|
->setStatusCode(201);
|
||||||
'status' => $claim->status,
|
|
||||||
'message' => "Your claim has been submitted. We will notify you at {$claim->email} when approved.",
|
|
||||||
], 201);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
||||||
28
app/Mod/Names/Resources/ClaimResource.php
Normal file
28
app/Mod/Names/Resources/ClaimResource.php
Normal 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(),
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
||||||
51
app/Mod/Names/Resources/NameResource.php
Normal file
51
app/Mod/Names/Resources/NameResource.php
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Add table
Reference in a new issue