Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions src/generators/typescripts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export const generate = async (events: EventSchema[], serverSide: boolean): Prom
const shouldParamsBeOptional = !Object.keys(event.schemaJson.properties ?? {}).length
// eslint-disable-next-line max-len
if (serverSide) {
return `${params}\n${formatDescription(event)}export const ${functionName} = async (userId: string, properties${shouldParamsBeOptional ? '?' : ''}: ${interfaceName}) => mixpanel?.track('${event.name}', { distinct_id: await sha256(userId), ...properties })`
return `${params}\n${formatDescription(event)}export const ${functionName} = async (userId: string, properties${shouldParamsBeOptional ? '?' : ''}: ${interfaceName}, isDriver = false) => mixpanel?.track('${event.name}', { distinct_id: isDriver ? await driverHash(userId) : await dispatcherHash(userId), ...properties })`
}
// eslint-disable-next-line max-len
return `${params}\n${formatDescription(event)}export const ${functionName} = (properties${shouldParamsBeOptional ? '?' : ''}: ${interfaceName}) => mixpanel.track('${event.name}', properties)`
Expand All @@ -41,10 +41,16 @@ let mixpanel: Mixpanel.Mixpanel | undefined
export const initMixpanel = (token: string) => {
mixpanel = Mixpanel.init(token)
}
const sha256 = async (str: string): Promise<string> => {
const dispatcherHash = async (str: string): Promise<string> => {
const buf = await crypto.subtle.digest('SHA-256', new TextEncoder().encode(str))
return Array.prototype.map.call(new Uint8Array(buf), x => (('00' + x.toString(16)).slice(-2))).join('').slice(0, 16)
}
const driverHash = async (str: string): Promise<string> => {
const buf = await crypto.subtle.digest('SHA-256', new TextEncoder().encode(str))
return Array.from(new Uint8Array(buf))
.map(x => x.toString(16).padStart(2, "0"))
.join("")
}
${formattedEvents.join('\n\n')}`
}
return `
Expand Down
Loading