Skip to content

Commit

Permalink
feat: add nuxt helpers (#160)
Browse files Browse the repository at this point in the history
  • Loading branch information
mathe42 authored Jul 18, 2020
1 parent 8102ba5 commit 8a9885e
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 0 deletions.
1 change: 1 addition & 0 deletions docs/.vuepress/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ module.exports = {
'/helpers/useFetch',
'/helpers/useMeta',
'/helpers/useStatic',
'/helpers/defineNuxtHelpers',
],
},
{
Expand Down
56 changes: 56 additions & 0 deletions docs/helpers/defineNuxtHelpers.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
---
---

# defineNuxt... helpers

We added some helpers to optimize developer experience.

## defineNuxtPlugin

Create a Nuxt plugin with types with

```ts
import { defineNuxtPlugin } from 'nuxt-composition-api'

export default defineNuxtPlugin((ctx) => {
// do stuff
})
```

## defineNuxtMiddleware

Create a Nuxt plugin with types with

```ts
import { defineNuxtMiddleware } from 'nuxt-composition-api'

export default defineNuxtMiddleware((ctx) => {
// do stuff
})
```

## defineNuxtModule

Create a Nuxt plugin with types with

```ts
import { defineNuxtModule } from 'nuxt-composition-api'

export default defineNuxtModule<{myOption: boolean}>((moduleOptions) => {
// do stuff
})
```


## defineNuxtServerMiddleware

Create a Nuxt plugin with types with

```ts
import { defineNuxtServerMiddleware } from 'nuxt-composition-api'

export default defineNuxtServerMiddleware((req, res, next) => {
// do stuff
})
```

10 changes: 10 additions & 0 deletions src/defineHelpers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { Middleware, Plugin, Module, ServerMiddleware } from '@nuxt/types'

export const defineNuxtPlugin = (plugin: Plugin) => plugin
export const defineNuxtMiddleware = (middleware: Middleware) => middleware
export const defineNuxtModule = <T extends Record<string, unknown>>(
module: Module<T>
) => module
export const defineNuxtServerMiddleware = (
serverMiddleware: ServerMiddleware
) => serverMiddleware
1 change: 1 addition & 0 deletions src/entrypoint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export { globalPlugin, onGlobalSetup } from './hooks'
export { useMeta } from './meta'
export { ssrRef, shallowSsrRef, setSSRContext, ssrPromise } from './ssr-ref'
export { useStatic } from './static'
export * from './defineHelpers'

export type {
ComponentRenderProxy,
Expand Down
37 changes: 37 additions & 0 deletions test/tsd/createHelpers.test-d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import {
defineNuxtPlugin,
defineNuxtMiddleware,
defineNuxtModule,
defineNuxtServerMiddleware,
} from '../..'
import { expectType } from 'tsd'

defineNuxtPlugin((context, inject) => {
const hello = (msg: string) => console.log(`Hello ${msg}!`)

expectType<boolean>(context.isClient)

inject('hello', hello)
})

defineNuxtModule<{
option: string
}>(function (options) {
// expectType<string>(this.options.rootDir)

this.addPlugin('filename')

expectType<string>(options.option)
})

defineNuxtMiddleware(({ store, redirect }) => {
if (!store.state.authenticated) {
return redirect('/login')
}
})

defineNuxtServerMiddleware((req, res, next) => {
expectType<string | undefined>(req.url)

next()
})

1 comment on commit 8a9885e

@vercel
Copy link

@vercel vercel bot commented on 8a9885e Jul 18, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.