From 182875a7813b4fc6a37ed203fd94436e0d5884fb Mon Sep 17 00:00:00 2001 From: wobsoriano Date: Wed, 2 Nov 2022 09:30:40 -0700 Subject: [PATCH] feat: add option to specify if plugin should be installed --- README.md | 5 ++++- src/module.ts | 33 ++++++++++++++++++++++----------- 2 files changed, 26 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index 11d553d..ac72b6f 100644 --- a/README.md +++ b/README.md @@ -28,8 +28,9 @@ import { defineNuxtConfig } from 'nuxt' export default defineNuxtConfig({ modules: ['trpc-nuxt'], trpc: { - baseURL: '', // Set empty string (default) to make requests by relative address + baseURL: '', // Set empty string (default) to make requests by relative address endpoint: '/trpc', // defaults to /trpc + installPlugin: true, // defaults to true. Add @trpc/client plugin and composables }, typescript: { strict: true // required to make input/output types work @@ -113,6 +114,8 @@ headers.value.Authorization = `Bearer ${token}` // All client calls will now include the Authorization header. ``` +If your app needs transformers and other option that needs to be passed in `@trpc/client`, you will need to opt-out of the plugin by setting `installPlugin` to false in your `nuxt.config.ts` file and create your own. You can just copy the contents of the client plugin [here](https://github.com/wobsoriano/trpc-nuxt/blob/master/src/runtime/client.ts). + ## Options trpc-nuxt accepts the following options exposed under `~/server/trpc/index.ts`: diff --git a/src/module.ts b/src/module.ts index 2df4a06..716a66e 100644 --- a/src/module.ts +++ b/src/module.ts @@ -3,23 +3,29 @@ import { join, resolve } from 'pathe' import { defu } from 'defu' import dedent from 'dedent' -import { addImports, addPlugin, addServerHandler, addTemplate, defineNuxtModule } from '@nuxt/kit' +import { addImports, addPlugin, addServerHandler, addTemplate, defineNuxtModule, useLogger } from '@nuxt/kit' export interface ModuleOptions { baseURL: string endpoint: string + installPlugin?: boolean } +const metaName = 'trpc-nuxt' + export default defineNuxtModule({ meta: { - name: 'trpc-nuxt', + name: metaName, configKey: 'trpc', }, defaults: { baseURL: '', endpoint: '/trpc', + installPlugin: true, }, async setup(options, nuxt) { + const logger = useLogger(metaName) + const runtimeDir = fileURLToPath(new URL('./runtime', import.meta.url)) nuxt.options.build.transpile.push(runtimeDir, '#build/trpc-handler') @@ -30,22 +36,14 @@ export default defineNuxtModule({ const finalConfig = nuxt.options.runtimeConfig.public.trpc = defu(nuxt.options.runtimeConfig.public.trpc, { baseURL: options.baseURL, endpoint: options.endpoint, + installPlugin: options.installPlugin, }) - addImports([ - { name: 'useClient', from: join(runtimeDir, 'client') }, - { name: 'useAsyncQuery', from: join(runtimeDir, 'client') }, - { name: 'useClientHeaders', from: join(runtimeDir, 'client') }, - { name: 'getQueryKey', from: join(runtimeDir, 'client') }, - ]) - addServerHandler({ route: `${finalConfig.endpoint}/*`, handler: handlerPath, }) - addPlugin(resolve(runtimeDir, 'plugin')) - addTemplate({ filename: 'trpc-handler.ts', write: true, @@ -61,6 +59,19 @@ export default defineNuxtModule({ ` }, }) + + if (options.installPlugin) { + addImports([ + { name: 'useClient', from: join(runtimeDir, 'client') }, + { name: 'useAsyncQuery', from: join(runtimeDir, 'client') }, + { name: 'useClientHeaders', from: join(runtimeDir, 'client') }, + { name: 'getQueryKey', from: join(runtimeDir, 'client') }, + ]) + + addPlugin(resolve(runtimeDir, 'plugin')) + + logger.success('Plugin successfully installed.') + } }, })