summaryrefslogtreecommitdiff
path: root/app_expo/utils
diff options
context:
space:
mode:
authorKirill Rogovoy <[email protected]>2021-07-20 21:24:52 +0300
committerKirill Rogovoy <[email protected]>2021-07-20 21:24:52 +0300
commit5f4611d65e40eae3ca6191a15f68d69ea5a1c4cb (patch)
tree273dfc086444533d86d580961c92ba8d14781a67 /app_expo/utils
parentf0bf4e7afdcd8b02a62be45ab3e7d047ed865a79 (diff)
WIP
Diffstat (limited to 'app_expo/utils')
-rw-r--r--app_expo/utils/delay.ts6
-rw-r--r--app_expo/utils/ignore-warnings.ts10
-rw-r--r--app_expo/utils/keychain.ts63
-rw-r--r--app_expo/utils/storage/index.ts1
-rw-r--r--app_expo/utils/storage/storage.test.ts39
-rw-r--r--app_expo/utils/storage/storage.ts79
-rw-r--r--app_expo/utils/validate.ts77
7 files changed, 275 insertions, 0 deletions
diff --git a/app_expo/utils/delay.ts b/app_expo/utils/delay.ts
new file mode 100644
index 0000000..6a2ef8d
--- /dev/null
+++ b/app_expo/utils/delay.ts
@@ -0,0 +1,6 @@
+/**
+ * A "modern" sleep statement.
+ *
+ * @param ms The number of milliseconds to wait.
+ */
+export const delay = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms))
diff --git a/app_expo/utils/ignore-warnings.ts b/app_expo/utils/ignore-warnings.ts
new file mode 100644
index 0000000..3d02efd
--- /dev/null
+++ b/app_expo/utils/ignore-warnings.ts
@@ -0,0 +1,10 @@
+/**
+ * Ignore some yellowbox warnings. Some of these are for deprecated functions
+ * that we haven't gotten around to replacing yet.
+ */
+import { LogBox } from 'react-native'
+
+// prettier-ignore
+LogBox.ignoreLogs([
+ "Require cycle:",
+])
diff --git a/app_expo/utils/keychain.ts b/app_expo/utils/keychain.ts
new file mode 100644
index 0000000..bcbc379
--- /dev/null
+++ b/app_expo/utils/keychain.ts
@@ -0,0 +1,63 @@
+import * as ReactNativeKeychain from 'react-native-keychain'
+
+/**
+ * Saves some credentials securely.
+ *
+ * @param username The username
+ * @param password The password
+ * @param server The server these creds are for.
+ */
+export async function save(username: string, password: string, server?: string) {
+ if (server) {
+ await ReactNativeKeychain.setInternetCredentials(server, username, password)
+ return true
+ } else {
+ return ReactNativeKeychain.setGenericPassword(username, password)
+ }
+}
+
+/**
+ * Loads credentials that were already saved.
+ *
+ * @param server The server that these creds are for
+ */
+export async function load(server?: string) {
+ if (server) {
+ const creds = await ReactNativeKeychain.getInternetCredentials(server)
+ return {
+ username: creds ? creds.username : null,
+ password: creds ? creds.password : null,
+ server,
+ }
+ } else {
+ const creds = await ReactNativeKeychain.getGenericPassword()
+ if (typeof creds === 'object') {
+ return {
+ username: creds.username,
+ password: creds.password,
+ server: null,
+ }
+ } else {
+ return {
+ username: null,
+ password: null,
+ server: null,
+ }
+ }
+ }
+}
+
+/**
+ * Resets any existing credentials for the given server.
+ *
+ * @param server The server which has these creds
+ */
+export async function reset(server?: string) {
+ if (server) {
+ await ReactNativeKeychain.resetInternetCredentials(server)
+ return true
+ } else {
+ const result = await ReactNativeKeychain.resetGenericPassword()
+ return result
+ }
+}
diff --git a/app_expo/utils/storage/index.ts b/app_expo/utils/storage/index.ts
new file mode 100644
index 0000000..69b61ec
--- /dev/null
+++ b/app_expo/utils/storage/index.ts
@@ -0,0 +1 @@
+export * from './storage'
diff --git a/app_expo/utils/storage/storage.test.ts b/app_expo/utils/storage/storage.test.ts
new file mode 100644
index 0000000..bb241c0
--- /dev/null
+++ b/app_expo/utils/storage/storage.test.ts
@@ -0,0 +1,39 @@
+import AsyncStorage from '@react-native-async-storage/async-storage'
+import { load, loadString, save, saveString, clear, remove } from './storage'
+
+// fixtures
+const VALUE_OBJECT = { x: 1 }
+const VALUE_STRING = JSON.stringify(VALUE_OBJECT)
+
+beforeEach(() => (AsyncStorage.getItem as jest.Mock).mockReturnValue(Promise.resolve(VALUE_STRING)))
+afterEach(() => jest.clearAllMocks())
+
+test('load', async () => {
+ const value = await load('something')
+ expect(value).toEqual(JSON.parse(VALUE_STRING))
+})
+
+test('loadString', async () => {
+ const value = await loadString('something')
+ expect(value).toEqual(VALUE_STRING)
+})
+
+test('save', async () => {
+ await save('something', VALUE_OBJECT)
+ expect(AsyncStorage.setItem).toHaveBeenCalledWith('something', VALUE_STRING)
+})
+
+test('saveString', async () => {
+ await saveString('something', VALUE_STRING)
+ expect(AsyncStorage.setItem).toHaveBeenCalledWith('something', VALUE_STRING)
+})
+
+test('remove', async () => {
+ await remove('something')
+ expect(AsyncStorage.removeItem).toHaveBeenCalledWith('something')
+})
+
+test('clear', async () => {
+ await clear()
+ expect(AsyncStorage.clear).toHaveBeenCalledWith()
+})
diff --git a/app_expo/utils/storage/storage.ts b/app_expo/utils/storage/storage.ts
new file mode 100644
index 0000000..659e738
--- /dev/null
+++ b/app_expo/utils/storage/storage.ts
@@ -0,0 +1,79 @@
+import AsyncStorage from '@react-native-async-storage/async-storage'
+
+/**
+ * Loads a string from storage.
+ *
+ * @param key The key to fetch.
+ */
+export async function loadString(key: string): Promise<string | null> {
+ try {
+ return await AsyncStorage.getItem(key)
+ } catch {
+ // not sure why this would fail... even reading the RN docs I'm unclear
+ return null
+ }
+}
+
+/**
+ * Saves a string to storage.
+ *
+ * @param key The key to fetch.
+ * @param value The value to store.
+ */
+export async function saveString(key: string, value: string): Promise<boolean> {
+ try {
+ await AsyncStorage.setItem(key, value)
+ return true
+ } catch {
+ return false
+ }
+}
+
+/**
+ * Loads something from storage and runs it thru JSON.parse.
+ *
+ * @param key The key to fetch.
+ */
+export async function load(key: string): Promise<any | null> {
+ try {
+ const almostThere = await AsyncStorage.getItem(key)
+ return JSON.parse(almostThere)
+ } catch {
+ return null
+ }
+}
+
+/**
+ * Saves an object to storage.
+ *
+ * @param key The key to fetch.
+ * @param value The value to store.
+ */
+export async function save(key: string, value: any): Promise<boolean> {
+ try {
+ await AsyncStorage.setItem(key, JSON.stringify(value))
+ return true
+ } catch {
+ return false
+ }
+}
+
+/**
+ * Removes something from storage.
+ *
+ * @param key The key to kill.
+ */
+export async function remove(key: string): Promise<void> {
+ try {
+ await AsyncStorage.removeItem(key)
+ } catch {}
+}
+
+/**
+ * Burn it all to the ground.
+ */
+export async function clear(): Promise<void> {
+ try {
+ await AsyncStorage.clear()
+ } catch {}
+}
diff --git a/app_expo/utils/validate.ts b/app_expo/utils/validate.ts
new file mode 100644
index 0000000..838c0c1
--- /dev/null
+++ b/app_expo/utils/validate.ts
@@ -0,0 +1,77 @@
+const ValidateJS = require('validate.js')
+
+// HACK(steve): wierd typescript situation because of strange typings
+const Validate: any = ValidateJS.default ? ValidateJS.default : ValidateJS
+
+/**
+ * Validates that 1 attribute doesn't appear in another's attributes content.
+ */
+Validate.validators.excludes = function custom(value, options, key, attributes) {
+ const list = attributes[options.attribute] || []
+ if (value && list.includes(value)) {
+ return options.message || `${value} is in the list`
+ }
+}
+
+/**
+ * Validates that another attribute isn't true.
+ */
+Validate.validators.tripped = function custom(value, options, key, attributes) {
+ if (value && attributes[options.attribute] === true) {
+ return options.message || `${options.attribute} is true`
+ }
+}
+
+/**
+ * Defines the rules for validating.
+ *
+ * Example:
+ * ```ts
+ * const RULES = {
+ * favoriteBand: {
+ * inclusion: { ['Weezer', 'Other'], message: 'Pick wisely.' }
+ * },
+ * name: {
+ * presence: { message: 'A developer has no name?' }
+ * }
+ * }
+ * validate(RULES, {})
+ * ```
+ *
+ * See https://validatejs.org/#validators for more examples.
+ *
+ */
+export interface ValidationRules {
+ [key: string]: Record<string, unknown>
+}
+
+/**
+ * An object containing any errors found.
+ *
+ * Example:
+ * ```js
+ * {
+ * email: ['Invalid email address.'],
+ * password: [
+ * 'Password must be 6 characters.',
+ * 'Password must have at least 1 digit.'
+ * ]
+ * }
+ * ```
+ */
+export interface ValidationErrors {
+ [key: string]: string[]
+}
+
+/**
+ * Runs the given rules against the data object.
+ *
+ * @param rules The rules to apply.
+ * @param data The object to validate.
+ */
+export function validate(rules: ValidationRules, data: Record<string, unknown>): ValidationErrors {
+ if (typeof data !== 'object') {
+ return {} as ValidationErrors
+ }
+ return Validate(data, rules, { fullMessages: false }) || {}
+}