-
-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: relocate group of functions for clarity (#965)
* refactor: relocate group of functions to nuxt.ts for clarity * refactor: relocate group of functions to composabels dir for clarity * refactor: relocate group of functions to composabels dir for clarity * refactor: relocate group of functions to composabels dir for clarity * refactor: relocate group of functions to nuxt.ts for clarity * refactor: relocate group of functions to composabels dir for clarity * refactor: relocate group of functions to composabels dir for clarity * refactor: relocate group of functions to composabels dir for clarity
- Loading branch information
Showing
17 changed files
with
254 additions
and
230 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,90 +1,2 @@ | ||
import type { DefineComponent } from 'vue' | ||
import { useHead } from '@unhead/vue' | ||
import type { NuxtAppCompat } from '@nuxt/bridge-schema' | ||
import { defineComponent, getCurrentInstance } from './composables' | ||
|
||
export const isVue2 = true | ||
export const isVue3 = false | ||
|
||
export const defineNuxtComponent: typeof defineComponent = | ||
function defineNuxtComponent (...args: any[]): any { | ||
const [options, key] = args | ||
const { setup, head, ...opts } = options | ||
|
||
// Avoid wrapping if no options api is used | ||
if (!setup && !options.asyncData && !options.head) { | ||
return { | ||
...options | ||
} | ||
} | ||
|
||
return { | ||
_fetchKeyBase: key, | ||
...opts, | ||
setup (props, ctx) { | ||
const nuxtApp = useNuxtApp() | ||
const res = setup ? callWithNuxt(nuxtApp, setup, [props, ctx]) : {} | ||
|
||
if (options.head) { | ||
const nuxtApp = useNuxtApp() | ||
useHead(typeof options.head === 'function' ? () => options.head(nuxtApp) : options.head) | ||
} | ||
|
||
return res | ||
} | ||
} as DefineComponent | ||
} | ||
|
||
export interface Context { | ||
$_nuxtApp: NuxtAppCompat | ||
} | ||
|
||
let currentNuxtAppInstance: NuxtAppCompat | null | ||
|
||
export const setNuxtAppInstance = (nuxt: NuxtAppCompat | null) => { | ||
currentNuxtAppInstance = nuxt | ||
} | ||
|
||
/** | ||
* Ensures that the setup function passed in has access to the Nuxt instance via `useNuxt`. | ||
* @param nuxt A Nuxt instance | ||
* @param setup The function to call | ||
*/ | ||
export function callWithNuxt<T extends (...args: any[]) => any> (nuxt: NuxtAppCompat, setup: T, args?: Parameters<T>) { | ||
setNuxtAppInstance(nuxt) | ||
const p: ReturnType<T> = args ? setup(...args as Parameters<T>) : setup() | ||
if (process.server) { | ||
// Unset nuxt instance to prevent context-sharing in server-side | ||
setNuxtAppInstance(null) | ||
} | ||
return p | ||
} | ||
|
||
interface Plugin { | ||
(nuxt: NuxtAppCompat): Promise<void> | Promise<{ provide?: Record<string, any> }> | void | { provide?: Record<string, any> } | ||
} | ||
|
||
export function defineNuxtPlugin (plugin: Plugin): (ctx: Context, inject: (id: string, value: any) => void) => void { | ||
return async (ctx, inject) => { | ||
const result = await callWithNuxt(ctx.$_nuxtApp, plugin, [ctx.$_nuxtApp]) | ||
if (result && result.provide) { | ||
for (const key in result.provide) { | ||
inject(key, result.provide[key]) | ||
} | ||
} | ||
return result | ||
} | ||
} | ||
|
||
export const useNuxtApp = (): NuxtAppCompat => { | ||
const vm = getCurrentInstance() | ||
|
||
if (!vm) { | ||
if (!currentNuxtAppInstance) { | ||
throw new Error('nuxt app instance unavailable') | ||
} | ||
return currentNuxtAppInstance | ||
} | ||
|
||
return vm.proxy.$_nuxtApp | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
packages/bridge/src/runtime/asyncData.ts → ...idge/src/runtime/composables/asyncData.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
|
||
import { defineComponent } from 'vue' | ||
import type { DefineComponent } from 'vue' | ||
import { useHead } from '@unhead/vue' | ||
import { useNuxtApp, callWithNuxt } from '../nuxt' | ||
|
||
export const defineNuxtComponent: typeof defineComponent = | ||
function defineNuxtComponent (...args: any[]): any { | ||
const [options, key] = args | ||
const { setup, head, ...opts } = options | ||
|
||
// Avoid wrapping if no options api is used | ||
if (!setup && !options.asyncData && !options.head) { | ||
return { | ||
...options | ||
} | ||
} | ||
|
||
return { | ||
_fetchKeyBase: key, | ||
...opts, | ||
setup (props, ctx) { | ||
const nuxtApp = useNuxtApp() | ||
const res = setup ? callWithNuxt(nuxtApp, setup, [props, ctx]) : {} | ||
|
||
if (options.head) { | ||
const nuxtApp = useNuxtApp() | ||
useHead(typeof options.head === 'function' ? () => options.head(nuxtApp) : options.head) | ||
} | ||
|
||
return res | ||
} | ||
} as DefineComponent | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
packages/bridge/src/runtime/error.ts → ...s/bridge/src/runtime/composables/error.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 2 additions & 1 deletion
3
packages/bridge/src/runtime/fetch.ts → ...s/bridge/src/runtime/composables/fetch.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
export { useLazyAsyncData, refreshNuxtData } from './asyncData' | ||
export * from './component' | ||
export { useCookie } from './cookie' | ||
export { clearError, createError, isNuxtError, throwError, showError, useError } from './error' | ||
export { useLazyFetch } from './fetch' | ||
export * from './router' | ||
export { useRequestHeaders, useRequestEvent } from './ssr' | ||
export * from './state' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.