@ng-openapi/http-resource
Version:
HTTP Resource plugin for ng-openapi - Angular HTTP utilities with caching and state management
123 lines (99 loc) β’ 4.01 kB
Markdown
<div align="center">
<h1 align="center"><img src="https://raw.githubusercontent.com/ng-openapi/ng-openapi/HEAD/docs/public/ng-openapi-logo.svg" alt="Logo" style="height: 12vh; margin-bottom: 2vh;" width="160"></h1>
<h1 align="center"><b>HTTP Resource Plugin</b></h1>
<p align="center">π Angular httpResource integration for <a href="https://www.npmjs.com/package/ng-openapi">ng-openapi</a></p>
</div>
<p align="center">
<a href="https://stackblitz.com/@Mr-Jami/collections/ng-openapi-examples">β‘Examples</a>
<span> β’ </span>
<a href="https://ng-openapi.dev/">πDocumentation</a>
<span> β’ </span>
<a href="https://github.com/ng-openapi/ng-openapi/issues">πIssues</a>
</p>
<p align="center">
<a href="https://www.npmjs.com/package/@ng-openapi/http-resource" rel="nofollow"><img src="https://img.shields.io/npm/v/@ng-openapi/http-resource.svg" alt="npm version"></a>
<a href="https://opensource.org/license/mit" rel="nofollow"><img src="https://img.shields.io/github/license/ng-openapi/ng-openapi" alt="MIT License"></a>
<a href="https://github.com/ng-openapi/ng-openapi/actions?query=branch%3Amain"><img src="https://img.shields.io/github/last-commit/ng-openapi/ng-openapi" alt="Last commit" /></a>
<a href="https://github.com/ng-openapi/ng-openapi/actions?query=branch%3Amain"><img src="https://github.com/ng-openapi/ng-openapi/actions/workflows/release.yml/badge.svg?event=push&branch=main" alt="CI status" /></a>
</p>
<br/>
## What is HTTP Resource Plugin?
The HTTP Resource plugin generates Angular services using the new **experimental** `httpResource` API instead of
traditional `HttpClient`. This provides automatic caching, state management, and reactive data loading for your OpenAPI
endpoints.
> β οΈ **Experimental Feature**: `httpResource` is still experimental in Angular. Use with caution in production.
## Installation
```bash
npm install @ng-openapi/http-resource ng-openapi --save-dev
```
## Quick Start
### 1. Configure Plugin
```typescript
// openapi.config.ts
import { GeneratorConfig } from 'ng-openapi';
import { HttpResourcePlugin } from '@ng-openapi/http-resource';
export default {
input: './swagger.json',
output: './src/api',
clientName: 'NgOpenApi',
plugins: [HttpResourcePlugin],
} as GeneratorConfig;
```
### 2. Generate Resources
```bash
ng-openapi -c openapi.config.ts
```
### 3. Setup Providers
```typescript
// app.config.ts
import { ApplicationConfig } from '@angular/core';
import { provideDefaultClient } from './api/providers';
export const appConfig: ApplicationConfig = {
providers: [
provideNgOpenApiClient({
basePath: 'https://api.example.com'
})
]
};
```
### 4. Use Generated Resources
```typescript
import { Component, inject } from '@angular/core';
import { UsersResource } from './api/resources';
@Component({
selector: 'app-users',
template: `
<div>
@if (users.isLoading()) {
<p>Loading...</p>
}
@if (users.error()) {
<p>Error: {{ users.error() }}</p>
}
@for (user of users.value(); track user.id) {
<div>{{ user.name }}</div>
}
</div>
`
})
export class UsersComponent {
private readonly usersResource = inject(UsersResource);
// Automatic caching and reactive updates
readonly users = this.usersResource.getUsers();
}
```
## Generated Structure
```
src/api/
βββ models/ # TypeScript interfaces
βββ resources/ # HTTP Resource services
β βββ index.ts # Resource exports
β βββ *.resource.ts # Generated resources
βββ services/ # Traditional HttpClient services
βββ providers.ts # Provider functions
βββ index.ts # Main exports
```
## Documentation
- π [Full Documentation](https://ng-openapi.dev/plugins/http-resource)
- π― [Angular httpResource Guide](https://angular.dev/guide/http/resource)
- π [Live Examples](https://stackblitz.com/@Mr-Jami/collections/ng-openapi-examples)