Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 | 557x 557x 127816x 127816x 127816x 557x 558x 1x 557x 317x 240x 5x 235x 557x 558x 558x 558x 558x 553x 12x 4x 1x 2139x 1705x 434x 434x 281x 281x 153x 3x 150x 434x 21x 21x 21x 21x 21x 16x 16x 16x 16x 16x 16x 16x 11x 8x 8x 11x 5x | import type { z, ZodObject, ZodOptional, ZodRecord } from 'zod/v4'
import type { FactoryContext } from '../internal/context/factory-context.mjs'
export type ClassType = new (...args: any[]) => any
export type ClassTypeWithoutArguments = new () => any
export type ClassTypeWithArgument<Arg> = new (arg: Arg) => any
export type ClassTypeWithOptionalArgument<Arg> = new (arg?: Arg) => any
export type ClassTypeWithInstance<T> = new (...args: any[]) => T
export type ClassTypeWithInstanceAndArgument<T, Arg> = new (arg: Arg) => T
export type ClassTypeWithInstanceAndOptionalArgument<T, Arg> = new (
arg?: Arg,
) => T
export type BaseInjectionTokenSchemaType = ZodObject | ZodRecord
export type OptionalInjectionTokenSchemaType =
| ZodOptional<ZodObject>
| ZodOptional<ZodRecord>
export type InjectionTokenSchemaType =
| BaseInjectionTokenSchemaType
| OptionalInjectionTokenSchemaType
/**
* Simple hash function for deterministic ID generation
*/
function simpleHash(str: string): string {
let hash = 0
for (let i = 0; i < str.length; i++) {
const char = str.charCodeAt(i)
hash = (hash << 5) - hash + char
hash = hash & hash // Convert to 32-bit integer
}
return Math.abs(hash).toString(36)
}
/**
* Generate deterministic ID from token name
*/
function generateTokenId(name: string | symbol | ClassType, customId?: string): string {
if (customId) {
return customId
}
let base: string
if (typeof name === 'function') {
base = `${name.name}_${name.toString()}`
} else if (typeof name === 'symbol') {
base = `symbol_${name.toString()}`
} else {
base = `token_${name}`
}
return `${base.split('_')[0]}_${simpleHash(base)}`
}
export class InjectionToken<
// oxlint-disable-next-line no-unused-vars
T,
S extends InjectionTokenSchemaType | unknown = unknown,
// oxlint-disable-next-line no-unused-vars
Required extends boolean = S extends ZodOptional<ZodObject>
? false
: S extends ZodOptional<ZodRecord>
? false
: S extends ZodObject
? true
: S extends ZodRecord
? true
: false,
> {
public readonly id: string
private formattedName: string | null = null
constructor(
public readonly name: string | symbol | ClassType,
public readonly schema: ZodObject | undefined,
customId?: string,
) {
this.id = generateTokenId(name, customId)
}
static create<T extends ClassType>(
name: T,
): InjectionToken<InstanceType<T>, undefined>
static create<T extends ClassType, Schema extends InjectionTokenSchemaType>(
name: T,
schema: Schema,
): Schema['_def']['type'] extends 'ZodOptional'
? InjectionToken<InstanceType<T>, Schema, false>
: InjectionToken<InstanceType<T>, Schema, true>
static create<T>(name: string | symbol): InjectionToken<T, undefined>
static create<T, Schema extends InjectionTokenSchemaType>(
name: string | any,
schema: Schema,
): InjectionToken<T, Schema>
static create(name: string | symbol, schema?: unknown, customId?: string) {
// @ts-expect-error
return new InjectionToken(name, schema, customId)
}
static bound<T, S extends InjectionTokenSchemaType>(
token: InjectionToken<T, S>,
value: z.input<S>,
): BoundInjectionToken<T, S> {
return new BoundInjectionToken(token, value)
}
static factory<T, S extends InjectionTokenSchemaType>(
token: InjectionToken<T, S>,
factory: (ctx: FactoryContext) => Promise<z.input<S>>,
): FactoryInjectionToken<T, S> {
return new FactoryInjectionToken(token, factory)
}
static refineType<T>(
token: BoundInjectionToken<any, any>,
): BoundInjectionToken<T, any> {
return token as BoundInjectionToken<T, any>
}
toString() {
if (this.formattedName) {
return this.formattedName
}
const { name } = this
if (typeof name === 'function') {
const className = name.name
this.formattedName = `${className}(${this.id})`
} else if (typeof name === 'symbol') {
this.formattedName = `${name.toString()}(${this.id})`
} else {
this.formattedName = `${name}(${this.id})`
}
return this.formattedName
}
}
export class BoundInjectionToken<T, S extends InjectionTokenSchemaType> {
public id: string
public name: string | symbol | ClassType
public schema: InjectionTokenSchemaType
constructor(
public readonly token: InjectionToken<T, S>,
public readonly value: z.input<S>,
) {
this.name = token.name
this.id = token.id
this.schema = token.schema as InjectionTokenSchemaType
}
toString() {
return this.token.toString()
}
}
export class FactoryInjectionToken<T, S extends InjectionTokenSchemaType> {
public value?: z.input<S>
public resolved = false
public id: string
public name: string | symbol | ClassType
public schema: InjectionTokenSchemaType
constructor(
public readonly token: InjectionToken<T, S>,
public readonly factory: (ctx: FactoryContext) => Promise<z.input<S>>,
) {
this.name = token.name
this.id = token.id
this.schema = token.schema as InjectionTokenSchemaType
}
async resolve(ctx: FactoryContext): Promise<z.input<S>> {
if (!this.value) {
this.value = await this.factory(ctx)
this.resolved = true
}
return this.value
}
toString() {
return this.token.toString()
}
}
export type AnyInjectableType =
| ClassType
| InjectionToken<any, any>
| BoundInjectionToken<any, any>
| FactoryInjectionToken<any, any>
export type InjectionTokenType =
| InjectionToken<any, any>
| BoundInjectionToken<any, any>
| FactoryInjectionToken<any, any>
|