-
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(contract): new
type
util for custom schema (#94)
* feat(contract):new type util for custom schema * map input
- Loading branch information
Showing
5 changed files
with
70 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import type { SchemaInput, SchemaOutput } from './types' | ||
import { type } from './schema-utils' | ||
|
||
describe('type', () => { | ||
it('without map', () => { | ||
const schema = type<string>() | ||
|
||
expectTypeOf<SchemaInput<typeof schema>>().toEqualTypeOf<string>() | ||
expectTypeOf<SchemaOutput<typeof schema>>().toEqualTypeOf<string>() | ||
}) | ||
|
||
it('with map', () => { | ||
const schema2 = type<string, number>((val) => { | ||
expectTypeOf(val).toEqualTypeOf<string>() | ||
|
||
return Number(val) | ||
}) | ||
|
||
expectTypeOf<SchemaInput<typeof schema2>>().toEqualTypeOf<string>() | ||
expectTypeOf<SchemaOutput<typeof schema2>>().toEqualTypeOf<number>() | ||
|
||
// @ts-expect-error - map is required when TInput !== TOutput | ||
type<string, number>() | ||
|
||
// @ts-expect-error - output not match number | ||
type<string, number>(() => '123') | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { type } from './schema-utils' | ||
|
||
describe('type', async () => { | ||
it('without map', async () => { | ||
const schema = type() | ||
const val = {} | ||
expect((await schema['~standard'].validate(val) as any).value).toBe(val) | ||
}) | ||
|
||
it('with map', async () => { | ||
const val = {} | ||
const check = vi.fn().mockReturnValueOnce('__mapped__') | ||
const schema = type(check) | ||
expect((await schema['~standard'].validate(val) as any).value).toBe('__mapped__') | ||
expect(check).toHaveBeenCalledWith(val) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import type { IsEqual, Promisable } from '@orpc/shared' | ||
import type { StandardSchemaV1 } from '@standard-schema/spec' | ||
|
||
export type TypeRest<TInput, TOutput> = | ||
| [map: (input: TInput) => Promisable<TOutput>] | ||
| (IsEqual<TInput, TOutput> extends true ? [] : never) | ||
|
||
export function type<TInput, TOutput = TInput>(...[map]: TypeRest<TInput, TOutput>): StandardSchemaV1<TInput, TOutput> { | ||
return { | ||
'~standard': { | ||
vendor: 'custom', | ||
version: 1, | ||
async validate(value) { | ||
if (map) { | ||
return { value: await map(value as TInput) as TOutput } | ||
} | ||
|
||
return { value: value as TOutput } | ||
}, | ||
}, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters