From e5021187e96b78b53203bd95d08d6818aea47d17 Mon Sep 17 00:00:00 2001 From: "Thomas F. K. Jorna" Date: Wed, 14 Jul 2021 15:10:31 +0200 Subject: New Ignite 7.0.6 app --- app/utils/delay.ts | 6 +++ app/utils/ignore-warnings.ts | 10 +++++ app/utils/keychain.ts | 63 +++++++++++++++++++++++++++++++ app/utils/storage/index.ts | 1 + app/utils/storage/storage.test.ts | 39 +++++++++++++++++++ app/utils/storage/storage.ts | 79 +++++++++++++++++++++++++++++++++++++++ app/utils/validate.ts | 77 ++++++++++++++++++++++++++++++++++++++ 7 files changed, 275 insertions(+) create mode 100644 app/utils/delay.ts create mode 100644 app/utils/ignore-warnings.ts create mode 100644 app/utils/keychain.ts create mode 100644 app/utils/storage/index.ts create mode 100644 app/utils/storage/storage.test.ts create mode 100644 app/utils/storage/storage.ts create mode 100644 app/utils/validate.ts (limited to 'app/utils') diff --git a/app/utils/delay.ts b/app/utils/delay.ts new file mode 100644 index 0000000..6a2ef8d --- /dev/null +++ b/app/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/utils/ignore-warnings.ts b/app/utils/ignore-warnings.ts new file mode 100644 index 0000000..18802fe --- /dev/null +++ b/app/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/utils/keychain.ts b/app/utils/keychain.ts new file mode 100644 index 0000000..34774bb --- /dev/null +++ b/app/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/utils/storage/index.ts b/app/utils/storage/index.ts new file mode 100644 index 0000000..ff88148 --- /dev/null +++ b/app/utils/storage/index.ts @@ -0,0 +1 @@ +export * from "./storage" diff --git a/app/utils/storage/storage.test.ts b/app/utils/storage/storage.test.ts new file mode 100644 index 0000000..d5cd977 --- /dev/null +++ b/app/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/utils/storage/storage.ts b/app/utils/storage/storage.ts new file mode 100644 index 0000000..05c098a --- /dev/null +++ b/app/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 { + 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 { + 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 { + 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 { + 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 { + try { + await AsyncStorage.removeItem(key) + } catch {} +} + +/** + * Burn it all to the ground. + */ +export async function clear(): Promise { + try { + await AsyncStorage.clear() + } catch {} +} diff --git a/app/utils/validate.ts b/app/utils/validate.ts new file mode 100644 index 0000000..91db9b6 --- /dev/null +++ b/app/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 +} + +/** + * 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): ValidationErrors { + if (typeof data !== "object") { + return {} as ValidationErrors + } + return Validate(data, rules, { fullMessages: false }) || {} +} -- cgit v1.2.3