-
-
Notifications
You must be signed in to change notification settings - Fork 442
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
55 additions
and
123 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,90 +1,49 @@ | ||
import { z } from "zod"; | ||
import { monitorFlyRegionSchema, monitorPeriodicitySchema } from "../constants"; | ||
|
||
// This is not a database table but just a schema for the limits of the plan | ||
// REMINDER: this is not a database table but just a schema for the limits of the plan | ||
// default values are set to the free plan limits | ||
|
||
export const limitsV1 = z.object({ | ||
export const limitsSchema = z.object({ | ||
version: z.undefined(), | ||
monitors: z.number(), | ||
"synthetic-checks": z.number(), | ||
periodicity: monitorPeriodicitySchema.array(), | ||
"multi-region": z.boolean(), | ||
"max-regions": z.number(), | ||
"data-retention": z.enum(["14 days", "3 months", "12 months", "24 months"]), | ||
// status pages | ||
"status-pages": z.number(), | ||
maintenance: z.boolean(), | ||
"status-subscribers": z.boolean(), | ||
"custom-domain": z.boolean(), | ||
"password-protection": z.boolean(), | ||
"white-label": z.boolean(), | ||
// alerts | ||
notifications: z.boolean(), | ||
pagerduty: z.boolean(), | ||
sms: z.boolean(), | ||
"notification-channels": z.number(), | ||
// collaboration | ||
members: z.literal("Unlimited").or(z.number()), | ||
"audit-log": z.boolean(), | ||
regions: monitorFlyRegionSchema.array(), | ||
/** | ||
* Monitor limits | ||
*/ | ||
monitors: z.number().default(1), | ||
"synthetic-checks": z.number().default(30), | ||
periodicity: monitorPeriodicitySchema.array().default(["10m", "30m", "1h"]), | ||
"multi-region": z.boolean().default(true), | ||
"max-regions": z.number().default(6), | ||
"data-retention": z | ||
.enum(["14 days", "3 months", "12 months", "24 months"]) | ||
.default("14 days"), | ||
regions: monitorFlyRegionSchema | ||
.array() | ||
.default(["ams", "gru", "iad", "jnb", "hkg", "syd"]), | ||
"private-locations": z.boolean().default(false), | ||
screenshots: z.boolean().default(false), | ||
/** | ||
* Status page limits | ||
*/ | ||
"status-pages": z.number().default(1), | ||
maintenance: z.boolean().default(true), | ||
"monitor-values-visibility": z.boolean().default(true), | ||
"status-subscribers": z.boolean().default(false), | ||
"custom-domain": z.boolean().default(false), | ||
"password-protection": z.boolean().default(false), | ||
"white-label": z.boolean().default(false), | ||
/** | ||
* Notification limits | ||
*/ | ||
notifications: z.boolean().default(true), | ||
pagerduty: z.boolean().default(false), | ||
sms: z.boolean().default(false), | ||
"notification-channels": z.number().default(1), | ||
/** | ||
* Collaboration limits | ||
*/ | ||
members: z.literal("Unlimited").or(z.number()).default(1), | ||
"audit-log": z.boolean().default(false), | ||
}); | ||
|
||
export type LimitsV1 = z.infer<typeof limitsV1>; | ||
export const limitsV2 = limitsV1.extend({ | ||
version: z.literal("v2"), | ||
"private-locations": z.boolean(), | ||
"monitor-values-visibility": z.boolean(), | ||
}); | ||
|
||
export const limitsV3 = limitsV2.extend({ | ||
version: z.literal("v3"), | ||
screenshots: z.boolean(), | ||
}); | ||
|
||
export type LimitsV2 = z.infer<typeof limitsV2>; | ||
export type LimitsV3 = z.infer<typeof limitsV3>; | ||
|
||
const unknownLimit = z.discriminatedUnion("version", [ | ||
limitsV1, | ||
limitsV2, | ||
limitsV3, | ||
]); | ||
|
||
export function migrateFromV1ToV2({ data }: { data: LimitsV1 }) { | ||
return { | ||
version: "v2", | ||
...data, | ||
"private-locations": true, | ||
"monitor-values-visibility": true, | ||
}; | ||
} | ||
|
||
export function migrateFromV2ToV3({ data }: { data: LimitsV2 }) { | ||
return { | ||
...data, | ||
version: "v3", | ||
screenshots: true, | ||
}; | ||
} | ||
|
||
export function migrateFromV1ToV3({ data }: { data: LimitsV1 }) { | ||
return { | ||
...data, | ||
version: "v3", | ||
screenshots: true, | ||
"private-locations": true, | ||
"monitor-values-visibility": true, | ||
}; | ||
} | ||
|
||
export const limitSchema = unknownLimit.transform((val) => { | ||
if (!val.version) { | ||
return migrateFromV1ToV3({ data: val }); | ||
} | ||
if (val.version === "v2") { | ||
return migrateFromV2ToV3({ data: val }); | ||
} | ||
return val; | ||
}); | ||
|
||
export type Limits = z.infer<typeof unknownLimit>; | ||
export type Limits = z.infer<typeof limitsSchema>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters