Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Export types from global TypeScript file. #310

Merged
merged 8 commits into from
Jan 26, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion __tests__/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@ jest.mock('../src/transaction-manager');
jest.mock('../src/utils');

import Auth0Client from '../src/Auth0Client';
import createAuth0Client from '../src/index';
import createAuth0Client, {
PopupConfigOptions,
GetTokenSilentlyOptions
} from '../src/index';
import { AuthenticationError } from '../src/errors';
import version from '../src/version';
import { DEFAULT_AUTHORIZE_TIMEOUT_IN_SECONDS } from '../src/constants';
Expand Down
16 changes: 15 additions & 1 deletion src/Auth0Client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,20 @@ import { AuthenticationError, GenericError } from './errors';
import * as ClientStorage from './storage';
import { DEFAULT_POPUP_CONFIG_OPTIONS } from './constants';
import version from './version';
import {
Auth0ClientOptions,
BaseLoginOptions,
AuthorizeOptions,
RedirectLoginOptions,
PopupLoginOptions,
PopupConfigOptions,
GetUserOptions,
GetIdTokenClaimsOptions,
RedirectLoginResult,
GetTokenSilentlyOptions,
GetTokenWithPopupOptions,
LogoutOptions
} from './global';

const lock = new Lock();
const GET_TOKEN_SILENTLY_LOCK_KEY = 'auth0.lock.getTokenSilently';
Expand Down Expand Up @@ -282,7 +296,7 @@ export default class Auth0Client {
* @param options
*/
public async getIdTokenClaims(
options: getIdTokenClaimsOptions = {
options: GetIdTokenClaimsOptions = {
audience: this.options.audience || 'default',
scope: this.options.scope || this.DEFAULT_SCOPE
}
Expand Down
2 changes: 2 additions & 0 deletions src/cache.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { IdToken } from './global';

interface CacheKeyData {
audience: string;
scope: string;
Expand Down
2 changes: 2 additions & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { PopupConfigOptions } from './global';

/**
* @ignore
*/
Expand Down
38 changes: 22 additions & 16 deletions src/global.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* @ignore
*/
interface BaseLoginOptions {
export interface BaseLoginOptions {
/**
* - `'page'`: displays the UI with a full page view
* - `'popup'`: displays the UI with a popup window
Expand Down Expand Up @@ -61,7 +61,7 @@ interface BaseLoginOptions {
[key: string]: any;
}

interface Auth0ClientOptions extends BaseLoginOptions {
export interface Auth0ClientOptions extends BaseLoginOptions {
/**
* Your Auth0 account domain such as `'example.auth0.com'`,
* `'example.eu.auth0.com'` or , `'example.mycompany.com'`
Expand Down Expand Up @@ -109,7 +109,7 @@ interface Auth0ClientOptions extends BaseLoginOptions {
/**
* @ignore
*/
interface AuthorizeOptions extends BaseLoginOptions {
export interface AuthorizeOptions extends BaseLoginOptions {
response_type: string;
response_mode: string;
redirect_uri: string;
Expand All @@ -120,7 +120,7 @@ interface AuthorizeOptions extends BaseLoginOptions {
code_challenge_method: string;
}

interface RedirectLoginOptions extends BaseLoginOptions {
export interface RedirectLoginOptions extends BaseLoginOptions {
/**
* The URL where Auth0 will redirect your browser to with
* the authentication result. It must be whitelisted in
Expand All @@ -138,24 +138,24 @@ interface RedirectLoginOptions extends BaseLoginOptions {
fragment?: string;
}

interface RedirectLoginResult {
export interface RedirectLoginResult {
/**
* State stored when the redirect request was made
*/
appState?: any;
}

interface PopupLoginOptions extends BaseLoginOptions {}
export interface PopupLoginOptions extends BaseLoginOptions {}

interface PopupConfigOptions {
export interface PopupConfigOptions {
/**
* The number of seconds to wait for a popup response before
* throwing a timeout error. Defaults to 60s
*/
timeoutInSeconds?: number;
}

interface GetUserOptions {
export interface GetUserOptions {
/**
* The scope that was used in the authentication request
*/
Expand All @@ -166,7 +166,7 @@ interface GetUserOptions {
audience: string;
}

interface getIdTokenClaimsOptions {
export interface GetIdTokenClaimsOptions {
/**
* The scope that was used in the authentication request
*/
Expand All @@ -177,7 +177,13 @@ interface getIdTokenClaimsOptions {
audience: string;
}

interface GetTokenSilentlyOptions {
/*
* TODO: Remove this on the next major
*/
export type getIdTokenClaimsOptions = GetIdTokenClaimsOptions;

export interface GetTokenSilentlyOptions {

/**
* When `true`, ignores the cache and always sends a
* request to Auth0.
Expand Down Expand Up @@ -210,9 +216,9 @@ interface GetTokenSilentlyOptions {
*/
[key: string]: any;
}
interface GetTokenWithPopupOptions extends PopupLoginOptions {}
export interface GetTokenWithPopupOptions extends PopupLoginOptions {}
stevehobbsdev marked this conversation as resolved.
Show resolved Hide resolved

interface LogoutOptions {
export interface LogoutOptions {
/**
* The URL where Auth0 will redirect your browser to after the logout.
*
Expand Down Expand Up @@ -242,7 +248,7 @@ interface LogoutOptions {
/**
* @ignore
*/
interface AuthenticationResult {
export interface AuthenticationResult {
state: string;
code?: string;
error?: string;
Expand All @@ -258,7 +264,7 @@ interface TokenEndpointOptions {
/**
* @ignore
*/
interface OAuthTokenOptions extends TokenEndpointOptions {
export interface OAuthTokenOptions extends TokenEndpointOptions {
code_verifier: string;
code: string;
}
Expand All @@ -273,7 +279,7 @@ interface RefreshTokenOptions extends TokenEndpointOptions {
/**
* @ignore
*/
interface JWTVerifyOptions {
export interface JWTVerifyOptions {
iss: string;
aud: string;
id_token: string;
Expand All @@ -285,7 +291,7 @@ interface JWTVerifyOptions {
/**
* @ignore
*/
interface IdToken {
export interface IdToken {
__raw: string;
name?: string;
given_name?: string;
Expand Down
9 changes: 7 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,16 @@ import 'core-js/es/string/includes';
import 'promise-polyfill/src/polyfill';
import 'fast-text-encoding';

// @ts-ignore
import Auth0Client from './Auth0Client';
import * as ClientStorage from './storage';

//this is necessary to export the type definitions used in this file
import { Auth0ClientOptions } from './global';
import './global';

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there any reason why it's left there? It looks like you can delete this line.

Copy link
Contributor Author

@maxswa maxswa Dec 22, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is to prevent breaking changes for people that are using the types when they are exposed globally by

import createAuth0Client from '@auth0/auth0-spa-js';

Edit: See comment below

Copy link
Contributor Author

@maxswa maxswa Dec 31, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually after looking into this more, it looks like it actually is introducing breaking changes as the types are no longer ambiently declared after being exported. I'm looking into a fix for this now

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@maxswa What is the best way to reproduce the failure with the type export that you're seeing?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@maxswa Did you manage to look into this?

Copy link
Contributor Author

@maxswa maxswa Jan 17, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I import createAuth0Client from '@auth0/auth0-spa-js'; with my local version of the lib, I cannot use the Auth0 TypeScript types without explicitly importing them. I have not been able to find a solution that lets us export all of the types while also ambiently declaring them. Would this be an acceptable breaking change for this library? It would only effect TypeScript projects and the migration would be very straightforward.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@maxswa I'm not sure that will at least make it any worse than it is now. Let me carve out some time on Monday to play with this but I don't think the types can be picked up ambiently at the moment anyway. Right now people have to import the types, but doing so by importing the specific type definition file rather than just importing from @auth0/auth0-spa-js.

Copy link
Contributor

@stevehobbsdev stevehobbsdev Jan 17, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm playing with it now and the only problem I can see so far is that VS Code wants to auto-resolve the Auth0Client type to @auth0/auth0-spa-js/dist/typings/Auth0Client, but if I manually import it from the correct package, i.e.:

import createAuth0Client, { Auth0Client } from "@auth0/auth0-spa-js";

It seems to work fine.


import { validateCrypto, getUniqueScopes } from './utils';

export * from './global';

export default async function createAuth0Client(options: Auth0ClientOptions) {
validateCrypto();

Expand All @@ -38,3 +41,5 @@ export default async function createAuth0Client(options: Auth0ClientOptions) {

return auth0;
}

export type Auth0Client = Auth0Client;
1 change: 1 addition & 0 deletions src/jwt.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { urlDecodeB64 } from './utils';
import { IdToken, JWTVerifyOptions } from './global';

const isNumber = n => typeof n === 'number';

Expand Down
5 changes: 5 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import fetch from 'unfetch';

import { DEFAULT_AUTHORIZE_TIMEOUT_IN_SECONDS } from './constants';
import {
AuthenticationResult,
PopupConfigOptions,
OAuthTokenOptions
} from './global';

const dedupe = arr => arr.filter((x, i) => arr.indexOf(x) === i);

Expand Down