Skip to content

Commit e781585

Browse files
chore: Wip
1 parent f309250 commit e781585

File tree

6 files changed

+38
-42
lines changed

6 files changed

+38
-42
lines changed

config/env.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ export default {
184184
},
185185

186186
STRIPE_PUBLIC_KEY: {
187-
validation: schema.string(),
187+
validation: schema.string(),
188188
default: '',
189189
},
190190

storage/framework/core/buddy/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,4 +136,4 @@
136136
}
137137
},
138138
"web-types": "./web-types.json"
139-
}
139+
}

storage/framework/core/cache/src/drivers/index.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import type { CacheDriver } from '@stacksjs/types'
33
import type { DynamoDBOptions } from './dynamodb'
44
import type { FileSystemOptions } from './filesystem'
55
import type { RedisOptions } from './redis'
6-
import { config } from '@stacksjs/config'
76
import { dynamodb, DynamoDBCacheDriver } from './dynamodb'
87
import { fileSystem, FileSystemCacheDriver } from './filesystem'
98
import { memory, MemoryCacheDriver } from './memory'

storage/framework/core/development/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,4 +62,4 @@
6262
"devDependencies": {
6363
"@total-typescript/ts-reset": "^0.6.1"
6464
}
65-
}
65+
}

storage/framework/core/env/src/types.ts

Lines changed: 34 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
import type { BooleanValidatorType, EnumValidatorType, NumberValidatorType, StringValidatorType, ValidationType } from '@stacksjs/ts-validation'
1+
import type { VineBoolean, VineEnum, VineNumber, VineString } from '@vinejs/vine'
2+
import type { SchemaTypes, Infer } from '@vinejs/vine/types'
23
import type { EnvKey } from '../../../env'
3-
import { schema } from '@stacksjs/validation'
4+
import schema from '@vinejs/vine'
45
import env from '~/config/env'
56

67
interface EnumObject {
@@ -16,79 +17,76 @@ export const envEnum: EnumObject = {
1617
}
1718

1819
interface StringEnvConfig {
19-
validation: StringValidatorType
20+
validation: VineString
2021
default: string
2122
}
2223

2324
interface NumberEnvConfig {
24-
validation: NumberValidatorType
25+
validation: VineNumber
2526
default: number
2627
}
2728

2829
interface BooleanEnvConfig {
29-
validation: BooleanValidatorType
30+
validation: VineBoolean
3031
default: boolean
3132
}
3233

3334
interface EnumEnvConfig {
34-
validation: EnumValidatorType<string>
35+
validation: VineEnum<any>
3536
default: string
3637
}
3738

3839
type EnvValueConfig = StringEnvConfig | NumberEnvConfig | BooleanEnvConfig | EnumEnvConfig
3940

4041
export type EnvConfig = Partial<Record<EnvKey, EnvValueConfig>>
4142

42-
type EnvMap = Record<string, string>
43-
44-
type TypeFromString<T extends string> = T extends 'string'
45-
? string
46-
: T extends 'number'
47-
? number
48-
: T extends 'boolean'
49-
? boolean
50-
: T extends 'enum'
51-
? string
52-
: never
53-
54-
type Infer<T extends Record<string, string>> = {
55-
[K in keyof T]: TypeFromString<T[K]>
56-
}
43+
type EnvMap = Record<string, SchemaTypes>
5744

5845
const envStructure: EnvMap = Object.entries(env).reduce((acc, [key, value]) => {
5946
if (typeof value === 'object' && value !== null && 'validation' in value) {
60-
acc[key] = (value as EnvValueConfig).validation.name
47+
acc[key] = (value as EnvValueConfig).validation
6148
return acc
6249
}
6350

64-
let typeName: string
51+
let validatorType: SchemaTypes
6552
switch (typeof value) {
6653
case 'string':
67-
typeName = 'string'
54+
validatorType = schema.string()
6855
break
6956
case 'number':
70-
typeName = 'number'
57+
validatorType = schema.number()
7158
break
7259
case 'boolean':
73-
typeName = 'boolean'
60+
validatorType = schema.boolean()
7461
break
7562
default:
7663
if (Array.isArray(value)) {
77-
typeName = 'enum'
64+
validatorType = schema.enum(value as string[])
7865
break
7966
}
8067

8168
// check if is on object
8269
if (typeof value === 'object' && value !== null) {
83-
const schemaName = (value as { name: string }).name
70+
const schemaNameSymbol = Symbol.for('schema_name')
71+
const schemaName = (value as { [key: symbol]: string })[schemaNameSymbol]
72+
73+
if (schemaName === 'vine.string') {
74+
validatorType = schema.string()
75+
break
76+
}
8477

85-
if (schemaName === 'string' || schemaName === 'number' || schemaName === 'boolean') {
86-
typeName = schemaName
78+
if (schemaName === 'vine.number') {
79+
validatorType = schema.number()
80+
break
81+
}
82+
83+
if (schemaName === 'vine.boolean') {
84+
validatorType = schema.boolean()
8785
break
8886
}
8987

9088
if (!schemaName && key in envEnum) {
91-
typeName = 'enum'
89+
validatorType = schema.enum(envEnum[key as keyof typeof envEnum])
9290
break
9391
}
9492

@@ -98,15 +96,14 @@ const envStructure: EnvMap = Object.entries(env).reduce((acc, [key, value]) => {
9896
throw new Error(`Invalid env value for ${key}`)
9997
}
10098

101-
acc[key] = typeName
99+
acc[key] = validatorType
102100
return acc
103-
}, {} as Record<string, string>)
101+
}, {} as EnvMap)
104102

105-
export type Env = {
106-
[K in keyof typeof envStructure]: typeof envStructure[K]
107-
}
103+
export const envSchema: ReturnType<typeof schema.object> = schema.object(envStructure)
104+
export type Env = Infer<typeof envSchema>
108105

109-
export type EnvSchema = ReturnType<typeof schema.object<typeof envStructure>>
106+
export type EnvOptions = Env
110107

111108
export interface FrontendEnv {
112109
FRONTEND_APP_ENV: 'local' | 'development' | 'staging' | 'production'

storage/framework/core/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -352,4 +352,4 @@
352352
"@stacksjs/vite-config": "^0.70.23",
353353
"@stacksjs/vite-plugin": "^0.70.23"
354354
}
355-
}
355+
}

0 commit comments

Comments
 (0)