Skip to content

Commit

Permalink
fix: fix to transform recursive call,typescript type missing
Browse files Browse the repository at this point in the history
  • Loading branch information
tclxshunquan-wang committed Sep 6, 2024
1 parent 04964cf commit 7d69927
Show file tree
Hide file tree
Showing 7 changed files with 129 additions and 23 deletions.
5 changes: 5 additions & 0 deletions .changeset/loud-rockets-press.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@hyperse/track": patch
---

Fix to transform recursive call,typescript type missing
10 changes: 4 additions & 6 deletions examples/next-example/track/track.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,13 @@ export const reportTrack = () => {
.before((ctx, eventType, eventData) => {
console.log('before', ctx, eventType, eventData);
})
.transform(['addCart', 'addCart'], (ctx, eventType, eventData) => {
.transform(['addCart', 'real_addCart'], (ctx, eventType, eventData) => {
if (eventData) {
return [eventData];
}
return [];
})
.transform(['addCartList', 'addCart'], (ctx, eventType, eventData) => {
.transform(['addCartList', 'real_addCart'], (ctx, eventType, eventData) => {
return eventData || [];
})
.transform('pv', (ctx, eventType, eventData) => {
Expand All @@ -63,10 +63,8 @@ export const reportTrack = () => {
});

return trackBuilder
.init(() => {
return {
reportAdapter: adapter,
};
.init({
reportAdapter: adapter,
})
.before((ctx) => {
console.log('before track', ctx);
Expand Down
4 changes: 2 additions & 2 deletions examples/next-example/track/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ export type ReportRealEventData = {
userName: string;
userId: string;
};
addCart?: GoodsRecord[];
};
real_addCart?: GoodsRecord[];
} & ReportEventData;

export interface GoodsRecord {
goodsName: string;
Expand Down
34 changes: 20 additions & 14 deletions src/adapter/adapter-builder.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { UnionToTuple } from '../types/type-union-tuple.js';
import {
AdapterAfterFunction,
AdapterBeforeFunction,
Expand All @@ -9,6 +8,7 @@ import {
} from '../types/types-adapter.js';
import { TrackAdapterOptions, TrackContext } from '../types/types-create.js';
import { TrackEventDataBase } from '../types/types-track.js';
import { TransformHook } from '../types/types-transform-hook.js';

/**
* A builder for creating a track adapter.
Expand Down Expand Up @@ -44,22 +44,37 @@ export class AdapterBuilder<
return {
setup: this.mountSetupHook,
before: this.mountBeforeHook,
transform: this.mountTransformHook,
transform: this.mountTransformHook as TransformHook<
ReturnType<typeof this.buildTransformChainer>,
Context,
EventData,
RealEventData
>,
build: this.executeBuild,
};
}

private buildSetupChainer() {
return {
before: this.mountBeforeHook,
transform: this.mountTransformHook,
transform: this.mountTransformHook as TransformHook<
ReturnType<typeof this.buildTransformChainer>,
Context,
EventData,
RealEventData
>,
build: this.executeBuild,
};
}

private buildBeforeChainer() {
return {
transform: this.mountTransformHook,
transform: this.mountTransformHook as TransformHook<
ReturnType<typeof this.buildTransformChainer>,
Context,
EventData,
RealEventData
>,
build: this.executeBuild,
};
}
Expand Down Expand Up @@ -139,19 +154,10 @@ export class AdapterBuilder<
return this.mountTransformHook<RightKey, RightEventData>(eventType, fun);
};

const result = {
return {
transform: transform,
...this.buildTransformChainer(),
};

return result as UnionToTuple<
Exclude<
keyof LeftEventData,
TransformEventType<Key, RealEventData, LeftEventData>
>
>['length'] extends 0
? ReturnType<typeof this.buildTransformChainer>
: typeof result;
};

private mountAfterHook = (
Expand Down
1 change: 1 addition & 0 deletions src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ export * from './types-adapter.js';
export * from './types-create.js';
export * from './types-logger.js';
export * from './types-track.js';
export * from './types-transform-hook.js';
96 changes: 96 additions & 0 deletions src/types/types-transform-hook.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
import { UnionToTuple } from './type-union-tuple.js';
import {
TransformEventData,
TransformEventType,
TransformReturns,
} from './types-adapter.js';
import { TrackContext } from './types-create.js';
import { TrackEventDataBase } from './types-track.js';

/**
* Represents the return type of the TransformHookReturns function.
*
* @template OtherChainer - The type of the other chainer.
* @template Context - The type of the context.
* @template Key - The type of the key.
* @template LeftEventData - The type of the left event data.
* @template RealEventData - The type of the real event data.
*/
export type TransformHookReturns<
OtherChainer,
Context,
Key,
LeftEventData,
RealEventData,
> = <
RightKey extends
| keyof RightEventData
| [keyof RightEventData, keyof RealEventData],
RightEventData = Key extends keyof LeftEventData
? Omit<LeftEventData, Key>
: Key extends [keyof LeftEventData, keyof RealEventData]
? Omit<LeftEventData, Key[0]>
: never,
>(
eventType: RightKey,
fun: (
ctx: Context,
eventType: TransformEventType<RightKey, RealEventData, RightEventData>,
eventData: TransformEventData<RightKey, RealEventData, RightEventData>
) => TransformReturns<RightKey, RealEventData, RightEventData>
) => UnionToTuple<
Exclude<
keyof RightEventData,
TransformEventType<RightKey, RealEventData, RightEventData>
>
>['length'] extends 0
? OtherChainer
: OtherChainer & {
transform: TransformHookReturns<
OtherChainer,
Context,
RightKey,
RightEventData,
RealEventData
>;
};

/**
* Represents a transform hook function.
*
* @template OtherChainer - The type of the other chainer.
* @template Context - The type of the track context.
* @template EventData - The type of the track event data.
* @template RealEventData - The type of the real event data.
*/
export type TransformHook<
OtherChainer,
Context extends TrackContext<any>,
EventData extends TrackEventDataBase,
RealEventData extends TrackEventDataBase = EventData,
> = <
Key extends keyof LeftEventData | [keyof LeftEventData, keyof RealEventData],
LeftEventData = EventData,
>(
eventType: Key,
fun: (
ctx: Context,
eventType: TransformEventType<Key, RealEventData, LeftEventData>,
eventData: TransformEventData<Key, RealEventData, LeftEventData>
) => TransformReturns<Key, RealEventData, LeftEventData>
) => UnionToTuple<
Exclude<
keyof LeftEventData,
TransformEventType<Key, RealEventData, LeftEventData>
>
>['length'] extends 0
? OtherChainer
: OtherChainer & {
transform: TransformHookReturns<
OtherChainer,
Context,
Key,
EventData,
RealEventData
>;
};
2 changes: 1 addition & 1 deletion website/docs/api/track-builder.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ The `select` method is used to choose which adapters should be used during the t

The context in which the tracking is occurring. This typically includes details such as user information, environment, or other contextual data relevant to the tracking event.

- **adapterMap** : `Record<string, TrackAdapter<Context, EventData>>`
- **adapterMap** : `Record<string, TrackAdapter<Context, EventData, AdapterOptions, RealEventData>>`

A map of available adapters, where the key is the adapter name and the value is the adapter instance.

Expand Down

0 comments on commit 7d69927

Please sign in to comment.