Skip to content

OpenPanel is an open-source web and product analytics platform, an open-source alternative to Mixpanel with optional self-hosting.

fix(sdk): inline validation types in dts so consumers don't need @openpanel/validation (#335)

Carl-Gerhard Lindesvärda762fa9383d4
diff --git a/packages/sdks/sdk/src/index.ts b/packages/sdks/sdk/src/index.ts
index 23db874c7..896dc1b74 100644
--- a/packages/sdks/sdk/src/index.ts
+++ b/packages/sdks/sdk/src/index.ts
@@ -1,27 +1,30 @@
-/** biome-ignore-all lint/style/noExportedImports: lazy */
-
+// Deep-imported on purpose: rollup-plugin-dts (used by tsup) only inlines
+// types when the import resolves to a single source file. Importing from
+// the package root ('@openpanel/validation') leaves an external reference
+// in dist/index.d.ts, and `@openpanel/validation` is not published so
+// consumers would see a broken import. We tested tsdown too; it hits the
+// same limitation via rolldown-plugin-dts (Oxc/tsc both fail to follow
+// `export *` chains across source-only workspace packages).
import type {
- IAliasPayload as AliasPayload,
- IAssignGroupPayload as AssignGroupPayload,
- IDecrementPayload as DecrementPayload,
- IGroupPayload as GroupPayload,
- IIdentifyPayload as IdentifyPayload,
- IIncrementPayload as IncrementPayload,
- ITrackHandlerPayload as TrackHandlerPayload,
- ITrackPayload as TrackPayload,
-} from '@openpanel/validation';
+ IAliasPayload,
+ IAssignGroupPayload,
+ IDecrementPayload,
+ IGroupPayload,
+ IIdentifyPayload,
+ IIncrementPayload,
+ ITrackHandlerPayload,
+ ITrackPayload,
+} from '@openpanel/validation/src/track.validation';
import { Api } from './api';
-export type {
- AliasPayload,
- AssignGroupPayload,
- DecrementPayload,
- GroupPayload,
- IdentifyPayload,
- IncrementPayload,
- TrackHandlerPayload,
- TrackPayload,
-};
+export type AliasPayload = IAliasPayload;
+export type AssignGroupPayload = IAssignGroupPayload;
+export type DecrementPayload = IDecrementPayload;
+export type GroupPayload = IGroupPayload;
+export type IdentifyPayload = IIdentifyPayload;
+export type IncrementPayload = IIncrementPayload;
+export type TrackHandlerPayload = ITrackHandlerPayload;
+export type TrackPayload = ITrackPayload;
export interface TrackProperties {
[key: string]: unknown;
diff --git a/packages/sdks/sdk/tsup.config.ts b/packages/sdks/sdk/tsup.config.ts
index 6ec80f8c1..7ebb7ff82 100644
--- a/packages/sdks/sdk/tsup.config.ts
+++ b/packages/sdks/sdk/tsup.config.ts
@@ -3,7 +3,10 @@ import { defineConfig } from 'tsup';
export default defineConfig({
entry: ['index.ts'],
format: ['cjs', 'esm'],
- dts: true,
+ // Inline @openpanel/validation types into the bundled .d.ts so consumers
+ // don't see `import type { … } from '@openpanel/validation'` (the
+ // package is workspace-internal and not published).
+ dts: { resolve: [/^@openpanel\//] },
splitting: false,
sourcemap: false,
clean: true,
diff --git a/packages/validation/index.ts b/packages/validation/index.ts
deleted file mode 100644
index 273d53f83..000000000
--- a/packages/validation/index.ts
+++ /dev/null
@@ -1,3 +0,0 @@
-export * from './src/index';
-export * from './src/types.validation';
-export * from './src/types.insights';
diff --git a/packages/validation/package.json b/packages/validation/package.json
index a92b4c6ba..7ba7c0a84 100644
--- a/packages/validation/package.json
+++ b/packages/validation/package.json
@@ -2,7 +2,7 @@
"name": "@openpanel/validation",
"version": "0.0.1",
"type": "module",
- "main": "index.ts",
+ "main": "src/index.ts",
"scripts": {
"typecheck": "tsc --noEmit"
},
diff --git a/packages/validation/src/index.ts b/packages/validation/src/index.ts
index b79cf79cc..7d4ec29f5 100644
--- a/packages/validation/src/index.ts
+++ b/packages/validation/src/index.ts
@@ -657,6 +657,7 @@ export const zCreateImport = z.object({
export type ICreateImport = z.infer<typeof zCreateImport>;
export * from './types.insights';
+export * from './types.validation';
export * from './track.validation';
export * from './event-blocklist';
export * from './chat';
diff --git a/packages/validation/src/track.validation.ts b/packages/validation/src/track.validation.ts
index 96b0b6875..c97b51a08 100644
--- a/packages/validation/src/track.validation.ts
+++ b/packages/validation/src/track.validation.ts
@@ -2,19 +2,101 @@ import { RESERVED_EVENT_NAMES } from '@openpanel/constants';
import { z } from 'zod';
import { isBlockedEventName } from './event-blocklist';
+// ----- Hand-written types (source of truth) -----
+//
+// These interfaces are duplicated in code that ships in our SDK type
+// declarations. We hand-write them (instead of using `z.infer<…>`) so:
+// 1. Generated `.d.ts` files for the SDKs are readable plain TypeScript
+// with no `import type { ITrackPayload } from '@openpanel/validation'`
+// lines (the package isn't published).
+// 2. The interfaces stay clean — no zod internals leaking through.
+// Each schema below has `satisfies z.ZodType<…>` attached so a drift
+// between the interface and the schema fails to compile.
+
+export type IProfileId = string | number;
+
+export interface IGroupPayload {
+ id: string;
+ type: string;
+ name: string;
+ properties?: Record<string, unknown>;
+}
+
+export interface IAssignGroupPayload {
+ groupIds: string[];
+ profileId?: IProfileId;
+}
+
+export interface ITrackPayload {
+ name: string;
+ properties?: Record<string, unknown>;
+ profileId?: IProfileId;
+ groups?: string[];
+}
+
+export interface IIdentifyPayload {
+ profileId: IProfileId;
+ firstName?: string;
+ lastName?: string;
+ email?: string;
+ avatar?: string;
+ properties?: Record<string, unknown>;
+}
+
+export interface IIncrementPayload {
+ profileId: IProfileId;
+ property: string;
+ value?: number;
+}
+
+export interface IDecrementPayload {
+ profileId: IProfileId;
+ property: string;
+ value?: number;
+}
+
+export interface IAliasPayload {
+ profileId: IProfileId;
+ alias: string;
+}
+
+export interface IReplayPayload {
+ chunk_index: number;
+ events_count: number;
+ is_full_snapshot: boolean;
+ started_at: string;
+ ended_at: string;
+ payload: string;
+}
+
+export type ITrackHandlerPayload =
+ | { type: 'track'; payload: ITrackPayload }
+ | { type: 'identify'; payload: IIdentifyPayload }
+ | { type: 'increment'; payload: IIncrementPayload }
+ | { type: 'decrement'; payload: IDecrementPayload }
+ | { type: 'alias'; payload: IAliasPayload }
+ | { type: 'replay'; payload: IReplayPayload }
+ | { type: 'group'; payload: IGroupPayload }
+ | { type: 'assign_group'; payload: IAssignGroupPayload };
+
+// ----- Schemas (each `satisfies` its hand-written interface) -----
+
+export const zProfileId = z.union([
+ z.string().min(1),
+ z.number(),
+]) satisfies z.ZodType<IProfileId>;
+
export const zGroupPayload = z.object({
id: z.string().min(1),
type: z.string().min(1),
name: z.string().min(1),
properties: z.record(z.string(), z.unknown()).optional(),
-});
-
-export const zProfileId = z.union([z.string().min(1), z.number()]);
+}) satisfies z.ZodType<IGroupPayload>;
export const zAssignGroupPayload = z.object({
groupIds: z.array(z.string().min(1)),
profileId: zProfileId.optional(),
-});
+}) satisfies z.ZodType<IAssignGroupPayload>;
export const zTrackPayload = z
.object({
@@ -50,7 +132,7 @@ export const zTrackPayload = z
message: '__revenue must be an integer (no floats or strings)',
path: ['properties', '__revenue'],
}
- );
+ ) satisfies z.ZodType<ITrackPayload>;
export const zIdentifyPayload = z.object({
profileId: zProfileId,
@@ -59,24 +141,24 @@ export const zIdentifyPayload = z.object({
email: z.string().email().optional(),
avatar: z.string().url().optional(),
properties: z.record(z.string(), z.unknown()).optional(),
-});
+}) satisfies z.ZodType<IIdentifyPayload>;
export const zIncrementPayload = z.object({
profileId: zProfileId,
property: z.string().min(1),
value: z.number().positive().optional(),
-});
+}) satisfies z.ZodType<IIncrementPayload>;
export const zDecrementPayload = z.object({
profileId: zProfileId,
property: z.string().min(1),
value: z.number().positive().optional(),
-});
+}) satisfies z.ZodType<IDecrementPayload>;
export const zAliasPayload = z.object({
profileId: zProfileId,
alias: z.string().min(1),
-});
+}) satisfies z.ZodType<IAliasPayload>;
export const zReplayPayload = z.object({
chunk_index: z.number().int().min(0).max(65_535),
@@ -85,7 +167,7 @@ export const zReplayPayload = z.object({
started_at: z.string().datetime(),
ended_at: z.string().datetime(),
payload: z.string().max(1_048_576 * 2), // 2MB max
-});
+}) satisfies z.ZodType<IReplayPayload>;
export const zTrackHandlerPayload = z.discriminatedUnion('type', [
z
@@ -136,17 +218,7 @@ export const zTrackHandlerPayload = z.discriminatedUnion('type', [
payload: zAssignGroupPayload,
})
.meta({ title: 'Assign Group' }),
-]);
-
-export type ITrackPayload = z.infer<typeof zTrackPayload>;
-export type IIdentifyPayload = z.infer<typeof zIdentifyPayload>;
-export type IIncrementPayload = z.infer<typeof zIncrementPayload>;
-export type IDecrementPayload = z.infer<typeof zDecrementPayload>;
-export type IAliasPayload = z.infer<typeof zAliasPayload>;
-export type IReplayPayload = z.infer<typeof zReplayPayload>;
-export type IGroupPayload = z.infer<typeof zGroupPayload>;
-export type IAssignGroupPayload = z.infer<typeof zAssignGroupPayload>;
-export type ITrackHandlerPayload = z.infer<typeof zTrackHandlerPayload>;
+]) satisfies z.ZodType<ITrackHandlerPayload>;
// Deprecated types for beta version of the SDKs