0
Fork 0
mirror of https://github.com/withastro/astro.git synced 2025-01-06 22:10:10 -05:00
astro/.changeset/curvy-otters-jog.md
Luiz Ferraz 2e70741362
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>
2024-07-17 11:58:57 -04:00

1.4 KiB

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:

// 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:

// 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:

// other-integration/index.ts
import 'your-integration/types.ts';

export default (): AstroIntegration => {
  return {
    name: 'other-integration',
    hooks: {
      'myLib:eventHappened': async (your, values) => {
        // ...
      },
    }
  }
}