Skip to content

Commit 142268c

Browse files
authored
feat: deprecate module hook and use nitro hooks (#4)
1 parent ced2cf6 commit 142268c

File tree

8 files changed

+55
-23
lines changed

8 files changed

+55
-23
lines changed

.github/workflows/ci.yml

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,19 @@ jobs:
2323
run: npx nypm@latest i
2424

2525
- name: Lint
26-
run: npm run lint
26+
run: pnpm lint
2727

2828
- name: Playground prepare
29-
run: npm run dev:prepare
29+
run: pnpm dev:prepare
3030

3131
- name: Test
32-
run: npm run test
32+
run: pnpm test
33+
34+
- name: Typecheck
35+
run: pnpm typecheck
36+
37+
- name: Build
38+
run: pnpm prepack
3339

3440
- name: Publish
3541
# add `--compact` option after releasing on npm

README.md

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ The module provides a hooks system that allows you to dynamically extend both do
126126

127127
### Available Hooks
128128

129-
#### `generate:llms(event, options)`
129+
#### `llms:generate(event, options)`
130130

131131
This hook is called for every request to `/llms.txt`. Use this hook to modify the structured documentation, It allows you to add sections, links, and metadata.
132132

@@ -135,7 +135,7 @@ This hook is called for every request to `/llms.txt`. Use this hook to modify th
135135
- `options`: ModuleOptions - The module options that you can modify to add sections, links, etc.
136136

137137

138-
#### `generate:llms_full(event, options, contents)`
138+
#### `llms:generate:llms_full(event, options, contents)`
139139

140140
This hook is called for every request to `/llms_full.txt`. It allows you to add custom content sections in any format.
141141

@@ -150,11 +150,9 @@ Create a server plugin in your `server/plugins` directory:
150150

151151
```ts
152152
// server/plugins/llms.ts
153-
import { onLLMsGenerate, onLLMsGenerateFull, llmsHooks } from 'nuxt-llms/runtime'
154-
155-
export default defineNitroPlugin(() => {
153+
export default defineNitroPlugin((nitroApp) => {
156154
// Method 1: Using the hooks directly
157-
llmsHooks.hook('generate', (event, options) => {
155+
nitroApp.hooks.hook('llms:generate', (event, options) => {
158156
// Add a new section to llms.txt
159157
options.sections.push({
160158
title: 'API Documentation',
@@ -170,7 +168,7 @@ export default defineNitroPlugin(() => {
170168
})
171169

172170
// Method 2: Using the helper function
173-
onLLMsGenerateFull((event, options, contents) => {
171+
nitroApp.hooks.hook('llms:generate:full', (event, options, contents) => {
174172
// Add detailed documentation to llms_full.txt
175173
contents.push(`## API Authentication
176174
@@ -199,8 +197,8 @@ If you're developing a Nuxt module that needs to extend the LLMs documentation:
199197
1. Create a server plugin in your module:
200198
```ts
201199
// module/runtime/server/plugins/my-module-llms.ts
202-
export default defineNitroPlugin(() => {
203-
onLLMsGenerate((event, options) => {
200+
export default defineNitroPlugin((nitroApp) => {
201+
nitroApp.hooks.hook('llms:generate', (event, options) => {
204202
options.sections.push({
205203
title: 'My Module',
206204
description: 'Documentation for my module features',

src/module.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { defineNuxtModule, createResolver, addServerHandler, addPrerenderRoutes, addServerImports, useLogger } from '@nuxt/kit'
2-
import type { ModuleOptions } from './types'
2+
import type { ModuleOptions } from './runtime/types'
33

4-
export type * from './types'
4+
export type * from './runtime/types'
55

66
export default defineNuxtModule<ModuleOptions>({
77
meta: {

src/runtime/server/routes/llms.txt.get.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
import { eventHandler, setHeader } from 'h3'
22
import type { ModuleOptions } from 'nuxt-llms'
33
import { llmsHooks } from 'nuxt-llms/runtime'
4-
import { useRuntimeConfig } from '#imports'
4+
// @ts-expect-error - useNitroApp is not typed
5+
import { useRuntimeConfig, useNitroApp } from '#imports'
56

67
export default eventHandler(async (event) => {
78
const options = useRuntimeConfig(event).llms as ModuleOptions
89

910
const llms: ModuleOptions = JSON.parse(JSON.stringify(options))
1011

12+
await useNitroApp().hooks.callHook('llms:generate', event, llms)
1113
await llmsHooks.callHook('generate', event, llms)
1214

1315
const document = [

src/runtime/server/routes/llms_full.txt.get.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
import { eventHandler, setHeader } from 'h3'
22
import type { ModuleOptions } from 'nuxt-llms'
33
import { llmsHooks } from 'nuxt-llms/runtime'
4-
import { useRuntimeConfig } from '#imports'
4+
// @ts-expect-error - useNitroApp is not typed
5+
import { useRuntimeConfig, useNitroApp } from '#imports'
56

67
export default eventHandler(async (event) => {
78
const options = useRuntimeConfig(event).llms as ModuleOptions
89

910
const contents = [] as string[]
1011
const llms: ModuleOptions = JSON.parse(JSON.stringify(options))
1112

13+
await useNitroApp().hooks.callHook('llms:generate:full', event, llms, contents)
1214
await llmsHooks.callHook('generate:full', event, llms, contents)
1315

1416
setHeader(event, 'Content-Type', 'text/plain')

src/runtime/server/tsconfig.json

Lines changed: 0 additions & 3 deletions
This file was deleted.

src/runtime/server/utils/hooks.ts

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,41 @@
11
import { createHooks } from 'hookable'
22
import type { H3Event } from 'h3'
33
import type { ModuleOptions } from 'nuxt-llms'
4+
import type { NitroRuntimeHooks } from 'nitropack/types'
5+
// @ts-expect-error - useNitroApp is not typed
6+
import { useNitroApp } from '#imports'
47

8+
/**
9+
* @deprecated Custom hooks are deprecated in favor of NitroRuntimeHooks.
10+
*/
511
export interface LLMSHooks {
612
'generate': (event: H3Event, options: ModuleOptions) => void
713
'generate:full': (event: H3Event, options: ModuleOptions, contents: string[]) => void
814
}
915

16+
/**
17+
* @deprecated Custom hooks are deprecated in favor of NitroRuntimeHooks.
18+
*/
1019
export const llmsHooks = createHooks<LLMSHooks>()
1120

21+
llmsHooks.beforeEach(() => {
22+
console.warn('[nuxt-llms] `llmsHooks` are deprecated and will be removed in future versions. Use `useNitroApp().hooks.hook(\'llms:generate\', (event, options) => {})` instead')
23+
})
24+
1225
/**
1326
* Run a callback when LLMs is being generated.
27+
*
28+
* @deprecated Use `useNitroApp().hooks.hook('llms:generate', (event, options) => {})` instead
1429
*/
15-
export function onLLMsGenerate(cb: LLMSHooks['generate']) {
16-
return llmsHooks.hook('generate', cb)
30+
export function onLLMsGenerate(cb: NitroRuntimeHooks['llms:generate']) {
31+
return useNitroApp().hooks.hook('llms:generate', cb)
1732
}
1833

1934
/**
2035
* Run a callback when Full LLMs is being generated.
36+
*
37+
* @deprecated Use `useNitroApp().hooks.hook('llms:generate:full', (event, options, contents) => {})` instead
2138
*/
22-
export function onLLMsGenerateFull(cb: LLMSHooks['generate:full']) {
23-
return llmsHooks.hook('generate:full', cb)
39+
export function onLLMsGenerateFull(cb: NitroRuntimeHooks['llms:generate:full']) {
40+
return useNitroApp().hooks.hook('llms:generate:full', cb)
2441
}

src/types.ts renamed to src/runtime/types.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,13 @@
1+
import type { H3Event } from 'h3'
2+
3+
declare module 'nitropack/types' {
4+
5+
interface NitroRuntimeHooks {
6+
'llms:generate': (event: H3Event, options: ModuleOptions) => void
7+
'llms:generate:full': (event: H3Event, options: ModuleOptions, contents: string[]) => void
8+
}
9+
}
10+
111
export interface LLMsSection {
212
title: string
313
description?: string

0 commit comments

Comments
 (0)