|
| 1 | +# @fecommunity/reactpress-toolkit |
| 2 | + |
| 3 | +A TypeScript-based API client toolkit for ReactPress, automatically generated from OpenAPI/Swagger specifications. |
| 4 | + |
| 5 | +## Overview |
| 6 | + |
| 7 | +The ReactPress Toolkit is a comprehensive API client library that provides strongly-typed interfaces for interacting with the ReactPress backend services. It includes: |
| 8 | + |
| 9 | +- **Auto-generated API clients** for all ReactPress modules |
| 10 | +- **TypeScript definitions** for all data contracts |
| 11 | +- **Utility functions** for common operations |
| 12 | +- **HTTP client** with built-in authentication and error handling |
| 13 | + |
| 14 | +## Installation |
| 15 | + |
| 16 | +```bash |
| 17 | +npm install @fecommunity/reactpress-toolkit |
| 18 | +``` |
| 19 | + |
| 20 | +or |
| 21 | + |
| 22 | +```bash |
| 23 | +yarn add @fecommunity/reactpress-toolkit |
| 24 | +``` |
| 25 | + |
| 26 | +or |
| 27 | + |
| 28 | +```bash |
| 29 | +pnpm add @fecommunity/reactpress-toolkit |
| 30 | +``` |
| 31 | + |
| 32 | +## Quick Start |
| 33 | + |
| 34 | +### Using the Default API Instance |
| 35 | + |
| 36 | +```typescript |
| 37 | +import api from '@fecommunity/reactpress-toolkit'; |
| 38 | + |
| 39 | +// Get all articles |
| 40 | +const articles = await api.article.findAll(); |
| 41 | + |
| 42 | +// Get a specific article by ID |
| 43 | +const article = await api.article.findById('article-id'); |
| 44 | + |
| 45 | +// Create a new article |
| 46 | +const newArticle = await api.article.create({ |
| 47 | + title: 'My New Article', |
| 48 | + content: 'Article content here...', |
| 49 | + // ... other properties |
| 50 | +}); |
| 51 | +``` |
| 52 | + |
| 53 | +### Using Named Exports |
| 54 | + |
| 55 | +```typescript |
| 56 | +import { api, types, utils } from '@fecommunity/reactpress-toolkit'; |
| 57 | + |
| 58 | +// Working with typed data |
| 59 | +const article: types.IArticle = { |
| 60 | + id: '1', |
| 61 | + title: 'Sample Article', |
| 62 | + // ... other properties |
| 63 | +}; |
| 64 | + |
| 65 | +// Using utility functions |
| 66 | +const formattedDate = utils.formatDate(new Date()); |
| 67 | +``` |
| 68 | + |
| 69 | +## API Modules |
| 70 | + |
| 71 | +The toolkit provides clients for all ReactPress backend modules: |
| 72 | + |
| 73 | +- `api.article` - Article management |
| 74 | +- `api.auth` - Authentication services |
| 75 | +- `api.category` - Category management |
| 76 | +- `api.comment` - Comment system |
| 77 | +- `api.file` - File management |
| 78 | +- `api.knowledge` - Knowledge base |
| 79 | +- `api.page` - Page management |
| 80 | +- `api.search` - Search functionality |
| 81 | +- `api.setting` - System settings |
| 82 | +- `api.smtp` - SMTP services |
| 83 | +- `api.tag` - Tag management |
| 84 | +- `api.user` - User management |
| 85 | +- `api.view` - View analytics |
| 86 | + |
| 87 | +## Configuration |
| 88 | + |
| 89 | +You can create a custom API instance with specific configuration: |
| 90 | + |
| 91 | +```typescript |
| 92 | +import { createApiInstance } from '@fecommunity/reactpress-toolkit'; |
| 93 | + |
| 94 | +const customApi = createApiInstance({ |
| 95 | + baseURL: 'https://api.yourdomain.com', |
| 96 | + timeout: 5000, |
| 97 | + // ... other axios configuration options |
| 98 | +}); |
| 99 | + |
| 100 | +// Use the custom instance |
| 101 | +const articles = await customApi.article.findAll(); |
| 102 | +``` |
| 103 | + |
| 104 | +## Type Definitions |
| 105 | + |
| 106 | +All data models are strongly typed and available through the `types` export: |
| 107 | + |
| 108 | +```typescript |
| 109 | +import { types } from '@fecommunity/reactpress-toolkit'; |
| 110 | + |
| 111 | +const user: types.IUser = { |
| 112 | + name: 'John Doe', |
| 113 | + |
| 114 | + // ... other properties |
| 115 | +}; |
| 116 | +``` |
| 117 | + |
| 118 | +Available type definitions include: |
| 119 | +- `types.IUser` |
| 120 | +- `types.IArticle` |
| 121 | +- `types.ICategory` |
| 122 | +- `types.ITag` |
| 123 | +- `types.IComment` |
| 124 | +- `types.IFile` |
| 125 | +- `types.ISetting` |
| 126 | +- `types.IPage` |
| 127 | +- `types.IKnowledge` |
| 128 | +- `types.IView` |
| 129 | +- `types.I_SMTP` |
| 130 | + |
| 131 | +## Utility Functions |
| 132 | + |
| 133 | +The toolkit includes helpful utility functions: |
| 134 | + |
| 135 | +```typescript |
| 136 | +import { utils } from '@fecommunity/reactpress-toolkit'; |
| 137 | + |
| 138 | +// Date formatting |
| 139 | +const formattedDate = utils.formatDate(new Date(), 'YYYY-MM-DD'); |
| 140 | + |
| 141 | +// Deep cloning |
| 142 | +const clonedObject = utils.deepClone(originalObject); |
| 143 | + |
| 144 | +// Error handling |
| 145 | +if (utils.ApiError.isInstance(error)) { |
| 146 | + console.log(`API Error: ${error.code} - ${error.message}`); |
| 147 | +} |
| 148 | +``` |
| 149 | + |
| 150 | +## Development |
| 151 | + |
| 152 | +### Generating API Definitions |
| 153 | + |
| 154 | +The toolkit is automatically generated from the ReactPress backend's OpenAPI specification: |
| 155 | + |
| 156 | +```bash |
| 157 | +npm run generate |
| 158 | +``` |
| 159 | + |
| 160 | +This command will: |
| 161 | +1. Generate a new Swagger JSON from the ReactPress server |
| 162 | +2. Create TypeScript definitions from the specification |
| 163 | +3. Organize the generated files into appropriate directories |
| 164 | +4. Create API clients for each module |
| 165 | + |
| 166 | +### Building |
| 167 | + |
| 168 | +```bash |
| 169 | +npm run build |
| 170 | +``` |
| 171 | + |
| 172 | +This will compile the TypeScript code to JavaScript in the `dist` directory. |
| 173 | + |
| 174 | +## Contributing |
| 175 | + |
| 176 | +1. Fork the repository |
| 177 | +2. Create your feature branch (`git checkout -b feature/AmazingFeature`) |
| 178 | +3. Commit your changes (`git commit -m 'Add some AmazingFeature'`) |
| 179 | +4. Push to the branch (`git push origin feature/AmazingFeature`) |
| 180 | +5. Open a pull request |
| 181 | + |
| 182 | +## License |
| 183 | + |
| 184 | +ISC |
| 185 | + |
| 186 | +## Related Projects |
| 187 | + |
| 188 | +- [ReactPress](https://github.com/fecommunity/reactpress) - The main ReactPress platform |
| 189 | +- [ReactPress Server](https://github.com/fecommunity/reactpress/server) - Backend server |
| 190 | +- [ReactPress Client](https://github.com/fecommunity/reactpress/client) - Frontend client |
0 commit comments