-
-
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: Refactor integration hooks type (#11304)
* feat: Refactor integration hooks type * Revert formatting changes * More detailed changelog * Add changeset for Astro DB * Apply suggestions from code review Co-authored-by: Florian Lefebvre <contact@florian-lefebvre.dev> Co-authored-by: Sarah Rainsberger <sarah@rainsberger.ca> --------- Co-authored-by: Florian Lefebvre <contact@florian-lefebvre.dev> Co-authored-by: Sarah Rainsberger <sarah@rainsberger.ca>
- Loading branch information
1 parent
683417f
commit 2e70741
Showing
6 changed files
with
179 additions
and
91 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,23 @@ | ||
--- | ||
'@astrojs/db': minor | ||
--- | ||
|
||
Removes the `AstroDbIntegration` type | ||
|
||
Astro integration hooks can now be extended and as such `@astrojs/db` no longer needs to declare it's own integration type. Using `AstroIntegration` will have the same type. | ||
|
||
If you were using the `AstroDbIntegration` type, apply this change to your integration code: | ||
|
||
```diff | ||
- import { defineDbIntegration, type AstroDbIntegration } from '@astrojs/db/utils'; | ||
+ import { defineDbIntegration } from '@astrojs/db/utils'; | ||
import type { AstroIntegration } from 'astro'; | ||
|
||
- export default (): AstroDbIntegration => { | ||
+ export default (): AstroIntegration => { | ||
return defineDbIntegration({ | ||
name: 'your-integration', | ||
hooks: {}, | ||
}); | ||
} | ||
``` |
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,58 @@ | ||
--- | ||
'astro': minor | ||
--- | ||
|
||
Refactors the type for integration hooks so that integration authors writing custom integration hooks can now allow runtime interactions between their integration and other integrations. | ||
|
||
This internal change should not break existing code for integration authors. | ||
|
||
To declare your own hooks for your integration, extend the `Astro.IntegrationHooks` interface: | ||
|
||
```ts | ||
// your-integration/types.ts | ||
declare global { | ||
namespace Astro { | ||
interface IntegrationHooks { | ||
'myLib:eventHappened': (your: string, parameters: number) => Promise<void>; | ||
} | ||
} | ||
} | ||
``` | ||
|
||
Call your hooks on all other integrations installed in a project at the appropriate time. For example, you can call your hook on initialization before either the Vite or Astro config have resolved: | ||
|
||
```ts | ||
// your-integration/index.ts | ||
import './types.ts'; | ||
|
||
export default (): AstroIntegration => { | ||
return { | ||
name: 'your-integration', | ||
hooks: { | ||
'astro:config:setup': async ({ config }) => { | ||
for (const integration of config.integrations) { | ||
await integration.hooks['myLib:eventHappened'].?('your values', 123); | ||
} | ||
}, | ||
} | ||
} | ||
} | ||
``` | ||
Other integrations can also now declare your hooks: | ||
```ts | ||
// other-integration/index.ts | ||
import 'your-integration/types.ts'; | ||
|
||
export default (): AstroIntegration => { | ||
return { | ||
name: 'other-integration', | ||
hooks: { | ||
'myLib:eventHappened': async (your, values) => { | ||
// ... | ||
}, | ||
} | ||
} | ||
} | ||
``` |
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