@polymerdao/ts-token
v0.1.0
Published
Token authentication helpers for Polymer's Prove API, ready for Cloudflare Workers and Next.js Edge runtimes.
Readme
ts-token
Utility helpers for creating and verifying Polymer auth tokens in Cloudflare Workers, Next.js (Edge/Pages), or any other runtime that exposes standard Web APIs.
Installation
npm install @polymerdao/ts-tokenUsage
import { create, verify } from '@polymerdao/ts-token';
const body = JSON.stringify({ jsonrpc: '2.0', method: 'proof_request', params: [] });
// Generate a token for your fetch call
const token = create({
body,
urlPath: '/rpc',
host: 'polymer.example',
secret: env.POLYMER_AUTH_SECRET,
});
await fetch(env.POLYMER_API_URL, {
method: 'POST',
headers: {
Authorization: `Bearer ${env.POLYMER_API_KEY}`,
'X-Polymer-Auth-Token': token,
'Content-Type': 'application/json',
},
body,
});
// Verify an incoming token if you proxy traffic through your own service
const verification = verify({
token: request.headers.get('X-Polymer-Auth-Token') ?? '',
body,
urlPath: '/rpc',
host: 'polymer.example',
secret: env.POLYMER_AUTH_SECRET,
});
if (!verification.valid) throw new Error(`Invalid token: ${verification.reason}`);
Development
npm run dev # Type-checks in watch mode
npm run lint # ESLint against src/ + tests/
npm test # Vitest suite
npm run build # Emit ESM bundle and type declarationsPublishing
- Update
package.jsonwith the target version (or runnpm version <major|minor|patch>). - Create a matching git tag following the
v1.2.3convention and push it (git tag v1.2.3 && git push origin main --tags). - Run
npm run releaseto build and publish to npm (requiresNPM_TOKENwith publish rights).
