@founderroute/analytics-node 0.1.0-beta.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (4) hide show
  1. package/LICENSE +21 -0
  2. package/index.d.ts +6 -0
  3. package/index.js +22 -0
  4. package/package.json +15 -0
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 FounderRoute
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/index.d.ts ADDED
@@ -0,0 +1,6 @@
1
+ export function signIdentity(input:{secret:string;propertyId:string;userId:string;expiresInSeconds?:number}):string;
2
+ export class FounderRouteServer {
3
+ constructor(options:{secret:string;endpoint:string;fetch?:typeof fetch});
4
+ event(name:string,input:{consent:boolean;verificationId?:string;userId:string;anonymousId:string;eventId?:string;outcomeId?:string;accountId?:string;occurredAt?:string;traits?:Record<string,unknown>;properties?:Record<string,unknown>}):Record<string,unknown>|null;
5
+ send(events:Array<Record<string,unknown>|null>):Promise<{results:Array<{event_id:string;status:string;reason?:string}>}>;
6
+ }
package/index.js ADDED
@@ -0,0 +1,22 @@
1
+ import { createHash, createHmac, randomUUID } from "node:crypto";
2
+ export function signIdentity({secret,propertyId,userId,expiresInSeconds=3600}) {
3
+ if(!secret?.startsWith("fr_sk_")||!propertyId||!userId)throw new Error("A server key, property, and opaque user ID are required.");
4
+ const payload=Buffer.from(JSON.stringify({property_id:propertyId,user_id:userId,exp:Math.floor(Date.now()/1000)+Math.min(604800,Math.max(60,expiresInSeconds))})).toString("base64url");
5
+ const signingKey=createHash("sha256").update(secret).digest("hex");
6
+ return `${payload}.${createHmac("sha256",signingKey).update(payload).digest("base64url")}`;
7
+ }
8
+ export class FounderRouteServer {
9
+ constructor({secret,endpoint,fetch:transport=globalThis.fetch}) { if(!secret?.startsWith("fr_sk_"))throw new Error("A server ingest secret is required.");this.secret=secret;this.endpoint=endpoint.replace(/\/$/,"");this.fetch=transport; }
10
+ event(name,{consent,userId,anonymousId,eventId=randomUUID(),outcomeId,accountId,occurredAt=new Date().toISOString(),traits={},properties={},verificationId}) {
11
+ if(consent!==true)return null;
12
+ if(!userId||!anonymousId)throw new Error("Pass a stable user ID and the originating anonymous ID.");
13
+ return {event_id:eventId,protocol:1,name,kind:"custom",consent:true,occurred_at:occurredAt,anonymous_id:anonymousId,user_id:userId,...(outcomeId?{outcome_id:outcomeId}:{}),...(accountId?{account_id:accountId}:{}),traits,properties,context:{sdk:"node",sdk_version:"0.1.0-beta.1",...(verificationId?{verification_id:verificationId}:{})}};
14
+ }
15
+ async send(events) {
16
+ const batch=events.filter(Boolean);if(!batch.length)return {results:[]};
17
+ if(batch.length>50)throw new Error("Send at most 50 events per batch.");
18
+ const body=JSON.stringify({events:batch});if(Buffer.byteLength(body)>65536)throw new Error("Batch exceeds 64 KiB.");
19
+ const response=await this.fetch(`${this.endpoint}/api/analytics/v1/server-events`,{method:"POST",headers:{"Content-Type":"application/json",Authorization:`Bearer ${this.secret}`},body});
20
+ const result=await response.json();if(!response.ok){const error=new Error(result.error??"Analytics delivery failed");error.status=response.status;error.retryAfter=response.headers.get("Retry-After");throw error;}return result;
21
+ }
22
+ }
package/package.json ADDED
@@ -0,0 +1,15 @@
1
+ {
2
+ "name": "@founderroute/analytics-node",
3
+ "version": "0.1.0-beta.1",
4
+ "description": "FounderRoute server analytics SDK for verified outcomes and identity assertions",
5
+ "type": "module",
6
+ "main": "index.js",
7
+ "types": "index.d.ts",
8
+ "files": ["index.js", "index.d.ts", "LICENSE"],
9
+ "engines": { "node": ">=20" },
10
+ "license": "MIT",
11
+ "repository": { "type": "git", "url": "git+https://github.com/itsreed/founderroute-analytics.git", "directory": "packages/node" },
12
+ "homepage": "https://github.com/itsreed/founderroute-analytics#readme",
13
+ "bugs": { "url": "https://github.com/itsreed/founderroute-analytics/issues" },
14
+ "publishConfig": { "access": "public", "provenance": true }
15
+ }