Skip to content

Commit e59ad40

Browse files
committed
feat(types): package typescript type declarations
1 parent 83860be commit e59ad40

File tree

7 files changed

+266
-0
lines changed

7 files changed

+266
-0
lines changed

.travis.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ env:
4545
- SAUCE_USERNAME=ptitjes
4646

4747
matrix:
48+
- COMMAND=test-types
49+
4850
- SERVER=couchdb:latest CLIENT=node COMMAND=test
4951
- SERVER=couchdb:latest CLIENT=phantom COMMAND=test
5052

package-lock.json

Lines changed: 21 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
"main": "lib/index.js",
3333
"jsnext:main": "lib/index.es.js",
3434
"module": "lib/index.es.js",
35+
"types": "types/index.d.ts",
3536
"repository": {
3637
"type": "git",
3738
"url": "git@github.com:pouchdb-community/pouchdb-authentication.git"
@@ -51,6 +52,7 @@
5152
"test-local": "CLIENT=local npm run test",
5253
"test-node": "CLIENT=node npm run test",
5354
"test-phantom": "CLIENT=phantom npm run test",
55+
"test-types": "tsc --noEmit -p types",
5456
"release": "standard-version"
5557
},
5658
"dependencies": {
@@ -63,6 +65,7 @@
6365
"url-parse": "1.2.0"
6466
},
6567
"devDependencies": {
68+
"@types/pouchdb-core": "^6.1.9",
6669
"add-cors-to-couchdb": "0.0.6",
6770
"brfs": "^1.4.3",
6871
"browserify": "^14.5.0",
@@ -91,6 +94,7 @@
9194
"rollup-plugin-node-resolve": "^3.0.0",
9295
"rollup-plugin-replace": "^2.0.0",
9396
"standard-version": "^4.2.0",
97+
"typescript": "^2.6.2",
9498
"uglify-js": "^3.1.9",
9599
"watchify": "^3.9.0"
96100
},

types/index.d.ts

Lines changed: 181 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,181 @@
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+
}

types/tsconfig.json

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
"compilerOptions": {
3+
"module": "commonjs",
4+
"lib": [
5+
"es6",
6+
"dom"
7+
],
8+
"noImplicitAny": true,
9+
"noImplicitThis": true,
10+
"strictNullChecks": true,
11+
"strictFunctionTypes": true,
12+
"baseUrl": "../",
13+
"moduleResolution": "node",
14+
"types": [],
15+
"noEmit": true,
16+
"forceConsistentCasingInFileNames": true
17+
},
18+
"files": [
19+
"index.d.ts",
20+
"type-tests.ts"
21+
]
22+
}

types/tslint.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"extends": "dtslint/dt.json",
3+
"rules": {
4+
"no-declare-current-package": false,
5+
"no-any-union": false
6+
}
7+
}

types/type-tests.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
function testAuthentication() {
2+
const db = new PouchDB<{ foo: number }>();
3+
4+
db.logIn('username', 'password');
5+
6+
db.logOut();
7+
8+
db.getSession();
9+
10+
db.signUp('username', 'password', {
11+
roles: ['role1', 'role2'], metadata: { anyStuff: 'whatever' },
12+
});
13+
14+
db.putUser('username', {
15+
roles: ['role1', 'role2'], metadata: { anyStuff: 'whatever' },
16+
});
17+
18+
db.getUser('username');
19+
20+
db.deleteUser('username');
21+
22+
db.changePassword('username', 'password');
23+
24+
db.changeUsername('oldUsername', 'newUsername');
25+
26+
db.signUpAdmin('username', 'password');
27+
28+
db.deleteAdmin('username');
29+
}

0 commit comments

Comments
 (0)