|
| 1 | +--- |
| 2 | +name: devup-api |
| 3 | +description: A tool for generating fully typed API clients from OpenAPI schemas. It offers a fetch-compatible API, auto-generated types without generics, and integrates with major build tools like Vite, Next.js, and Webpack. |
| 4 | +--- |
| 5 | + |
| 6 | +# devup-api |
| 7 | + |
| 8 | +This skill helps you invoke the `devup-api` library to generate and use fully typed API clients in your TypeScript projects. `devup-api` reads your `openapi.json` and automatically generates a type-safe client that feels like `fetch` but with strict typing for paths, parameters, bodies, and responses. |
| 9 | + |
| 10 | +## Key Features |
| 11 | + |
| 12 | +- **OpenAPI-driven:** Generates types directly from `openapi.json`. |
| 13 | +- **Fetch-compatible:** Ergonomic API similar to standard `fetch`. |
| 14 | +- **Zero Generics:** No complex generic types to manage manually. |
| 15 | +- **Build Tool Integration:** Plugins for Vite, Next.js, Webpack, and Rsbuild. |
| 16 | +- **Two-phase Typing:** "Cold Typing" (relaxed types for initial setup) and "Bold Typing" (strict types after generation). |
| 17 | + |
| 18 | +## Usage Instructions |
| 19 | + |
| 20 | +### 1. Installation |
| 21 | + |
| 22 | +Install the core fetch package and the plugin for your build tool: |
| 23 | + |
| 24 | +```bash |
| 25 | +npm install @devup-api/fetch @devup-api/vite-plugin # For Vite |
| 26 | +# OR |
| 27 | +npm install @devup-api/fetch @devup-api/next-plugin # For Next.js |
| 28 | +# See README for Webpack and Rsbuild |
| 29 | +``` |
| 30 | + |
| 31 | +### 2. Configuration |
| 32 | + |
| 33 | +Add the plugin to your build configuration (e.g., `vite.config.ts`, `next.config.ts`). |
| 34 | + |
| 35 | +**Vite Example:** |
| 36 | +```ts |
| 37 | +import { defineConfig } from 'vite' |
| 38 | +import devupApi from '@devup-api/vite-plugin' |
| 39 | + |
| 40 | +export default defineConfig({ |
| 41 | + plugins: [devupApi()], |
| 42 | +}) |
| 43 | +``` |
| 44 | + |
| 45 | +### 3. TypeScript Setup |
| 46 | + |
| 47 | +Include the generated types in your `tsconfig.json`: |
| 48 | + |
| 49 | +```json |
| 50 | +{ |
| 51 | + "include": [ |
| 52 | + "src", |
| 53 | + "df/**/*.d.ts" |
| 54 | + ] |
| 55 | +} |
| 56 | +``` |
| 57 | +*Note: `df` is the default temporary directory.* |
| 58 | + |
| 59 | +### 4. Create and Use Client |
| 60 | + |
| 61 | +```ts |
| 62 | +import { createApi } from '@devup-api/fetch' |
| 63 | + |
| 64 | +// Initialize |
| 65 | +const api = createApi('https://api.example.com') |
| 66 | + |
| 67 | +// GET Request (using operationId or path) |
| 68 | +const users = await api.get('getUsers', { query: { page: 1 } }) |
| 69 | +// OR |
| 70 | +const user = await api.get('/users/{id}', { params: { id: '123' } }) |
| 71 | + |
| 72 | +// POST Request |
| 73 | +const newUser = await api.post('createUser', { |
| 74 | + body: { name: 'Alice', email: '[email protected]' } |
| 75 | +}) |
| 76 | +``` |
| 77 | + |
| 78 | +## Examples |
| 79 | + |
| 80 | +### Complete Workflow |
| 81 | + |
| 82 | +1. **Project Setup:** Ensure `openapi.json` is in your project root. |
| 83 | +2. **Configure:** Add `devup-api/vite-plugin` to `vite.config.ts`. |
| 84 | +3. **Run:** Run `npm run dev` or `npm run build`. This generates `df/api.d.ts`. |
| 85 | +4. **Code:** Use `createApi` to make requests. IntelliSense will now show available paths and required parameters. |
| 86 | + |
| 87 | +### Handling Responses |
| 88 | + |
| 89 | +`devup-api` returns an object with either `data` (success) or `error` (failure). |
| 90 | + |
| 91 | +```ts |
| 92 | +const response = await api.get('getUser', { params: { id: '1' } }) |
| 93 | + |
| 94 | +if (response.data) { |
| 95 | + console.log('User Name:', response.data.name) |
| 96 | +} else if (response.error) { |
| 97 | + console.error('Error:', response.error.message) |
| 98 | +} |
| 99 | +``` |
| 100 | + |
| 101 | +## Guidelines |
| 102 | + |
| 103 | +- **"Cold" vs "Bold" Typing:** When you first start, types might be `any` (Cold Typing). Run your build command (`dev` or `build`) to generate the types and enable strict checking (Bold Typing). |
| 104 | +- **Operation IDs vs Paths:** You can use either the OpenAPI `operationId` (e.g., `getUsers`) or the URL path (e.g., `/users`). `operationId` is often more concise. |
| 105 | +- **Generated Files:** Do not manually edit the files in the `df` (or configured temp) directory. They are auto-generated. |
| 106 | +- **Verification:** If types seem missing, ensure `tsconfig.json` includes the generated folder and that the build script has run at least once. |
0 commit comments