Skip to content

Commit

Permalink
feat(client): scalar statically validate sddm present (#1241)
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonkuhrt authored Oct 31, 2024
1 parent d697ae9 commit 83ebaeb
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 10 deletions.
11 changes: 11 additions & 0 deletions src/layers/6_client/chainExtensions/scalar.test-d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { schemaMap } from '../../../../tests/_/schemas/kitchen-sink/graffle/__.js'
import { Graffle } from '../../../entrypoints/__Graffle.js'
import { assertEqual } from '../../../lib/assert-equal.js'
import type { SomeFunction } from '../../../lib/prelude.js'
import type { TypeErrorMissingSchemaMap } from './scalar.js'

const g1 = Graffle.create({ schema: `foo` })
assertEqual<typeof g1.scalar, TypeErrorMissingSchemaMap>()

const g2 = Graffle.create({ schema: `foo`, schemaMap })
assertEqual<typeof g2.scalar, SomeFunction>()
22 changes: 16 additions & 6 deletions src/layers/6_client/chainExtensions/scalar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,25 @@ import { type Context } from '../context.js'
export interface Scalar_ extends Chain.Extension {
context: Context
// @ts-expect-error untyped params
return: Scalar<this['params']>
return: Simplify<ScalarExtension<this['params']>>
}

type Scalar<$Args extends Chain.Extension.Parameters<Scalar_>> = {
interface ScalarExtension<$Args extends Chain.Extension.Parameters<Scalar_>> {
/**
* TODO Docs.
*/
scalar: null extends $Args['context']['schemaMap'] ? TypeErrorMissingSchemaMap : ScalarMethod<$Args>
}

export type TypeErrorMissingSchemaMap =
`Error: Your client must have a schemaMap in order to apply registered scalars. Therefore we're providing this static error type message here instead of allowing you continue registering scalars that will never be applied.`

type ScalarMethod<$Args extends Chain.Extension.Parameters<Scalar_>> = {
/**
* TODO Docs.
*/
// TODO limit $Name to what is in the schema.
scalar<$Name extends string, $Decoded>(name: $Name, $Codec: {
<$Name extends string, $Decoded>(name: $Name, $Codec: {
decode: (value: string) => $Decoded
encode: (value: $Decoded) => string
}): Chain.Definition.MaterializeWithNewContext<
Expand All @@ -29,10 +39,10 @@ type Scalar<$Args extends Chain.Extension.Parameters<Scalar_>> = {
>
>

/**
/*
* TODO Docs.
*/
scalar<$Scalar extends Schema.Scalar>(scalar: $Scalar): Chain.Definition.MaterializeWithNewContext<
<$Scalar extends Schema.Scalar>(scalar: $Scalar): Chain.Definition.MaterializeWithNewContext<
$Args['chain'],
ConfigManager.SetAtPath<
$Args['context'],
Expand Down Expand Up @@ -64,5 +74,5 @@ export const scalarProperties = Chain.Extension.create<Scalar_>((builder, state)
},
})
},
}
} as any
})
2 changes: 1 addition & 1 deletion src/lib/prelude.ts
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ export const capitalizeFirstLetter = (string: string) => string.charAt(0).toUppe

export type SomeAsyncFunction = (...args: unknown[]) => Promise<unknown>

export type SomeFunction = (...args: unknown[]) => MaybePromise<unknown>
export type SomeFunction = (...args: unknown[]) => MaybePromise<any>

export type Deferred<T> = {
promise: Promise<T>
Expand Down
6 changes: 3 additions & 3 deletions tests/_/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import * as Path from 'node:path'
import type { Mock } from 'vitest'
import { test as testBase, vi } from 'vitest'
import { Graffle } from '../../src/entrypoints/main.js'
import type { Context } from '../../src/entrypoints/utilities-for-generated.js'
import type { Context, SchemaDrivenDataMap } from '../../src/entrypoints/utilities-for-generated.js'
import type { Client } from '../../src/layers/6_client/client.js'
import type { ConfigManager } from '../../src/lib/config-manager/__.js'
import { Grafaid } from '../../src/lib/grafaid/__.js'
Expand Down Expand Up @@ -37,8 +37,8 @@ interface Fixtures {
fetch: Mock<(request: Request) => Promise<Response>>
pokemonService: SchemaService
graffle: Client<Context>
kitchenSink: Client<ConfigManager.SetAtPath<Context, ['name'], 'default'>>
kitchenSinkHttp: Client<ConfigManager.SetAtPath<Context, ['name'], 'default'>>
kitchenSink: Client<ConfigManager.SetProperties<Context, { name: `default`; schemaMap: SchemaDrivenDataMap }>>
kitchenSinkHttp: Client<ConfigManager.SetProperties<Context, { name: `default`; schemaMap: SchemaDrivenDataMap }>>
kitchenSinkData: typeof db
project: Project
}
Expand Down

0 comments on commit 83ebaeb

Please sign in to comment.