Merge pull request #30726 from LinusU/simple-oauth2-generics

[simple-oauth2] Fix typing of authorizeURL params (dynamic name)
This commit is contained in:
Mine Starks
2018-11-28 07:33:18 -08:00
committed by GitHub
2 changed files with 19 additions and 6 deletions
+7 -6
View File
@@ -3,13 +3,14 @@
// Definitions by: Michael Müller <https://github.com/mad-mike>,
// Troy Lamerton <https://github.com/troy-lamerton>
// Martín Rodriguez <https://github.com/netux>
// Linus Unnebäck <https://github.com/LinusU>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
// TypeScript Version: 2.9
/** Creates a new simple-oauth2 client with the passed configuration */
export function create(options: ModuleOptions): OAuthClient;
export function create<ClientIdName extends string = 'client_id'>(options: ModuleOptions<ClientIdName>): OAuthClient<ClientIdName>;
export interface ModuleOptions {
export interface ModuleOptions<ClientIdName extends string = 'client_id'> {
client: {
/** Service registered client id. Required. */
id: string,
@@ -18,7 +19,7 @@ export interface ModuleOptions {
/** Parameter name used to send the client secret. Default to client_secret. */
secretParamName?: string,
/** Parameter name used to send the client id. Default to client_id. */
idParamName?: string
idParamName?: ClientIdName
};
auth: {
/** String used to set the host to request the tokens to. Required. */
@@ -91,7 +92,7 @@ export interface ClientCredentialTokenConfig {
scope?: string | string[];
}
export interface OAuthClient {
export interface OAuthClient<ClientIdName extends string = 'client_id'> {
authorizationCode: {
/**
* Redirect the user to the autorization page
@@ -99,8 +100,8 @@ export interface OAuthClient {
*/
authorizeURL(
params?: {
/** A key-value pair where key is ModuleOptions#client.idParamName and the value represents the Client-ID */
[ idParamName: string ]: string | undefined
/** A string that represents the Client-ID */
[key in ClientIdName]?: string
} & {
/** A string that represents the registered application URI where the user is redirected after authentication */
redirect_uri?: string,
@@ -17,6 +17,12 @@ const credentials: oauth2lib.ModuleOptions = {
const oauth2 = oauth2lib.create(credentials);
// Test custom `idParamName`
{
const oauth2 = oauth2lib.create({ client: { id: 'x', secret: 'x', idParamName: 'foobar' }, auth: { tokenHost: 'x' } });
oauth2.authorizationCode.authorizeURL({ foobar: 'x' });
}
// #Authorization Code flow
(async () => {
// Authorization oauth2 URI
@@ -26,6 +32,12 @@ const oauth2 = oauth2lib.create(credentials);
state: '<state>'
});
oauth2.authorizationCode.authorizeURL({
redirect_uri: 'http://localhost:3000/callback',
scope: ['<scope1>', '<scope2>'],
state: '<state>'
});
// Redirect example using Express (see http://expressjs.com/api.html#res.redirect)
// res.redirect(authorizationUri);