Skip to content

Commit

Permalink
feat: allow factory function for customClient (#219)
Browse files Browse the repository at this point in the history
* feat: allow factory function for customClient

* docs: wording

* chore: use runWithContext
  • Loading branch information
huang-julien authored Aug 31, 2024
1 parent e244648 commit bd7d02d
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
7 changes: 6 additions & 1 deletion docs/content/2.guides/2.advanced/1.custom-client.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,16 @@ description: ''

# Custom Client

You can make the module use your own [`@prismicio/client`](https://prismic.io/docs/technical-reference/prismicio-client?utm_campaign=devexp&utm_source=nuxt3doc&utm_medium=doc) instance. To do so, export it from `~/prismic/client` (`~/app/prismic/client` on Nuxt 3):
You can make the module use your own [`@prismicio/client`](https://prismic.io/docs/technical-reference/prismicio-client?utm_campaign=devexp&utm_source=nuxt3doc&utm_medium=doc) instance. To do so, export it from `~/prismic/client` (`~/app/prismic/client` on Nuxt 3), it can be either a factory function or the custom client:

```javascript [~/prismic/client.[jt]s]
import { createClient } from '@prismicio/client'

export default () => {
// this will be run inside by a nuxt plugin. So you will have access to the nuxt instance inside of it.
return createClient('my-repository')
}

export default createClient('my-repository')
```

Expand Down
6 changes: 4 additions & 2 deletions src/runtime/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,16 @@ import NuxtLink from '#app/components/nuxt-link'
import { defineNuxtPlugin, useCookie, useRequestEvent, onNuxtReady, refreshNuxtData, useHead, useRuntimeConfig, useRouter } from '#imports'

// @ts-expect-error vfs cannot be resolved here
import client from '#build/prismic/proxy/client'
import _client from '#build/prismic/proxy/client'
// @ts-expect-error vfs cannot be resolved here
import linkResolver from '#build/prismic/proxy/linkResolver'
// @ts-expect-error vfs cannot be resolved here
import richTextSerializer from '#build/prismic/proxy/richTextSerializer'

export default defineNuxtPlugin((nuxtApp) => {
export default defineNuxtPlugin(async (nuxtApp) => {

Check failure on line 15 in src/runtime/plugin.ts

View workflow job for this annotation

GitHub Actions / test (os ubuntu-latest, node 18)

The inferred type of 'default' cannot be named without a reference to '.pnpm/nuxt@3.13.0_@parcel+watcher@2.4.1_@types+node@20.14.12_eslint@9.9.1_jiti@1.21.6__ioredis@5.4._sqyrdato2lgkvg37xa7csw3nz4/node_modules/nuxt/app'. This is likely not portable. A type annotation is necessary.

Check failure on line 15 in src/runtime/plugin.ts

View workflow job for this annotation

GitHub Actions / test (os ubuntu-latest, node 20)

The inferred type of 'default' cannot be named without a reference to '.pnpm/nuxt@3.13.0_@parcel+watcher@2.4.1_@types+node@20.14.12_eslint@9.9.1_jiti@1.21.6__ioredis@5.4._sqyrdato2lgkvg37xa7csw3nz4/node_modules/nuxt/app'. This is likely not portable. A type annotation is necessary.

Check failure on line 15 in src/runtime/plugin.ts

View workflow job for this annotation

GitHub Actions / test (os ubuntu-latest, node lts/*)

The inferred type of 'default' cannot be named without a reference to '.pnpm/nuxt@3.13.0_@parcel+watcher@2.4.1_@types+node@20.14.12_eslint@9.9.1_jiti@1.21.6__ioredis@5.4._sqyrdato2lgkvg37xa7csw3nz4/node_modules/nuxt/app'. This is likely not portable. A type annotation is necessary.

Check failure on line 15 in src/runtime/plugin.ts

View workflow job for this annotation

GitHub Actions / test (os windows-latest, node 18)

The inferred type of 'default' cannot be named without a reference to '.pnpm/nuxt@3.13.0_@parcel+watcher@2.4.1_@types+node@20.14.12_eslint@9.9.1_jiti@1.21.6__ioredis@5.4._sqyrdato2lgkvg37xa7csw3nz4/node_modules/nuxt/app'. This is likely not portable. A type annotation is necessary.

Check failure on line 15 in src/runtime/plugin.ts

View workflow job for this annotation

GitHub Actions / test (os windows-latest, node 20)

The inferred type of 'default' cannot be named without a reference to '.pnpm/nuxt@3.13.0_@parcel+watcher@2.4.1_@types+node@20.14.12_eslint@9.9.1_jiti@1.21.6__ioredis@5.4._sqyrdato2lgkvg37xa7csw3nz4/node_modules/nuxt/app'. This is likely not portable. A type annotation is necessary.

Check failure on line 15 in src/runtime/plugin.ts

View workflow job for this annotation

GitHub Actions / test (os windows-latest, node lts/*)

The inferred type of 'default' cannot be named without a reference to '.pnpm/nuxt@3.13.0_@parcel+watcher@2.4.1_@types+node@20.14.12_eslint@9.9.1_jiti@1.21.6__ioredis@5.4._sqyrdato2lgkvg37xa7csw3nz4/node_modules/nuxt/app'. This is likely not portable. A type annotation is necessary.

Check failure on line 15 in src/runtime/plugin.ts

View workflow job for this annotation

GitHub Actions / test (os macos-latest, node 18)

The inferred type of 'default' cannot be named without a reference to '.pnpm/nuxt@3.13.0_@parcel+watcher@2.4.1_@types+node@20.14.12_eslint@9.9.1_jiti@1.21.6__ioredis@5.4._sqyrdato2lgkvg37xa7csw3nz4/node_modules/nuxt/app'. This is likely not portable. A type annotation is necessary.

Check failure on line 15 in src/runtime/plugin.ts

View workflow job for this annotation

GitHub Actions / test (os macos-latest, node 20)

The inferred type of 'default' cannot be named without a reference to '.pnpm/nuxt@3.13.0_@parcel+watcher@2.4.1_@types+node@20.14.12_eslint@9.9.1_jiti@1.21.6__ioredis@5.4._sqyrdato2lgkvg37xa7csw3nz4/node_modules/nuxt/app'. This is likely not portable. A type annotation is necessary.

Check failure on line 15 in src/runtime/plugin.ts

View workflow job for this annotation

GitHub Actions / test (os macos-latest, node lts/*)

The inferred type of 'default' cannot be named without a reference to '.pnpm/nuxt@3.13.0_@parcel+watcher@2.4.1_@types+node@20.14.12_eslint@9.9.1_jiti@1.21.6__ioredis@5.4._sqyrdato2lgkvg37xa7csw3nz4/node_modules/nuxt/app'. This is likely not portable. A type annotation is necessary.
const options: PrismicModuleOptions = useRuntimeConfig().public.prismic
const client = typeof _client === 'function' ? await nuxtApp.runWithContext(() => _client()) : _client

const endpoint = options.environment || options.endpoint || (client as Client | undefined)?.endpoint || ''

const prismicPlugin = createPrismic({
Expand Down

0 comments on commit bd7d02d

Please sign in to comment.