Skip to content

Commit

Permalink
Convert remaining identifier javascript code to typescript
Browse files Browse the repository at this point in the history
Convert remaining identifier javascript code to typescript and add all possible type notations as installation packages.
  • Loading branch information
soun059 committed Aug 22, 2024
1 parent 5fabe20 commit 98bdfdb
Show file tree
Hide file tree
Showing 11 changed files with 104 additions and 64 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@ import {

import { handleAxiosError } from './utils';
import * as types from './types';
import { Dispatch } from 'redux';
import { AppDispatch, RootState } from '../store';

export function receiveError(error) {
export function receiveError(error: any) {

Check failure on line 16 in identifier/src/actions/common.ts

View workflow job for this annotation

GitHub Actions / build

Unexpected any. Specify a different type
return {
type: types.RECEIVE_ERROR,
error
Expand All @@ -24,7 +26,7 @@ export function resetHello() {
};
}

export function receiveHello(hello) {
export function receiveHello(hello: {success?: boolean, username: string, displayName?: string}) {
const { success, username, displayName } = hello;

return {
Expand All @@ -37,7 +39,7 @@ export function receiveHello(hello) {
}

export function executeHello() {
return function(dispatch, getState) {
return function(dispatch:Dispatch , getState: () => RootState) {
dispatch(resetHello());

const { flow, query } = getState().common;
Expand Down Expand Up @@ -78,7 +80,7 @@ export function executeHello() {
}

export function retryHello() {
return function(dispatch) {
return function(dispatch: AppDispatch) {
dispatch(receiveError(null));

return dispatch(executeHello());
Expand All @@ -91,15 +93,15 @@ export function requestLogoff() {
};
}

export function receiveLogoff(state) {
export function receiveLogoff(state: boolean) {
return {
type: types.RECEIVE_LOGOFF,
state
};
}

export function executeLogoff() {
return function(dispatch) {
return function(dispatch: AppDispatch) {
dispatch(resetHello());
dispatch(requestLogoff());

Expand Down
42 changes: 22 additions & 20 deletions identifier/src/actions/login.js → identifier/src/actions/login.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,35 +15,36 @@ import {
import * as types from './types';
import { receiveHello } from './common';
import { handleAxiosError } from './utils';
import { AppDispatch, PromiseDispatch, RootState } from '../store';

// Modes for logon.
export const ModeLogonUsernameEmptyPasswordCookie = '0';
export const ModeLogonUsernamePassword = '1';

export function updateInput(name, value) {
export function updateInput(name: string, value?: string | null) {
return {
type: types.UPDATE_INPUT,
name,
value
};
}

export function receiveValidateLogon(errors) {
export function receiveValidateLogon(errors: {[key: string]: any}) {

Check failure on line 32 in identifier/src/actions/login.ts

View workflow job for this annotation

GitHub Actions / build

Unexpected any. Specify a different type
return {
type: types.RECEIVE_VALIDATE_LOGON,
errors
};
}

export function requestLogon(username, password) {
export function requestLogon(username: string, password: string) {
return {
type: types.REQUEST_LOGON,
username,
password
};
}

export function receiveLogon(logon) {
export function receiveLogon(logon: {success: boolean, errors: {http: any}}) {

Check failure on line 47 in identifier/src/actions/login.ts

View workflow job for this annotation

GitHub Actions / build

Unexpected any. Specify a different type
const { success, errors } = logon;

return {
Expand All @@ -59,7 +60,7 @@ export function requestConsent(allow=false) {
};
}

export function receiveConsent(logon) {
export function receiveConsent(logon: {success: boolean, errors: {http: any}}) {

Check failure on line 63 in identifier/src/actions/login.ts

View workflow job for this annotation

GitHub Actions / build

Unexpected any. Specify a different type
const { success, errors } = logon;

return {
Expand All @@ -69,8 +70,8 @@ export function receiveConsent(logon) {
};
}

export function executeLogon(username, password, mode=ModeLogonUsernamePassword) {
return function(dispatch, getState) {
export function executeLogon(username: string, password: string, mode=ModeLogonUsernamePassword) {
return function(dispatch: AppDispatch, getState: () => RootState) {
dispatch(requestLogon(username, password));
dispatch(receiveHello({
username
Expand Down Expand Up @@ -151,7 +152,7 @@ export function executeLogon(username, password, mode=ModeLogonUsernamePassword)
}

export function executeConsent(allow=false, scope='') {
return function(dispatch, getState) {
return function(dispatch: AppDispatch, getState: () => RootState) {
dispatch(requestConsent(allow));

const { query } = getState().common;
Expand Down Expand Up @@ -205,10 +206,10 @@ export function executeConsent(allow=false, scope='') {
};
}

export function validateUsernamePassword(username, password, isSignedIn) {
return function(dispatch) {
export function validateUsernamePassword(username: string, password: string, isSignedIn: boolean) {
return function(dispatch: AppDispatch) {
return new Promise((resolve, reject) => {
const errors = {};
const errors:{[key: string]: any} = {};

Check failure on line 212 in identifier/src/actions/login.ts

View workflow job for this annotation

GitHub Actions / build

Unexpected any. Specify a different type

if (!username) {
errors.username = new Error(ERROR_LOGIN_VALIDATE_MISSINGUSERNAME);
Expand All @@ -227,14 +228,14 @@ export function validateUsernamePassword(username, password, isSignedIn) {
};
}

export function executeLogonIfFormValid(username, password, isSignedIn) {
return (dispatch) => {
export function executeLogonIfFormValid(username: string, password: string, isSignedIn: boolean) {
return (dispatch: PromiseDispatch) => {
return dispatch(
validateUsernamePassword(username, password, isSignedIn)
).then(() => {
const mode = isSignedIn ? ModeLogonUsernameEmptyPasswordCookie : ModeLogonUsernamePassword;
return dispatch(executeLogon(username, password, mode));
}).catch((errors) => {
}).catch((errors: any) => {

Check failure on line 238 in identifier/src/actions/login.ts

View workflow job for this annotation

GitHub Actions / build

Unexpected any. Specify a different type
return {
success: false,
errors: errors
Expand All @@ -243,8 +244,9 @@ export function executeLogonIfFormValid(username, password, isSignedIn) {
};
}

export function advanceLogonFlow(success, history, done=false, extraQuery={}) {
return (dispatch, getState) => {

export function advanceLogonFlow(success: boolean, history: any, done=false, extraQuery={}) {

Check failure on line 248 in identifier/src/actions/login.ts

View workflow job for this annotation

GitHub Actions / build

Unexpected any. Specify a different type
return (dispatch:AppDispatch, getState: () => RootState) => {
if (!success) {
return;
}
Expand All @@ -256,16 +258,16 @@ export function advanceLogonFlow(success, history, done=false, extraQuery={}) {
case 'oauth':
case 'consent':
case 'oidc':
if (hello.details.flow !== flow) {
if (hello?.details.flow !== flow) {
// Ignore requested flow if hello flow does not match.
break;
}

if (!done && hello.details.next === 'consent') {
if (!done && hello?.details.next === 'consent') {
history.replace(`/consent${history.location.search}${history.location.hash}`);
return;
}
if (hello.details.continue_uri) {
if (hello?.details.continue_uri) {
q.prompt = 'none';
window.location.replace(hello.details.continue_uri + '?' + queryString.stringify(q));
return;
Expand All @@ -276,7 +278,7 @@ export function advanceLogonFlow(success, history, done=false, extraQuery={}) {
default:
// Legacy stupid modes.
if (q.continue && q.continue.indexOf(document.location.origin) === 0) {
window.location.replace(q.continue);
window.location.replace(q.continue as string);
return;
}
}
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import { AxiosError } from 'axios';
import {
ExtendedError,
ERROR_HTTP_NETWORK_ERROR,
ERROR_HTTP_UNEXPECTED_RESPONSE_STATUS
} from '../errors';

export function handleAxiosError(error) {
if (error.request) {
export function handleAxiosError(error: AxiosError | ExtendedError) {
if ((error as AxiosError).request) {
// Axios errors.
if (error.response) {
error = new ExtendedError(ERROR_HTTP_UNEXPECTED_RESPONSE_STATUS, error.response);
if ((error as AxiosError).response) {
error = new ExtendedError(ERROR_HTTP_UNEXPECTED_RESPONSE_STATUS, (error as AxiosError).response);
} else {
error = new ExtendedError(ERROR_HTTP_NETWORK_ERROR);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
/* eslint react/prop-types: 0 */

import { TFunction } from 'i18next';
import { ComponentType } from 'react';
import { withTranslation } from 'react-i18next';

export const ERROR_LOGIN_VALIDATE_MISSINGUSERNAME = 'konnect.error.login.validate.missingUsername';
Expand All @@ -13,7 +15,7 @@ export const ERROR_HTTP_UNEXPECTED_RESPONSE_STATE = 'konnect.error.http.unexpect
export class ExtendedError extends Error {
values = undefined;

constructor(message, values) {
constructor(message: string, values: any) {

Check failure on line 18 in identifier/src/errors/index.ts

View workflow job for this annotation

GitHub Actions / build

Unexpected any. Specify a different type
super(message);
if (Error.captureStackTrace !== undefined) {
Error.captureStackTrace(this, ExtendedError);
Expand All @@ -23,7 +25,7 @@ export class ExtendedError extends Error {
}

// Component to translate error text with values.
function ErrorMessageComponent(props) {
function ErrorMessageComponent(props: { error?:any, t: TFunction, values: any }) {

Check failure on line 28 in identifier/src/errors/index.ts

View workflow job for this annotation

GitHub Actions / build

Unexpected any. Specify a different type

Check failure on line 28 in identifier/src/errors/index.ts

View workflow job for this annotation

GitHub Actions / build

Unexpected any. Specify a different type
const { error, t, values } = props;

if (!error) {
Expand Down Expand Up @@ -60,4 +62,4 @@ function ErrorMessageComponent(props) {
return f(messageDescriptor.defaultMessage, messageDescriptor.values);
}

export const ErrorMessage = withTranslation()(ErrorMessageComponent);
export const ErrorMessage = withTranslation()(ErrorMessageComponent as ComponentType<any>);
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
export function newHelloRequest(flow, query) {
const r = {};
export function newHelloRequest(flow: string, query: {scope?: string, client_id?: string, redirect_uri: string, id_token_hint?: string, max_age?: string,claims_scope?: string, prompt?: any }) {
const r:{[key: string]: string} = {};

if (query.prompt) {
// TODO(longsleep): Validate prompt values?
r.prompt = query.prompt;
}

let selectedFlow = flow;
let selectedFlow: string | null = flow;
switch (flow) {
case 'oauth':
case 'consent':
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,22 @@ const defaultPathPrefix = (() => {
return pathPrefix;
})();

const defaultState = {
type commonStateType = {
hello: {
state: any,
username: string,
displayName: string,
details: any
} | null,
branding: string | null,
error: any,
flow: string | (string | null)[],
query: queryString.ParsedQuery<string>,
pathPrefix: string,
updateAvailable: boolean
}

const defaultState:commonStateType = {
hello: null,
branding: null,
error: null,
Expand All @@ -30,7 +45,7 @@ const defaultState = {
pathPrefix: defaultPathPrefix
};

function commonReducer(state = defaultState, action) {
function commonReducer(state = defaultState, action: {type: string, error: any, state: any, username: string, displayName: string, hello: any}) {
switch (action.type) {
case RECEIVE_ERROR:
return Object.assign({}, state, {
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,20 @@ import {
UPDATE_INPUT
} from '../actions/types';

function loginReducer(state = {

type loginState = {
loading: string,
username: string,
password: string,
errors: {[key: string] : string}
}

function loginReducer(state:loginState = {
loading: '',
username: '',
password: '',
errors: {}
}, action) {
}, action: {errors: any, type: string, success?: boolean, name: string, value: any}) {
switch (action.type) {
case RECEIVE_VALIDATE_LOGON:
return Object.assign({}, state, {
Expand Down
24 changes: 0 additions & 24 deletions identifier/src/store.js

This file was deleted.

34 changes: 34 additions & 0 deletions identifier/src/store.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { createStore, applyMiddleware, compose, Middleware, AnyAction, Action, Dispatch } from 'redux';
import thunkMiddleware, { ThunkAction, ThunkDispatch, ThunkMiddleware } from 'redux-thunk';
import { createLogger } from 'redux-logger';

import rootReducer from './reducers';

declare global {
interface Window {
__REDUX_DEVTOOLS_EXTENSION_COMPOSE__?: typeof compose;
}
}

const middlewares: (Middleware<{}, any, any> | (ThunkMiddleware<any, AnyAction, undefined> & { withExtraArgument<ExtraThunkArg, State = any, BasicAction extends Action<any> = AnyAction>(extraArgument: ExtraThunkArg): ThunkMiddleware<any>; }))[] = [
thunkMiddleware
];

if (process.env.NODE_ENV !== 'development') { // eslint-disable-line no-undef
middlewares.push(createLogger()); // must be last middleware in the chain.
}

const composeEnhancers = (window['__REDUX_DEVTOOLS_EXTENSION_COMPOSE__'] as typeof compose) || compose || compose;

const store = createStore(
rootReducer,
composeEnhancers(applyMiddleware(
...middlewares,
))
);

export type RootState = ReturnType<typeof store.getState>
export type AppDispatch = typeof store.dispatch;
export type PromiseDispatch = ThunkDispatch<RootState, any, Action>;

export default store;

0 comments on commit 98bdfdb

Please sign in to comment.