-
Notifications
You must be signed in to change notification settings - Fork 308
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(ts-client): isError helper function
- Loading branch information
1 parent
ee66c10
commit 2a77493
Showing
27 changed files
with
461 additions
and
26 deletions.
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,17 @@ | ||
/* eslint-disable */ | ||
import { expectTypeOf, test } from 'vitest' | ||
import { isError } from '../../tests/_/schema/generated/Error.js' | ||
import * as Schema from '../../tests/_/schema/schema.js' | ||
import { create } from './client.js' | ||
|
||
const client = create<Schema.Index>({ schema: Schema.schema, schemaIndex: Schema.$Index }) | ||
|
||
test('isError utility function narrows for error objects', async () => { | ||
const result = await client.query.result({ $: { case: 'Object1' }, __typename: true }) | ||
|
||
if (isError(result)) { | ||
expectTypeOf(result).toEqualTypeOf<{ __typename: 'ErrorOne' } | { __typename: 'ErrorTwo' }>() | ||
} else { | ||
expectTypeOf(result).toEqualTypeOf<null | { __typename: 'Object1' }>() | ||
} | ||
}) |
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,34 @@ | ||
import type { Config } from './generateCode.js' | ||
|
||
export const moduleNameError = `Error` | ||
|
||
export const generateError = (config: Config) => { | ||
const code: string[] = [] | ||
|
||
code.push( | ||
`type Include<T, U> = Exclude<T, Exclude<T, U>>`, | ||
`type ObjectWithTypeName = { __typename: string }`, | ||
) | ||
|
||
code.push(` | ||
const ErrorObjectsTypeNameSelectedEnum = { | ||
${config.error.objects.map(_ => `${_.name}: { __typename: '${_.name}' }`).join(`,\n`)} | ||
} as ${config.error.objects.length > 0 ? `const` : `Record<string,ObjectWithTypeName>`} | ||
const ErrorObjectsTypeNameSelected = Object.values(ErrorObjectsTypeNameSelectedEnum) | ||
type ErrorObjectsTypeNameSelected = (typeof ErrorObjectsTypeNameSelected)[number] | ||
`) | ||
|
||
code.push( | ||
`export const isError = <$Value>(value:$Value): value is Include<$Value, ErrorObjectsTypeNameSelected> => { | ||
return typeof value === 'object' && value !== null && '__typename' in value && | ||
ErrorObjectsTypeNameSelected.some(_ => _.__typename === value.__typename) | ||
}`, | ||
) | ||
|
||
return { | ||
code: code.join(`\n\n`), | ||
moduleName: moduleNameError, | ||
} | ||
} |
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
Oops, something went wrong.