mirror of
https://github.com/ArthurDanjou/trpc-nuxt.git
synced 2026-01-14 20:19:33 +01:00
Compare commits
16 Commits
v0.4.0-bet
...
v0.3.3
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
a8e5538b05 | ||
|
|
24ecb41429 | ||
|
|
e8bf427d2b | ||
|
|
347d49482f | ||
|
|
2f7a1276ad | ||
|
|
f6db0c78cd | ||
|
|
d3203f0536 | ||
|
|
83e2b2810d | ||
|
|
2b4fff9faa | ||
|
|
752fe721a2 | ||
|
|
808cac4756 | ||
|
|
0c44f73c9f | ||
|
|
182875a781 | ||
|
|
0298f836b8 | ||
|
|
ea50430945 | ||
|
|
0e240663ca |
2
.gitignore
vendored
2
.gitignore
vendored
@@ -49,3 +49,5 @@ coverage
|
||||
Network Trash Folder
|
||||
Temporary Items
|
||||
.apdisk
|
||||
|
||||
.vercel
|
||||
|
||||
@@ -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`:
|
||||
|
||||
27
package.json
27
package.json
@@ -1,20 +1,19 @@
|
||||
{
|
||||
"name": "trpc-nuxt",
|
||||
"type": "module",
|
||||
"version": "0.3.1",
|
||||
"packageManager": "pnpm@7.5.0",
|
||||
"version": "0.3.3",
|
||||
"license": "MIT",
|
||||
"main": "./dist/module.cjs",
|
||||
"types": "./dist/types.d.ts",
|
||||
"exports": {
|
||||
"./package.json": "./package.json",
|
||||
".": {
|
||||
"import": "./dist/module.mjs",
|
||||
"require": "./dist/module.cjs"
|
||||
"require": "./dist/module.cjs",
|
||||
"import": "./dist/module.mjs"
|
||||
},
|
||||
"./api": {
|
||||
"import": "./dist/runtime/api.mjs",
|
||||
"types": "./dist/runtime/api.d.ts"
|
||||
"types": "./dist/runtime/api.d.ts",
|
||||
"import": "./dist/runtime/api.mjs"
|
||||
}
|
||||
},
|
||||
"files": [
|
||||
@@ -22,35 +21,37 @@
|
||||
"*.d.ts"
|
||||
],
|
||||
"scripts": {
|
||||
"prepublishOnly": "nr build",
|
||||
"prepublishOnly": "pnpm build",
|
||||
"build": "nuxt-module-build",
|
||||
"play": "nr build && nuxi dev playground",
|
||||
"play": "pnpm build && nuxi dev playground",
|
||||
"build:playground": "nuxi build playground",
|
||||
"lint": "eslint .",
|
||||
"lint:fix": "eslint . --fix",
|
||||
"release": "bumpp --commit --push --tag && npm publish",
|
||||
"prepare": "nuxi prepare playground"
|
||||
},
|
||||
"peerDependencies": {
|
||||
"@trpc/client": "<10.0.0",
|
||||
"@trpc/server": "<10.0.0"
|
||||
},
|
||||
"dependencies": {
|
||||
"@nuxt/kit": "3.0.0-rc.12",
|
||||
"@nuxt/kit": "3.0.0-rc.13",
|
||||
"@trpc/client": "^9.27.4",
|
||||
"@trpc/server": "^9.27.4",
|
||||
"dedent": "^0.7.0",
|
||||
"defu": "^6.1.0",
|
||||
"h3": "^0.8.5",
|
||||
"h3": "^0.8.6",
|
||||
"ohash": "^0.1.5",
|
||||
"pathe": "^0.3.9",
|
||||
"ufo": "^0.8.6"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@antfu/eslint-config": "^0.23.1",
|
||||
"@antfu/ni": "^0.16.2",
|
||||
"@nuxt/module-builder": "^0.2.0",
|
||||
"@types/dedent": "^0.7.0",
|
||||
"bumpp": "^7.2.0",
|
||||
"eslint": "^8.25.0",
|
||||
"nuxt": "3.0.0-rc.12",
|
||||
"pnpm": "^7.5.0",
|
||||
"nuxt": "3.0.0-rc.13",
|
||||
"trpc-nuxt": "workspace:*",
|
||||
"zod": "^3.19.1"
|
||||
},
|
||||
|
||||
@@ -6,7 +6,4 @@ export default defineNuxtConfig({
|
||||
runtimeConfig: {
|
||||
baseURL: '',
|
||||
},
|
||||
typescript: {
|
||||
strict: true,
|
||||
},
|
||||
})
|
||||
|
||||
1748
pnpm-lock.yaml
generated
1748
pnpm-lock.yaml
generated
File diff suppressed because it is too large
Load Diff
@@ -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<ModuleOptions>({
|
||||
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<ModuleOptions>({
|
||||
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,22 @@ export default defineNuxtModule<ModuleOptions>({
|
||||
`
|
||||
},
|
||||
})
|
||||
|
||||
if (finalConfig.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.')
|
||||
}
|
||||
else {
|
||||
logger.info('Plugin not installed. Create your own @trpc/client client plugin and composables.')
|
||||
}
|
||||
},
|
||||
})
|
||||
|
||||
|
||||
@@ -51,25 +51,14 @@ export async function useAsyncQuery<
|
||||
): Promise<AsyncData<PickFrom<ReturnType<Transform>, PickKeys>, TError>> {
|
||||
const { $client } = useNuxtApp()
|
||||
const key = getQueryKey(pathAndInput)
|
||||
const serverError = useState<TError | null>(`error-${key}`, () => null)
|
||||
const { error, data, ...rest } = await useAsyncData(
|
||||
const result = await useAsyncData(
|
||||
key,
|
||||
() => $client.query(...pathAndInput),
|
||||
// @ts-expect-error: Internal
|
||||
options,
|
||||
)
|
||||
)
|
||||
|
||||
if (error.value && !serverError.value)
|
||||
serverError.value = error.value as any
|
||||
|
||||
if (data.value)
|
||||
serverError.value = null
|
||||
|
||||
return {
|
||||
...rest,
|
||||
data,
|
||||
error: serverError,
|
||||
} as any
|
||||
return result as any
|
||||
}
|
||||
|
||||
export function useClient(): TRPCClient<AppRouter> {
|
||||
|
||||
Reference in New Issue
Block a user