khatran-test-event
v0.4.10
Published
Lightweight frontend event logger — POST JSON to baseUrl paths with Bearer auth
Readme
khatran-test-event
Lightweight frontend helper to send analytics-style events as POST JSON to your API, with a shared base URL, bearer token, and default fields.
Install
pnpm add khatran-test-event
# or: npm install khatran-test-event / yarn add khatran-test-eventRequires a global fetch (browser or Node 18+).
Quick start
Call initEvent once when your app boots (for example after reading env), then call logEvent wherever you need to record an event.
import { initEvent, logEvent } from "khatran-test-event";
initEvent({
token: import.meta.env.VITE_EVENT_TOKEN,
baseUrl: import.meta.env.VITE_EVENT_BASE_URL,
defaultParams: { app: "my-app", env: import.meta.env.MODE },
});
logEvent("page_view", { path: "/home" });That sends:
- URL:
{baseUrl}/page_view(slashes are normalized so you do not get double slashes) - Method:
POST - Headers:
Content-Type: application/json,Authorization: Bearer {token}, plus any extra headers from config - Body:
{ ...defaultParams, ...params }(shallow merge; per-call keys override defaults)
API
initEvent(config)
| Field | Required | Description |
|-------|----------|-------------|
| baseUrl | yes | API base URL; each event path is appended to this |
| token | yes | Bearer token for Authorization |
| defaultParams | no | Object merged into every request body |
| headers | no | Extra headers merged with defaults |
| fetchImpl | no | Custom fetch (tests or non-browser environments) |
| onError | no | Called when the request fails; if omitted, failures are logged with console.warn |
If logEvent runs before initEvent, the call is ignored and a one-time console.warn is emitted. If fetch is missing, sends are skipped after a warning from initEvent.
logEvent(url, params?)
url— Path segment(s) underbaseUrl, e.g."page_view"or"segment/nested".params— Optional object merged into the JSON body on top ofdefaultParams.
Product events
Product-specific helpers post under a fixed service path. For example, productViewed sends to {baseUrl}/product-service/product-viewed (see the package source for the exact segment and path).
import { productViewed } from "khatran-test-event";
productViewed({
product_id: "…",
product_name: "…",
product_price: 0,
product_image: "…",
product_url: "…",
product_category: "…",
product_subcategory: "…",
product_brand: "…",
product_variant: "…",
});Types for the payload are exported as ProductViewedParams.
TypeScript
The package ships TypeScript declarations (types in package.json). Use InitEventConfig, EventParams, and ProductViewedParams when you need explicit typing.
Developing this package
Clone the repo, install dependencies, and build:
pnpm install
pnpm run buildUse pnpm run dev for a watch build while you change src/. Before publishing, prepublishOnly runs pnpm run build so dist/ is up to date.
For linking this package into another app locally, see docs/local-development.md in the repository.
CI
Pull requests open a workflow that runs pnpm install and pnpm build. Pushes to main run the same build plus the Changesets GitHub Action to open a Version PR when needed and run pnpm exec changeset publish when a release is ready (the action versions via PRs; it must not run pnpm release, which includes changeset version and can fail when there are no pending changesets).
Add a repository secret NPM_TOKEN: an npm automation token with permission to publish this package.
Versioning
This repo uses Changesets.
- After code changes, run
pnpm changesetand pick the semver bump plus a short summary (this adds a file under.changeset/). - Merge to
main(often via the “Version Packages” PR from CI). When versions are ready, CI publishes withchangeset publish(prepublishOnlyrunspnpm run buildfirst).
Locally, pnpm release runs changeset version then changeset publish. To also git push after versioning, use pnpm release:full.
To only bump versions locally without publishing, use pnpm version-packages.
Plain pnpm publish only uploads the current version from package.json; it does not create a new version from changesets.
License
MIT
