Skip to content

Commit 55e7529

Browse files
committed
refactor: slight re-org of types
1 parent 346457b commit 55e7529

File tree

4 files changed

+176
-159
lines changed

4 files changed

+176
-159
lines changed

packages/types/lib/blocks.d.ts

+82
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
import type {
2+
ButtonProps,
3+
DialogProps,
4+
DividerProps,
5+
Grid2Props,
6+
SxProps,
7+
TypographyProps,
8+
} from '@mui/material'
9+
10+
interface BaseBlock {
11+
gridSizes?: GridSizes
12+
gridStyle?: React.CSSProperties
13+
gridSx?: SxProps
14+
donorOnly?: boolean
15+
freeloaderOnly?: boolean
16+
loggedInOnly?: boolean
17+
loggedOutOnly?: boolean
18+
text?: string | null
19+
content?: string | null
20+
link?: string | null
21+
href?: string | null
22+
}
23+
24+
interface CustomText
25+
extends Omit<OnlyType<TypographyProps, Function, false>>,
26+
BaseBlock {
27+
type: 'text'
28+
}
29+
30+
interface CustomDivider
31+
extends Omit<OnlyDType<DividerProps, Function, false>>,
32+
BaseBlock {
33+
type: 'divider'
34+
}
35+
36+
interface CustomButton
37+
extends Omit<OnlyType<ButtonProps, Function, false>>,
38+
BaseBlock {
39+
type: 'button'
40+
}
41+
42+
interface CustomImg extends ImgProps, BaseBlock {
43+
type: 'img'
44+
}
45+
46+
interface CustomDiscord extends BaseBlock {
47+
type: 'discord'
48+
link: string
49+
}
50+
51+
interface CustomTelegram extends BaseBlock {
52+
type: 'telegram'
53+
telegramBotName: string
54+
telegramAuthUrl: string
55+
}
56+
57+
interface CustomLocal extends BaseBlock {
58+
type: 'localLogin'
59+
localAuthUrl: string
60+
link: string
61+
style: React.CSSProperties
62+
}
63+
64+
interface CustomLocale extends BaseBlock {
65+
type: 'localeSelection'
66+
}
67+
68+
interface ParentBlock extends BaseBlock, Grid2Props {
69+
type: 'parent'
70+
components: CustomComponent[]
71+
}
72+
73+
export type CustomComponent =
74+
| CustomText
75+
| CustomDivider
76+
| CustomButton
77+
| CustomImg
78+
| CustomDiscord
79+
| CustomTelegram
80+
| CustomLocal
81+
| CustomLocale
82+
| ParentBlock

packages/types/lib/config.d.ts

+28-159
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,21 @@
11
import type { LogLevelNames } from 'loglevel'
2-
import type {
3-
ButtonProps,
4-
DialogProps,
5-
DividerProps,
6-
Grid2Props,
7-
SxProps,
8-
TypographyProps,
9-
} from '@mui/material'
2+
import type { DialogProps } from '@mui/material'
103
import type { UiconsIndex } from 'uicons.js'
4+
115
import { Props as ImgProps } from '@components/Img'
126

7+
import type { CustomComponent } from './blocks'
138
import config = require('server/src/configs/default.json')
149
import example = require('server/src/configs/local.example.json')
1510

1611
import type { Schema } from './server'
17-
import { OnlyType, DeepMerge, ComparisonReport } from './utility'
12+
import {
13+
OnlyType,
14+
DeepMerge,
15+
ComparisonReport,
16+
Paths,
17+
ObjectPathValue,
18+
} from './utility'
1819

