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.
This commit is contained in:
evykassirer 2024-10-08 16:51:23 -07:00 committed by Tim Abbott
parent 905a234966
commit c133f7f219
3 changed files with 19 additions and 16 deletions

View File

@ -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),
})

View File

@ -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(),

View File

@ -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<typeof stream_schema>;
export type StreamSpecificNotificationSettings = z.infer<
@ -15,18 +15,21 @@ export type StreamSpecificNotificationSettings = z.infer<
>;
export type NeverSubscribedStream = z.infer<typeof never_subscribed_stream_schema>;
export type StreamProperties = z.infer<typeof stream_properties_schema>;
export type ApiStreamSubscription = z.infer<typeof stream_subscription_schema>;
// 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<typeof api_stream_subscription_schema>;
// This is the actual type of subscription objects we use in the app.
export type StreamSubscription = Omit<ApiStreamSubscription, "subscribers"> & 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<typeof stream_subscription_schema>;
const subs_by_stream_id = new Map<number, StreamSubscription>();