-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(types): Add TypeScript support for reduxful (#9)
* Add basic typescript starter files * Add basic typescript around reduxful package * Refactor Map types based on review * Refactor types based on review * Update RequestDescription method doc/type
- Loading branch information
1 parent
67d2059
commit e75420c
Showing
9 changed files
with
167 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import { makeFetchAdapter } from 'reduxful'; | ||
|
||
makeFetchAdapter(fetch, {}); | ||
|
||
export default makeFetchAdapter(fetch); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
declare module 'reduxful' { | ||
// fetchUtils | ||
export interface RequestAdapterOptions { | ||
method: string; | ||
url: string; | ||
headers: { [key: string]: string }; | ||
withCredentials: boolean; | ||
body: any; | ||
} | ||
|
||
export type RequestAdapter = (options: RequestAdapterOptions) => Promise<any>; | ||
|
||
export function makeFetchAdapter( | ||
fetcher: typeof fetch, | ||
defaultOptions?: Object | ||
): RequestAdapter; | ||
|
||
// utils | ||
export interface Resource { | ||
isLoaded?: boolean; | ||
isUpdating?: boolean; | ||
hasError?: boolean; | ||
} | ||
|
||
export function isLoaded(resource?: Resource): boolean; | ||
|
||
export function isUpdating(resource?: Resource): boolean; | ||
|
||
export function hasError(resource?: Resource): boolean; | ||
|
||
export function getResourceKey( | ||
reqName: string, | ||
params?: { [key: string]: string | number } | ||
): string; | ||
|
||
// reduxful | ||
export type TransformFn = (data: any, context?: { params?: Object }) => any; | ||
|
||
export type UrlTemplateFn = (getState: () => any) => string; | ||
|
||
export type OptionsFn<Options = Object> = (getState: () => any) => Options; | ||
|
||
export interface RequestDescription<Options = Object> { | ||
url: string | UrlTemplateFn; | ||
method?: string; | ||
resourceAlias?: string; | ||
resourceData?: any; | ||
dataTransform?: TransformFn; | ||
errorTransform?: TransformFn; | ||
repeatRequestDelay?: number; | ||
options?: Options | OptionsFn<Options>; | ||
} | ||
|
||
export interface ApiDescription { | ||
[key: string]: RequestDescription; | ||
} | ||
|
||
export interface ApiConfig<Options = Object> { | ||
requestAdapter?: RequestAdapter; | ||
options?: Options | OptionsFn<Options>; | ||
} | ||
|
||
export interface Action { | ||
type: string; | ||
payload: string; | ||
meta: { key: string }; | ||
error?: boolean; | ||
} | ||
|
||
export type ActionCreatorThunkFn = ( | ||
dispatch: () => any, | ||
getState: () => any | ||
) => Promise<Action>; | ||
|
||
export type ActionCreatorFn<Options = Object> = ( | ||
params: { [paramName: string]: any }, | ||
options?: Options | OptionsFn<Options> | ||
) => ActionCreatorThunkFn; | ||
|
||
export type ReducerFn<S = any> = (state: S, action: Object) => S; | ||
|
||
export type SelectorFn = (state: Object, params: Object) => Resource; | ||
|
||
export interface ReduxfulProps { | ||
actionCreators: { [key: string]: ActionCreatorFn }; | ||
actions: { [key: string]: ActionCreatorFn }; | ||
reducers: { [key: string]: ReducerFn }; | ||
reducerMap: { [key: string]: ReducerFn }; | ||
selectors: { [key: string]: SelectorFn }; | ||
} | ||
|
||
class Reduxful implements ReduxfulProps { | ||
public actionCreators: { [key: string]: ActionCreatorFn }; | ||
public actions: { [key: string]: ActionCreatorFn }; | ||
public reducers: { [key: string]: ReducerFn }; | ||
public reducerMap: { [key: string]: ReducerFn }; | ||
public selectors: { [key: string]: SelectorFn }; | ||
constructor(apiName: string, apiDesc: ApiDescription, apiConfig?: ApiConfig); | ||
} | ||
|
||
export function setupApi( | ||
apiName: string, | ||
apiDesc: ApiDescription, | ||
config?: ApiConfig | ||
): Reduxful; | ||
|
||
export default Reduxful; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import Reduxful, { setupApi } from 'reduxful'; | ||
import requestAdapter from './fetchUtils.test'; | ||
|
||
const apiDesc = { | ||
getDoodad: { | ||
url: 'http://api.my-service.com/doodads/:id', | ||
}, | ||
getDoodadList: { | ||
url: 'http://api.my-service.com/doodads', | ||
}, | ||
}; | ||
|
||
const apiConfig = { requestAdapter }; | ||
const doodadApi = new Reduxful('doodadApi', apiDesc, apiConfig); | ||
|
||
export const reduxFul = setupApi('test-name', {}); | ||
|
||
export default doodadApi; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { isLoaded, isUpdating, hasError, getResourceKey } from 'reduxful'; | ||
|
||
isLoaded(); | ||
isLoaded(null); | ||
isLoaded({ isLoaded: false }); | ||
|
||
isUpdating(); | ||
isUpdating(null); | ||
isUpdating({ isUpdating: false }); | ||
|
||
hasError(); | ||
hasError(null); | ||
hasError({ hasError: false }); | ||
|
||
const params = { | ||
id: 'user123', | ||
count: 777, | ||
}; | ||
|
||
getResourceKey('test', params); | ||
getResourceKey('test'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters