Skip to content

Commit

Permalink
fix(typing): fix misleading authorizer typing
Browse files Browse the repository at this point in the history
  • Loading branch information
kamalbennani-aircall committed Nov 29, 2023
1 parent 3fc566e commit 648d563
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 26 deletions.
19 changes: 8 additions & 11 deletions src/core/auth/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,28 +51,25 @@ export type AuthTransportCallback =
| ChannelAuthorizationCallback
| UserAuthenticationCallback;

export interface AuthOptionsT<AuthHandler> {

export interface InternalAuthOptions {
transport: 'ajax' | 'jsonp';
endpoint: string;
params?: any;
headers?: any;
paramsProvider?: () => any;
headersProvider?: () => any;
customHandler?: AuthHandler;
}

export type CustomAuthOptions<AuthHandler> = {
customHandler: AuthHandler;
}

export type AuthOptionsT<AuthHandler> = InternalAuthOptions | CustomAuthOptions<AuthHandler>

export declare type UserAuthenticationOptions = AuthOptionsT<
UserAuthenticationHandler
>;
export declare type ChannelAuthorizationOptions = AuthOptionsT<
ChannelAuthorizationHandler
>;

export interface InternalAuthOptions {
transport: 'ajax' | 'jsonp';
endpoint: string;
params?: any;
headers?: any;
paramsProvider?: () => any;
headersProvider?: () => any;
}
37 changes: 22 additions & 15 deletions src/core/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ import Defaults from './defaults';
import {
ChannelAuthorizationHandler,
UserAuthenticationHandler,
ChannelAuthorizationOptions
ChannelAuthorizationOptions,
AuthOptionsT,
CustomAuthOptions
} from './auth/options';
import UserAuthenticator from './auth/user_authenticator';
import ChannelAuthorizer from './auth/channel_authorizer';
Expand Down Expand Up @@ -131,15 +133,17 @@ function getEnableStatsConfig(opts: Options): boolean {
return false;
}

const hasCustomHandler = <T>(auth: AuthOptionsT<T>): auth is CustomAuthOptions<T> => {
return 'customHandler' in auth && auth['customHandler'] != null;
}

function buildUserAuthenticator(opts: Options): UserAuthenticationHandler {
const userAuthentication = {
...Defaults.userAuthentication,
...opts.userAuthentication
};
if (
'customHandler' in userAuthentication &&
userAuthentication['customHandler'] != null
) {

if (hasCustomHandler(userAuthentication)) {
return userAuthentication['customHandler'];
}

Expand All @@ -158,17 +162,22 @@ function buildChannelAuth(opts: Options, pusher): ChannelAuthorizationOptions {
transport: opts.authTransport || Defaults.authTransport,
endpoint: opts.authEndpoint || Defaults.authEndpoint
};

if ('auth' in opts) {
if ('params' in opts.auth) channelAuthorization.params = opts.auth.params;
if ('headers' in opts.auth)
channelAuthorization.headers = opts.auth.headers;
}
if ('authorizer' in opts)
channelAuthorization.customHandler = ChannelAuthorizerProxy(
pusher,
channelAuthorization,
opts.authorizer
);

if ('authorizer' in opts) {
return {
customHandler: ChannelAuthorizerProxy(
pusher,
channelAuthorization,
opts.authorizer
)
}
}
}
return channelAuthorization;
}
Expand All @@ -178,10 +187,8 @@ function buildChannelAuthorizer(
pusher
): ChannelAuthorizationHandler {
const channelAuthorization = buildChannelAuth(opts, pusher);
if (
'customHandler' in channelAuthorization &&
channelAuthorization['customHandler'] != null
) {

if (hasCustomHandler(channelAuthorization)) {
return channelAuthorization['customHandler'];
}

Expand Down

0 comments on commit 648d563

Please sign in to comment.