@geophrase/react 1.2.0 → 1.3.1

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,9 +1,102 @@
1
1
  # @geophrase/react
2
2
 
3
- The official React and Next.js hooks for integrating the Geophrase Connect address selector to reduce Return to Origin (RTO) costs.
3
+ The official React and Next.js hook for Geophrase Connect. Drop the Geophrase address selector into any React checkout in minutes and capture perfectly structured Indian addresses and coordinates to reduce Return to Origin (RTO) costs.
4
4
 
5
- 👉 **[Read the full documentation and integration guide here](https://business.geophrase.com/docs)**
5
+ 👉 **[Full documentation and integration guide](https://business.geophrase.com/docs)**
6
6
 
7
7
  ---
8
8
 
9
- *Note: This package requires the [`@geophrase/core`](https://www.npmjs.com/package/@geophrase/core) peer dependency. If you are not using React, you should use the core package directly.*
9
+ ## Install
10
+
11
+ ```bash
12
+ npm install @geophrase/react
13
+ ```
14
+
15
+ `@geophrase/core` is installed automatically as a dependency. `react >= 16.8` is a peer dependency.
16
+
17
+ ## Quick Start
18
+
19
+ The snippet below uses `mode: 'server'` so you can drop it into any React app and see the widget end-to-end **without creating an API key first**.
20
+
21
+ ```jsx
22
+ import { useState } from 'react';
23
+ import { useGeophrase } from '@geophrase/react';
24
+
25
+ export default function Checkout() {
26
+ const [result, setResult] = useState(null);
27
+
28
+ const { open } = useGeophrase({
29
+ mode: 'server', // widget returns a token your backend exchanges for the address
30
+ theme: 'system', // 'light' | 'dark' | 'system'
31
+ orderId: 'ORD-98765', // your internal reference id
32
+ phone: '9999999999', // pre-fill the customer phone
33
+
34
+ onSuccess: (data) => setResult(data),
35
+ onError: (error) => console.error('Error:', error.message),
36
+ onClose: () => console.log('User dismissed the widget.')
37
+ });
38
+
39
+ return (
40
+ <div>
41
+ <button onClick={open}>Select Delivery Address</button>
42
+ {result && <pre>{JSON.stringify(result, null, 2)}</pre>}
43
+ </div>
44
+ );
45
+ }
46
+ ```
47
+
48
+ ## Server vs Client Mode
49
+
50
+ The SDK supports two modes. Pick whichever fits your architecture:
51
+
52
+ - **`server`**: the widget returns `{ token }`. Your backend exchanges it for the full address using your API key, which never touches the frontend.
53
+ - **`client`**: the widget resolves the address in the browser and passes it straight to `onSuccess`. Requires `key` in the options. You should restrict that key to your domain(s) in the [Geophrase Business Dashboard](https://business.geophrase.com).
54
+
55
+ If your app has a backend, `server` mode keeps your API key entirely off the frontend, which is the most secure option. If you don't have a backend (or want the fastest possible integration), `client` mode with a domain-restricted key is fine.
56
+
57
+ ```jsx
58
+ // client mode, no backend required
59
+ const { open } = useGeophrase({
60
+ mode: 'client',
61
+ key: 'YOUR_API_KEY',
62
+ onSuccess: (address) => setResult(address)
63
+ });
64
+ ```
65
+
66
+ ## Configuration Options
67
+
68
+ | Parameter | Type | Required | Description |
69
+ | :--- | :--- | :--- | :--- |
70
+ | `mode` | `string` | Optional | `'client'` (default) or `'server'`. Determines the architectural flow of the SDK. |
71
+ | `key` | `string` | **Conditional** | Your Geophrase API key. **Required if `mode` is `'client'`.** Omit if using server mode. |
72
+ | `theme` | `string` | Optional | `'light'`, `'dark'`, or `'system'`. Defaults to `'system'`. |
73
+ | `orderId` | `string` | Optional | Your internal reference ID for this checkout session. |
74
+ | `phone` | `string` | Optional | The customer's 10-digit phone number (pre-fills the widget). |
75
+ | `onSuccess` | `function` | **Yes** | Called upon completion. Returns an `Address` object (`mode: 'client'`) or a `Token` object (`mode: 'server'`). |
76
+ | `onError` | `function` | Optional | Called if the API fails or a network error occurs. Returns an `Error` object. |
77
+ | `onClose` | `function` | Optional | Called when the user closes the modal without completing the flow. |
78
+
79
+ ### Next.js (App Router)
80
+
81
+ The component calling `useGeophrase` relies on browser APIs, so it must be a **Client Component**. Add `"use client";` at the top of the file.
82
+
83
+ ### TypeScript
84
+
85
+ The hook is fully typed. Import `GeophraseAddress` and `GeophraseToken` from `@geophrase/core` to type `onSuccess` results:
86
+
87
+ ```tsx
88
+ import { GeophraseAddress, GeophraseToken } from '@geophrase/core';
89
+
90
+ const [result, setResult] = useState<GeophraseAddress | GeophraseToken | null>(null);
91
+ ```
92
+
93
+ ## What the hook returns
94
+
95
+ | Value | Description |
96
+ | :--- | :--- |
97
+ | `open()` | Opens the modal. Safe to wire directly to a button's `onClick`. |
98
+ | `close()` | Closes the modal without firing `onSuccess`. |
99
+
100
+ The underlying `Geophrase` instance is created on mount and destroyed on unmount automatically, so you do not need to call `destroy()` manually.
101
+
102
+ See the [full docs](https://business.geophrase.com/docs) for the complete configuration reference, security guidance, and response schemas.
package/package.json CHANGED
@@ -1,7 +1,9 @@
1
1
  {
2
2
  "name": "@geophrase/react",
3
- "version": "1.2.0",
4
- "description": "React and Next.js hooks for the Geophrase Connect SDK",
3
+ "version": "1.3.1",
4
+ "description": "React & Next.js hook for the Geophrase address picker. Drop-in widget that captures structured Indian addresses with coordinates to reduce RTO.",
5
+ "license": "MIT",
6
+ "author": "Geophrase",
5
7
  "homepage": "https://business.geophrase.com/docs",
6
8
  "repository": {
7
9
  "type": "git",
@@ -12,16 +14,26 @@
12
14
  },
13
15
  "keywords": [
14
16
  "react",
17
+ "react-hook",
15
18
  "nextjs",
16
19
  "hooks",
20
+ "sdk",
21
+ "widget",
17
22
  "address",
23
+ "address-picker",
24
+ "address-autocomplete",
25
+ "address-verification",
26
+ "delivery-address",
27
+ "pincode",
18
28
  "india",
19
29
  "rto",
20
30
  "ecommerce",
21
31
  "checkout"
22
32
  ],
33
+ "type": "module",
23
34
  "main": "src/index.js",
24
35
  "types": "index.d.ts",
36
+ "sideEffects": false,
25
37
  "files": [
26
38
  "src",
27
39
  "index.d.ts",
@@ -32,6 +44,9 @@
32
44
  "react": ">=16.8.0"
33
45
  },
34
46
  "dependencies": {
35
- "@geophrase/core": "^1.2.0"
47
+ "@geophrase/core": "^1.3.1"
48
+ },
49
+ "publishConfig": {
50
+ "access": "public"
36
51
  }
37
- }
52
+ }
@@ -23,43 +23,44 @@ export const useGeophrase = (options = {}) => {
23
23
  useEffect(() => {
24
24
  if (typeof window === 'undefined') return;
25
25
 
26
- // 1. Strict Developer Warning
27
- if (!options.key) {
28
- console.warn("Geophrase: 'key' is required to initialize the widget.");
26
+ try {
27
+ geoInstance.current = new Geophrase({
28
+ mode: options.mode,
29
+ theme: options.theme,
30
+ key: options.key,
31
+ orderId: options.orderId,
32
+ phone: options.phone,
33
+ onSuccess: (data) => {
34
+ if (savedCallbacks.current.onSuccess) savedCallbacks.current.onSuccess(data);
35
+ },
36
+ onError: (error) => {
37
+ if (savedCallbacks.current.onError) savedCallbacks.current.onError(error);
38
+ },
39
+ onClose: () => {
40
+ if (savedCallbacks.current.onClose) savedCallbacks.current.onClose();
41
+ }
42
+ });
43
+ } catch (error) {
44
+ console.error(error.message);
29
45
  }
30
46
 
31
- geoInstance.current = new Geophrase({
32
- key: options.key,
33
- order_id: options.order_id,
34
- phone: options.phone,
35
- onSuccess: (data) => {
36
- if (savedCallbacks.current.onSuccess) savedCallbacks.current.onSuccess(data);
37
- },
38
- onError: (error) => {
39
- if (savedCallbacks.current.onError) savedCallbacks.current.onError(error);
40
- },
41
- onClose: () => {
42
- if (savedCallbacks.current.onClose) savedCallbacks.current.onClose();
43
- }
44
- });
45
-
46
47
  return () => {
47
48
  if (geoInstance.current) {
48
49
  geoInstance.current.destroy();
49
50
  geoInstance.current = null;
50
51
  }
51
52
  };
52
- }, [options.key, options.order_id, options.phone]);
53
+ // Include mode and theme in the dependency array so the instance rebuilds if they change state
54
+ }, [options.mode, options.theme, options.key, options.orderId, options.phone]);
53
55
 
54
56
  const open = useCallback(() => {
55
57
  if (geoInstance.current) {
56
58
  geoInstance.current.open();
57
59
  } else {
58
- console.warn("Geophrase widget is not ready yet.");
60
+ console.warn("Geophrase widget is not ready yet or failed to initialize.");
59
61
  }
60
62
  }, []);
61
63
 
62
- // 2. Fixed missing close implementation
63
64
  const close = useCallback(() => {
64
65
  if (geoInstance.current) {
65
66
  geoInstance.current.close();