diff --git a/src/layers/6_client/chainExtensions/scalar.test-d.ts b/src/layers/6_client/chainExtensions/scalar.test-d.ts new file mode 100644 index 000000000..892070510 --- /dev/null +++ b/src/layers/6_client/chainExtensions/scalar.test-d.ts @@ -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() + +const g2 = Graffle.create({ schema: `foo`, schemaMap }) +assertEqual() diff --git a/src/layers/6_client/chainExtensions/scalar.ts b/src/layers/6_client/chainExtensions/scalar.ts index a0922bf68..1fe3448c4 100644 --- a/src/layers/6_client/chainExtensions/scalar.ts +++ b/src/layers/6_client/chainExtensions/scalar.ts @@ -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 + return: Simplify> } -type Scalar<$Args extends Chain.Extension.Parameters> = { +interface ScalarExtension<$Args extends Chain.Extension.Parameters> { + /** + * 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> = { /** * 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< @@ -29,10 +39,10 @@ type Scalar<$Args extends Chain.Extension.Parameters> = { > > - /** + /* * 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'], @@ -64,5 +74,5 @@ export const scalarProperties = Chain.Extension.create((builder, state) }, }) }, - } + } as any }) diff --git a/src/lib/prelude.ts b/src/lib/prelude.ts index ba3ce1908..47177d057 100644 --- a/src/lib/prelude.ts +++ b/src/lib/prelude.ts @@ -255,7 +255,7 @@ export const capitalizeFirstLetter = (string: string) => string.charAt(0).toUppe export type SomeAsyncFunction = (...args: unknown[]) => Promise -export type SomeFunction = (...args: unknown[]) => MaybePromise +export type SomeFunction = (...args: unknown[]) => MaybePromise export type Deferred = { promise: Promise diff --git a/tests/_/helpers.ts b/tests/_/helpers.ts index 20e063dde..9e70039ee 100644 --- a/tests/_/helpers.ts +++ b/tests/_/helpers.ts @@ -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' @@ -37,8 +37,8 @@ interface Fixtures { fetch: Mock<(request: Request) => Promise> pokemonService: SchemaService graffle: Client - kitchenSink: Client> - kitchenSinkHttp: Client> + kitchenSink: Client> + kitchenSinkHttp: Client> kitchenSinkData: typeof db project: Project }