Compare commits

...

24 Commits

Author SHA1 Message Date
wobsoriano
ff4bbebb0b chore: release v0.3.4 2022-12-20 01:34:59 -08:00
wobsoriano
354bf5d707 fix: deprecated h3 properties 2022-12-20 01:34:47 -08:00
wobsoriano
e7d160ee48 fix: client auto-imports 2022-12-20 01:32:59 -08:00
wobsoriano
9bf6d57c2c chore: update deps 2022-12-20 01:14:37 -08:00
Robert Soriano
6bca1e621d Update README.md 2022-11-12 21:18:08 -08:00
wobsoriano
a8e5538b05 release v0.3.3 2022-11-04 07:58:20 -07:00
wobsoriano
24ecb41429 eslint fix 2022-11-04 07:58:16 -07:00
wobsoriano
e8bf427d2b fix types 2022-11-04 07:57:38 -07:00
wobsoriano
347d49482f reinstall deps 2022-11-04 07:55:39 -07:00
wobsoriano
2f7a1276ad feat(deps): bump @nuxt/kit to 3.0.0-rc.13 2022-11-04 07:53:42 -07:00
wobsoriano
f6db0c78cd add .vercel to .gitignore 2022-11-04 07:51:44 -07:00
wobsoriano
d3203f0536 remove docs 2022-11-02 09:43:35 -07:00
wobsoriano
83e2b2810d release v0.3.2 2022-11-02 09:42:08 -07:00
wobsoriano
2b4fff9faa feat(deps): bump h3 to 0.8.6 2022-11-02 09:41:39 -07:00
wobsoriano
752fe721a2 feat(deps): lock @trpc/server and @trpc/client to version 9 2022-11-02 09:40:48 -07:00
wobsoriano
808cac4756 eslint fixes 2022-11-02 09:38:14 -07:00
wobsoriano
0c44f73c9f log info about plugin installation 2022-11-02 09:34:16 -07:00
wobsoriano
182875a781 feat: add option to specify if plugin should be installed 2022-11-02 09:30:40 -07:00
wobsoriano
0298f836b8 more eslint fixes 2022-11-02 09:21:15 -07:00
wobsoriano
ea50430945 eslint fixes 2022-11-02 09:18:17 -07:00
wobsoriano
0e240663ca remove local pnpm deps 2022-11-02 09:18:00 -07:00
Robert Soriano
bdeada0e6e Merge pull request #32 from littlejon85/9-server-error-discard
fix: don't discard error in client
2022-10-28 14:53:09 -07:00
Jan Dlouhý
88214185a8 fix: use useAsyncData error in client 2022-10-15 22:36:28 +02:00
Jan Dlouhý
ad03ae7b65 fix: don't discard error in client 2022-10-11 14:35:21 +02:00
12 changed files with 1405 additions and 1858 deletions

2
.gitignore vendored
View File

@@ -49,3 +49,5 @@ coverage
Network Trash Folder
Temporary Items
.apdisk
.vercel

2
.nuxtrc Normal file
View File

@@ -0,0 +1,2 @@
imports.autoImport=false
typescript.includeWorkspace=true

View File

