@onshorehq/react-native 0.0.0-stage → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -1,3 +1,13 @@
1
- # Temporary Holding Version
1
+ # @onshorehq/react-native
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ Collect and show sensitive values in a React Native app with Onshore secure components. The app holds no credentials: your server mints a single-use client token, and the form returns tokens, never the values.
4
+
5
+ ```sh
6
+ npm install @onshorehq/react-native
7
+ ```
8
+
9
+ ```tsx
10
+ import { createClient, CollectForm } from "@onshorehq/react-native";
11
+ ```
12
+
13
+ Full reference: https://docs.onshorevault.com/developers/sdks/react-native/
@@ -0,0 +1,45 @@
1
+ /** Talks to your own install's secure component endpoints. No React in here. */
2
+ export type VaultErrorCode = "invalid_client_token" | "validation_failed" | "policy_denied" | "rate_limited" | "server" | "transport";
3
+ /** What went wrong. It never holds a value. */
4
+ export declare class VaultError extends Error {
5
+ code: VaultErrorCode;
6
+ fields: string[];
7
+ status: number;
8
+ constructor(code: VaultErrorCode, fields?: string[], status?: number);
9
+ }
10
+ export interface FieldDescription {
11
+ name: string;
12
+ data_type: string;
13
+ }
14
+ export interface ComponentDescription {
15
+ kind: "collect" | "reveal";
16
+ fields: FieldDescription[];
17
+ tokens: number;
18
+ }
19
+ export interface RevealedValue {
20
+ decision?: string;
21
+ value?: string;
22
+ error?: {
23
+ code: string;
24
+ };
25
+ }
26
+ export interface ClientOptions {
27
+ /** Your install, for example https://vault.yourbank.ng */
28
+ baseUrl: string;
29
+ /** `rvapp://` and your app's id, listed under Secure components for the vault. */
30
+ appOrigin: string;
31
+ fetch?: typeof fetch;
32
+ timeoutMs?: number;
33
+ }
34
+ /**
35
+ * Holds no credentials. Your server mints a single-use client token and hands it to the
36
+ * app: never put a service account key in an app.
37
+ */
38
+ export declare function createClient(opts: ClientOptions): {
39
+ describe: (clientToken: string) => Promise<ComponentDescription>;
40
+ /** Returns a token for each field, which is safe to give your app and server. */
41
+ collect: (clientToken: string, values: Record<string, string>) => Promise<Record<string, string>>;
42
+ /** Asks for the values behind the tokens the client token names. Policy decides each one. */
43
+ reveal: (clientToken: string) => Promise<RevealedValue[]>;
44
+ };
45
+ export type SecureClient = ReturnType<typeof createClient>;
package/dist/client.js ADDED
@@ -0,0 +1,58 @@
1
+ /** Talks to your own install's secure component endpoints. No React in here. */
2
+ /** What went wrong. It never holds a value. */
3
+ export class VaultError extends Error {
4
+ constructor(code, fields = [], status = 0) {
5
+ super(code);
6
+ this.code = code;
7
+ this.fields = fields;
8
+ this.status = status;
9
+ this.name = "VaultError";
10
+ }
11
+ }
12
+ /**
13
+ * Holds no credentials. Your server mints a single-use client token and hands it to the
14
+ * app: never put a service account key in an app.
15
+ */
16
+ export function createClient(opts) {
17
+ const doFetch = opts.fetch ?? fetch;
18
+ const post = async (path, body) => {
19
+ const controller = new AbortController();
20
+ const timer = setTimeout(() => controller.abort(), opts.timeoutMs ?? 15000);
21
+ let res;
22
+ let json;
23
+ try {
24
+ res = await doFetch(`${opts.baseUrl.replace(/\/$/, "")}/embed/v1/${path}`, {
25
+ method: "POST",
26
+ headers: { "content-type": "application/json" },
27
+ body: JSON.stringify({ ...body, parent_origin: opts.appOrigin }),
28
+ signal: controller.signal,
29
+ });
30
+ json = await res.json();
31
+ }
32
+ catch {
33
+ // The system's error can carry the request, so none of it is passed on.
34
+ throw new VaultError("transport");
35
+ }
36
+ finally {
37
+ clearTimeout(timer);
38
+ }
39
+ if (res.ok)
40
+ return json;
41
+ const err = json?.error ?? {};
42
+ switch (err.code) {
43
+ case "invalid_client_token": throw new VaultError("invalid_client_token", [], res.status);
44
+ case "validation_failed": throw new VaultError("validation_failed", (err.fields ?? []).map((f) => (typeof f === "string" ? f : f.field)), res.status);
45
+ case "policy_denied": throw new VaultError("policy_denied", [], res.status);
46
+ case "rate_limited":
47
+ case "too_many_requests": throw new VaultError("rate_limited", [], res.status);
48
+ default: throw new VaultError("server", [], res.status);
49
+ }
50
+ };
51
+ return {
52
+ describe: (clientToken) => post("describe", { client_token: clientToken }),
53
+ /** Returns a token for each field, which is safe to give your app and server. */
54
+ collect: async (clientToken, values) => (await post("collect", { client_token: clientToken, values })).tokens,
55
+ /** Asks for the values behind the tokens the client token names. Policy decides each one. */
56
+ reveal: async (clientToken) => (await post("reveal", { client_token: clientToken })).results,
57
+ };
58
+ }
@@ -0,0 +1,26 @@
1
+ import React from "react";
2
+ import { VaultError, type SecureClient } from "./client.js";
3
+ export interface CollectFormProps {
4
+ client: SecureClient;
5
+ clientToken: string;
6
+ labels?: Record<string, string>;
7
+ submitLabel?: string;
8
+ onTokens: (tokens: Record<string, string>) => void;
9
+ onError?: (e: VaultError) => void;
10
+ }
11
+ /**
12
+ * Collects values, and hands your code only tokens. Values stay in this component's state,
13
+ * with no prop or callback to read them. **React Native runs all of an app's JavaScript in
14
+ * one context, so this keeps a value out of your code and logs by design, and does not
15
+ * isolate it from a hostile package in the same bundle.** The native SDKs have the same limit
16
+ * in a smaller way, and a browser frame does not.
17
+ */
18
+ export declare function CollectForm({ client, clientToken, labels, submitLabel, onTokens, onError }: CollectFormProps): React.JSX.Element;
19
+ export interface RevealViewProps {
20
+ client: SecureClient;
21
+ clientToken: string;
22
+ /** Seconds until the values are hidden. They also go when the app leaves the foreground. */
23
+ hideAfter?: number;
24
+ onError?: (e: VaultError) => void;
25
+ }
26
+ export declare function RevealView({ client, clientToken, hideAfter, onError }: RevealViewProps): React.JSX.Element;
@@ -0,0 +1,66 @@
1
+ import React, { useEffect, useRef, useState } from "react";
2
+ import { AppState, Pressable, Text, TextInput, View } from "react-native";
3
+ import { VaultError } from "./client.js";
4
+ const NUMERIC = new Set(["bvn", "nin", "ng_11_digit_id", "nuban"]);
5
+ /**
6
+ * Collects values, and hands your code only tokens. Values stay in this component's state,
7
+ * with no prop or callback to read them. **React Native runs all of an app's JavaScript in
8
+ * one context, so this keeps a value out of your code and logs by design, and does not
9
+ * isolate it from a hostile package in the same bundle.** The native SDKs have the same limit
10
+ * in a smaller way, and a browser frame does not.
11
+ */
12
+ export function CollectForm({ client, clientToken, labels = {}, submitLabel = "Continue", onTokens, onError }) {
13
+ const [fields, setFields] = useState([]);
14
+ const [values, setValues] = useState({});
15
+ const [invalid, setInvalid] = useState([]);
16
+ const [busy, setBusy] = useState(false);
17
+ const report = (e) => onError?.(e instanceof VaultError ? e : new VaultError("transport"));
18
+ useEffect(() => {
19
+ let live = true;
20
+ client.describe(clientToken).then((d) => { if (live)
21
+ setFields(d.fields); }, report);
22
+ return () => { live = false; setValues({}); };
23
+ }, [client, clientToken]);
24
+ const submit = async () => {
25
+ setBusy(true);
26
+ try {
27
+ const tokens = await client.collect(clientToken, values);
28
+ setValues({});
29
+ onTokens(tokens);
30
+ }
31
+ catch (e) {
32
+ if (e instanceof VaultError && e.code === "validation_failed")
33
+ setInvalid(e.fields);
34
+ report(e);
35
+ }
36
+ finally {
37
+ setBusy(false);
38
+ }
39
+ };
40
+ return (React.createElement(View, null,
41
+ fields.map((f) => (React.createElement(View, { key: f.name },
42
+ React.createElement(Text, null, labels[f.name] ?? f.name),
43
+ React.createElement(TextInput, { accessibilityLabel: labels[f.name] ?? f.name, value: values[f.name] ?? "", onChangeText: (s) => { setValues((v) => ({ ...v, [f.name]: s })); setInvalid((i) => i.filter((n) => n !== f.name)); }, keyboardType: NUMERIC.has(f.data_type) ? "number-pad" : "default", autoComplete: "off", autoCorrect: false, autoCapitalize: "none", spellCheck: false, contextMenuHidden: true, textContentType: "none", importantForAutofill: "no" }),
44
+ invalid.includes(f.name) && React.createElement(Text, null, "Check this value")))),
45
+ React.createElement(Pressable, { accessibilityRole: "button", disabled: busy || fields.length === 0, onPress: submit },
46
+ React.createElement(Text, null, submitLabel))));
47
+ }
48
+ export function RevealView({ client, clientToken, hideAfter = 30, onError }) {
49
+ const [results, setResults] = useState([]);
50
+ const timer = useRef(undefined);
51
+ useEffect(() => {
52
+ let live = true;
53
+ client.reveal(clientToken).then((r) => {
54
+ if (!live)
55
+ return;
56
+ setResults(r);
57
+ timer.current = setTimeout(() => setResults([]), hideAfter * 1000);
58
+ }, (e) => onError?.(e instanceof VaultError ? e : new VaultError("transport")));
59
+ const sub = AppState.addEventListener("change", (s) => { if (s !== "active")
60
+ setResults([]); });
61
+ return () => { live = false; clearTimeout(timer.current); sub.remove(); setResults([]); };
62
+ }, [client, clientToken, hideAfter]);
63
+ return (React.createElement(View, null, results.map((r, i) => (r.value !== undefined
64
+ ? React.createElement(Text, { key: i, selectable: false }, r.value)
65
+ : React.createElement(Text, { key: i }, r.error?.code === "policy_denied" ? "You do not have access to this" : "Not available")))));
66
+ }
@@ -0,0 +1,2 @@
1
+ export * from "./client.js";
2
+ export * from "./components.js";
package/dist/index.js ADDED
@@ -0,0 +1,2 @@
1
+ export * from "./client.js";
2
+ export * from "./components.js";
package/package.json CHANGED
@@ -1,6 +1,38 @@
1
1
  {
2
2
  "name": "@onshorehq/react-native",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "0.1.0",
4
+ "description": "Secure collect and reveal for React Native. Holds no credentials.",
5
+ "type": "module",
6
+ "main": "dist/index.js",
7
+ "types": "dist/index.d.ts",
8
+ "files": [
9
+ "dist",
10
+ "README.md"
11
+ ],
12
+ "scripts": {
13
+ "build": "tsc -p tsconfig.json",
14
+ "test": "node --test test/*.test.mjs",
15
+ "prepublishOnly": "npm run build"
16
+ },
17
+ "peerDependencies": {
18
+ "react": ">=18",
19
+ "react-native": ">=0.72"
20
+ },
21
+ "devDependencies": {
22
+ "@types/react": "18.3.12",
23
+ "react": "18.3.1",
24
+ "react-test-renderer": "18.3.1",
25
+ "typescript": "5.6.3"
26
+ },
27
+ "publishConfig": {
28
+ "access": "public"
29
+ },
30
+ "homepage": "https://docs.onshorevault.com/developers/sdks/react-native/",
31
+ "keywords": [
32
+ "onshore",
33
+ "data-residency",
34
+ "tokenization",
35
+ "vault",
36
+ "pii"
37
+ ]
38
+ }