-
Notifications
You must be signed in to change notification settings - Fork 1
send buffered events to event capture service #112
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
cbrady
merged 2 commits into
main
from
chris/schy-4-update-sdks-to-send-events-to-capture-rather-than-api
May 4, 2026
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,124 @@ | ||
| import { CreateEventRequestBody, EventBody, EventType } from "./api"; | ||
| import * as serializers from "./serialization"; | ||
| import type { FetchFunction } from "./core/fetcher/Fetcher"; | ||
|
|
||
| export const DEFAULT_EVENT_CAPTURE_BASE_URL = "https://c.schematichq.com"; | ||
| const DEFAULT_TIMEOUT_MS = 10_000; | ||
|
|
||
| export interface EventCaptureClientOptions { | ||
| apiKey: string; | ||
| /** Fetcher created by the SchematicClient — reused so that offline mode, | ||
| * default headers, and retry/logging behavior stay consistent. */ | ||
| fetcher: FetchFunction; | ||
| /** Static headers to include on every request (e.g. X-Fern-SDK-Name, | ||
| * X-Fern-SDK-Version) so the capture service receives the same SDK | ||
| * identifying headers as the REST API. */ | ||
| headers?: Record<string, string>; | ||
| baseUrl?: string; | ||
| timeoutMs?: number; | ||
| } | ||
|
|
||
| interface CapturePayload { | ||
| api_key: string; | ||
| type: EventType; | ||
| body?: unknown; | ||
| sent_at?: string; | ||
| } | ||
|
|
||
| interface BatchPayload { | ||
| events: CapturePayload[]; | ||
| } | ||
|
|
||
| const buildEndpoint = (baseUrl: string): string => { | ||
| return baseUrl.replace(/\/+$/, "") + "/batch"; | ||
| }; | ||
|
|
||
| const toCapturePayload = (event: CreateEventRequestBody, apiKey: string): CapturePayload => { | ||
| const payload: CapturePayload = { | ||
| api_key: apiKey, | ||
| type: event.eventType, | ||
| }; | ||
|
|
||
| if (event.body !== undefined) { | ||
| payload.body = serializers.EventBody.jsonOrThrow(event.body as EventBody, { | ||
| unrecognizedObjectKeys: "strip", | ||
| }); | ||
| } | ||
|
|
||
| if (event.sentAt !== undefined) { | ||
| payload.sent_at = event.sentAt instanceof Date ? event.sentAt.toISOString() : event.sentAt; | ||
| } | ||
|
|
||
| return payload; | ||
| }; | ||
|
|
||
| const buildBatch = (events: CreateEventRequestBody[], apiKey: string): BatchPayload => { | ||
| return { | ||
| events: events.map((e) => toCapturePayload(e, apiKey)), | ||
| }; | ||
| }; | ||
|
|
||
| const describeFetcherError = (error: unknown): string => { | ||
| if (typeof error !== "object" || error === null || !("reason" in error)) { | ||
| return "unknown error"; | ||
| } | ||
| const err = error as { reason: string; statusCode?: number; errorMessage?: string; body?: unknown }; | ||
| switch (err.reason) { | ||
| case "status-code": { | ||
| const body = typeof err.body === "string" ? err.body : JSON.stringify(err.body ?? ""); | ||
| return `HTTP ${err.statusCode}: ${body}`; | ||
| } | ||
| case "timeout": | ||
| return "request timed out"; | ||
| case "non-json": | ||
| return `non-JSON response (HTTP ${err.statusCode})`; | ||
| case "body-is-null": | ||
| return `empty response body (HTTP ${err.statusCode})`; | ||
| default: | ||
| return err.errorMessage ?? "unknown error"; | ||
| } | ||
| }; | ||
|
|
||
| /** | ||
| * HTTP client for sending event batches directly to the Schematic event | ||
| * capture service (default: https://c.schematichq.com/batch). | ||
| */ | ||
| export class EventCaptureClient { | ||
| private readonly apiKey: string; | ||
| private readonly baseUrl: string; | ||
| private readonly timeoutMs: number; | ||
| private readonly fetcher: FetchFunction; | ||
| private readonly headers: Record<string, string>; | ||
|
|
||
| constructor(options: EventCaptureClientOptions) { | ||
| this.apiKey = options.apiKey; | ||
| this.baseUrl = options.baseUrl ?? DEFAULT_EVENT_CAPTURE_BASE_URL; | ||
| this.timeoutMs = options.timeoutMs ?? DEFAULT_TIMEOUT_MS; | ||
| this.fetcher = options.fetcher; | ||
| this.headers = options.headers ?? {}; | ||
| } | ||
|
|
||
| public async sendBatch(events: CreateEventRequestBody[]): Promise<void> { | ||
| if (events.length === 0) { | ||
| return; | ||
| } | ||
|
|
||
| const response = await this.fetcher({ | ||
| url: buildEndpoint(this.baseUrl), | ||
| method: "POST", | ||
| contentType: "application/json", | ||
| requestType: "json", | ||
| headers: { | ||
| ...this.headers, | ||
| "X-Schematic-Api-Key": this.apiKey, | ||
| }, | ||
| body: buildBatch(events, this.apiKey), | ||
| timeoutMs: this.timeoutMs, | ||
| maxRetries: 0, | ||
| }); | ||
|
|
||
| if (!response.ok) { | ||
| throw new Error(`capture service returned ${describeFetcherError(response.error)}`); | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same issue here - I'd like to have our standard SDK type, version etc headers