@nuxtjs/auth typings (#38132)

* Add nuxt auth module typings first draft

* Remove js-cookie dependency

* Fix lint for nuxt/auth

* Fix types reference
This commit is contained in:
Ruskin 2019-09-24 23:59:53 +01:00 committed by Ben Lichtman
parent 2455292097
commit 0ebd4b8fc9
5 changed files with 99 additions and 0 deletions

57
types/nuxtjs__auth/index.d.ts vendored Normal file
View File

@ -0,0 +1,57 @@
// Type definitions for @nuxtjs/auth 4.8
// Project: https://auth.nuxtjs.org
// Definitions by: Ruskin Constant <https://github.com/jonnyparris>
// Daniel Leal <https://github.com/danielgek>
// Nick Bolles <https://github.com/NickBolles>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
// TypeScript Version: 3.1
import Vue, { ComponentOptions } from 'vue';
export interface Storage {
setUniversal(key: string, value: any, isJson?: boolean): string;
getUniversal(key: string, isJson?: boolean): any;
syncUniversal(key: string, defaultValue: any, isJson?: boolean): any;
// Local State
setState(key: string, val: any): string;
getState(key: string): string;
watchState(key: string, handler: (newValue: any) => void): any;
// Cookies
setCookie(key: string, val: any, options?: object): any;
getCookie(key: string, isJson?: boolean): any;
// Local Storage
setLocalStorage(key: string, val: any, isJson?: boolean): any;
getLocalStorage(key: string, isJson?: boolean): any;
}
export interface Auth<T = any> {
ctx: any;
$state: any;
$storage: Storage;
user: Partial<T>;
loggedIn: boolean;
loginWith(strategyName: string, ...args: any): Promise<never>;
login(...args: any): Promise<never>;
logout(): Promise<never>;
fetchUser(): Promise<never>;
fetchUserOnce(): Promise<never>;
hasScope(scopeName: string): boolean;
setToken(strategyName: string, token?: string): string;
syncToken(strategyName: string): string;
onError(handler: (error: Error, name: string, endpoint: any) => void): any;
setUser(user?: Partial<T>): any;
reset(): Promise<never>;
redirect(name: string): any;
}
declare module 'vue/types/options' {
interface ComponentOptions<V extends Vue> {
auth?: boolean | string;
}
}
declare module 'vue/types/vue' {
interface Vue {
$auth: Auth;
}
}

View File

@ -0,0 +1,16 @@
import Vue from 'vue';
new Vue({
el: '#app',
methods: {
logout(): void {
this.$auth.logout();
},
},
template: `
Logout
<a @click="logout">
Logout
</a>
`,
});

View File

@ -0,0 +1,6 @@
{
"private": true,
"dependencies": {
"vue": "^2.6.10"
}
}

View File

@ -0,0 +1,19 @@
{
"compilerOptions": {
"module": "commonjs",
"lib": ["es6", "dom"],
"noImplicitAny": true,
"noImplicitThis": true,
"strictFunctionTypes": true,
"strictNullChecks": true,
"baseUrl": "../",
"typeRoots": ["../"],
"types": [],
"noEmit": true,
"forceConsistentCasingInFileNames": true,
"paths": {
"@nuxtjs/auth": ["nuxtjs__auth"]
}
},
"files": ["index.d.ts", "nuxtjs__auth-tests.ts"]
}

View File

@ -0,0 +1 @@
{ "extends": "dtslint/dt.json" }