@founderroute/analytics-node 0.1.0-beta.1 → 1.0.0-rc.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 (3) hide show
  1. package/index.d.ts +1 -1
  2. package/index.js +12 -4
  3. package/package.json +21 -6
package/index.d.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  export function signIdentity(input:{secret:string;propertyId:string;userId:string;expiresInSeconds?:number}):string;
2
2
  export class FounderRouteServer {
3
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;
4
+ event(name:string,input:{collectionMode?:"automatic"|"consent";consentState?:"not_provided"|"granted"|"denied";optedOut?:boolean;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
5
  send(events:Array<Record<string,unknown>|null>):Promise<{results:Array<{event_id:string;status:string;reason?:string}>}>;
6
6
  }
package/index.js CHANGED
@@ -7,16 +7,24 @@ export function signIdentity({secret,propertyId,userId,expiresInSeconds=3600}) {
7
7
  }
8
8
  export class FounderRouteServer {
9
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;
10
+ event(name,{collectionMode,consentState,optedOut=false,consent,userId,anonymousId,eventId=randomUUID(),outcomeId,accountId,occurredAt=new Date().toISOString(),traits={},properties={},verificationId}) {
11
+ if (collectionMode != null && !['automatic','consent'].includes(collectionMode)) throw new Error('Invalid collection mode');
12
+ if (consentState != null && !['not_provided','granted','denied'].includes(consentState)) throw new Error('Invalid consent state');
13
+ if (optedOut || consent === false || consentState === 'denied') return null;
14
+ const mode = collectionMode ?? 'consent';
15
+ const granted = consentState === 'granted' || consent === true;
16
+ if (mode === 'consent' && !granted) return null;
17
+ const collection = collectionMode == null
18
+ ? {protocol:1,consent:true}
19
+ : {protocol:2,collection_mode:mode,consent_state:granted?'granted':'not_provided'};
12
20
  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}:{})}};
21
+ return {event_id:eventId,...collection,name,kind:"custom",occurred_at:occurredAt,anonymous_id:anonymousId,user_id:userId,...(outcomeId?{outcome_id:outcomeId}:{}),...(accountId?{account_id:accountId}:{}),traits,properties,context:{sdk:"node",sdk_version:"1.0.0-rc.1",...(verificationId?{verification_id:verificationId}:{})}};
14
22
  }
15
23
  async send(events) {
16
24
  const batch=events.filter(Boolean);if(!batch.length)return {results:[]};
17
25
  if(batch.length>50)throw new Error("Send at most 50 events per batch.");
18
26
  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});
27
+ const response=await this.fetch(`${this.endpoint}/api/analytics/v${batch.some(event=>event.protocol===2)?2:1}/server-events`,{method:"POST",headers:{"Content-Type":"application/json",Authorization:`Bearer ${this.secret}`},body});
20
28
  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
29
  }
22
30
  }
package/package.json CHANGED
@@ -1,15 +1,30 @@
1
1
  {
2
2
  "name": "@founderroute/analytics-node",
3
- "version": "0.1.0-beta.1",
3
+ "version": "1.0.0-rc.1",
4
4
  "description": "FounderRoute server analytics SDK for verified outcomes and identity assertions",
5
5
  "type": "module",
6
6
  "main": "index.js",
7
7
  "types": "index.d.ts",
8
- "files": ["index.js", "index.d.ts", "LICENSE"],
9
- "engines": { "node": ">=20" },
8
+ "files": [
9
+ "index.js",
10
+ "index.d.ts",
11
+ "LICENSE"
12
+ ],
13
+ "engines": {
14
+ "node": ">=20"
15
+ },
10
16
  "license": "MIT",
11
- "repository": { "type": "git", "url": "git+https://github.com/itsreed/founderroute-analytics.git", "directory": "packages/node" },
17
+ "repository": {
18
+ "type": "git",
19
+ "url": "git+https://github.com/itsreed/founderroute-analytics.git",
20
+ "directory": "packages/node"
21
+ },
12
22
  "homepage": "https://github.com/itsreed/founderroute-analytics#readme",
13
- "bugs": { "url": "https://github.com/itsreed/founderroute-analytics/issues" },
14
- "publishConfig": { "access": "public", "provenance": true }
23
+ "bugs": {
24
+ "url": "https://github.com/itsreed/founderroute-analytics/issues"
25
+ },
26
+ "publishConfig": {
27
+ "access": "public",
28
+ "provenance": true
29
+ }
15
30
  }