1920
type BaseConfig = typeof config
2021
type ExampleConfig = typeof example
@@ -54,12 +55,8 @@ export type Config<Client extends boolean = false> = DeepMerge<
5455
methods: string[]
5556
strategies: {
5657
trialPeriod: {
57-
start: {
58-
js: Date
59-
}
60-
end: {
61-
js: Date
62-
}
58+
start: TrialPeriodDate
59+
end: TrialPeriodDate
6360
roles: string[]
6461
}
6562
allowedGuilds: string[]
@@ -151,6 +148,20 @@ export interface Icons extends Omit<BaseConfig['icons'], 'styles'> {
151148
defaultIcons: Record<string, string>
152149
}
153150

151+
type BaseTrial =
152+
BaseConfig['authentication']['strategies'][number]['trialPeriod']['start']
153+
154+
export interface TrialPeriodDate extends BaseTrial {
155+
year?: number
156+
month?: number
157+
day?: number
158+
hour?: number
159+
minute?: number
160+
second?: number
161+
millisecond?: number
162+
js?: Date
163+
}
164+
154165
export interface ExtraField {
155166
name: string
156167
database: string
@@ -181,150 +192,8 @@ export interface GridSizes {
181192
xl?: number
182193
}
183194

184-
export interface BaseBlock {
185-
gridSizes?: GridSizes
186-
gridStyle?: React.CSSProperties
187-
gridSx?: SxProps
188-
donorOnly?: boolean
189-
freeloaderOnly?: boolean
190-
loggedInOnly?: boolean
191-
loggedOutOnly?: boolean
192-
text?: string | null
193-
content?: string | null
194-
link?: string | null
195-
href?: string | null
196-
}
197-
export interface CustomText
198-
extends Omit<OnlyType<TypographyProps, Function, false>>,
199-
BaseBlock {
200-
type: 'text'
201-
}
202-
203-
export interface CustomDivider
204-
extends Omit<OnlyDType<DividerProps, Function, false>>,
205-
BaseBlock {
206-
type: 'divider'
207-
}
208-
209-
export interface CustomButton
210-
extends Omit<OnlyType<ButtonProps, Function, false>>,
211-
BaseBlock {
212-
type: 'button'
213-
}
214-
215-
export interface CustomImg extends ImgProps, BaseBlock {
216-
type: 'img'
217-
}
218-
219-
export interface CustomDiscord extends BaseBlock {
220-
type: 'discord'
221-
link: string
222-
}
223-
224-
export interface CustomTelegram extends BaseBlock {
225-
type: 'telegram'
226-
telegramBotName: string
227-
telegramAuthUrl: string
228-
}
229-
230-
export interface CustomLocal extends BaseBlock {
231-
type: 'localLogin'
232-
localAuthUrl: string
233-
link: string
234-
style: React.CSSProperties
235-
}
236-
237-
export interface CustomLocale extends BaseBlock {
238-
type: 'localeSelection'
239-
}
240-
241-
export interface ParentBlock extends BaseBlock, Grid2Props {
242-
type: 'parent'
243-
components: CustomComponent[]
244-
}
245-
246-
export type CustomComponent =
247-
| CustomText
248-
| CustomDivider
249-
| CustomButton
250-
| CustomImg
251-
| CustomDiscord
252-
| CustomTelegram
253-
| CustomLocal
254-
| CustomLocale
255-
| ParentBlock
256-
257-
export type DeepKeys<T, P extends string = ''> = {
258-
[K in keyof T]-?: K extends string
259-
? P extends ''
260-
? `${K}` | `${K}.${DeepKeys<T[K], K>}`
261-
: `${P}.${K}.${DeepKeys<T[K], P & K>}`
262-
: never
263-
}[keyof T]
264-
265-
export type ConfigPaths<T extends object> = DeepKeys<T>
266-
267-
export type PathValue<T, P> = P extends `${infer K}.${infer Rest}`
268-
? K extends keyof T
269-
? Rest extends DeepKeys<T[K]>
270-
? PathValue<T[K], Rest>
271-
: never
272-
: never
273-
: P extends keyof T
274-
? T[P]
275-
: never
276-
277-
export type ConfigPathValue<
278-
T extends object,
279-
P extends ConfigPaths<T>,
280-
> = PathValue<T, P>
281-
282-
export type Join<K, P> = K extends string | number
283-
? P extends string | number
284-
? `${K}${'' extends P ? '' : '.'}${P}`
285-
: never
286-
: never
287-
288-
export type Prev = [
289-
never,
290-
0,
291-
1,
292-
2,
293-
3,
294-
4,
295-
5,
296-
6,
297-
7,
298-
8,
299-
9,
300-
10,
301-
11,
302-
12,
303-
13,
304-
14,
305-
15,
306-
16,
307-
17,
308-
18,
309-
19,
310-
20,
311-
...0[],
312-
]
313-
314-
export type Paths<T, D extends number = 10> = [D] extends [never]
315-
? never
316-
: T extends object
317-
? {
318-
[K in keyof T]-?: K extends string | number
319-
? `${K}` | Join<K, Paths<T[K], Prev[D]>>
320-
: never
321-
}[keyof T]
322-
: ''
323-
324-
export type NestedObjectPaths = Paths<Config>
325-
326-
export type GetSafeConfig = <P extends NestedObjectPaths>(
195+
export type GetSafeConfig = <P extends Paths<Config>>(
327196
path: P,
328-
) => ConfigPathValue<Config, P>
197+
) => ObjectPathValue<Config, P>
329198

330199
export type ConfigEqualReport = ComparisonReport<Omit<Config, 'areas'>>

packages/types/lib/index.d.ts

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import './augmentations'
22

3+
export * from './blocks'
34
export * from './client'
45
export * from './config'
56
export * from './general'

packages/types/lib/utility.d.ts

+65
Original file line numberDiff line numberDiff line change
@@ -78,3 +78,68 @@ export type ComparisonReport<T> = T extends Array<infer U>
7878
changed: string[]
7979
}
8080
: boolean
81+
82+
export type DeepKeys<T, P extends string = ''> = {
83+
[K in keyof T]-?: K extends string
84+
? P extends ''
85+
? `${K}` | `${K}.${DeepKeys<T[K], K>}`
86+
: `${P}.${K}.${DeepKeys<T[K], P & K>}`
87+
: never
88+
}[keyof T]
89+
90+
export type PathValue<T, P> = P extends `${infer K}.${infer Rest}`
91+
? K extends keyof T
92+
? Rest extends DeepKeys<T[K]>
93+
? PathValue<T[K], Rest>
94+
: never
95+
: never
96+
: P extends keyof T
97+
? T[P]
98+
: never
99+
100+
export type ObjectPathValue<T extends object, P extends Paths<T>> = PathValue<
101+
T,
102+
P
103+
>
104+
105+
export type Join<K, P> = K extends string | number
106+
? P extends string | number
107+
? `${K}${'' extends P ? '' : '.'}${P}`
108+
: never
109+
: never
110+
111+
export type Prev = [
112+
never,
113+
0,
114+
1,
115+
2,
116+
3,
117+
4,
118+
5,
119+
6,
120+
7,
121+
8,
122+
9,
123+
10,
124+
11,
125+
12,
126+
13,
127+
14,
128+
15,
129+
16,
130+
17,
131+
18,
132+
19,
133+
20,
134+
...0[],
135+
]
136+
137+
export type Paths<T, D extends number = 10> = [D] extends [never]
138+
? never
139+
: T extends object
140+
? {
141+
[K in keyof T]-?: K extends string | number
142+
? `${K}` | Join<K, Paths<T[K], Prev[D]>>
143+
: never
144+
}[keyof T]
145+
: ''

0 commit comments

Comments
 (0)