|
| 1 | +// Type definitions for pouchdb-authentication 1.0 |
| 2 | +// Project: https://pouchdb.com/ |
| 3 | +// Definitions by: Didier Villevalois <ptitjes@free.fr> |
| 4 | +// Definitions: https://github.com/pouchdb-community/pouchdb-authentication |
| 5 | +// TypeScript Version: 2.3 |
| 6 | + |
| 7 | +/// <reference types="pouchdb-core" /> |
| 8 | + |
| 9 | +// TODO: Fixing this lint error will require a large refactor |
| 10 | +/* tslint:disable:no-single-declare-module */ |
| 11 | + |
| 12 | +declare namespace PouchDB { |
| 13 | + namespace Authentication { |
| 14 | + |
| 15 | + interface UserContext { |
| 16 | + name: string; |
| 17 | + roles?: string[]; |
| 18 | + } |
| 19 | + |
| 20 | + interface User extends UserContext { |
| 21 | + } |
| 22 | + |
| 23 | + interface LoginResponse extends Core.BasicResponse, UserContext { |
| 24 | + } |
| 25 | + |
| 26 | + interface SessionResponse extends Core.BasicResponse { |
| 27 | + info: { |
| 28 | + authenticated: string; |
| 29 | + authentication_db: string; |
| 30 | + authentication_handlers: string[]; |
| 31 | + }; |
| 32 | + userCtx: UserContext; |
| 33 | + } |
| 34 | + |
| 35 | + interface PutUserOptions extends Core.Options { |
| 36 | + metadata?: any; |
| 37 | + roles?: string[]; |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + interface Database<Content extends {} = {}> { |
| 42 | + /** |
| 43 | + * Log in an existing user. |
| 44 | + * Throws an error if the user doesn't exist yet, the password is wrong, the HTTP server is unreachable, or a meteor struck your computer. |
| 45 | + */ |
| 46 | + logIn(username: string, password: string, |
| 47 | + callback: Core.Callback<Authentication.LoginResponse>): void; |
| 48 | + |
| 49 | + logIn(username: string, password: string, |
| 50 | + options: Core.Options, |
| 51 | + callback: Core.Callback<Authentication.LoginResponse>): void; |
| 52 | + |
| 53 | + logIn(username: string, password: string, |
| 54 | + options?: Core.Options): Promise<Authentication.LoginResponse>; |
| 55 | + |
| 56 | + /** |
| 57 | + * Logs out whichever user is currently logged in. |
| 58 | + * If nobody's logged in, it does nothing and just returns `{"ok" : true}`. |
| 59 | + */ |
| 60 | + logOut(callback: Core.Callback<Core.BasicResponse>): void; |
| 61 | + |
| 62 | + logOut(): Promise<Core.BasicResponse>; |
| 63 | + |
| 64 | + /** |
| 65 | + * Returns information about the current session. |
| 66 | + * In other words, this tells you which user is currently logged in. |
| 67 | + */ |
| 68 | + getSession(callback: Core.Callback<Authentication.SessionResponse>): void; |
| 69 | + |
| 70 | + getSession(): Promise<Authentication.SessionResponse>; |
| 71 | + |
| 72 | + /** |
| 73 | + * Sign up a new user who doesn't exist yet. |
| 74 | + * Throws an error if the user already exists or if the username is invalid, or if some network error occurred. |
| 75 | + * CouchDB has some limitations on user names (e.g. they cannot contain the character `:`). |
| 76 | + */ |
| 77 | + signUp(username: string, password: string, |
| 78 | + callback: Core.Callback<Core.Response>): void; |
| 79 | + |
| 80 | + signUp(username: string, password: string, |
| 81 | + options: Authentication.PutUserOptions, |
| 82 | + callback: Core.Callback<Core.Response>): void; |
| 83 | + |
| 84 | + signUp(username: string, password: string, |
| 85 | + options?: Authentication.PutUserOptions): Promise<Core.Response>; |
| 86 | + |
| 87 | + /** |
| 88 | + * Returns the user document associated with a username. |
| 89 | + * (CouchDB, in a pleasing show of consistency, stores users as JSON documents in the special `_users` database.) |
| 90 | + * This is the primary way to get metadata about a user. |
| 91 | + */ |
| 92 | + getUser(username: string, |
| 93 | + callback: Core.Callback<Core.Document<Content & Authentication.User> & Core.GetMeta>): void; |
| 94 | + |
| 95 | + getUser(username: string, |
| 96 | + options: PouchDB.Core.Options, |
| 97 | + callback: Core.Callback<Core.Document<Content & Authentication.User> & Core.GetMeta>): void; |
| 98 | + |
| 99 | + getUser(username: string, |
| 100 | + options?: PouchDB.Core.Options): Promise<Core.Document<Content & Authentication.User> & Core.GetMeta>; |
| 101 | + |
| 102 | + /** |
| 103 | + * Update the metadata of a user. |
| 104 | + */ |
| 105 | + putUser(username: string, |
| 106 | + callback: Core.Callback<Core.Response>): void; |
| 107 | + |
| 108 | + putUser(username: string, options: Authentication.PutUserOptions, |
| 109 | + callback: Core.Callback<Core.Response>): void; |
| 110 | + |
| 111 | + putUser(username: string, options?: Authentication.PutUserOptions): Promise<Core.Response>; |
| 112 | + |
| 113 | + /** |
| 114 | + * Delete a user. |
| 115 | + */ |
| 116 | + deleteUser(username: string, |
| 117 | + callback: Core.Callback<Core.Response>): void; |
| 118 | + |
| 119 | + deleteUser(username: string, |
| 120 | + options: Core.Options, |
| 121 | + callback: Core.Callback<Core.Response>): void; |
| 122 | + |
| 123 | + deleteUser(username: string, |
| 124 | + options?: Core.Options): Promise<Core.Response>; |
| 125 | + |
| 126 | + /** |
| 127 | + * Set new `password` for user `username`. |
| 128 | + */ |
| 129 | + changePassword(username: string, password: string, |
| 130 | + callback: Core.Callback<Core.Response>): void; |
| 131 | + |
| 132 | + changePassword(username: string, password: string, |
| 133 | + options: Core.Options, |
| 134 | + callback: Core.Callback<Core.Response>): void; |
| 135 | + |
| 136 | + changePassword(username: string, password: string, |
| 137 | + options?: Core.Options): Promise<Core.Response>; |
| 138 | + |
| 139 | + /** |
| 140 | + * Renames `oldUsername` to `newUsername`. |
| 141 | + */ |
| 142 | + changeUsername(oldUsername: string, newUsername: string, |
| 143 | + callback: Core.Callback<Core.Response>): void; |
| 144 | + |
| 145 | + changeUsername(oldUsername: string, newUsername: string, |
| 146 | + options: Core.Options, |
| 147 | + callback: Core.Callback<Core.Response>): void; |
| 148 | + |
| 149 | + changeUsername(oldUsername: string, newUsername: string, |
| 150 | + options?: Core.Options): Promise<Core.Response>; |
| 151 | + |
| 152 | + /** |
| 153 | + * Sign up a new admin. |
| 154 | + */ |
| 155 | + signUpAdmin(username: string, password: string, |
| 156 | + callback: Core.Callback<string>): void; |
| 157 | + |
| 158 | + signUpAdmin(username: string, password: string, |
| 159 | + options: Authentication.PutUserOptions, |
| 160 | + callback: Core.Callback<string>): void; |
| 161 | + |
| 162 | + signUpAdmin(username: string, password: string, |
| 163 | + options?: Authentication.PutUserOptions): Promise<string>; |
| 164 | + |
| 165 | + /** |
| 166 | + * Delete an admin. |
| 167 | + */ |
| 168 | + deleteAdmin(username: string, |
| 169 | + callback: Core.Callback<string>): void; |
| 170 | + |
| 171 | + deleteAdmin(username: string, options: Core.Options, |
| 172 | + callback: Core.Callback<string>): void; |
| 173 | + |
| 174 | + deleteAdmin(username: string, options?: Core.Options): Promise<string>; |
| 175 | + } |
| 176 | +} |
| 177 | + |
| 178 | +declare module 'pouchdb-authentication' { |
| 179 | + const plugin: PouchDB.Plugin; |
| 180 | + export = plugin; |
| 181 | +} |
0 commit comments