@@ -18,7 +18,7 @@ End-to-end typesafe APIs with [tRPC.io](https://trpc.io/) in Nuxt applications.
## Install
```bash
npm i trpc-nuxt
npm i trpc-nuxt@0.3
```
```ts
@@ -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`:

View File

@@ -1,20 +1,19 @@
{
"name": "trpc-nuxt",
"type": "module",
"version": "0.3.1",
"packageManager": "pnpm@7.5.0",
"version": "0.3.4",
"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,37 +21,39 @@
"*.d.ts"
],
"scripts": {
"prepublishOnly": "nr build",
"build": "nuxt-module-build",
"play": "nr build && nuxi dev playground",
"build:playground": "nuxi build playground",
"dev": "nuxi dev playground",
"dev:build": "nuxi build playground",
"dev:preview": "nuxi preview playground",
"dev:prepare": "nuxt-module-build --stub && nuxi prepare 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",
"@trpc/client": "^9.27.4",
"@trpc/server": "^9.27.4",
"dedent": "^0.7.0",
"defu": "^6.1.0",
"h3": "^0.8.5",
"ohash": "^0.1.5",
"pathe": "^0.3.9",
"ufo": "^0.8.6"
"defu": "^6.1.1",
"h3": "^1.0.2",
"ohash": "^1.0.0",
"pathe": "^1.0.0",
"ufo": "^1.0.1"
},
"devDependencies": {
"@antfu/eslint-config": "^0.23.1",
"@antfu/ni": "^0.16.2",
"@nuxt/module-builder": "^0.2.0",
"@antfu/eslint-config": "^0.34.0",
"@nuxt/module-builder": "^0.2.1",
"@types/dedent": "^0.7.0",
"bumpp": "^7.2.0",
"bumpp": "^8.2.1",
"eslint": "^8.25.0",
"nuxt": "3.0.0-rc.12",
"pnpm": "^7.5.0",
"trpc-nuxt": "workspace:*",
"zod": "^3.19.1"
"nuxt": "3.0.0",
"zod": "^3.20.2"
},
"eslintConfig": {
"extends": "@antfu",

View File

@@ -6,7 +6,4 @@ export default defineNuxtConfig({
runtimeConfig: {
baseURL: '',
},
typescript: {
strict: true,
},
})

View File

@@ -1,19 +0,0 @@
<script setup lang="ts">
const counter = useCookie('counter')
counter.value = counter.value || Math.round(Math.random() * 1000)
</script>
<template>
<div>
<h1> Counter: {{ counter || '-' }}</h1>
<button @click="counter = null">
reset
</button>
<button @click="counter--">
-
</button>
<button @click="counter++">
+
</button>
</div>
</template>

View File

@@ -1,4 +1,5 @@
<script setup lang="ts">
import { useClient, useClientHeaders, useAsyncQuery } from '#imports'
const client = useClient()
const headers = useClientHeaders()
const { data: todos, pending, error, refresh } = await useAsyncQuery(['getTodos'])

View File

@@ -1,4 +1,5 @@
<script setup lang="ts">
import { useRoute, useAsyncQuery } from '#imports'
const route = useRoute()
const { data: todo, pending, error } = await useAsyncQuery(['getTodo', Number(route.params.id)])
</script>
@@ -11,8 +12,8 @@ const { data: todo, pending, error } = await useAsyncQuery(['getTodo', Number(ro
{{ error.data.code }}
</div>
<div v-else>
ID: {{ todo.id }} <br>
Title: {{ todo.title }} <br>
Completed: {{ todo.completed }}
ID: {{ todo?.id }} <br>
Title: {{ todo?.title }} <br>
Completed: {{ todo?.completed }}
</div>
</template>

3116
pnpm-lock.yaml generated

File diff suppressed because it is too large Load Diff

View File

@@ -3,25 +3,31 @@ 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')
nuxt.options.build.transpile.push(runtimeDir)
const handlerPath = join(nuxt.options.buildDir, 'trpc-handler.ts')
const trpcOptionsPath = join(nuxt.options.srcDir, 'server/trpc')
@@ -30,28 +36,20 @@ 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,
getContents() {
return dedent`
import { createTRPCHandler } from 'trpc-nuxt/api'
import { createTRPCHandler } from ${JSON.stringify(join(runtimeDir, 'api'))}
import * as functions from '${trpcOptionsPath}'
export default createTRPCHandler({
@@ -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.')
}
},
})

View File

@@ -30,7 +30,7 @@ export interface OnErrorPayload<TRouter extends AnyRouter> {
error: TRPCError
type: ProcedureType | 'unknown'
path: string | undefined
req: H3Event['req']
req: H3Event['node']['req']
input: unknown
ctx: undefined | inferRouterContext<TRouter>
}
@@ -54,7 +54,7 @@ export function createTRPCHandler<Router extends AnyRouter>({
const {
req,
res,
} = event
} = event.node
const $url = createURL(req.url!)

View File

@@ -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 (process.server && 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> {