ngx-openapi-typescript 0.0.2 → 0.0.3

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.
Files changed (2) hide show
  1. package/README.md +138 -0
  2. package/package.json +1 -1
package/README.md ADDED
@@ -0,0 +1,138 @@
1
+ # ngx-openapi-typescript
2
+
3
+ Type-safe Angular resource signals and HTTP client backed by [openapi-typescript](https://openapi-ts.dev/).
4
+
5
+ This library provides two main utilities:
6
+
7
+ - **`createOpenApiResource`** — a reactive factory built on Angular's `httpResource` that gives you compile-time type safety for every HTTP request and response shape defined in your OpenAPI schema.
8
+ - **`OpenApiHttpClient`** — a typed wrapper around Angular's `HttpClient` for imperative (non-reactive) HTTP calls.
9
+
10
+ Both are driven by the types generated by `openapi-typescript`, so typos in paths, wrong parameter names, or incorrect body shapes are caught at **compile time**, not at runtime.
11
+
12
+ ## Installation
13
+
14
+ ```sh
15
+ npm install ngx-openapi-typescript openapi-typescript-helpers
16
+ ```
17
+
18
+ This package is built for Angular `^21.2.0` and `^22.0.0` and expects these peer dependencies in your app:
19
+
20
+ - `@angular/common ^21.2.0 || ^22.0.0`
21
+ - `@angular/core ^21.2.0 || ^22.0.0`
22
+ - `openapi-typescript-helpers ^0.1.0`
23
+
24
+ If you want to generate types locally, also install the generator as a dev dependency:
25
+
26
+ ```sh
27
+ npm install -D openapi-typescript
28
+ ```
29
+
30
+ ## Generating types
31
+
32
+ Generate type definitions by pointing `openapi-typescript` at your OpenAPI spec:
33
+
34
+ ```sh
35
+ npx openapi-typescript https://api.example.com/openapi.yaml -o src/api.types.ts
36
+ ```
37
+
38
+ This creates a `paths` interface that both utilities consume.
39
+
40
+ ## API
41
+
42
+ ### `createOpenApiResource`
43
+
44
+ A factory for creating reactive Angular resource signals. Must be called inside an Angular injection context (component/service field initializer or constructor).
45
+
46
+ ```ts
47
+ import type { paths } from './api.types';
48
+ import { createOpenApiResource } from 'ngx-openapi-typescript';
49
+
50
+ @Component({ ... })
51
+ export class PetListComponent {
52
+ private readonly api = createOpenApiResource<paths>({
53
+ baseUrl: 'https://api.example.com',
54
+ });
55
+
56
+ // Simple GET — no params required
57
+ protected readonly pets = this.api('/pets');
58
+
59
+ // Reactive GET with params — re-fetches when the signals read inside the lambda change
60
+ protected readonly petsLimited = this.api('/pets', () => ({
61
+ params: { query: { limit: 10 } },
62
+ }));
63
+
64
+ // Pass signal-driven path params — re-fetches when petId() changes
65
+ private readonly petId = signal(1);
66
+ protected readonly pet = this.api('/pets/{petId}', () => ({
67
+ params: { path: { petId: this.petId() } },
68
+ }));
69
+
70
+ // Return undefined to put the resource in Idle state (no request made)
71
+ private readonly enabled = signal(false);
72
+ protected readonly lazyPets = this.api('/pets', () =>
73
+ this.enabled() ? {} : undefined,
74
+ );
75
+ }
76
+ ```
77
+
78
+ #### Method Override
79
+
80
+ The factory defaults to GET. You can pass a `method` option to use a different HTTP method,
81
+ but Angular recommends using `httpResource` only for reads. Prefer `OpenApiHttpClient` for mutations.
82
+
83
+ ```ts
84
+ // Explicit method override (not recommended for production use)
85
+ protected readonly createResult = this.api('/pets', () => ({
86
+ body: { name: 'Buddy' },
87
+ }), { method: 'post' });
88
+ ```
89
+
90
+ ### `OpenApiHttpClient`
91
+
92
+ A base class that wraps Angular's `HttpClient` with OpenAPI types. Ideal for imperative calls, mutations, or service-layer code.
93
+
94
+ ```ts
95
+ import type { paths } from './api.types';
96
+ import { OpenApiHttpClient } from 'ngx-openapi-typescript';
97
+ import { Injectable } from '@angular/core';
98
+
99
+ @Injectable({ providedIn: 'root' })
100
+ export class PetApiClient extends OpenApiHttpClient<paths> {
101
+ constructor() {
102
+ super({ baseUrl: 'https://api.example.com' });
103
+ }
104
+ }
105
+ ```
106
+
107
+ ```ts
108
+ // In a component/service
109
+ @Component({ ... })
110
+ export class PetDetailComponent {
111
+ private readonly api = inject(PetApiClient);
112
+
113
+ load(id: number) {
114
+ this.api
115
+ .get('/pets/{petId}', { params: { path: { petId: id } } })
116
+ .subscribe(pet => console.log(pet)); // pet is typed as Pet
117
+ }
118
+
119
+ create(name: string) {
120
+ this.api
121
+ .post('/pets', { body: { name } })
122
+ .subscribe(newPet => console.log(newPet));
123
+ }
124
+ }
125
+ ```
126
+
127
+ ## Development
128
+
129
+ ```sh
130
+ # Build the library
131
+ ng build ngx-openapi-typescript
132
+
133
+ # Run tests
134
+ ng test ngx-openapi-typescript
135
+
136
+ # Start the demo app
137
+ ng serve demo
138
+ ```
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ngx-openapi-typescript",
3
- "version": "0.0.2",
3
+ "version": "0.0.3",
4
4
  "description": "Type-safe Angular httpResource and HttpClient backed by openapi-typescript",
5
5
  "keywords": [
6
6
  "angular",