refactor: add types to action creators/invariant (#65)

* refactor: add types to `NavigationActions`

* refactor: add types to `invariant`

* refactor: add types to `getNavigationActionCreators`

* Update invariant.ts
This commit is contained in:
Ben Salili-James
2019-09-16 12:21:32 +01:00
parent 513d5d4d8a
commit 363c8b5d9f
4 changed files with 123 additions and 65 deletions

View File

@@ -1,46 +0,0 @@
// Action constants
export const BACK = 'Navigation/BACK';
export const INIT = 'Navigation/INIT';
export const NAVIGATE = 'Navigation/NAVIGATE';
export const SET_PARAMS = 'Navigation/SET_PARAMS';
// Action creators
export const back = (payload = {}) => ({
type: BACK,
key: payload.key,
immediate: payload.immediate,
});
export const init = (payload = {}) => {
const action = {
type: INIT,
};
if (payload.params) {
action.params = payload.params;
}
return action;
};
export const navigate = payload => {
const action = {
type: NAVIGATE,
routeName: payload.routeName,
};
if (payload.params) {
action.params = payload.params;
}
if (payload.action) {
action.action = payload.action;
}
if (payload.key) {
action.key = payload.key;
}
return action;
};
export const setParams = payload => ({
type: SET_PARAMS,
key: payload.key,
params: payload.params,
preserveFocus: true,
});

View File

@@ -0,0 +1,102 @@
export interface NavigationParams {
[key: string]: any;
}
export interface NavigationNavigateActionPayload {
routeName: string;
params?: NavigationParams;
// The action to run inside the sub-router
action?: NavigationNavigateAction;
key?: string;
}
export interface NavigationNavigateAction
extends NavigationNavigateActionPayload {
type: 'Navigation/NAVIGATE';
}
export interface NavigationBackActionPayload {
key?: string | null;
immediate?: boolean;
}
export interface NavigationBackAction extends NavigationBackActionPayload {
type: 'Navigation/BACK';
}
export interface NavigationInitActionPayload {
params?: NavigationParams;
}
export interface NavigationInitAction extends NavigationInitActionPayload {
type: 'Navigation/INIT';
}
export interface NavigationSetParamsActionPayload {
// The key of the route where the params should be set
key: string;
// The new params to merge into the existing route params
params?: NavigationParams;
}
export interface NavigationSetParamsAction
extends NavigationSetParamsActionPayload {
type: 'Navigation/SET_PARAMS';
preserveFocus: true;
}
// Action constants
export const BACK = 'Navigation/BACK';
export const INIT = 'Navigation/INIT';
export const NAVIGATE = 'Navigation/NAVIGATE';
export const SET_PARAMS = 'Navigation/SET_PARAMS';
// Action creators
export const back = (
payload: NavigationBackActionPayload = {}
): NavigationBackAction => ({
type: BACK,
key: payload.key,
immediate: payload.immediate,
});
export const init = (payload: NavigationInitActionPayload = {}) => {
const action: NavigationInitAction = {
type: INIT,
};
if (payload.params) {
action.params = payload.params;
}
return action;
};
export const navigate = (
payload: NavigationNavigateActionPayload
): NavigationNavigateAction => {
const action: NavigationNavigateAction = {
type: NAVIGATE,
routeName: payload.routeName,
};
if (payload.params) {
action.params = payload.params;
}
if (payload.action) {
action.action = payload.action;
}
if (payload.key) {
action.key = payload.key;
}
return action;
};
export const setParams = (
payload: NavigationSetParamsActionPayload
): NavigationSetParamsAction => ({
type: SET_PARAMS,
key: payload.key,
params: payload.params,
preserveFocus: true,
});

View File

@@ -1,9 +1,14 @@
import * as NavigationActions from '../NavigationActions';
import invariant from '../utils/invariant';
const getNavigationActionCreators = route => {
interface NavigationParams {
[key: string]: any;
}
// TODO: Type `route`
const getNavigationActionCreators = (route: any) => {
return {
goBack: key => {
goBack: (key?: string | null) => {
let actualizedKey = key;
if (key === undefined && route.key) {
invariant(typeof route.key === 'string', 'key should be a string');
@@ -11,7 +16,11 @@ const getNavigationActionCreators = route => {
}
return NavigationActions.back({ key: actualizedKey });
},
navigate: (navigateTo, params, action) => {
navigate: (
navigateTo: string | NavigationActions.NavigationNavigateActionPayload,
params?: NavigationParams,
action?: NavigationActions.NavigationNavigateAction
) => {
if (typeof navigateTo === 'string') {
return NavigationActions.navigate({
routeName: navigateTo,
@@ -33,7 +42,7 @@ const getNavigationActionCreators = route => {
);
return NavigationActions.navigate(navigateTo);
},
setParams: params => {
setParams: (params?: NavigationParams) => {
invariant(
route.key && typeof route.key === 'string',
'setParams cannot be called by root navigator'

View File

@@ -1,5 +1,3 @@
/* eslint-disable import/no-commonjs */
/**
* Use invariant() to assert state which your program assumes to be true.
*
@@ -11,33 +9,28 @@
* will remain to ensure logic does not differ in production.
*/
var validateFormat = function() {};
let validateFormat: (format?: string) => void = function() {};
if (process.env !== 'production') {
validateFormat = function(format) {
if (process.env.NODE_ENV !== 'production') {
validateFormat = function(format?: string) {
if (format === undefined) {
throw new Error('invariant requires an error message argument');
}
};
}
function invariant(condition, format, a, b, c, d, e, f) {
function invariant(condition: boolean, format?: string, ...args: any[]) {
validateFormat(format);
if (!condition) {
var error;
let error: Error & { framesToPop?: number };
if (format === undefined) {
error = new Error(
'Minified exception occurred; use the non-minified dev environment for the full error message and additional helpful warnings.'
);
} else {
var args = [a, b, c, d, e, f];
var argIndex = 0;
error = new Error(
format.replace(/%s/g, function() {
return args[argIndex++];
})
);
let argIndex = 0;
error = new Error(format.replace(/%s/g, () => args[argIndex++]));
error.name = 'Invariant Violation';
}
@@ -46,4 +39,4 @@ function invariant(condition, format, a, b, c, d, e, f) {
}
}
module.exports = invariant;
export default invariant;