Skip to content

Commit

Permalink
Updated types
Browse files Browse the repository at this point in the history
  • Loading branch information
victorgarciaesgi committed Dec 20, 2022
1 parent 5d02f7f commit f86720f
Show file tree
Hide file tree
Showing 8 changed files with 26 additions and 46 deletions.
17 changes: 4 additions & 13 deletions playground/generated/typed-router.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ type TypedRouteLocationRaw<T extends TypedRouteList> = RouteQueryAndHash &
TypedLocationAsRelativeRaw<T> &
RouteLocationOptions;

export interface TypedRouter {
interface _TypedRouter {
/**
* Remove an existing route by its name.
*
Expand Down Expand Up @@ -121,16 +121,7 @@ export interface TypedRouter {
): Promise<NavigationFailure | void | undefined>;
}

declare module 'nuxt/dist/app/nuxt' {
export interface NuxtApp {
$typedRouter: TypedRouter;
$routesList: RouteListDecl;
}
}

declare module '@vue/runtime-core' {
interface ComponentCustomProperties {
$typedRouter: TypedRouter;
$routesList: RouteListDecl;
}
export interface TypedRouter extends _TypedRouter {}
declare global {
export interface TypedRouter extends _TypedRouter {}
}
1 change: 1 addition & 0 deletions playground/pages/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { useTypedRouter } from '../generated';
import { callOutsideComponent } from '../store';
const { router, routes } = useTypedRouter();
const { $routesList, $typedRouter } = useNuxtApp();
function navigate() {
// console.log($typedRouter, $routesList);
// router.push({ name: 'activate' });
Expand Down
5 changes: 3 additions & 2 deletions playground/plugins/__typed-router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@

import { defineNuxtPlugin } from '#app';

export default defineNuxtPlugin((nuxtApp) => {
export default defineNuxtPlugin(() => {
const router = useRouter();
const routesList = {
activate: 'activate',
index: 'index',
Expand All @@ -33,7 +34,7 @@ export default defineNuxtPlugin((nuxtApp) => {

return {
provide: {
typedRouter: nuxtApp.$router,
typedRouter: router as TypedRouter,
routesList,
},
};
Expand Down
3 changes: 3 additions & 0 deletions playground/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"extends": "./.nuxt/tsconfig.json"
}
5 changes: 3 additions & 2 deletions src/generators/output.generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@ export function createRuntimePluginFile(routesDeclTemplate: string): string {
${signatureTemplate}
import { defineNuxtPlugin } from '#app';
export default defineNuxtPlugin((nuxtApp) => {
export default defineNuxtPlugin(() => {
const router = useRouter();
const routesList = ${routesDeclTemplate};
return {
provide: {
typedRouter: nuxtApp.$router,
typedRouter: router as TypedRouter,
routesList,
},
};
Expand Down
17 changes: 4 additions & 13 deletions src/generators/output.templates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export const staticDeclarations = `
TypedLocationAsRelativeRaw<T> &
RouteLocationOptions;
export interface TypedRouter {
interface _TypedRouter {
/**
* Remove an existing route by its name.
*
Expand Down Expand Up @@ -78,18 +78,9 @@ export const staticDeclarations = `
to: TypedRouteLocationRaw<T>
): Promise<NavigationFailure | void | undefined>;
}
declare module 'nuxt/dist/app/nuxt' {
export interface NuxtApp {
$typedRouter: TypedRouter;
$routesList: RouteListDecl;
}
}
declare module '@vue/runtime-core' {
interface ComponentCustomProperties {
$typedRouter: TypedRouter;
$routesList: RouteListDecl;
}
export interface TypedRouter extends _TypedRouter {}
declare global {
export interface TypedRouter extends _TypedRouter {}
}
`;
2 changes: 1 addition & 1 deletion test/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"extends": "../../tsconfig.json",
"extends": "../tsconfig.json",
"compilerOptions": {
"types": ["node", "vitest/globals"]
},
Expand Down
22 changes: 7 additions & 15 deletions test/unit/__snapshots__/root.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ type TypedRouteLocationRaw<T extends TypedRouteList> = RouteQueryAndHash &
TypedLocationAsRelativeRaw<T> &
RouteLocationOptions;
export interface TypedRouter {
interface _TypedRouter {
/**
* Remove an existing route by its name.
*
Expand Down Expand Up @@ -172,18 +172,9 @@ export interface TypedRouter {
): Promise<NavigationFailure | void | undefined>;
}
declare module 'nuxt/dist/app/nuxt' {
export interface NuxtApp {
$typedRouter: TypedRouter;
$routesList: RouteListDecl;
}
}
declare module '@vue/runtime-core' {
interface ComponentCustomProperties {
$typedRouter: TypedRouter;
$routesList: RouteListDecl;
}
export interface TypedRouter extends _TypedRouter {}
declare global {
export interface TypedRouter extends _TypedRouter {}
}
"
`;
Expand Down Expand Up @@ -255,7 +246,8 @@ exports[`Init route file > should generate to correct tree for the pages folder
import { defineNuxtPlugin } from '#app';
export default defineNuxtPlugin((nuxtApp) => {
export default defineNuxtPlugin(() => {
const router = useRouter();
const routesList = {
activate: 'activate',
index: 'index',
Expand All @@ -282,7 +274,7 @@ export default defineNuxtPlugin((nuxtApp) => {
return {
provide: {
typedRouter: nuxtApp.$router,
typedRouter: router as TypedRouter,
routesList,
},
};
Expand Down

0 comments on commit f86720f

Please sign in to comment.