@simtec-consult/field-location-ingest
v0.2.0
Published
HTTP ingest helpers for field location pings; validates with field-location-contract.
Downloads
251
Readme
@simtec-consult/field-location-ingest
Validates POST JSON bodies with @simtec-consult/field-location-contract, optional Bearer auth (constant-time compare via SHA-256 digests), then runs your onPing callback.
Install
pnpm add @simtec-consult/field-location-ingest @simtec-consult/field-location-contractUsage (Next.js App Router)
// app/api/field-pings/route.ts
import { createNextFieldLocationIngestHandler } from "@simtec-consult/field-location-ingest";
export const POST = createNextFieldLocationIngestHandler({
bearerToken: process.env.FIELD_LOCATION_INGEST_SECRET,
async onPing(payload, meta) {
// await db.insert(...). Do not log lat/lng in production.
// If this throws, catch in a wrapper and return 500 — this helper does not catch onPing errors.
},
});Wrap onPing in try/catch if you need a consistent 500 JSON body.
Low-level API
Use handleLocationIngest({ method, body | rawBody, headers, options }) and map the result:
const result = await handleLocationIngest({ ... });
return Response.json(result.body, { status: result.status });Responses
| Status | When |
|--------|------|
| 200 | Valid ping; onPing finished; body { ok: true } |
| 400 | Invalid JSON, missing body, or schema error (code: INVALID_JSON, BODY_REQUIRED, VALIDATION_ERROR) |
| 401 | bearerToken option set and Bearer missing/wrong (UNAUTHORIZED) |
| 405 | Method not POST |
Logging
Do not log full coordinates in production. Prefer meta.requestId from x-request-id when present.
See also
Root INTEGRATION.md for Drizzle + env + curl examples.
