From c133f7f219692d5579888c467fedcbb665cce119 Mon Sep 17 00:00:00 2001 From: evykassirer Date: Tue, 8 Oct 2024 16:51:23 -0700 Subject: [PATCH] sub_store: Generate StreamSubscription type from a schema. This will be useful for later when we'll need the schema for converting settings_org to typescript. --- web/src/state_data.ts | 6 +++--- web/src/stream_types.ts | 2 +- web/src/sub_store.ts | 27 +++++++++++++++------------ 3 files changed, 19 insertions(+), 16 deletions(-) diff --git a/web/src/state_data.ts b/web/src/state_data.ts index 7b5b40319b..f593f87259 100644 --- a/web/src/state_data.ts +++ b/web/src/state_data.ts @@ -3,9 +3,9 @@ import {z} from "zod"; import {server_add_bot_schema} from "./bot_types"; import {realm_default_settings_schema} from "./realm_user_settings_defaults"; import { + api_stream_subscription_schema, never_subscribed_stream_schema, stream_schema, - stream_subscription_schema, } from "./stream_types"; import {user_settings_schema} from "./user_settings"; import {user_status_schema} from "./user_status_types"; @@ -473,8 +473,8 @@ export const state_data_schema = z .and( z .object({ - subscriptions: z.array(stream_subscription_schema), - unsubscribed: z.array(stream_subscription_schema), + subscriptions: z.array(api_stream_subscription_schema), + unsubscribed: z.array(api_stream_subscription_schema), never_subscribed: z.array(never_subscribed_stream_schema), realm_default_streams: z.array(stream_schema), }) diff --git a/web/src/stream_types.ts b/web/src/stream_types.ts index 5cd3eb0dcb..cb6b37455f 100644 --- a/web/src/stream_types.ts +++ b/web/src/stream_types.ts @@ -50,7 +50,7 @@ export const stream_properties_schema = stream_specific_notification_settings_sc }); // This is the raw data we get from the server for a subscription. -export const stream_subscription_schema = stream_schema.merge(stream_properties_schema).extend({ +export const api_stream_subscription_schema = stream_schema.merge(stream_properties_schema).extend({ email_address: z.string().optional(), stream_weekly_traffic: z.number().nullable(), subscribers: z.array(z.number()).optional(), diff --git a/web/src/sub_store.ts b/web/src/sub_store.ts index beac48f5ea..4a7d388ef4 100644 --- a/web/src/sub_store.ts +++ b/web/src/sub_store.ts @@ -1,4 +1,4 @@ -import type {z} from "zod"; +import {z} from "zod"; import * as blueslip from "./blueslip"; import type { @@ -6,8 +6,8 @@ import type { stream_properties_schema, stream_schema, stream_specific_notification_settings_schema, - stream_subscription_schema, } from "./stream_types"; +import {api_stream_subscription_schema} from "./stream_types"; export type Stream = z.infer; export type StreamSpecificNotificationSettings = z.infer< @@ -15,18 +15,21 @@ export type StreamSpecificNotificationSettings = z.infer< >; export type NeverSubscribedStream = z.infer; export type StreamProperties = z.infer; -export type ApiStreamSubscription = z.infer; - -// These properties are added in `stream_data` when hydrating the streams and are not present in the data we get from the server. -export type ExtraStreamAttrs = { - render_subscribers: boolean; - newly_subscribed: boolean; - subscribed: boolean; - previously_subscribed: boolean; -}; +export type ApiStreamSubscription = z.infer; // This is the actual type of subscription objects we use in the app. -export type StreamSubscription = Omit & ExtraStreamAttrs; +export const stream_subscription_schema = api_stream_subscription_schema + .omit({ + subscribers: true, + }) + .extend({ + // These properties are added in `stream_data` when hydrating the streams and are not present in the data we get from the server. + render_subscribers: z.boolean(), + newly_subscribed: z.boolean(), + subscribed: z.boolean(), + previously_subscribed: z.boolean(), + }); +export type StreamSubscription = z.infer; const subs_by_stream_id = new Map();