Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Networking improvements #31

Merged
merged 28 commits into from
Jun 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
234bd92
bump up node version
blazlew May 30, 2023
9eba1bc
improve networking typescript types & throw detailed http errors inst…
blazlew May 30, 2023
4090efb
Add auth token management
blazlew Jun 2, 2023
079a915
rename user slice to auth slice and unify with authSaga
blazlew Jun 2, 2023
04c7dcc
Android post RN upgrade small bug fixes
blazlew Jun 5, 2023
c2052d5
Add persistance for redux
blazlew Jun 5, 2023
a62b282
Store / rehydrate token from EncryptedStorage
blazlew Jun 6, 2023
2085b37
Add logout
blazlew Jun 8, 2023
82e42bf
Move login demo to login screen
blazlew Jun 9, 2023
f0f9d68
improve regydrate action type
blazlew Jun 9, 2023
9956be6
fix flipper related warnings in jest
blazlew Jun 12, 2023
aa1c07c
fix tests setup
blazlew Jun 12, 2023
751c73a
remove post rn upgrade leftover
blazlew Jun 12, 2023
e787b21
fix jest check flag
blazlew Jun 13, 2023
16cfe60
Add tests for common API functions
blazlew Jun 14, 2023
3eb1520
Add auth slice tests
blazlew Jun 14, 2023
7a34f05
Update readmes
blazlew Jun 14, 2023
e40e5aa
move remote data to @api directory
blazlew Jun 14, 2023
c3d5694
fix hasBody check
blazlew Jun 15, 2023
2bf2c36
reduce harshnes of scurity disclaimer
blazlew Jun 19, 2023
1bd2bcc
fix lint, by changing type cast form
blazlew Jun 19, 2023
8e127e6
mock flipper libraries instead of bloking it with 'isJest' flag
blazlew Jun 19, 2023
feb246c
remove isJest var
blazlew Jun 19, 2023
5686f38
replace eslint-disable line with variable prefixed with underscore fo…
blazlew Jun 19, 2023
0dd6bd1
add a comment explaing typography usage
blazlew Jun 19, 2023
12c5a7e
adjust root / app reducers names
blazlew Jun 19, 2023
779929e
fix root reducer's typescript
blazlew Jun 19, 2023
1985c02
Update README.md
szymonkoper Jun 21, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,20 @@ npx react-native init MyProject --template https://github.com/brains-and-beards/

Project inits with some demo usage files. You can browse them to see the proposed usage patterns. After init feel free to remove them:

- `src/__mocks__/fixtures`
- `src/api/mappers/comicMappers.ts`
- `src/api/types/comic.types.ts`
- `src/api/comics.ts`
- `src/components/inputs/DemoTextInput.tsx`
- `src/components/surfaces/DemoCard.tsx`
- `src/localization/pl`
- `src/model/ComicModel.ts`
- `src/screens/LoginScreen.tsx`
- `src/screens/DemoScreen.tsx`
- `src/screens/DemoScreen.test.tsx`
- `src/screens/TranslationsDemoScreen`
- `src/screens/demoSlice.ts`
- `src/screens/__tests__/demoSlice.test.ts`

## Security

