From d97373e0b4e471730d5b5232e1c22b6ac41e590e Mon Sep 17 00:00:00 2001 From: Neil Kakkar Date: Tue, 26 Sep 2023 11:24:21 +0100 Subject: [PATCH] fix(types): Move survey types back to main file --- src/__tests__/surveys.js | 3 +- src/extensions/surveys.ts | 2 +- src/posthog-core.ts | 3 +- src/posthog-surveys-types.ts | 90 ---------------------------------- src/posthog-surveys.ts | 93 +++++++++++++++++++++++++++++++++++- 5 files changed, 95 insertions(+), 96 deletions(-) delete mode 100644 src/posthog-surveys-types.ts diff --git a/src/__tests__/surveys.js b/src/__tests__/surveys.js index 20024e24b..93f8f4807 100644 --- a/src/__tests__/surveys.js +++ b/src/__tests__/surveys.js @@ -1,5 +1,4 @@ -import { PostHogSurveys } from '../posthog-surveys' -import { SurveyType, SurveyQuestionType } from '../posthog-surveys-types' +import { PostHogSurveys, SurveyType, SurveyQuestionType } from '../posthog-surveys' import { PostHogPersistence } from '../posthog-persistence' describe('surveys', () => { diff --git a/src/extensions/surveys.ts b/src/extensions/surveys.ts index 1b5891a57..bfb0c7a26 100644 --- a/src/extensions/surveys.ts +++ b/src/extensions/surveys.ts @@ -6,7 +6,7 @@ import { RatingSurveyQuestion, Survey, SurveyAppearance, -} from 'posthog-surveys-types' +} from 'posthog-surveys' const posthogLogo = '' diff --git a/src/posthog-core.ts b/src/posthog-core.ts index ac23dec7d..e52a055cd 100644 --- a/src/posthog-core.ts +++ b/src/posthog-core.ts @@ -55,10 +55,9 @@ import { SentryIntegration } from './extensions/sentry-integration' import { createSegmentIntegration } from './extensions/segment-integration' import { PageViewManager } from './page-view' import { ExceptionObserver } from './extensions/exceptions/exception-autocapture' -import { PostHogSurveys } from './posthog-surveys' +import { PostHogSurveys, SurveyCallback } from './posthog-surveys' import { RateLimiter } from './rate-limiter' import { uuidv7 } from './uuidv7' -import { SurveyCallback } from 'posthog-surveys-types' /* SIMPLE STYLE GUIDE: diff --git a/src/posthog-surveys-types.ts b/src/posthog-surveys-types.ts deleted file mode 100644 index d50dd262c..000000000 --- a/src/posthog-surveys-types.ts +++ /dev/null @@ -1,90 +0,0 @@ -/** - * Having Survey types in types.ts was confusing tsc - * and generating an invalid module.d.ts - * See https://github.com/PostHog/posthog-js/issues/698 - */ - -export interface SurveyAppearance { - // keep in sync with frontend/src/types.ts -> SurveyAppearance - backgroundColor?: string - submitButtonColor?: string - textColor?: string - submitButtonText?: string - descriptionTextColor?: string - ratingButtonColor?: string - ratingButtonHoverColor?: string - whiteLabel?: boolean - displayThankYouMessage?: boolean - thankYouMessageHeader?: string - thankYouMessageDescription?: string - // questionable: Not in frontend/src/types.ts -> SurveyAppearance, but used in site app - maxWidth?: string - zIndex?: string -} - -export enum SurveyType { - Popover = 'popover', - Button = 'button', - FullScreen = 'full_screen', - Email = 'email', - API = 'api', -} - -export type SurveyQuestion = BasicSurveyQuestion | LinkSurveyQuestion | RatingSurveyQuestion | MultipleSurveyQuestion - -interface SurveyQuestionBase { - question: string - description?: string | null - required?: boolean -} - -export interface BasicSurveyQuestion extends SurveyQuestionBase { - type: SurveyQuestionType.Open -} - -export interface LinkSurveyQuestion extends SurveyQuestionBase { - type: SurveyQuestionType.Link - link: string | null -} - -export interface RatingSurveyQuestion extends SurveyQuestionBase { - type: SurveyQuestionType.Rating - display: 'number' | 'emoji' - scale: number - lowerBoundLabel: string - upperBoundLabel: string -} - -export interface MultipleSurveyQuestion extends SurveyQuestionBase { - type: SurveyQuestionType.SingleChoice | SurveyQuestionType.MultipleChoice - choices: string[] -} - -export enum SurveyQuestionType { - Open = 'open', - MultipleChoice = 'multiple_choice', - SingleChoice = 'single_choice', - Rating = 'rating', - Link = 'link', -} - -export interface SurveyResponse { - surveys: Survey[] -} - -export type SurveyCallback = (surveys: Survey[]) => void - -export interface Survey { - // Sync this with the backend's SurveyAPISerializer! - id: string - name: string - description: string - type: SurveyType - linked_flag_key: string | null - targeting_flag_key: string | null - questions: SurveyQuestion[] - appearance: SurveyAppearance | null - conditions: { url?: string; selector?: string; seenSurveyWaitPeriodInDays?: number } | null - start_date: string | null - end_date: string | null -} diff --git a/src/posthog-surveys.ts b/src/posthog-surveys.ts index 20801da35..7d4057b63 100644 --- a/src/posthog-surveys.ts +++ b/src/posthog-surveys.ts @@ -1,6 +1,97 @@ import { PostHog } from './posthog-core' import { SURVEYS } from './constants' -import { SurveyCallback } from 'posthog-surveys-types' + +/** + * Having Survey types in types.ts was confusing tsc + * and generating an invalid module.d.ts + * See https://github.com/PostHog/posthog-js/issues/698 and + * https://github.com/PostHog/posthog-js/issues/807 + */ + +export interface SurveyAppearance { + // keep in sync with frontend/src/types.ts -> SurveyAppearance + backgroundColor?: string + submitButtonColor?: string + textColor?: string + submitButtonText?: string + descriptionTextColor?: string + ratingButtonColor?: string + ratingButtonHoverColor?: string + whiteLabel?: boolean + displayThankYouMessage?: boolean + thankYouMessageHeader?: string + thankYouMessageDescription?: string + // questionable: Not in frontend/src/types.ts -> SurveyAppearance, but used in site app + maxWidth?: string + zIndex?: string +} + +export enum SurveyType { + Popover = 'popover', + Button = 'button', + FullScreen = 'full_screen', + Email = 'email', + API = 'api', +} + +export type SurveyQuestion = BasicSurveyQuestion | LinkSurveyQuestion | RatingSurveyQuestion | MultipleSurveyQuestion + +interface SurveyQuestionBase { + question: string + description?: string | null + required?: boolean +} + +export interface BasicSurveyQuestion extends SurveyQuestionBase { + type: SurveyQuestionType.Open +} + +export interface LinkSurveyQuestion extends SurveyQuestionBase { + type: SurveyQuestionType.Link + link: string | null +} + +export interface RatingSurveyQuestion extends SurveyQuestionBase { + type: SurveyQuestionType.Rating + display: 'number' | 'emoji' + scale: number + lowerBoundLabel: string + upperBoundLabel: string +} + +export interface MultipleSurveyQuestion extends SurveyQuestionBase { + type: SurveyQuestionType.SingleChoice | SurveyQuestionType.MultipleChoice + choices: string[] +} + +export enum SurveyQuestionType { + Open = 'open', + MultipleChoice = 'multiple_choice', + SingleChoice = 'single_choice', + Rating = 'rating', + Link = 'link', +} + +export interface SurveyResponse { + surveys: Survey[] +} + +export type SurveyCallback = (surveys: Survey[]) => void + +export interface Survey { + // Sync this with the backend's SurveyAPISerializer! + id: string + name: string + description: string + type: SurveyType + linked_flag_key: string | null + targeting_flag_key: string | null + questions: SurveyQuestion[] + appearance: SurveyAppearance | null + conditions: { url?: string; selector?: string; seenSurveyWaitPeriodInDays?: number } | null + start_date: string | null + end_date: string | null +} export class PostHogSurveys { instance: PostHog