Skip to content

Commit 34f3a96

Browse files
dinoboffddo
authored andcommitted
Add Typescripts type declaration (#66)
1 parent fd441c8 commit 34f3a96

File tree

2 files changed

+193
-0
lines changed

2 files changed

+193
-0
lines changed

oauth-1.0a.d.ts

+192
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,192 @@
1+
// Type definitions for oauth-1.0a v2.2.3
2+
// Project: oauth1.0a
3+
// Definitions by: Ddo <http://ddo.me>
4+
5+
export as namespace OAuth;
6+
export = OAuth;
7+
8+
declare class OAuth {
9+
10+
body_hash_function: OAuth.BodyHashFunction;
11+
consumer: OAuth.Consumer;
12+
hash_function: OAuth.HashFunction;
13+
last_ampersand: boolean;
14+
nonce_length: number;
15+
parameter_seperator: string;
16+
realm: string;
17+
signature_metho: string;
18+
version: string;
19+
20+
constructor(opts?: OAuth.Options);
21+
22+
/**
23+
* Sign a request.
24+
*/
25+
authorize(request: OAuth.RequestOptions, token?: OAuth.Token): OAuth.Authorization;
26+
27+
/**
28+
* Generate the oauth signature (i.e. oauth_signature).
29+
*/
30+
getSignature(request: OAuth.RequestOptions, token_secret: string | undefined, oauth_data: OAuth.Data): string;
31+
32+
/**
33+
* Generate the body signature (i.e. oauth_body_hash).
34+
*/
35+
getBodyHash(request: OAuth.RequestOptions, token_secret: string | undefined): string;
36+
37+
/**
38+
* Encode the request attributes.
39+
*
40+
* Base String = "<Method>&<Base Url>&<ParameterString>"
41+
*/
42+
getBaseString(request: OAuth.RequestOptions, oauth_data: OAuth.Data): string;
43+
44+
/**
45+
* Encode the oauth data and the request parameter,
46+
*/
47+
getParameterString(request: OAuth.RequestOptions, oauth_data: OAuth.Data): string;
48+
49+
/**
50+
* Generate the signing key.
51+
*
52+
* Key = "<Consumer Key>&<Token Key or an empty string>"
53+
*/
54+
getSigningKey(token_secret: string | undefined): string;
55+
56+
/**
57+
* Return the the URL without its querystring.
58+
*/
59+
getBaseUrl(url: string): string;
60+
61+
/**
62+
* Parse querystring / form data.
63+
*/
64+
deParam(str: string): OAuth.Param;
65+
66+
/**
67+
* Parse querystring from an url
68+
*/
69+
deParamUrl(url: string): OAuth.Param;
70+
71+
/**
72+
* Form data encoding.
73+
*/
74+
percentEncode(str: string): string;
75+
76+
/**
77+
* Convert OAuth authorization data to an http header.
78+
*/
79+
toHeader(data: OAuth.Authorization): OAuth.Header;
80+
81+
/**
82+
* Generate a random nonce.
83+
*/
84+
getNonce(): string;
85+
86+
/**
87+
* Generate a current timestamp in second.
88+
*/
89+
getTimeStamp(): number;
90+
91+
/**
92+
* Merge two object into a new one.
93+
*/
94+
mergeObject<T, U>(obj1: T, obj2: U): T & U;
95+
96+
/**
97+
* Sort an object properties by keys.
98+
*/
99+
sortObject<O extends {[k: string]: any}, K extends string>(obj: O): Array<{key: keyof O, value: O[K]}>;
100+
}
101+
102+
declare namespace OAuth {
103+
104+
/**
105+
* OAuth data, including the signature.
106+
*/
107+
export interface Authorization extends Data {
108+
oauth_signature: string;
109+
}
110+
111+
/**
112+
* Method used to generate the body hash.
113+
*
114+
* Note: the key is used for implementation HMAC algorithms for the body hash,
115+
* but typically it should return SHA1 hash of base_string.
116+
*/
117+
export type BodyHashFunction = (base_string, key) => string;
118+
119+
/**
120+
* OAuth key/secret pair.
121+
*/
122+
export interface Consumer {
123+
key: string;
124+
secret: string;
125+
}
126+
127+
/**
128+
* OAuth data, excluding the signature.
129+
*/
130+
export interface Data {
131+
oauth_consumer_key: string;
132+
oauth_nonce: string;
133+
oauth_signature_method: string;
134+
oauth_timestamp: number;
135+
oauth_version: string;
136+
oauth_token?: string;
137+
oauth_body_hash?: string;
138+
}
139+
140+
/**
141+
* Method used to hash the the OAuth and form/querystring data.
142+
*/
143+
export type HashFunction = (base_string, key) => string;
144+
145+
/**
146+
* Authorization header.
147+
*/
148+
export interface Header {
149+
Authorization: string;
150+
}
151+
152+
/**
153+
* OAuth options.
154+
*/
155+
export interface Options {
156+
body_hash_function?: BodyHashFunction;
157+
consumer: Consumer;
158+
hash_function?: HashFunction;
159+
last_ampersand?: boolean;
160+
nonce_length?: number;
161+
parameter_seperator?: string;
162+
realm: string;
163+
signature_method?: string;
164+
version?: string;
165+
}
166+
167+
/**
168+
* Extra data.
169+
*/
170+
export interface Param {
171+
[key: string]: string | string[];
172+
}
173+
174+
/**
175+
* Request options.
176+
*/
177+
export interface RequestOptions {
178+
url: string;
179+
method: string;
180+
data?: any;
181+
includeBodyHash?: boolean;
182+
}
183+
184+
/**
185+
* OAuth token key/secret pair.
186+
*/
187+
export interface Token {
188+
key: string;
189+
secret: string;
190+
}
191+
192+
}

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
"test": "make test"
77
},
88
"main": "oauth-1.0a.js",
9+
"types": "oauth-1.0a.d.ts",
910
"repository": "https://github.com/ddo/oauth-1.0a.git",
1011
"keywords": [
1112
"oauth",

0 commit comments

Comments
 (0)