Skip to content

Commit

Permalink
add: types
Browse files Browse the repository at this point in the history
  • Loading branch information
s00d committed Aug 17, 2024
1 parent a2be7b5 commit 60a2c49
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 55 deletions.
2 changes: 1 addition & 1 deletion playground/nuxt.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { startSubprocess } from '@nuxt/devtools-kit'

export default defineNuxtConfig({
modules: [
'../src/module',
'nuxt-i18n-micro',
defineNuxtModule({
setup(_, nuxt) {
if (!nuxt.options.dev)
Expand Down
2 changes: 1 addition & 1 deletion playground/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@
},
"dependencies": {
"nuxt": "^3.12.4",
"nuxt-i18n-micro": "^1.1.1"
"nuxt-i18n-micro": "^1.1.2"
}
}
54 changes: 1 addition & 53 deletions src/module.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import path from 'node:path'
import { existsSync, mkdirSync, writeFileSync } from 'node:fs'
import { addPlugin, addServerHandler, addTypeTemplate, createResolver, defineNuxtModule, extendPages } from '@nuxt/kit'
import { addPlugin, addServerHandler, createResolver, defineNuxtModule, extendPages } from '@nuxt/kit'
import type { HookResult } from '@nuxt/schema'
import { setupDevToolsUI } from './devtools'

Expand Down Expand Up @@ -186,58 +186,6 @@ export default defineNuxtModule<ModuleOptions>({
// additionalRoutes.forEach(route => routesSet.add(route))
// })

addTypeTemplate({
filename: 'types/i18n.d.ts',
getContents() {
return `
interface PluralTranslations {
singular: string;
plural: string;
}
interface Translations {
[key: string]: string | number | boolean | Translations | PluralTranslations | unknown[] | null;
}
declare module '#app' {
interface NuxtApp {
$getLocale: () => string;
$getLocales: () => string[];
$t: <T extends Record<string, string | number | boolean>>(
key: string,
params?: T,
defaultValue?: string
) => string | number | boolean | Translations | PluralTranslations | unknown[] | unknown | null;
$tc: (key: string, count: number, defaultValue?: string) => string;
$mergeTranslations: (newTranslations: Translations) => void;
$switchLocale: (locale: string) => void;
$localeRoute: (to: RouteLocationRaw, locale?: string) => RouteLocationRaw;
$loadPageTranslations: (locale: string, routeName: string) => Promise<void>;
}
}
declare module 'vue/types/vue' {
interface Vue {
$getLocale: () => string;
$getLocales: () => string[];
$t: <T extends Record<string, string | number | boolean>>(
key: string,
params?: T,
defaultValue?: string
) => string | number | boolean | Translations | PluralTranslations | unknown[] | unknown | null;
$tc: (key: string, count: number, defaultValue?: string) => string;
$mergeTranslations: (newTranslations: Translations) => void;
$switchLocale: (locale: string) => void;
$localeRoute: (to: RouteLocationRaw, locale?: string) => RouteLocationRaw;
$loadPageTranslations: (locale: string, routeName: string) => Promise<void>;
}
}
export {};
`
},
})

// Setup DevTools integration
if (nuxt.options.dev)
setupDevToolsUI(options, resolver.resolve)
Expand Down
32 changes: 32 additions & 0 deletions src/runtime/01.plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -240,3 +240,35 @@ export default defineNuxtPlugin(async (_nuxtApp) => {
},
}
})

interface PluginsInjections {
$getLocale: () => string;

Check failure on line 245 in src/runtime/01.plugin.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected separator (;)
$getLocales: () => string[];

Check failure on line 246 in src/runtime/01.plugin.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected separator (;)
$t: <T extends Record<string, string | number | boolean>>(
key: string,
params?: T,
defaultValue?: string
) => string | number | boolean | Translations | PluralTranslations | unknown[] | unknown | null;

Check failure on line 251 in src/runtime/01.plugin.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected separator (;)
$tc: (key: string, count: number, defaultValue?: string) => string;

Check failure on line 252 in src/runtime/01.plugin.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected separator (;)
$mergeTranslations: (newTranslations: Translations) => void;

Check failure on line 253 in src/runtime/01.plugin.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected separator (;)
$switchLocale: (locale: string) => void;

Check failure on line 254 in src/runtime/01.plugin.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected separator (;)
$localeRoute: (to: RouteLocationRaw, locale?: string) => RouteLocationRaw;

Check failure on line 255 in src/runtime/01.plugin.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected separator (;)
$loadPageTranslations: (locale: string, routeName: string) => Promise<void>;

Check failure on line 256 in src/runtime/01.plugin.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected separator (;)
}

declare module '#app' {
// eslint-disable-next-line @typescript-eslint/no-empty-object-type
interface NuxtApp extends PluginsInjections {}
}

// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-expect-error
declare module 'nuxt/dist/app/nuxt' {
// eslint-disable-next-line @typescript-eslint/no-empty-object-type
interface NuxtApp extends PluginsInjections {}
}

declare module '@vue/runtime-core' {
// eslint-disable-next-line @typescript-eslint/no-empty-object-type
interface ComponentCustomProperties extends PluginsInjections {}
}

0 comments on commit 60a2c49

Please sign in to comment.