mirror of
https://github.com/ArthurDanjou/trpc-nuxt.git
synced 2026-01-30 11:47:54 +01:00
add module helper
This commit is contained in:
20
module.mjs
Normal file
20
module.mjs
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
import { defineNuxtModule, extendViteConfig } from '@nuxt/kit'
|
||||||
|
|
||||||
|
export default defineNuxtModule({
|
||||||
|
meta: {
|
||||||
|
name: 'trpc-nuxt',
|
||||||
|
configKey: 'trpc',
|
||||||
|
compatibility: {
|
||||||
|
nuxt: '^3.0.0',
|
||||||
|
},
|
||||||
|
},
|
||||||
|
async setup(_moduleOptions, nuxt) {
|
||||||
|
nuxt.options.build.transpile.push('trpc-nuxt')
|
||||||
|
|
||||||
|
extendViteConfig((config) => {
|
||||||
|
config.optimizeDeps = config.optimizeDeps || {}
|
||||||
|
config.optimizeDeps.exclude = config.optimizeDeps.exclude || []
|
||||||
|
config.optimizeDeps.include.push('trpc-nuxt/client')
|
||||||
|
})
|
||||||
|
},
|
||||||
|
})
|
||||||
@@ -14,6 +14,9 @@
|
|||||||
"./client": {
|
"./client": {
|
||||||
"require": "./dist/client/index.cjs",
|
"require": "./dist/client/index.cjs",
|
||||||
"import": "./dist/client/index.mjs"
|
"import": "./dist/client/index.mjs"
|
||||||
|
},
|
||||||
|
"./module": {
|
||||||
|
"import": "./module.mjs"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"main": "./dist/index.mjs",
|
"main": "./dist/index.mjs",
|
||||||
@@ -21,7 +24,8 @@
|
|||||||
"types": "./dist/index.d.ts",
|
"types": "./dist/index.d.ts",
|
||||||
"files": [
|
"files": [
|
||||||
"dist",
|
"dist",
|
||||||
"client.d.ts"
|
"client.d.ts",
|
||||||
|
"module.mjs"
|
||||||
],
|
],
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"prepublishOnly": "nr build",
|
"prepublishOnly": "nr build",
|
||||||
|
|||||||
@@ -5,8 +5,9 @@ import type {
|
|||||||
} from '@trpc/server'
|
} from '@trpc/server'
|
||||||
import { createFlatProxy, createRecursiveProxy } from '@trpc/server/shared'
|
import { createFlatProxy, createRecursiveProxy } from '@trpc/server/shared'
|
||||||
import { hash } from 'ohash'
|
import { hash } from 'ohash'
|
||||||
import { useAsyncData, useState } from 'nuxt/app'
|
|
||||||
import type { DecoratedProcedureRecord } from './types'
|
import type { DecoratedProcedureRecord } from './types'
|
||||||
|
// @ts-expect-error: Nuxt auto-imports
|
||||||
|
import { useAsyncData, useState } from '#imports'
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Calculates the key used for `useAsyncData` call
|
* Calculates the key used for `useAsyncData` call
|
||||||
@@ -18,17 +19,13 @@ export function getQueryKey(
|
|||||||
return input === undefined ? path : `${path}-${hash(input || '')}`
|
return input === undefined ? path : `${path}-${hash(input || '')}`
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
function createNuxtProxyDecoration<TRouter extends AnyRouter>(name: string, client: inferRouterProxyClient<TRouter>) {
|
||||||
* @internal
|
|
||||||
*/
|
|
||||||
export function createNuxtProxyDecoration<TRouter extends AnyRouter>(name: string, client: inferRouterProxyClient<TRouter>) {
|
|
||||||
return createRecursiveProxy((opts) => {
|
return createRecursiveProxy((opts) => {
|
||||||
const args = opts.args
|
const args = opts.args
|
||||||
|
|
||||||
const pathCopy = [name, ...opts.path]
|
const pathCopy = [name, ...opts.path]
|
||||||
|
|
||||||
// The last arg is for instance `.mutate` or `.query()`
|
// The last arg is for instance `.mutate` or `.query()`
|
||||||
|
|
||||||
const lastArg = pathCopy.pop()!
|
const lastArg = pathCopy.pop()!
|
||||||
|
|
||||||
const path = pathCopy.join('.')
|
const path = pathCopy.join('.')
|
||||||
|
|||||||
Reference in New Issue
Block a user