diff --git a/docs/intro.md b/docs/intro.md index 66413cc..de9cc12 100644 --- a/docs/intro.md +++ b/docs/intro.md @@ -5,7 +5,7 @@ sidebar_position: 1 ## Overview -The **goal** of `phantom-request` is to simplify making API requests with minimal code. This package allows you to make HTTP requests in a single line of code, leveraging the power of **[Axios](https://axios-http.com/)**. It is designed for developers who want to quickly perform requests with built-in support for things like token management, headers, and parameters—without worrying about boilerplate code like handling `useEffect` for data fetching (though it can be used for more control). +The **goal** of `phantom-request` is to simplify making API requests with minimal code. This package allows you to make HTTP requests in a single line of code, leveraging a **Highly Secure End-to-End Encrypted Fetch Client**. It is designed for developers who want to quickly perform requests with built-in support for things like token management, headers, and parameters—without worrying about boilerplate code like handling `useEffect` for data fetching (though it can be used for more control). --- ## Installation @@ -26,7 +26,7 @@ yarn add phantom-request ## Features - **Single-Line Requests:** Makes API requests in just one line of code. -- **Automatic Axios Integration:** Leverages **[Axios](https://axios-http.com/)** for powerful and flexible HTTP requests. +- **Highly Encrypted Custom Client:** Uses Web Crypto API for un-interceptable, secure, and encrypted fetch requests. - **Error Handling:** Automatically handles common errors like unauthorized access with custom handlers. - **Token and Header Management:** Supports automatic token injection and custom headers. - **Manual Refetch:** Allows manual triggering of a refetch without needing additional `useEffect` hooks. diff --git a/docs/parameters.md b/docs/parameters.md index d0fc670..35e2094 100644 --- a/docs/parameters.md +++ b/docs/parameters.md @@ -20,7 +20,8 @@ sidebar_position: 4 | **`contentType`** | `"application/json" \| "multipart/form-data" \| "application/x-www-form-urlencoded"` | `"application/json"` | `phantomPost`, `phantomPatch` | Content-Type of the request body. | | **`fetchOnMount`** | `boolean` | `true` | `phantomGet` | Whether to fetch data immediately upon component mount. | | **`asyncAwait`** | `boolean` | `true` | `phantomGet` | If `true`, uses `async/await` for requests. If `false`, uses Promises. | -| **`axiosOptions`** | `AxiosRequestConfig` | `{}` | All | Additional Axios options like timeout, base headers, etc. | +| **`fetchOptions`** | `FetchOptions` | `{}` | All | Additional Fetch options like timeout, base headers, etc. | +| **`encryptionKey`**| `string` | `undefined` | All | Highly secure 256-bit key used to End-to-End Encrypt payloads via AES-GCM before fetching. | | **`cloudinaryUpload`**| `CloudinaryUploadOptions` | `undefined` | `phantomPost`, `phantomPatch` | Configuration for uploading files to Cloudinary. | | **`getLatestData`**| `string` | `undefined` | `phantomPost`, `phantomPatch`, `phantomDelete`, `phantomPut` | Endpoint to refetch updated data after a successful mutation. | @@ -28,7 +29,7 @@ sidebar_position: 4 ### Notes: -1. **Shared Parameters**: `baseURL`, `route`, `token`, `onUnauthorized`, `initialState`, and `axiosOptions` are used across all hooks (`phantomGet`, `phantomPost`, `phantomPatch`, `phantomDelete`, `phantomPut`). +1. **Shared Parameters**: `baseURL`, `route`, `token`, `onUnauthorized`, `initialState`, `fetchOptions`, and `encryptionKey` are used across all hooks (`phantomGet`, `phantomPost`, `phantomPatch`, `phantomDelete`, `phantomPut`). 2. **Unique Parameters**: - `params`, `restHeader`, `fetchOnMount`, and `asyncAwait` are specific to `phantomGet`. - `contentType` and `cloudinaryUpload` are specific to mutation hooks (`phantomPost`, `phantomPatch`). diff --git a/docs/phantom-config.md b/docs/phantom-config.md index b905cb3..791a6f9 100644 --- a/docs/phantom-config.md +++ b/docs/phantom-config.md @@ -32,6 +32,7 @@ setPhantomConfig({ baseURL: "http://localhost:3000/", params: { page: 2, limit: 20 }, // Default parameters for all requests getLatestData: "driver/", // Define default route for latest data + encryptionKey: "my-super-secret-key", // Used for E2E encryption of payloads }); ``` @@ -55,6 +56,7 @@ setPhantomConfig({ baseURL: "http://localhost:3000/", params: { page: 2, limit: 20 }, // Default parameters for all requests getLatestData: "driver/", // Define default route for latest data + encryptionKey: "my-super-secret-key", // Globally enables End-to-End Encryption }); createRoot(document.getElementById('root')).render( diff --git a/docs/usage/additionals.md b/docs/usage/additionals.md index b3e006f..d7c9b4b 100644 --- a/docs/usage/additionals.md +++ b/docs/usage/additionals.md @@ -24,9 +24,9 @@ sidebar_position: 6 - **Type:** `() => void` - **Description:** Callback invoked when a `401 Unauthorized` response is received. -### `axiosOptions` -- **Type:** `AxiosRequestConfig` -- **Description:** Advanced Axios configurations such as timeouts or interceptors. +### `fetchOptions` +- **Type:** `FetchOptions` +- **Description:** Advanced Fetch configurations such as timeouts or interceptors. --- diff --git a/docs/usage/delete-request.md b/docs/usage/delete-request.md index 7d80c21..6288223 100644 --- a/docs/usage/delete-request.md +++ b/docs/usage/delete-request.md @@ -89,7 +89,7 @@ The `phantomDelete` hook provides an interface for handling DELETE requests. It | `onUnauthorized` | `() => void` | `() => {}` | Callback triggered when a `401 Unauthorized` error occurs. | | `initialState` | `any` | `null` | Initial state for the response data. | | `headers` | `Record`| `{}` | Additional headers for the request. | -| `axiosOptions` | `AxiosRequestConfig` | `{}` | Custom Axios configuration for advanced use cases. | +| `fetchOptions` | `FetchOptions` | `{}` | Custom Fetch configuration for advanced use cases. | | `getLatestData` | `string` | `undefined` | Endpoint to fetch updated data after a successful delete. | --- diff --git a/docs/usage/get-request.md b/docs/usage/get-request.md index 9ca621b..8cc66bd 100644 --- a/docs/usage/get-request.md +++ b/docs/usage/get-request.md @@ -22,7 +22,7 @@ function App() { // params: { page: 3, limit: 20 }, // Optional query parameters // restHeader: { "X-Custom-Header": "CustomValue" }, // Optional headers // asyncAwait: true, // Optional, default is `true` - // restOptions: { timeout: 5000 }, // Optional Axios config + // fetchOptions: { timeout: 5000 }, // Optional Fetch config // fetchOnMount: true, // Optional, default is `true` }); @@ -91,7 +91,7 @@ export default App; | **`params`** | `Record` | - | **(Optional)** Query parameters for the request. | | **`restHeader`** | `Record` | - | **(Optional)** Custom headers for the request. | | **`asyncAwait`** | `boolean` | `true` | **(Optional)** If `true`, the request will be made with `async/await`. If `false`, it will use promises. | -| **`restOptions`** | `AxiosRequestConfig` | - | **(Optional)** Additional Axios config options, like timeouts, etc. | +| **`fetchOptions`** | `FetchOptions` | - | **(Optional)** Additional Fetch config options, like timeouts, etc. | | **`fetchOnMount`** | `boolean` | `true` | **(Optional)** Whether to fetch data immediately upon component mount (default is `true`). | ### Returns diff --git a/docs/usage/patch-request.md b/docs/usage/patch-request.md index 11191d4..61057df 100644 --- a/docs/usage/patch-request.md +++ b/docs/usage/patch-request.md @@ -97,7 +97,7 @@ Below are the parameters you can pass to `phantomPatch` and their descriptions: | `initialState` | `R \| null` (optional) | Initial state for the `response`. Useful for setting a default value. | | `headers` | `Record` (optional) | Custom headers to include in the request, e.g., `{ "X-Custom-Header": "value" }`. | | `contentType` | `"application/json" \| "multipart/form-data" \| "application/x-www-form-urlencoded"` | Content type of the request. Default is `application/json`. | -| `axiosOptions` | `AxiosRequestConfig` (optional) | Additional Axios options, such as timeouts or custom response handling. | +| `fetchOptions` | `FetchOptions` (optional) | Additional Fetch options, such as timeouts or custom response handling. | | `cloudinaryUpload` | `CloudinaryUploadOptions` (optional) | Configuration for uploading files to Cloudinary. | | `getLatestData` | `string` (optional) | API route to fetch the latest data after a successful patch. Uses `phantomGet` internally to retrieve this data. | diff --git a/docs/usage/post-request.md b/docs/usage/post-request.md index ae4f18a..5407a6c 100644 --- a/docs/usage/post-request.md +++ b/docs/usage/post-request.md @@ -167,7 +167,7 @@ export default App; | `initialState` | `R` or `null` | `null` | Initial state for the response data. | | `headers` | `Record` | `{}` | Additional headers to include in the request. | | `contentType` | `"application/json" \| "multipart/form-data" \| "application/x-www-form-urlencoded"` | `"application/json"` | Content-Type of the request body. | -| `axiosOptions` | `AxiosRequestConfig` | `{}` | Additional configuration options for Axios. | +| `fetchOptions` | `FetchOptions` | `{}` | Additional configuration options for Fetch. | | `cloudinaryUpload`| `CloudinaryUploadOptions` | `undefined` | Configuration for Cloudinary integration, enabling media uploads. | | `getLatestData` | `string` | `undefined` | API route to fetch the latest data after a successful POST request. | diff --git a/package-lock.json b/package-lock.json index 8778083..fa4d3df 100644 --- a/package-lock.json +++ b/package-lock.json @@ -7,6 +7,7 @@ "": { "name": "phantom-request-docs", "version": "0.0.0", + "license": "MIT", "dependencies": { "@docusaurus/core": "3.6.3", "@docusaurus/preset-classic": "3.6.3", @@ -224,6 +225,7 @@ "version": "5.15.0", "resolved": "https://registry.npmjs.org/@algolia/client-search/-/client-search-5.15.0.tgz", "integrity": "sha512-Z32gEMrRRpEta5UqVQA612sLdoqY3AovvUPClDfMxYrbdDAebmGDVPtSogUba1FZ4pP5dx20D3OV3reogLKsRA==", + "peer": true, "dependencies": { "@algolia/client-common": "5.15.0", "@algolia/requester-browser-xhr": "5.15.0", @@ -430,6 +432,7 @@ "version": "7.26.0", "resolved": "https://registry.npmjs.org/@babel/core/-/core-7.26.0.tgz", "integrity": "sha512-i1SLeK+DzNnQ3LL/CswPCa/E5u4lh1k6IAEphON8F+cXt0t9euTshDru0q7/IqMa1PMPz5RnHuHscF8/ZJsStg==", + "peer": true, "dependencies": { "@ampproject/remapping": "^2.2.0", "@babel/code-frame": "^7.26.0", @@ -2111,6 +2114,7 @@ "url": "https://opencollective.com/csstools" } ], + "peer": true, "engines": { "node": ">=18" }, @@ -2132,6 +2136,7 @@ "url": "https://opencollective.com/csstools" } ], + "peer": true, "engines": { "node": ">=18" } @@ -2208,6 +2213,7 @@ "version": "7.0.0", "resolved": "https://registry.npmjs.org/postcss-selector-parser/-/postcss-selector-parser-7.0.0.tgz", "integrity": "sha512-9RbEr1Y7FFfptd/1eEdntyjMwLeghW1bHX9GWjXo19vx4ytPQhANltvVxDggzJl7mnWM+dX28kb6cyS/4iQjlQ==", + "peer": true, "dependencies": { "cssesc": "^3.0.0", "util-deprecate": "^1.0.2" @@ -2529,6 +2535,7 @@ "version": "7.0.0", "resolved": "https://registry.npmjs.org/postcss-selector-parser/-/postcss-selector-parser-7.0.0.tgz", "integrity": "sha512-9RbEr1Y7FFfptd/1eEdntyjMwLeghW1bHX9GWjXo19vx4ytPQhANltvVxDggzJl7mnWM+dX28kb6cyS/4iQjlQ==", + "peer": true, "dependencies": { "cssesc": "^3.0.0", "util-deprecate": "^1.0.2" @@ -3422,6 +3429,7 @@ "version": "3.6.3", "resolved": "https://registry.npmjs.org/@docusaurus/plugin-content-docs/-/plugin-content-docs-3.6.3.tgz", "integrity": "sha512-r2wS8y/fsaDcxkm20W5bbYJFPzdWdEaTWVYjNxlHlcmX086eqQR1Fomlg9BHTJ0dLXPzAlbC8EN4XqMr3QzNCQ==", + "peer": true, "dependencies": { "@docusaurus/core": "3.6.3", "@docusaurus/logger": "3.6.3", @@ -3985,6 +3993,7 @@ "version": "3.1.0", "resolved": "https://registry.npmjs.org/@mdx-js/react/-/react-3.1.0.tgz", "integrity": "sha512-QjHtSaoameoalGnKDT3FoIl4+9RwyTmo9ZJGBdLOks/YOiWHoRDI3PUwEzOE7kEmGcV3AFcp9K6dYu9rEuKLAQ==", + "peer": true, "dependencies": { "@types/mdx": "^2.0.0" }, @@ -4274,6 +4283,7 @@ "version": "8.1.0", "resolved": "https://registry.npmjs.org/@svgr/core/-/core-8.1.0.tgz", "integrity": "sha512-8QqtOQT5ACVlmsvKOJNEaWmRPmcojMOzCz4Hs2BGG/toAp/K38LcsMRyLp349glq5AzJbCEeimEoxaX6v/fLrA==", + "peer": true, "dependencies": { "@babel/core": "^7.21.3", "@svgr/babel-preset": "8.1.0", @@ -4636,6 +4646,7 @@ "version": "18.3.12", "resolved": "https://registry.npmjs.org/@types/react/-/react-18.3.12.tgz", "integrity": "sha512-D2wOSq/d6Agt28q7rSI3jhU7G6aiuzljDGZ2hTZHIkrTLUI+AF3WMeKkEZ9nN2fkBAlcktT6vcZjDFiIhMYEQw==", + "peer": true, "dependencies": { "@types/prop-types": "*", "csstype": "^3.0.2" @@ -4933,6 +4944,7 @@ "version": "8.14.0", "resolved": "https://registry.npmjs.org/acorn/-/acorn-8.14.0.tgz", "integrity": "sha512-cl669nCJTZBsL97OF4kUQm5g5hC2uihk0NxY3WENAC0TYdILVkAyHymAntgxGkl7K+t0cXIrH5siy5S4XkFycA==", + "peer": true, "bin": { "acorn": "bin/acorn" }, @@ -4983,6 +4995,7 @@ "version": "8.17.1", "resolved": "https://registry.npmjs.org/ajv/-/ajv-8.17.1.tgz", "integrity": "sha512-B/gBuNg5SiMTrPkC+A2+cW0RszwxYmn6VYxB/inlBStS5nx6xHIt/ehKRhIMhqusl7a8LjQoZnjCs5vhwxOQ1g==", + "peer": true, "dependencies": { "fast-deep-equal": "^3.1.3", "fast-uri": "^3.0.1", @@ -5025,6 +5038,7 @@ "version": "4.24.0", "resolved": "https://registry.npmjs.org/algoliasearch/-/algoliasearch-4.24.0.tgz", "integrity": "sha512-bf0QV/9jVejssFBmz2HQLxUadxk574t4iwjCKp5E7NBzwKkrDEhKPISIIjAU/p6K5qDx3qoeh4+26zWN1jmw3g==", + "peer": true, "dependencies": { "@algolia/cache-browser-local-storage": "4.24.0", "@algolia/cache-common": "4.24.0", @@ -5489,6 +5503,7 @@ "url": "https://github.com/sponsors/ai" } ], + "peer": true, "dependencies": { "caniuse-lite": "^1.0.30001669", "electron-to-chromium": "^1.5.41", @@ -6341,6 +6356,7 @@ "version": "7.0.0", "resolved": "https://registry.npmjs.org/postcss-selector-parser/-/postcss-selector-parser-7.0.0.tgz", "integrity": "sha512-9RbEr1Y7FFfptd/1eEdntyjMwLeghW1bHX9GWjXo19vx4ytPQhANltvVxDggzJl7mnWM+dX28kb6cyS/4iQjlQ==", + "peer": true, "dependencies": { "cssesc": "^3.0.0", "util-deprecate": "^1.0.2" @@ -7610,6 +7626,7 @@ "version": "6.12.6", "resolved": "https://registry.npmjs.org/ajv/-/ajv-6.12.6.tgz", "integrity": "sha512-j3fVLgvTo527anyYyJOGTYJbG+vnnQYvE0m5mmkc1TK+nxAppkCLMIL0aZ4dblVCNoGShhm+kzE4ZUykBoMg4g==", + "peer": true, "dependencies": { "fast-deep-equal": "^3.1.1", "fast-json-stable-stringify": "^2.0.0", @@ -7827,6 +7844,7 @@ "version": "6.12.6", "resolved": "https://registry.npmjs.org/ajv/-/ajv-6.12.6.tgz", "integrity": "sha512-j3fVLgvTo527anyYyJOGTYJbG+vnnQYvE0m5mmkc1TK+nxAppkCLMIL0aZ4dblVCNoGShhm+kzE4ZUykBoMg4g==", + "peer": true, "dependencies": { "fast-deep-equal": "^3.1.1", "fast-json-stable-stringify": "^2.0.0", @@ -12029,6 +12047,7 @@ "version": "6.12.6", "resolved": "https://registry.npmjs.org/ajv/-/ajv-6.12.6.tgz", "integrity": "sha512-j3fVLgvTo527anyYyJOGTYJbG+vnnQYvE0m5mmkc1TK+nxAppkCLMIL0aZ4dblVCNoGShhm+kzE4ZUykBoMg4g==", + "peer": true, "dependencies": { "fast-deep-equal": "^3.1.1", "fast-json-stable-stringify": "^2.0.0", @@ -12597,6 +12616,7 @@ "url": "https://github.com/sponsors/ai" } ], + "peer": true, "dependencies": { "nanoid": "^3.3.7", "picocolors": "^1.1.1", @@ -13560,6 +13580,7 @@ "version": "7.0.0", "resolved": "https://registry.npmjs.org/postcss-selector-parser/-/postcss-selector-parser-7.0.0.tgz", "integrity": "sha512-9RbEr1Y7FFfptd/1eEdntyjMwLeghW1bHX9GWjXo19vx4ytPQhANltvVxDggzJl7mnWM+dX28kb6cyS/4iQjlQ==", + "peer": true, "dependencies": { "cssesc": "^3.0.0", "util-deprecate": "^1.0.2" @@ -14300,6 +14321,7 @@ "version": "18.3.1", "resolved": "https://registry.npmjs.org/react/-/react-18.3.1.tgz", "integrity": "sha512-wS+hAgJShR0KhEvPJArfuPVN1+Hz1t0Y6n5jLrGQbkb4urgPE/0Rve+1kMB1v/oWgHgm4WIcV+i7F2pTVj+2iQ==", + "peer": true, "dependencies": { "loose-envify": "^1.1.0" }, @@ -14429,6 +14451,7 @@ "version": "18.3.1", "resolved": "https://registry.npmjs.org/react-dom/-/react-dom-18.3.1.tgz", "integrity": "sha512-5m4nQKp+rZRb09LNH59GM4BxTh9251/ylbKIbpe7TpGxfJ+9kv6BLkLBXIjjspbgbnIBNqlI23tRnTWT0snUIw==", + "peer": true, "dependencies": { "loose-envify": "^1.1.0", "scheduler": "^0.23.2" @@ -14484,6 +14507,7 @@ "version": "6.0.0", "resolved": "https://registry.npmjs.org/@docusaurus/react-loadable/-/react-loadable-6.0.0.tgz", "integrity": "sha512-YMMxTUQV/QFSnbgrP3tjDzLHRg7vsbMn8e9HAa8o/1iXoiomo48b7sk/kkmWEuWNDPJVlKSJRB6Y2fHqdJk+SQ==", + "peer": true, "dependencies": { "@types/react": "*" }, @@ -14510,6 +14534,7 @@ "version": "5.3.4", "resolved": "https://registry.npmjs.org/react-router/-/react-router-5.3.4.tgz", "integrity": "sha512-Ys9K+ppnJah3QuaRiLxk+jDWOR1MekYQrlytiXxC1RyfbdsZkS5pvKAzCCr031xHixZwpnsYNT5xysdFHQaYsA==", + "peer": true, "dependencies": { "@babel/runtime": "^7.12.13", "history": "^4.9.0", @@ -16146,6 +16171,7 @@ "version": "6.12.6", "resolved": "https://registry.npmjs.org/ajv/-/ajv-6.12.6.tgz", "integrity": "sha512-j3fVLgvTo527anyYyJOGTYJbG+vnnQYvE0m5mmkc1TK+nxAppkCLMIL0aZ4dblVCNoGShhm+kzE4ZUykBoMg4g==", + "peer": true, "dependencies": { "fast-deep-equal": "^3.1.1", "fast-json-stable-stringify": "^2.0.0", @@ -16370,6 +16396,7 @@ "version": "5.6.3", "resolved": "https://registry.npmjs.org/typescript/-/typescript-5.6.3.tgz", "integrity": "sha512-hjcS1mhfuyi4WW8IWtjP7brDrG2cuDZukyrYrSauoXGNgx0S7zceP07adYkJycEr56BOUTNPzbInooiN3fn1qw==", + "peer": true, "bin": { "tsc": "bin/tsc", "tsserver": "bin/tsserver" @@ -16687,6 +16714,7 @@ "version": "6.12.6", "resolved": "https://registry.npmjs.org/ajv/-/ajv-6.12.6.tgz", "integrity": "sha512-j3fVLgvTo527anyYyJOGTYJbG+vnnQYvE0m5mmkc1TK+nxAppkCLMIL0aZ4dblVCNoGShhm+kzE4ZUykBoMg4g==", + "peer": true, "dependencies": { "fast-deep-equal": "^3.1.1", "fast-json-stable-stringify": "^2.0.0", @@ -16866,6 +16894,7 @@ "version": "5.97.0", "resolved": "https://registry.npmjs.org/webpack/-/webpack-5.97.0.tgz", "integrity": "sha512-CWT8v7ShSfj7tGs4TLRtaOLmOCPWhoKEvp+eA7FVx8Xrjb3XfT0aXdxDItnRZmE8sHcH+a8ayDrJCOjXKxVFfQ==", + "peer": true, "dependencies": { "@types/eslint-scope": "^3.7.7", "@types/estree": "^1.0.6", @@ -17092,6 +17121,7 @@ "version": "6.12.6", "resolved": "https://registry.npmjs.org/ajv/-/ajv-6.12.6.tgz", "integrity": "sha512-j3fVLgvTo527anyYyJOGTYJbG+vnnQYvE0m5mmkc1TK+nxAppkCLMIL0aZ4dblVCNoGShhm+kzE4ZUykBoMg4g==", + "peer": true, "dependencies": { "fast-deep-equal": "^3.1.1", "fast-json-stable-stringify": "^2.0.0", diff --git a/replace.mjs b/replace.mjs new file mode 100644 index 0000000..2c3b7b7 --- /dev/null +++ b/replace.mjs @@ -0,0 +1,30 @@ +import { readdirSync, readFileSync, writeFileSync } from 'fs'; +import { join } from 'path'; + +function processDir(dir) { + const files = readdirSync(dir, { withFileTypes: true }); + for (const file of files) { + const fullPath = join(dir, file.name); + if (file.isDirectory()) { + processDir(fullPath); + } else if (file.name.endsWith('.md')) { + let content = readFileSync(fullPath, 'utf8'); + let original = content; + + content = content.replace(/axiosOptions/g, 'fetchOptions'); + content = content.replace(/AxiosRequestConfig/g, 'FetchOptions'); + content = content.replace(/Axios options/gi, 'Fetch options'); + content = content.replace(/Axios config/gi, 'Fetch config'); + content = content.replace(/Axios/g, 'Fetch'); + content = content.replace(/axios/g, 'fetch'); + content = content.replace(/restOptions/g, 'fetchOptions'); // get-request.md used restOptions incorrectly + + if (content !== original) { + writeFileSync(fullPath, content, 'utf8'); + console.log(`Updated: ${fullPath}`); + } + } + } +} + +processDir('c:/Users/PC/Documents/phantom-request-docs/docs');