Project contains basic code for auth token management. It stores sensitive information with [react-native-encrypted-storage](https://github.com/emeraldsanto/react-native-encrypted-storage#readme) library. Although it has been listed in [react-native's official docs](https://reactnative.dev/docs/security#:~:text=react%2Dnative%2Dencrypted%2Dstorage%20%2D%20uses%20Keychain%20on%20iOS%20and%20EncryptedSharedPreferences%20on%20Android.), it's a community-maintained package. **Use at your own risk** or replace it with your own implementation.
6 changes: 0 additions & 6 deletions template/.buckconfig

This file was deleted.

6 changes: 6 additions & 0 deletions template/.eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ module.exports = {
'prettier',
'jest',
'eslint-comments',
'@jambit/typed-redux-saga',
],
env: {
'react-native/react-native': true,
Expand Down Expand Up @@ -65,6 +66,11 @@ module.exports = {
// React-Native Plugin

'react-native/no-inline-styles': 1,

// Typed Redux Saga Plugin

'@jambit/typed-redux-saga/use-typed-effects': 'error', // ensures, that you import from typed-redux-saga instead of redux-saga/effects
'@jambit/typed-redux-saga/delegate-effects': 'error', // ensures, that you use yield* on effects from typed-redux-saga
},
settings: {
react: {
Expand Down
2 changes: 1 addition & 1 deletion template/.node-version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
16
18
1 change: 0 additions & 1 deletion template/.prettierrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ module.exports = {
'^@hoc/(.*)$',
'^@hooks/(.*)$',
'^@localization/(.*)$',
'^@models/(.*)$',
'^@navigation/(.*)$',
'^@redux/(.*)$',
'^@screens/(.*)$',
Expand Down
11 changes: 10 additions & 1 deletion template/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,19 @@

This app has been generated using [react-native-template-redbeard](https://github.com/brains-and-beards/react-native-template-redbeard). It includes preconfigured file structure and packages. It also inits with some demo usage files, feel free to remove these:

- `src/__mocks__/fixtures`
- `src/api/mappers/comicMappers.ts`
- `src/api/types/comic.types.ts`
- `src/api/comics.ts`
- `src/components/inputs/DemoTextInput.tsx`
- `src/components/surfaces/DemoCard.tsx`
- `src/localization/pl`
- `src/model/ComicModel.ts`
- `src/screens/LoginScreen.tsx`
- `src/screens/DemoScreen.tsx`
- `src/screens/DemoScreen.test.tsx`
- `src/screens/TranslationsDemoScreen`
- `src/screens/demoSlice.ts`
- `src/screens/__tests__/demoSlice.test.ts`

## File tree

Expand All @@ -28,6 +33,10 @@ This app has been generated using [react-native-template-redbeard](https://githu
- `screens/` - App screens
- `utils/` - Universal helpers

## Authentication

App uses preconfigured auth token management. It stores sensitive information via [react-native-encrypted-storage](https://github.com/emeraldsanto/react-native-encrypted-storage#readme) library.

## Environments

There are three different environments preconfigured with [react-native-config](https://github.com/luggit/react-native-config). Use `.env.[development|staging|production]` files to place things like `API_URL`s etc.
Expand Down
17 changes: 1 addition & 16 deletions template/android/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ react {
// The list of variants to that are debuggable. For those we're going to
// skip the bundling of the JS bundle and the assets. By default is just 'debug'.
// If you add flavors like lite, prod, etc. you'll have to list your debuggableVariants.
// debuggableVariants = ["liteDebug", "prodDebug"]
debuggableVariants = ["developmentDebug", "stagingDebug", "productionDebug"]
/* Bundling */
// A list containing the node command and its flags. Default is just 'node'.
// nodeExecutableAndArgs = ["node"]
Expand Down Expand Up @@ -62,12 +62,6 @@ react {
// hermesFlags = ["-O", "-output-source-map"]
}

project.ext.react = [
enableHermes: true, // clean and rebuild if changing
]

apply from: "../../node_modules/react-native/react.gradle"

/**
* Set this to true to create four separate APKs instead of one,
* one for each native architecture. This is useful if you don't
Expand All @@ -94,15 +88,6 @@ def enableProguardInReleaseBuilds = false
*/
def jscFlavor = 'org.webkit:android-jsc:+'

/**
* Whether to enable the Hermes VM.
*
* This should be set on project.ext.react and that value will be read here. If it is not set
* on project.ext.react, JavaScript will not be compiled to Hermes Bytecode
* and the benefits of using Hermes will therefore be sharply reduced.
*/
def enableHermes = project.ext.react.get("enableHermes", false);

/**
* Architectures to build native code for.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,26 +38,6 @@ protected ReactActivityDelegate createReactActivityDelegate() {
);
}

public static class MainActivityDelegate extends ReactActivityDelegate {
public MainActivityDelegate(ReactActivity activity, String mainComponentName) {
super(activity, mainComponentName);
}
@Override
protected ReactRootView createRootView() {
ReactRootView reactRootView = new ReactRootView(getContext());
// If you opted-in for the New Architecture, we enable the Fabric Renderer.
reactRootView.setIsFabric(BuildConfig.IS_NEW_ARCHITECTURE_ENABLED);
return reactRootView;
}

@Override
protected boolean isConcurrentRootEnabled() {
// If you opted-in for the New Architecture, we enable Concurrent Root (i.e. React 18).
// More on this on https://reactjs.org/blog/2022/03/29/react-v18.html
return BuildConfig.IS_NEW_ARCHITECTURE_ENABLED;
}
}

@Override
protected void onCreate(Bundle savedInstanceState) {
SplashScreen.show(this, R.style.SplashScreen_SecondTheme, true);
Expand Down
1 change: 0 additions & 1 deletion template/babel.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ module.exports = {
'@hoc': './src/hoc',
'@hooks': './src/hooks',
'@localization': './src/localization',
'@models': './src/models',
'@navigation': './src/navigation',
'@redux': './src/redux',
'@screens': './src/screens',
Expand Down
29 changes: 25 additions & 4 deletions template/jest.setup.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import { enableFetchMocks } from 'jest-fetch-mock'
import 'react-native-gesture-handler/jestSetup'
import { Middleware } from 'redux'

jest.useFakeTimers()

const customGlobal = global
customGlobal.fetch = require('jest-fetch-mock')
enableFetchMocks()

// react-native-config-node: read env variables from .env.example
process.env.NODE_ENV = 'example'
Expand Down Expand Up @@ -34,3 +33,25 @@ jest.mock('react-native-splash-screen', () => ({
show: jest.fn(),
hide: jest.fn(),
}))
jest.mock('react-native-encrypted-storage', () => ({
setItem: jest.fn(() => Promise.resolve(true)),
getItem: jest.fn(() => Promise.resolve('fake-stored-value')),
removeItem: jest.fn(() => Promise.resolve()),
clear: jest.fn(() => Promise.resolve()),
}))
jest.mock('redux-persist', () => {
const real = jest.requireActual('redux-persist')
return {
...real,
persistReducer: jest.fn().mockImplementation((_config, reducers) => reducers),
}
})
jest.mock('react-native-mmkv-flipper-plugin', () => ({
initializeMMKVFlipper: jest.fn(),
}))
jest.mock('redux-flipper', () => {
const fakeMiddleware: Middleware = _store => next => action => next(action)
return {
default: jest.fn(() => fakeMiddleware),
}
})
14 changes: 10 additions & 4 deletions template/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
"version": "0.0.1",
"private": true,
"scripts": {
"android:dev": "react-native run-android --variant=developmentdebug --appIdSuffix development",
"android:dev": "react-native run-android --variant=developmentDebug --appIdSuffix development",
"android:devRelease": "react-native run-android --variant=developmentrelease --appIdSuffix development",
"android:stg": "react-native run-android --variant=stagingdebug --appIdSuffix staging",
"android:stg": "react-native run-android --variant=stagingDebug --appIdSuffix staging",
"android:stgRelease": "react-native run-android --variant=stagingrelease --appIdSuffix staging",
"android:prod": "react-native run-android --variant=productiondebug",
"android:prod": "react-native run-android --variant=productionDebug",
"android:prodRelease": "react-native run-android --variant=productionrelease",
"ios:dev": "react-native run-ios --scheme Development",
"ios:stg": "react-native run-ios --scheme Staging",
Expand All @@ -30,8 +30,11 @@
"react-i18next": "^11.11.4",
"react-native": "0.71.6",
"react-native-config": "^1.4.3",
"react-native-encrypted-storage": "^4.0.3",
"react-native-flipper": "^0.178.1",
"react-native-gesture-handler": "^1.10.3",
"react-native-mmkv": "^2.8.0",
"react-native-mmkv-flipper-plugin": "^1.0.0",
"react-native-reanimated": "^2.8.0",
"react-native-safe-area-context": "^3.2.0",
"react-native-screens": "^3.4.0",
Expand All @@ -40,12 +43,15 @@
"react-redux": "^7.2.4",
"redux": "^4.1.0",
"redux-flipper": "^2.0.1",
"redux-saga": "^1.1.3"
"redux-persist": "^6.0.0",
"redux-saga": "^1.1.3",
"typed-redux-saga": "^1.5.0"
},
"devDependencies": {
"@babel/core": "^7.20.0",
"@babel/preset-env": "^7.20.0",
"@babel/runtime": "^7.20.0",
"@jambit/eslint-plugin-typed-redux-saga": "^0.4.0",
"@react-native-community/eslint-config": "^3.2.0",
"@testing-library/jest-native": "^4.0.1",
"@testing-library/react-native": "^7.2.0",
Expand Down
20 changes: 0 additions & 20 deletions template/src/__mocks__/mockedApi.ts

This file was deleted.

File renamed without changes.
59 changes: 0 additions & 59 deletions template/src/api/apiSaga.ts

This file was deleted.

21 changes: 21 additions & 0 deletions template/src/api/auth.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { AuthTokens } from './common'
import { Credentials } from './types/auth.types'

// This is the place where you want to call your auth provider API

export const logIn = (_credentials: Credentials) => {
return new Promise<{ accessToken: string; refreshToken: string }>(resolve => {
const fakeAuthTokens = { accessToken: 'FAKE_ACCESS_TOKEN', refreshToken: 'FAKE_REFRESH_TOKEN' }
setTimeout(() => resolve(fakeAuthTokens), 500)
})
}

export const refreshTokens = (_refreshToken: AuthTokens['refreshToken']) => {
return new Promise<{ accessToken: string; refreshToken: string }>(resolve => {
const fakeRefreshedAuthTokens = {
accessToken: 'REFRESHED_ACCESS_TOKEN',
refreshToken: 'REFRESHED_REFRESH_TOKEN',
}
setTimeout(() => resolve(fakeRefreshedAuthTokens), 500)
})
}
Loading