-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: allow type `unknown` on `isInputError` * chore: move ErrorInferenceObject to internal utils * chore: changeset * deps: expect-type * feat: first types test * chore: add types test to general test command * refactor: use describe and it for organization
- Loading branch information
1 parent
ea8582f
commit 08baf56
Showing
9 changed files
with
90 additions
and
12 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
--- | ||
'astro': patch | ||
--- | ||
|
||
Expands the `isInputError()` utility from `astro:actions` to accept errors of any type. This should now allow type narrowing from a try / catch block. | ||
|
||
```ts | ||
// example.ts | ||
import { actions, isInputError } from 'astro:actions'; | ||
|
||
try { | ||
await actions.like(new FormData()); | ||
} catch (error) { | ||
if (isInputError(error)) { | ||
console.log(error.fields); | ||
} | ||
} | ||
``` |
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
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
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,31 @@ | ||
import { expectTypeOf } from 'expect-type'; | ||
import { isInputError, defineAction } from '../../dist/actions/runtime/virtual/server.js'; | ||
import { z } from '../../zod.mjs'; | ||
import { describe, it } from 'node:test'; | ||
|
||
const exampleAction = defineAction({ | ||
input: z.object({ | ||
name: z.string(), | ||
}), | ||
handler: () => {}, | ||
}); | ||
|
||
const result = await exampleAction.safe({ name: 'Alice' }); | ||
|
||
describe('isInputError', () => { | ||
it('isInputError narrows unknown error types', async () => { | ||
try { | ||
await exampleAction({ name: 'Alice' }); | ||
} catch (e) { | ||
if (isInputError(e)) { | ||
expectTypeOf(e.fields).toEqualTypeOf<Record<string, string[] | undefined>>(); | ||
} | ||
} | ||
}); | ||
|
||
it('`isInputError` preserves `fields` object type for ActionError objects', async () => { | ||
if (isInputError(result.error)) { | ||
expectTypeOf(result.error.fields).toEqualTypeOf<{ name?: string[] }>(); | ||
} | ||
}); | ||
}); |
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,9 @@ | ||
{ | ||
"extends": "../../tsconfig.base.json", | ||
"include": ["test/types"], | ||
"compilerOptions": { | ||
"allowJs": true, | ||
"emitDeclarationOnly": false, | ||
"noEmit": true, | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.