diff --git a/packages/client/src/actions/GetAction.ts b/packages/client/src/actions/GetAction.ts deleted file mode 100644 index 03d6d42..0000000 --- a/packages/client/src/actions/GetAction.ts +++ /dev/null @@ -1,6 +0,0 @@ -import {IAction} from './IAction' - -export type GetAction = - MyTypes extends IAction - ? MyTypes - : never diff --git a/packages/client/src/actions/GetPendingAction.ts b/packages/client/src/actions/GetPendingAction.ts deleted file mode 100644 index 6334b02..0000000 --- a/packages/client/src/actions/GetPendingAction.ts +++ /dev/null @@ -1,6 +0,0 @@ -import {IAsyncAction} from './IAsyncAction' - -export type GetPendingAction = - MyTypes extends IAsyncAction & {status: 'pending'} - ? MyTypes - : never diff --git a/packages/client/src/actions/GetResolvedAction.ts b/packages/client/src/actions/GetResolvedAction.ts deleted file mode 100644 index 8e5b5ea..0000000 --- a/packages/client/src/actions/GetResolvedAction.ts +++ /dev/null @@ -1,6 +0,0 @@ -import {IAsyncAction} from './IAsyncAction' - -export type GetResolvedAction = - MyTypes extends IAsyncAction & {status: 'resolved'} - ? MyTypes - : never diff --git a/packages/client/src/actions/IAsyncAction.ts b/packages/client/src/actions/TAsyncAction.ts similarity index 67% rename from packages/client/src/actions/IAsyncAction.ts rename to packages/client/src/actions/TAsyncAction.ts index 4fece8b..2c4fea1 100644 --- a/packages/client/src/actions/IAsyncAction.ts +++ b/packages/client/src/actions/TAsyncAction.ts @@ -2,9 +2,9 @@ import {IPendingAction} from './IPendingAction' import {IResolvedAction} from './IResolvedAction' import {IRejectedAction} from './IRejectedAction' -export type IAsyncStatus = 'pending' | 'resolved' | 'rejected' +export type TAsyncStatus = 'pending' | 'resolved' | 'rejected' -export type IAsyncAction = +export type TAsyncAction = IPendingAction | IResolvedAction | IRejectedAction diff --git a/packages/client/src/actions/TGetAction.ts b/packages/client/src/actions/TGetAction.ts new file mode 100644 index 0000000..31fb353 --- /dev/null +++ b/packages/client/src/actions/TGetAction.ts @@ -0,0 +1,6 @@ +import {IAction} from './IAction' + +export type TGetAction = + ActionTypes extends IAction + ? ActionTypes + : never diff --git a/packages/client/src/actions/TGetPendingAction.ts b/packages/client/src/actions/TGetPendingAction.ts new file mode 100644 index 0000000..5e5ac5c --- /dev/null +++ b/packages/client/src/actions/TGetPendingAction.ts @@ -0,0 +1,6 @@ +import {TAsyncAction} from './TAsyncAction' + +export type TGetPendingAction = + MyTypes extends TAsyncAction & {status: 'pending'} + ? MyTypes + : never diff --git a/packages/client/src/actions/TGetResolvedAction.ts b/packages/client/src/actions/TGetResolvedAction.ts new file mode 100644 index 0000000..9cb3e70 --- /dev/null +++ b/packages/client/src/actions/TGetResolvedAction.ts @@ -0,0 +1,6 @@ +import {TAsyncAction} from './TAsyncAction' + +export type TGetResolvedAction = + MyTypes extends TAsyncAction & {status: 'resolved'} + ? MyTypes + : never diff --git a/packages/client/src/actions/index.ts b/packages/client/src/actions/index.ts index 9e3b68b..ff3d921 100644 --- a/packages/client/src/actions/index.ts +++ b/packages/client/src/actions/index.ts @@ -1,9 +1,9 @@ -export * from './GetAction' -export * from './GetResolvedAction' -export * from './GetPendingAction' export * from './IAction' -export * from './IAsyncAction' export * from './IPendingAction' export * from './IRejectedAction' export * from './IResolvedAction' export * from './PendingAction' +export * from './TAsyncAction' +export * from './TGetAction' +export * from './TGetPendingAction' +export * from './TGetResolvedAction' diff --git a/packages/client/src/crud/CRUD.test.tsx b/packages/client/src/crud/CRUD.test.tsx index d223a2e..4edeb9e 100644 --- a/packages/client/src/crud/CRUD.test.tsx +++ b/packages/client/src/crud/CRUD.test.tsx @@ -1,9 +1,9 @@ import {createCRUDActions} from './CRUDActions' import React from 'react' import {AnyAction} from 'redux' -import {CRUDReducer, ICRUDMethod} from './' +import {CRUDReducer, TCRUDMethod} from './' import {HTTPClientMock, TestUtils, getError} from '../test-utils' -import {IMethod} from '@rondo/common' +import {TMethod} from '@rondo/common' import {IPendingAction} from '../actions' describe('CRUD', () => { @@ -100,7 +100,7 @@ describe('CRUD', () => { }) function dispatch( - method: ICRUDMethod, + method: TCRUDMethod, action: IPendingAction, ) { store.dispatch(action) @@ -109,13 +109,13 @@ describe('CRUD', () => { return action } - function getUrl(method: ICRUDMethod) { + function getUrl(method: TCRUDMethod) { return method === 'save' || method === 'findMany' ? '/one/1/two' : '/one/1/two/2' } - function getHTTPMethod(method: ICRUDMethod): IMethod { + function getHTTPMethod(method: TCRUDMethod): TMethod { switch (method) { case 'save': return 'post' @@ -131,7 +131,7 @@ describe('CRUD', () => { describe('Promise rejections', () => { const testCases: Array<{ - method: ICRUDMethod + method: TCRUDMethod params: any }> = [{ method: 'findOne', @@ -203,7 +203,7 @@ describe('CRUD', () => { const entity = {id: 100, name: 'test'} const testCases: Array<{ - method: ICRUDMethod + method: TCRUDMethod params: any body?: any response: any diff --git a/packages/client/src/crud/CRUDActions.ts b/packages/client/src/crud/CRUDActions.ts index eda55fa..30117ed 100644 --- a/packages/client/src/crud/CRUDActions.ts +++ b/packages/client/src/crud/CRUDActions.ts @@ -1,12 +1,10 @@ -import {ICRUDAction} from './ICRUDAction' -import {ICRUDMethod} from './ICRUDMethod' +import {TCRUDAction} from './TCRUDAction' +import {TCRUDMethod} from './TCRUDMethod' import {IHTTPClient, ITypedRequestParams} from '../http' -import {IRoutes, Filter, OnlyDefined} from '@rondo/common' +import {IRoutes, TFilter, TOnlyDefined} from '@rondo/common' -export type Optional = T extends {} ? T : undefined - -type Action = - Filter, {method: Method, status: 'pending'}> +type TAction = + TFilter, {method: Method, status: 'pending'}> export class SaveActionCreator< T extends IRoutes, @@ -20,10 +18,10 @@ export class SaveActionCreator< readonly type: ActionType, ) {} - save = (params: OnlyDefined<{ + save = (params: TOnlyDefined<{ body: T[Route]['post']['body'], params: T[Route]['post']['params'], - }>): Action => { + }>): TAction => { const p = params as any return { payload: this.http.post(this.route, p.body, p.params), @@ -46,10 +44,10 @@ export class FindOneActionCreator< readonly type: ActionType, ) {} - findOne = (params: OnlyDefined<{ - query: Optional, + findOne = (params: TOnlyDefined<{ + query: T[Route]['get']['query'], params: T[Route]['get']['params'], - }>): Action => { + }>): TAction => { const p = params as any return { payload: this.http.get(this.route, p.query, p.params), @@ -73,10 +71,10 @@ export class UpdateActionCreator< readonly type: ActionType, ) {} - update = (params: OnlyDefined<{ + update = (params: TOnlyDefined<{ body: T[Route]['put']['body'], params: T[Route]['put']['params'], - }>): Action => { + }>): TAction => { const p = params as any return { payload: this.http.put(this.route, p.body, p.params), @@ -100,10 +98,10 @@ export class RemoveActionCreator< readonly type: ActionType, ) {} - remove = (params: OnlyDefined<{ - body: Optional, + remove = (params: TOnlyDefined<{ + body: T[Route]['delete']['body'], params: T[Route]['delete']['params'], - }>): Action => { + }>): TAction => { const p = params as any return { payload: this.http.delete(this.route, p.body, p.params), @@ -126,8 +124,8 @@ export class FindManyActionCreator< readonly type: ActionType, ) {} - findMany = (params: OnlyDefined<{ - query: Optional, + findMany = (params: TOnlyDefined<{ + query: T[Route]['get']['query'], params: T[Route]['get']['params'], }>): { payload: Promise diff --git a/packages/client/src/crud/CRUDReducer.ts b/packages/client/src/crud/CRUDReducer.ts index 2cf9f8c..218530f 100644 --- a/packages/client/src/crud/CRUDReducer.ts +++ b/packages/client/src/crud/CRUDReducer.ts @@ -1,7 +1,7 @@ import {IAction, IResolvedAction} from '../actions' -import {ICRUDAction} from './ICRUDAction' -import {ICRUDMethod} from './ICRUDMethod' -import {indexBy, without, Filter} from '@rondo/common' +import {TCRUDAction} from './TCRUDAction' +import {TCRUDMethod} from './TCRUDMethod' +import {indexBy, without, TFilter} from '@rondo/common' export interface ICRUDEntity { readonly id: number @@ -65,7 +65,7 @@ export class CRUDReducer< handleRejected = ( state: ICRUDState, - method: ICRUDMethod, + method: TCRUDMethod, error: Error, ): ICRUDState => { return { @@ -82,7 +82,7 @@ export class CRUDReducer< handleLoading = ( state: ICRUDState, - method: ICRUDMethod, + method: TCRUDMethod, ): ICRUDState => { return { ...state, @@ -163,7 +163,7 @@ export class CRUDReducer< reduce = ( state: ICRUDState | undefined, - action: ICRUDAction, + action: TCRUDAction, ): ICRUDState => { const {defaultState} = this state = state || defaultState diff --git a/packages/client/src/crud/ICRUDAction.ts b/packages/client/src/crud/ICRUDAction.ts deleted file mode 100644 index 9b3a236..0000000 --- a/packages/client/src/crud/ICRUDAction.ts +++ /dev/null @@ -1,24 +0,0 @@ -import {IAsyncAction} from '../actions' -import {ICRUDMethod} from './ICRUDMethod' - -export type ICRUDSaveAction = - IAsyncAction & {method: Extract} - -export type ICRUDUpdateAction = - IAsyncAction & {method: Extract} - -export type ICRUDRemoveAction = - IAsyncAction & {method: Extract} - -export type ICRUDFindOneAction = - IAsyncAction & {method: Extract} - -export type ICRUDFindManyAction = - IAsyncAction & {method: Extract} - -export type ICRUDAction = - ICRUDSaveAction - | ICRUDUpdateAction - | ICRUDRemoveAction - | ICRUDFindOneAction - | ICRUDFindManyAction diff --git a/packages/client/src/crud/ICRUDMethod.ts b/packages/client/src/crud/ICRUDMethod.ts deleted file mode 100644 index fda0bcc..0000000 --- a/packages/client/src/crud/ICRUDMethod.ts +++ /dev/null @@ -1 +0,0 @@ -export type ICRUDMethod = 'save' | 'update' | 'findOne' | 'findMany' | 'remove' diff --git a/packages/client/src/crud/TCRUDAction.ts b/packages/client/src/crud/TCRUDAction.ts new file mode 100644 index 0000000..d782c3d --- /dev/null +++ b/packages/client/src/crud/TCRUDAction.ts @@ -0,0 +1,24 @@ +import {TAsyncAction} from '../actions' +import {TCRUDMethod} from './TCRUDMethod' + +export type TCRUDSaveAction = + TAsyncAction & {method: Extract} + +export type TCRUDUpdateAction = + TAsyncAction & {method: Extract} + +export type TCRUDRemoveAction = + TAsyncAction & {method: Extract} + +export type TCRUDFindOneAction = + TAsyncAction & {method: Extract} + +export type TCRUDFindManyAction = + TAsyncAction & {method: Extract} + +export type TCRUDAction = + TCRUDSaveAction + | TCRUDUpdateAction + | TCRUDRemoveAction + | TCRUDFindOneAction + | TCRUDFindManyAction diff --git a/packages/client/src/crud/TCRUDMethod.ts b/packages/client/src/crud/TCRUDMethod.ts new file mode 100644 index 0000000..167e9c4 --- /dev/null +++ b/packages/client/src/crud/TCRUDMethod.ts @@ -0,0 +1 @@ +export type TCRUDMethod = 'save' | 'update' | 'findOne' | 'findMany' | 'remove' diff --git a/packages/client/src/crud/index.ts b/packages/client/src/crud/index.ts index 7fac91d..00db918 100644 --- a/packages/client/src/crud/index.ts +++ b/packages/client/src/crud/index.ts @@ -1,4 +1,4 @@ export * from './CRUDActions' export * from './CRUDReducer' -export * from './ICRUDAction' -export * from './ICRUDMethod' +export * from './TCRUDAction' +export * from './TCRUDMethod' diff --git a/packages/client/src/crumbs/CrumbsActions.ts b/packages/client/src/crumbs/CrumbsActions.ts index 798d520..8e99e90 100644 --- a/packages/client/src/crumbs/CrumbsActions.ts +++ b/packages/client/src/crumbs/CrumbsActions.ts @@ -1,4 +1,4 @@ -import {GetAction, IAction} from '../actions' +import {TGetAction, IAction} from '../actions' export interface ICrumbLink { name: string @@ -10,10 +10,10 @@ export interface ICrumbs { current: string } -export type CrumbsActionType = +export type TCrumbsAction = IAction -type Action = GetAction +type Action = TGetAction export class CrumbsActions { setCrumbs(breadcrumbs: ICrumbs): Action<'BREADCRUMBS_SET'> { diff --git a/packages/client/src/crumbs/CrumbsConnector.tsx b/packages/client/src/crumbs/CrumbsConnector.tsx index c97359e..3445a88 100644 --- a/packages/client/src/crumbs/CrumbsConnector.tsx +++ b/packages/client/src/crumbs/CrumbsConnector.tsx @@ -1,12 +1,12 @@ import {Crumb} from './Crumb' -import {Connector, IStateSelector} from '../redux' +import {Connector, TStateSelector} from '../redux' import {ICrumbsState} from './CrumbsReducer' import {CrumbsActions} from './CrumbsActions' export class CrumbsConnector extends Connector { protected readonly breadcrumbsActions = new CrumbsActions() - connect(getLocalState: IStateSelector) { + connect(getLocalState: TStateSelector) { return this.wrap( getLocalState, state => state, diff --git a/packages/client/src/crumbs/CrumbsReducer.tsx b/packages/client/src/crumbs/CrumbsReducer.tsx index 1ff5a62..baa5436 100644 --- a/packages/client/src/crumbs/CrumbsReducer.tsx +++ b/packages/client/src/crumbs/CrumbsReducer.tsx @@ -1,4 +1,4 @@ -import {ICrumbs, CrumbsActionType} from './CrumbsActions' +import {ICrumbs, TCrumbsAction} from './CrumbsActions' export interface ICrumbsState extends ICrumbs { } @@ -8,7 +8,7 @@ const defaultState: ICrumbsState = { current: 'Home', } -export function Crumbs(state = defaultState, action: CrumbsActionType) +export function Crumbs(state = defaultState, action: TCrumbsAction) : ICrumbsState { switch (action.type) { case 'BREADCRUMBS_SET': diff --git a/packages/client/src/http/HTTPClient.ts b/packages/client/src/http/HTTPClient.ts index 1854f9b..9f32405 100644 --- a/packages/client/src/http/HTTPClient.ts +++ b/packages/client/src/http/HTTPClient.ts @@ -1,7 +1,7 @@ import axios from 'axios' import {IHTTPClient} from './IHTTPClient' import {IHeader} from './IHeader' -import {IMethod, IRoutes, URLFormatter} from '@rondo/common' +import {TMethod, IRoutes, URLFormatter} from '@rondo/common' import {IRequest} from './IRequest' import {IResponse} from './IResponse' import {ITypedRequestParams} from './ITypedRequestParams' @@ -31,7 +31,7 @@ export class HTTPClient implements IHTTPClient { async request< P extends keyof T & string, - M extends IMethod, + M extends TMethod, >(params: ITypedRequestParams): Promise { const url = this.formatter.format(params.path, params.params) diff --git a/packages/client/src/http/IHTTPClient.ts b/packages/client/src/http/IHTTPClient.ts index 4ba3c4e..d91803c 100644 --- a/packages/client/src/http/IHTTPClient.ts +++ b/packages/client/src/http/IHTTPClient.ts @@ -1,10 +1,10 @@ -import {IMethod, IRoutes} from '@rondo/common' +import {TMethod, IRoutes} from '@rondo/common' import {ITypedRequestParams} from './ITypedRequestParams' export interface IHTTPClient { request< P extends keyof T & string, - M extends IMethod, + M extends TMethod, >(params: ITypedRequestParams): Promise get

( diff --git a/packages/client/src/http/IRequest.ts b/packages/client/src/http/IRequest.ts index 9cfdf23..4953139 100644 --- a/packages/client/src/http/IRequest.ts +++ b/packages/client/src/http/IRequest.ts @@ -1,7 +1,7 @@ -import {IMethod} from '@rondo/common' +import {TMethod} from '@rondo/common' export interface IRequest { - method: IMethod, + method: TMethod, url: string, params?: {[key: string]: any}, data?: any, diff --git a/packages/client/src/http/ITypedRequestParams.ts b/packages/client/src/http/ITypedRequestParams.ts index e403ce1..4857f47 100644 --- a/packages/client/src/http/ITypedRequestParams.ts +++ b/packages/client/src/http/ITypedRequestParams.ts @@ -1,9 +1,9 @@ -import {IRoutes, IMethod} from '@rondo/common' +import {IRoutes, TMethod} from '@rondo/common' export interface ITypedRequestParams< T extends IRoutes, P extends keyof T & string, - M extends IMethod, + M extends TMethod, > { method: M, path: P, diff --git a/packages/client/src/login/LoginActions.ts b/packages/client/src/login/LoginActions.ts index 9e4e755..e2a6440 100644 --- a/packages/client/src/login/LoginActions.ts +++ b/packages/client/src/login/LoginActions.ts @@ -1,14 +1,14 @@ -import {GetAction, IAsyncAction, IAction, PendingAction} from '../actions' +import {TGetAction, TAsyncAction, IAction, PendingAction} from '../actions' import {IAPIDef, ICredentials, INewUser, IUser} from '@rondo/common' import {IHTTPClient} from '../http/IHTTPClient' -export type LoginActionType = - IAsyncAction - | IAsyncAction - | IAsyncAction +export type TLoginAction = + TAsyncAction + | TAsyncAction + | TAsyncAction | IAction<{redirectTo: string}, 'LOGIN_REDIRECT_SET'> -type Action = GetAction +type TAction = TGetAction export class LoginActions { constructor(protected readonly http: IHTTPClient) {} @@ -34,7 +34,7 @@ export class LoginActions { ) } - setRedirectTo = (redirectTo: string): Action<'LOGIN_REDIRECT_SET'> => { + setRedirectTo = (redirectTo: string): TAction<'LOGIN_REDIRECT_SET'> => { return { payload: {redirectTo}, type: 'LOGIN_REDIRECT_SET', diff --git a/packages/client/src/login/LoginConnector.tsx b/packages/client/src/login/LoginConnector.tsx index 6f6d404..3dd9fa2 100644 --- a/packages/client/src/login/LoginConnector.tsx +++ b/packages/client/src/login/LoginConnector.tsx @@ -1,7 +1,7 @@ import {Connector} from '../redux/Connector' import {ICredentials} from '@rondo/common' import {ILoginState} from './LoginReducer' -import {IStateSelector} from '../redux' +import {TStateSelector} from '../redux' import {LoginActions} from './LoginActions' import {LoginForm} from './LoginForm' import {bindActionCreators} from 'redux' @@ -18,7 +18,7 @@ export class LoginConnector extends Connector { super() } - connect(getLocalState: IStateSelector) { + connect(getLocalState: TStateSelector) { return this.wrap( getLocalState, state => ({ diff --git a/packages/client/src/login/LoginReducer.ts b/packages/client/src/login/LoginReducer.ts index 30cc038..4a5a32a 100644 --- a/packages/client/src/login/LoginReducer.ts +++ b/packages/client/src/login/LoginReducer.ts @@ -1,5 +1,5 @@ import {IUser} from '@rondo/common' -import {LoginActionType} from './LoginActions' +import {TLoginAction} from './LoginActions' export interface ILoginState { readonly error: string @@ -17,7 +17,7 @@ const defaultState: ILoginState = { export function Login( state = defaultState, - action: LoginActionType, + action: TLoginAction, ): ILoginState { switch (action.type) { // sync actions diff --git a/packages/client/src/login/RegisterConnector.tsx b/packages/client/src/login/RegisterConnector.tsx index d781f2f..6be48cc 100644 --- a/packages/client/src/login/RegisterConnector.tsx +++ b/packages/client/src/login/RegisterConnector.tsx @@ -1,7 +1,7 @@ import {Connector} from '../redux/Connector' import {INewUser} from '@rondo/common' import {ILoginState} from './LoginReducer' -import {IStateSelector} from '../redux' +import {TStateSelector} from '../redux' import {LoginActions} from './LoginActions' import {RegisterForm} from './RegisterForm' import {bindActionCreators} from 'redux' @@ -20,7 +20,7 @@ export class RegisterConnector extends Connector { super() } - connect(getLocalState: IStateSelector) { + connect(getLocalState: TStateSelector) { return this.wrap( getLocalState, state => ({ diff --git a/packages/client/src/redux/Connector.ts b/packages/client/src/redux/Connector.ts index ee27e1e..fe05f0c 100644 --- a/packages/client/src/redux/Connector.ts +++ b/packages/client/src/redux/Connector.ts @@ -1,4 +1,4 @@ -import {IStateSelector} from './IStateSelector' +import {TStateSelector} from './TStateSelector' import {connect, Omit} from 'react-redux' import {Dispatch} from 'redux' import {ComponentType} from 'react' @@ -30,7 +30,7 @@ export abstract class Connector { * https://stackoverflow.com/questions/54277411 */ abstract connect( - selectState: IStateSelector, + selectState: TStateSelector, ): ComponentType protected wrap< @@ -39,7 +39,7 @@ export abstract class Connector { StateProps extends Partial, DispatchProps extends Partial, >( - getLocalState: IStateSelector, + getLocalState: TStateSelector, mapStateToProps: (state: LocalState) => StateProps, mapDispatchToProps: (dispatch: Dispatch) => DispatchProps, Component: React.ComponentType, diff --git a/packages/client/src/redux/IStateSelector.ts b/packages/client/src/redux/TStateSelector.ts similarity index 62% rename from packages/client/src/redux/IStateSelector.ts rename to packages/client/src/redux/TStateSelector.ts index 54a095d..8a58ab4 100644 --- a/packages/client/src/redux/IStateSelector.ts +++ b/packages/client/src/redux/TStateSelector.ts @@ -1,5 +1,5 @@ /* * Select and return a part of the state */ -export type IStateSelector +export type TStateSelector = (state: GlobalState) => StateSlice diff --git a/packages/client/src/redux/index.ts b/packages/client/src/redux/index.ts index 60a3dee..1093b5a 100644 --- a/packages/client/src/redux/index.ts +++ b/packages/client/src/redux/index.ts @@ -1,2 +1,2 @@ export * from './Connector' -export * from './IStateSelector' +export * from './TStateSelector' diff --git a/packages/client/src/renderer/ClientRenderer.tsx b/packages/client/src/renderer/ClientRenderer.tsx index 886fb8a..ed3ade1 100644 --- a/packages/client/src/renderer/ClientRenderer.tsx +++ b/packages/client/src/renderer/ClientRenderer.tsx @@ -3,13 +3,13 @@ import ReactDOM from 'react-dom' import {Action} from 'redux' import {IClientConfig} from './IClientConfig' import {IRenderer} from './IRenderer' -import {IStoreFactory} from './IStoreFactory' +import {TStoreFactory} from './TStoreFactory' import {Provider} from 'react-redux' import {Router} from 'react-router-dom' import {createBrowserHistory} from 'history' export interface IClientRendererParams { - readonly createStore: IStoreFactory, + readonly createStore: TStoreFactory, readonly RootComponent: React.ComponentType<{config: IClientConfig}>, readonly target?: HTMLElement } diff --git a/packages/client/src/renderer/ServerRenderer.tsx b/packages/client/src/renderer/ServerRenderer.tsx index 259a0a9..c105d05 100644 --- a/packages/client/src/renderer/ServerRenderer.tsx +++ b/packages/client/src/renderer/ServerRenderer.tsx @@ -2,7 +2,7 @@ import React from 'react' import {Action} from 'redux' import {IClientConfig} from './IClientConfig' import {IRenderer} from './IRenderer' -import {IStoreFactory} from './IStoreFactory' +import {TStoreFactory} from './TStoreFactory' import {Provider} from 'react-redux' import {StaticRouterContext} from 'react-router' import {StaticRouter} from 'react-router-dom' @@ -10,7 +10,7 @@ import {renderToNodeStream} from 'react-dom/server' export class ServerRenderer implements IRenderer { constructor( - readonly createStore: IStoreFactory, + readonly createStore: TStoreFactory, readonly RootComponent: React.ComponentType<{config: IClientConfig}>, ) {} render(url: string, config: IClientConfig, state?: any) { diff --git a/packages/client/src/renderer/IStoreFactory.ts b/packages/client/src/renderer/TStoreFactory.ts similarity index 69% rename from packages/client/src/renderer/IStoreFactory.ts rename to packages/client/src/renderer/TStoreFactory.ts index d0f9e10..7588a9d 100644 --- a/packages/client/src/renderer/IStoreFactory.ts +++ b/packages/client/src/renderer/TStoreFactory.ts @@ -1,5 +1,5 @@ import {Action, Store} from 'redux' // TODO maybe Store should also be typed -export type IStoreFactory = +export type TStoreFactory = (state?: State) => Store diff --git a/packages/client/src/renderer/index.ts b/packages/client/src/renderer/index.ts index 5614ea8..c94a045 100644 --- a/packages/client/src/renderer/index.ts +++ b/packages/client/src/renderer/index.ts @@ -1,5 +1,5 @@ export * from './ClientRenderer' export * from './IClientConfig' export * from './IRenderer' -export * from './IStoreFactory' +export * from './TStoreFactory' export * from './isClientSide' diff --git a/packages/client/src/team/TeamActions.ts b/packages/client/src/team/TeamActions.ts index 77d8eb4..f2e7cb5 100644 --- a/packages/client/src/team/TeamActions.ts +++ b/packages/client/src/team/TeamActions.ts @@ -1,19 +1,19 @@ import {IAPIDef} from '@rondo/common' -import {GetPendingAction, IAsyncAction, PendingAction} from '../actions' +import {TGetPendingAction, TAsyncAction, PendingAction} from '../actions' import {IHTTPClient} from '../http/IHTTPClient' import {ITeam, IUser, IUserInTeam} from '@rondo/common' -export type TeamActionType = - IAsyncAction - | IAsyncAction - | IAsyncAction - | IAsyncAction<{id: number}, 'TEAM_REMOVE'> - | IAsyncAction - | IAsyncAction<{userId: number, teamId: number}, 'TEAM_USER_REMOVE'> - | IAsyncAction<{teamId: number, usersInTeam: IUserInTeam[]}, 'TEAM_USERS'> - | IAsyncAction +export type TTeamAction = + TAsyncAction + | TAsyncAction + | TAsyncAction + | TAsyncAction<{id: number}, 'TEAM_REMOVE'> + | TAsyncAction + | TAsyncAction<{userId: number, teamId: number}, 'TEAM_USER_REMOVE'> + | TAsyncAction<{teamId: number, usersInTeam: IUserInTeam[]}, 'TEAM_USERS'> + | TAsyncAction -type Action = GetPendingAction +type Action = TGetPendingAction export class TeamActions { constructor(protected readonly http: IHTTPClient) {} diff --git a/packages/client/src/team/TeamConnector.ts b/packages/client/src/team/TeamConnector.ts index 43577cf..527384a 100644 --- a/packages/client/src/team/TeamConnector.ts +++ b/packages/client/src/team/TeamConnector.ts @@ -1,5 +1,5 @@ import {Connector} from '../redux/Connector' -import {IStateSelector} from '../redux' +import {TStateSelector} from '../redux' import {ITeamState} from './TeamReducer' import {TeamActions} from './TeamActions' import {TeamManager} from './TeamManager' @@ -11,7 +11,7 @@ export class TeamConnector extends Connector { super() } - connect(getLocalState: IStateSelector) { + connect(getLocalState: TStateSelector) { const Component = this.wrap( getLocalState, state => ({ diff --git a/packages/client/src/team/TeamEditor.tsx b/packages/client/src/team/TeamEditor.tsx index 8fe5e18..683cbe6 100644 --- a/packages/client/src/team/TeamEditor.tsx +++ b/packages/client/src/team/TeamEditor.tsx @@ -4,7 +4,7 @@ import {ITeam} from '@rondo/common' import {TeamActions} from './TeamActions' import {FaPlusSquare, FaCheck, FaEdit} from 'react-icons/fa' -export type ITeamEditorProps = { +export type TTeamEditorProps = { type: 'add' onAddTeam: TeamActions['createTeam'] } | { @@ -20,8 +20,8 @@ export interface ITeamEditorState { } export class TeamEditor -extends React.PureComponent { - constructor(props: ITeamEditorProps) { +extends React.PureComponent { + constructor(props: TTeamEditorProps) { super(props) this.state = { error: '', @@ -31,7 +31,7 @@ extends React.PureComponent { getName(team?: ITeam) { return team ? team.name : '' } - componentWillReceiveProps(nextProps: ITeamEditorProps) { + componentWillReceiveProps(nextProps: TTeamEditorProps) { if (nextProps.type === 'update') { const {team} = nextProps if (team !== (this.props as any).team) { diff --git a/packages/client/src/team/TeamList.tsx b/packages/client/src/team/TeamList.tsx index e1e35d4..ab6f562 100644 --- a/packages/client/src/team/TeamList.tsx +++ b/packages/client/src/team/TeamList.tsx @@ -1,13 +1,13 @@ import React from 'react' import {Button, Panel, PanelHeading, PanelBlock} from 'bloomer' import {FaPlus, FaEdit, FaTimes} from 'react-icons/fa' -import {ITeam, ReadonlyRecord} from '@rondo/common' +import {ITeam, TReadonlyRecord} from '@rondo/common' import {Link} from 'react-router-dom' import {TeamActions} from './TeamActions' import {TeamEditor} from './TeamEditor' export interface ITeamListProps { - teamsById: ReadonlyRecord, + teamsById: TReadonlyRecord, teamIds: ReadonlyArray, onAddTeam: TeamActions['createTeam'] onRemoveTeam: TeamActions['removeTeam'] diff --git a/packages/client/src/team/TeamManager.tsx b/packages/client/src/team/TeamManager.tsx index b51befd..7bc63b7 100644 --- a/packages/client/src/team/TeamManager.tsx +++ b/packages/client/src/team/TeamManager.tsx @@ -1,6 +1,6 @@ import React from 'react' import {History, Location} from 'history' -import {ITeam, IUserInTeam, ReadonlyRecord} from '@rondo/common' +import {ITeam, IUserInTeam, TReadonlyRecord} from '@rondo/common' import {Panel, PanelBlock, PanelHeading} from 'bloomer' import {Route, Switch} from 'react-router-dom' import {TeamActions} from './TeamActions' @@ -24,11 +24,11 @@ export interface ITeamManagerProps { fetchUsersInTeam: TeamActions['fetchUsersInTeam'] findUserByEmail: TeamActions['findUserByEmail'] - teamsById: ReadonlyRecord + teamsById: TReadonlyRecord teamIds: ReadonlyArray - userKeysByTeamId: ReadonlyRecord> - usersByKey: ReadonlyRecord + userKeysByTeamId: TReadonlyRecord> + usersByKey: TReadonlyRecord } export class TeamManager extends React.PureComponent { diff --git a/packages/client/src/team/TeamReducer.ts b/packages/client/src/team/TeamReducer.ts index 15e1b95..0242460 100644 --- a/packages/client/src/team/TeamReducer.ts +++ b/packages/client/src/team/TeamReducer.ts @@ -1,17 +1,17 @@ import { - ITeam, IUserInTeam, ReadonlyRecord, indexBy, without, + ITeam, IUserInTeam, TReadonlyRecord, indexBy, without, } from '@rondo/common' -import {TeamActionType} from './TeamActions' -import {GetResolvedAction} from '../actions' +import {TTeamAction} from './TeamActions' +import {TGetResolvedAction} from '../actions' export interface ITeamState { readonly error: string readonly teamIds: ReadonlyArray - readonly teamsById: ReadonlyRecord + readonly teamsById: TReadonlyRecord - readonly userKeysByTeamId: ReadonlyRecord> - readonly usersByKey: ReadonlyRecord + readonly userKeysByTeamId: TReadonlyRecord> + readonly usersByKey: TReadonlyRecord } const defaultState: ITeamState = { @@ -26,7 +26,7 @@ const defaultState: ITeamState = { function removeUser( state: ITeamState, - action: GetResolvedAction, + action: TGetResolvedAction, ) { const {payload} = action @@ -52,7 +52,7 @@ function getUserKey(userInTeam: {userId: number, teamId: number}) { return `${userInTeam.teamId}_${userInTeam.userId}` } -export function Team(state = defaultState, action: TeamActionType): ITeamState { +export function Team(state = defaultState, action: TTeamAction): ITeamState { switch (action.status) { case 'pending': return state diff --git a/packages/client/src/team/TeamUserList.tsx b/packages/client/src/team/TeamUserList.tsx index 739a982..680c355 100644 --- a/packages/client/src/team/TeamUserList.tsx +++ b/packages/client/src/team/TeamUserList.tsx @@ -1,5 +1,5 @@ import React from 'react' -import {ITeam, IUser, IUserInTeam, ReadonlyRecord} from '@rondo/common' +import {ITeam, IUser, IUserInTeam, TReadonlyRecord} from '@rondo/common' import {TeamActions} from './TeamActions' import {FaUser, FaCheck, FaTimes} from 'react-icons/fa' @@ -18,8 +18,8 @@ export interface ITeamUsersProps { onRemoveUser: TeamActions['removeUser'] team: ITeam - userKeysByTeamId: ReadonlyRecord> - usersByKey: ReadonlyRecord + userKeysByTeamId: TReadonlyRecord> + usersByKey: TReadonlyRecord } export interface ITeamUserProps { diff --git a/packages/client/src/test-utils/TestUtils.tsx b/packages/client/src/test-utils/TestUtils.tsx index 3ff36cb..7d29c16 100644 --- a/packages/client/src/test-utils/TestUtils.tsx +++ b/packages/client/src/test-utils/TestUtils.tsx @@ -1,7 +1,7 @@ import React from 'react' import ReactDOM from 'react-dom' import T from 'react-dom/test-utils' -import {IStateSelector} from '../redux' +import {TStateSelector} from '../redux' import {Provider} from 'react-redux' import {createStore} from '../store' import { @@ -15,9 +15,9 @@ import { interface IRenderParams { reducers: ReducersMapObject - select: IStateSelector + select: TStateSelector // getComponent: ( - // select: IStateSelector) => React.ComponentType, + // select: TStateSelector) => React.ComponentType, // customJSX?: ( // Component: React.ComponentType, // props: Props, @@ -65,7 +65,7 @@ export class TestUtils { } const withComponent = ( - getComponent: (select: IStateSelector) => + getComponent: (select: TStateSelector) => React.ComponentType, ) => { const Component = getComponent(select) diff --git a/packages/common/src/IRoutes.ts b/packages/common/src/IRoutes.ts index 12d2789..5996f1e 100644 --- a/packages/common/src/IRoutes.ts +++ b/packages/common/src/IRoutes.ts @@ -1,4 +1,4 @@ -export type IMethod = 'get' +export type TMethod = 'get' | 'post' | 'put' | 'delete' diff --git a/packages/common/src/ReadonlyRecord.ts b/packages/common/src/ReadonlyRecord.ts deleted file mode 100644 index d07ce30..0000000 --- a/packages/common/src/ReadonlyRecord.ts +++ /dev/null @@ -1,2 +0,0 @@ -export type ReadonlyRecord = - Readonly> diff --git a/packages/common/src/index.ts b/packages/common/src/index.ts index c8387be..7e57cae 100644 --- a/packages/common/src/index.ts +++ b/packages/common/src/index.ts @@ -9,7 +9,6 @@ export * from './IUser' export * from './IUser' export * from './IUserInTeam' export * from './IUserTeam' -export * from './ReadonlyRecord' export * from './URLFormatter' export * from './indexBy' export * from './types' diff --git a/packages/common/src/types.ts b/packages/common/src/types.ts index e9f225c..ca1b41a 100644 --- a/packages/common/src/types.ts +++ b/packages/common/src/types.ts @@ -1,22 +1,25 @@ /** * transform unknown into undefined */ -export type Optional = T extends {} ? T : undefined +export type TOptional = T extends {} ? T : undefined -export type NonUndefinedPropertyNames = { +export type TNonUndefinedPropertyNames = { [K in keyof T]: T[K] extends undefined ? never: K }[keyof T] -export type OnlyRequired = Pick> +export type TOnlyRequired = Pick> -export type NonUnknownPropertyNames = { +export type TNonUnknownPropertyNames = { [K in keyof T]: T[K] extends {} ? K : never }[keyof T] -export type OnlyDefined = Pick> +export type TOnlyDefined = Pick> /** * Remove types from T that are not assignable to U * https://www.typescriptlang.org/docs/handbook/advanced-types.html */ -export type Filter = T extends U ? T : never +export type TFilter = T extends U ? T : never + +export type TReadonlyRecord = + Readonly> diff --git a/packages/server/src/database/TransactionManager.ts b/packages/server/src/database/TransactionManager.ts index 5078b03..bdc346d 100644 --- a/packages/server/src/database/TransactionManager.ts +++ b/packages/server/src/database/TransactionManager.ts @@ -8,12 +8,12 @@ import { Repository, } from 'typeorm' -export type IConnectionGetter = () => Connection +export type TConnectionGetter = () => Connection export class TransactionManager implements ITransactionManager { constructor( readonly ns: Namespace, - readonly getConnection: IConnectionGetter, + readonly getConnection: TConnectionGetter, ) {} getEntityManager = (): EntityManager => { diff --git a/packages/server/src/logger/LoggerFactory.ts b/packages/server/src/logger/LoggerFactory.ts index 6478f70..3d91dea 100644 --- a/packages/server/src/logger/LoggerFactory.ts +++ b/packages/server/src/logger/LoggerFactory.ts @@ -17,10 +17,10 @@ export function pad(text: string, n: number, trim: boolean) { return text } -export type ILogLevel = 'error' | 'warn' | 'info' | 'debug' | 'verbose' | 'off' +export type TLogLevel = 'error' | 'warn' | 'info' | 'debug' | 'verbose' | 'off' export interface IEnabledLoggers { - readonly [key: string]: ILogLevel + readonly [key: string]: TLogLevel } export interface IParams { @@ -48,9 +48,9 @@ export class LoggerFactory implements ILoggerFactory { } = {}) { const enabledLoggers = logs.split(',').reduce((logConfig, log) => { const [key, value] = log.split(':') - logConfig[key] = (value || 'info') as ILogLevel + logConfig[key] = (value || 'info') as TLogLevel return logConfig - }, {} as {[key: string]: ILogLevel}) + }, {} as {[key: string]: TLogLevel}) const params = opts.split(',').reduce((o, key) => { o[key] = true @@ -70,7 +70,7 @@ export class LoggerFactory implements ILoggerFactory { this.getCorrelationId = () => '' } - getLoggerLevel(name: string): ILogLevel { + getLoggerLevel(name: string): TLogLevel { const {enabledLoggers} = this.options const disabled = !!enabledLoggers['-' + name] if (disabled) { diff --git a/packages/server/src/middleware/Authenticator.ts b/packages/server/src/middleware/Authenticator.ts index 0f9a0c0..4575f08 100644 --- a/packages/server/src/middleware/Authenticator.ts +++ b/packages/server/src/middleware/Authenticator.ts @@ -1,13 +1,13 @@ import {Authenticator as A, Passport} from 'passport' import {IUserService} from '../services' import {Strategy as LocalStrategy} from 'passport-local' -import {IHandler} from './IHandler' +import {THandler} from './THandler' import {IMiddleware} from './IMiddleware' export class Authenticator implements IMiddleware { protected readonly passport: A - readonly handle: IHandler[] + readonly handle: THandler[] constructor(protected readonly userService: IUserService) { this.passport = new Passport() as any @@ -22,7 +22,7 @@ export class Authenticator implements IMiddleware { ] } - withLogInPromise: IHandler = (req, res, next) => { + withLogInPromise: THandler = (req, res, next) => { req.logInPromise = (user) => { return new Promise((resolve, reject) => { req.logIn(user, err => { @@ -72,7 +72,7 @@ export class Authenticator implements IMiddleware { .catch(done) } - authenticate(strategy: string | string[]): IHandler { + authenticate(strategy: string | string[]): THandler { return (req, res, next) => { return new Promise((resolve, reject) => { this.passport.authenticate(strategy, (err: Error, user, info) => { diff --git a/packages/server/src/middleware/CSRFMiddleware.ts b/packages/server/src/middleware/CSRFMiddleware.ts index dc2ec28..14c5b6e 100644 --- a/packages/server/src/middleware/CSRFMiddleware.ts +++ b/packages/server/src/middleware/CSRFMiddleware.ts @@ -1,5 +1,5 @@ import Csurf from 'csurf' -import {IHandler} from './IHandler' +import {THandler} from './THandler' import {IMiddleware} from './IMiddleware' import {UrlWithStringQuery} from 'url' @@ -9,7 +9,7 @@ export interface ICSRFParams { } export class CSRFMiddleware implements IMiddleware { - readonly handle: IHandler + readonly handle: THandler constructor(readonly params: ICSRFParams) { this.handle = Csurf({ diff --git a/packages/server/src/middleware/ErrorApiHandler.ts b/packages/server/src/middleware/ErrorApiHandler.ts index dc8a14d..2f47fc7 100644 --- a/packages/server/src/middleware/ErrorApiHandler.ts +++ b/packages/server/src/middleware/ErrorApiHandler.ts @@ -1,4 +1,4 @@ -import {IErrorHandler} from './IErrorHandler' +import {TErrorHandler} from './TErrorHandler' import {ILogger} from '../logger/ILogger' import {IMiddleware} from './IMiddleware' import {ValidationError} from '../validator' @@ -6,7 +6,7 @@ import {ValidationError} from '../validator' export class ErrorApiHandler implements IMiddleware { constructor(readonly logger: ILogger) {} - handle: IErrorHandler = (err, req, res, next) => { + handle: TErrorHandler = (err, req, res, next) => { this.logger.error('%s An API error occurred: %s', req.correlationId, err.stack) const statusCode = this.getStatus(err) diff --git a/packages/server/src/middleware/ErrorPageHandler.ts b/packages/server/src/middleware/ErrorPageHandler.ts index 4652171..7932d74 100644 --- a/packages/server/src/middleware/ErrorPageHandler.ts +++ b/packages/server/src/middleware/ErrorPageHandler.ts @@ -1,11 +1,11 @@ -import {IMiddleware} from './IMiddleware' -import {IErrorHandler} from './IErrorHandler' import {ILogger} from '../logger/ILogger' +import {IMiddleware} from './IMiddleware' +import {TErrorHandler} from './TErrorHandler' export class ErrorPageHandler implements IMiddleware { constructor(readonly logger: ILogger) {} - handle: IErrorHandler = (err, req, res, next) => { + handle: TErrorHandler = (err, req, res, next) => { this.logger.error( '%s An error occurred: %s', req.correlationId, err.stack) diff --git a/packages/server/src/middleware/IMiddleware.ts b/packages/server/src/middleware/IMiddleware.ts index 7a190ad..46400a5 100644 --- a/packages/server/src/middleware/IMiddleware.ts +++ b/packages/server/src/middleware/IMiddleware.ts @@ -1,6 +1,6 @@ -import {IHandler} from './IHandler' -import {IErrorHandler} from './IErrorHandler' +import {THandler} from './THandler' +import {TErrorHandler} from './TErrorHandler' export interface IMiddleware { - handle: IHandler | IHandler[] | IErrorHandler + handle: THandler | THandler[] | TErrorHandler } diff --git a/packages/server/src/middleware/RequestLogger.ts b/packages/server/src/middleware/RequestLogger.ts index 72623f6..673c996 100644 --- a/packages/server/src/middleware/RequestLogger.ts +++ b/packages/server/src/middleware/RequestLogger.ts @@ -1,4 +1,4 @@ -import {IHandler} from './IHandler' +import {THandler} from './THandler' import {ILogger} from '../logger/ILogger' import {IMiddleware} from './IMiddleware' import shortid from 'shortid' @@ -6,7 +6,7 @@ import shortid from 'shortid' export class RequestLogger implements IMiddleware { constructor(protected readonly logger: ILogger) {} - handle: IHandler = (req, res, next) => { + handle: THandler = (req, res, next) => { const start = Date.now() req.correlationId = shortid.generate() res.on('finish', () => { diff --git a/packages/server/src/middleware/SessionMiddleware.ts b/packages/server/src/middleware/SessionMiddleware.ts index b4b777f..3bb6531 100644 --- a/packages/server/src/middleware/SessionMiddleware.ts +++ b/packages/server/src/middleware/SessionMiddleware.ts @@ -1,5 +1,5 @@ import ExpressSession from 'express-session' -import {IHandler} from './IHandler' +import {THandler} from './THandler' import {IMiddleware} from './IMiddleware' import {ISession} from '../session/ISession' import {ITransactionManager} from '../database/ITransactionManager' @@ -15,7 +15,7 @@ export interface ISessionOptions { } export class SessionMiddleware implements IMiddleware { - readonly handle: IHandler + readonly handle: THandler constructor(readonly params: ISessionOptions) { this.handle = ExpressSession({ diff --git a/packages/server/src/middleware/IErrorHandler.ts b/packages/server/src/middleware/TErrorHandler.ts similarity index 82% rename from packages/server/src/middleware/IErrorHandler.ts rename to packages/server/src/middleware/TErrorHandler.ts index fffbaa0..32978b1 100644 --- a/packages/server/src/middleware/IErrorHandler.ts +++ b/packages/server/src/middleware/TErrorHandler.ts @@ -1,4 +1,4 @@ import {Request, Response, NextFunction} from 'express' -export type IErrorHandler = +export type TErrorHandler = (err: Error, req: Request, res: Response, next: NextFunction) => any diff --git a/packages/server/src/middleware/IHandler.ts b/packages/server/src/middleware/THandler.ts similarity index 53% rename from packages/server/src/middleware/IHandler.ts rename to packages/server/src/middleware/THandler.ts index db5c881..e7bd743 100644 --- a/packages/server/src/middleware/IHandler.ts +++ b/packages/server/src/middleware/THandler.ts @@ -1,3 +1,3 @@ import {Request, Response, NextFunction} from 'express' -export type IHandler = (req: Request, res: Response, next: NextFunction) => any +export type THandler = (req: Request, res: Response, next: NextFunction) => any diff --git a/packages/server/src/middleware/IPromiseHandler.ts b/packages/server/src/middleware/TPromiseHandler.ts similarity index 78% rename from packages/server/src/middleware/IPromiseHandler.ts rename to packages/server/src/middleware/TPromiseHandler.ts index 3b63798..43f52b3 100644 --- a/packages/server/src/middleware/IPromiseHandler.ts +++ b/packages/server/src/middleware/TPromiseHandler.ts @@ -1,4 +1,4 @@ import {Request, Response, NextFunction} from 'express' -export type IPromiseHandler = +export type TPromiseHandler = (req: Request, res: Response, next: NextFunction) => Promise diff --git a/packages/server/src/middleware/Transaction.ts b/packages/server/src/middleware/Transaction.ts index 6982e27..36d8518 100644 --- a/packages/server/src/middleware/Transaction.ts +++ b/packages/server/src/middleware/Transaction.ts @@ -1,6 +1,6 @@ import shortid from 'shortid' import {IMiddleware} from './IMiddleware' -import {IHandler} from './IHandler' +import {THandler} from './THandler' import {Namespace} from 'cls-hooked' export const CORRELATION_ID = 'CORRELATION_ID' @@ -8,7 +8,7 @@ export const CORRELATION_ID = 'CORRELATION_ID' export class Transaction implements IMiddleware { constructor(readonly ns: Namespace) {} - handle: IHandler = (req, res, next) => { + handle: THandler = (req, res, next) => { const {ns} = this ns.bindEmitter(req) ns.bindEmitter(res) diff --git a/packages/server/src/middleware/ensureLoggedIn.ts b/packages/server/src/middleware/ensureLoggedIn.ts index d6f15b9..9e7e999 100644 --- a/packages/server/src/middleware/ensureLoggedIn.ts +++ b/packages/server/src/middleware/ensureLoggedIn.ts @@ -1,10 +1,10 @@ import createError from 'http-errors' import {Request} from 'express' -import {IHandler} from './IHandler' +import {THandler} from './THandler' const isLoggedIn = (req: Request) => !!(req as any).user -export const ensureLoggedInApi: IHandler = (req, res, next) => { +export const ensureLoggedInApi: THandler = (req, res, next) => { if (!isLoggedIn(req)) { next(createError(401)) return @@ -12,7 +12,7 @@ export const ensureLoggedInApi: IHandler = (req, res, next) => { next() } -export const ensureLoggedInSite = (redirectTo: string): IHandler => { +export const ensureLoggedInSite = (redirectTo: string): THandler => { return function _ensureLoggedInSite(req, res, next) { if (!isLoggedIn(req)) { res.redirect(redirectTo) diff --git a/packages/server/src/middleware/handlePromise.ts b/packages/server/src/middleware/handlePromise.ts index f2a5e63..e3f4f56 100644 --- a/packages/server/src/middleware/handlePromise.ts +++ b/packages/server/src/middleware/handlePromise.ts @@ -1,7 +1,7 @@ -import {IHandler} from './IHandler' -import {IPromiseHandler} from './IPromiseHandler' +import {THandler} from './THandler' +import {TPromiseHandler} from './TPromiseHandler' -export function handlePromise(endpoint: IPromiseHandler): IHandler { +export function handlePromise(endpoint: TPromiseHandler): THandler { return (req, res, next) => { const promise = endpoint(req, res, next) promise diff --git a/packages/server/src/middleware/index.ts b/packages/server/src/middleware/index.ts index 61c5db5..cabfa71 100644 --- a/packages/server/src/middleware/index.ts +++ b/packages/server/src/middleware/index.ts @@ -1,13 +1,13 @@ export * from './Authenticator' export * from './CSRFMiddleware' -export * from './ensureLoggedIn' export * from './ErrorApiHandler' export * from './ErrorPageHandler' -export * from './handlePromise' -export * from './IErrorHandler' -export * from './IHandler' export * from './IMiddleware' -export * from './IPromiseHandler' export * from './RequestLogger' export * from './SessionMiddleware' +export * from './TErrorHandler' +export * from './THandler' +export * from './TPromiseHandler' export * from './Transaction' +export * from './ensureLoggedIn' +export * from './handlePromise' diff --git a/packages/server/src/router/AsyncRouter.ts b/packages/server/src/router/AsyncRouter.ts index c42b12c..02cf092 100644 --- a/packages/server/src/router/AsyncRouter.ts +++ b/packages/server/src/router/AsyncRouter.ts @@ -1,6 +1,6 @@ import express from 'express' -import {IRoutes, IMethod} from '@rondo/common' -import {ITypedHandler} from './ITypedHandler' +import {IRoutes, TMethod} from '@rondo/common' +import {TTypedHandler} from './TTypedHandler' export class AsyncRouter { readonly router: express.Router @@ -11,18 +11,18 @@ export class AsyncRouter { this.use = this.router.use.bind(this.router) as any } - protected addRoute( + protected addRoute( method: M, path: P, - handler: ITypedHandler, + handler: TTypedHandler, ) { const addRoute = this.router[method].bind(this.router as any) addRoute(path, this.wrapHandler(handler)) } - protected wrapHandler( - handler: ITypedHandler, + protected wrapHandler( + handler: TTypedHandler, ): express.RequestHandler { return (req, res, next) => { handler(req, res, next) @@ -35,43 +35,43 @@ export class AsyncRouter { get

( path: P, - handler: ITypedHandler, + handler: TTypedHandler, ) { this.addRoute('get', path, handler) } post

( path: P, - handler: ITypedHandler, + handler: TTypedHandler, ) { this.addRoute('post', path, handler) } put

( - path: P, handler: ITypedHandler, + path: P, handler: TTypedHandler, ) { this.addRoute('put', path, handler) } delete

( - path: P, handler: ITypedHandler) { + path: P, handler: TTypedHandler) { this.addRoute('delete', path, handler) } head

( path: P, - handler: ITypedHandler, + handler: TTypedHandler, ) { this.addRoute('head', path, handler) } options

( - path: P, handler: ITypedHandler) { + path: P, handler: TTypedHandler) { this.addRoute('options', path, handler) } patch

( - path: P, handler: ITypedHandler) { + path: P, handler: TTypedHandler) { this.addRoute('patch', path, handler) } diff --git a/packages/server/src/router/ITypedHandler.ts b/packages/server/src/router/TTypedHandler.ts similarity index 72% rename from packages/server/src/router/ITypedHandler.ts rename to packages/server/src/router/TTypedHandler.ts index 3c916e3..67402f6 100644 --- a/packages/server/src/router/ITypedHandler.ts +++ b/packages/server/src/router/TTypedHandler.ts @@ -1,11 +1,11 @@ import express from 'express' -import {IRoutes, IMethod} from '@rondo/common' +import {IRoutes, TMethod} from '@rondo/common' import {ITypedRequest} from './ITypedRequest' -export type ITypedHandler< +export type TTypedHandler< R extends IRoutes, P extends keyof R, - M extends IMethod + M extends TMethod > = ( req: ITypedRequest, res: express.Response, diff --git a/packages/server/src/router/TransactionalRouter.ts b/packages/server/src/router/TransactionalRouter.ts index 80ca18f..e80996a 100644 --- a/packages/server/src/router/TransactionalRouter.ts +++ b/packages/server/src/router/TransactionalRouter.ts @@ -1,16 +1,16 @@ import express from 'express' import {AsyncRouter} from './AsyncRouter' -import {IRoutes, IMethod} from '@rondo/common' +import {IRoutes, TMethod} from '@rondo/common' import {ITransactionManager} from '../database/ITransactionManager' -import {ITypedHandler} from './ITypedHandler' +import {TTypedHandler} from './TTypedHandler' export class TransactionalRouter extends AsyncRouter { constructor(readonly transactionManager: ITransactionManager) { super() } - protected wrapHandler( - handler: ITypedHandler, + protected wrapHandler( + handler: TTypedHandler, ): express.RequestHandler { return async (req, res, next) => { await this.transactionManager diff --git a/packages/server/src/router/index.ts b/packages/server/src/router/index.ts index 08791ae..ed89c82 100644 --- a/packages/server/src/router/index.ts +++ b/packages/server/src/router/index.ts @@ -1,4 +1,4 @@ export * from './AsyncRouter' -export * from './ITypedHandler' +export * from './TTypedHandler' export * from './ITypedRequest' export * from './TransactionalRouter' diff --git a/packages/server/src/routes/BaseRoute.ts b/packages/server/src/routes/BaseRoute.ts index ae4918e..c32f120 100644 --- a/packages/server/src/routes/BaseRoute.ts +++ b/packages/server/src/routes/BaseRoute.ts @@ -1,9 +1,9 @@ -import {IHandler} from '../middleware/IHandler' +import {THandler} from '../middleware/THandler' import {AsyncRouter} from '../router' import {IRoutes} from '@rondo/common' export abstract class BaseRoute { - readonly handle: IHandler + readonly handle: THandler constructor(protected readonly t: AsyncRouter) { this.handle = t.router diff --git a/packages/server/src/session/SessionStore.ts b/packages/server/src/session/SessionStore.ts index 51593d2..015f93d 100644 --- a/packages/server/src/session/SessionStore.ts +++ b/packages/server/src/session/SessionStore.ts @@ -9,11 +9,11 @@ type CallbackErr = (err?: any) => void export interface ISessionStoreOptions { readonly ttl: number readonly cleanup: number - readonly getRepository: IRepositoryFactory + readonly getRepository: TRepositoryFactory buildSession(sessionData: SessionData, session: ISession): S } -export type IRepositoryFactory = () => Repository +export type TRepositoryFactory = () => Repository // TODO casting as any because TypeScript complains. Looks like this is a // bug in TypeScript 3.2.2 @@ -22,7 +22,7 @@ export type IRepositoryFactory = () => Repository // https://github.com/Microsoft/TypeScript/issues/21592 export class SessionStore extends Store { - protected readonly getRepository: IRepositoryFactory + protected readonly getRepository: TRepositoryFactory constructor( protected readonly options: ISessionStoreOptions, diff --git a/packages/server/src/test-utils/RequestTester.ts b/packages/server/src/test-utils/RequestTester.ts index 82328e0..42832bf 100644 --- a/packages/server/src/test-utils/RequestTester.ts +++ b/packages/server/src/test-utils/RequestTester.ts @@ -1,6 +1,6 @@ import supertest from 'supertest' import { - IMethod, + TMethod, IRoutes, URLFormatter, } from '@rondo/common' @@ -13,7 +13,7 @@ interface ITest extends Omit, 'catch'> {} interface IResponse< R extends IRoutes, P extends keyof R, - M extends IMethod, + M extends TMethod, > extends supertest.Response { body: R[P][M]['response'] header: {[key: string]: string} @@ -22,7 +22,7 @@ interface IResponse< interface IRequest< R extends IRoutes, P extends keyof R, - M extends IMethod, + M extends TMethod, > extends ITest, Promise> { send(value: R[P][M]['body'] | string): this expect(status: number, body?: any): this @@ -37,7 +37,7 @@ interface IRequest< interface IRequestOptions< R extends IRoutes, P extends keyof R, - M extends IMethod, + M extends TMethod, > { params?: R[P][M]['params'], query?: R[P][M]['query'], @@ -62,7 +62,7 @@ export class RequestTester { return this } - request( + request( method: M, path: P, options: IRequestOptions = {}, ) : IRequest {