Skip to content

Commit 2166744

Browse files
constantantclaude
andcommitted
docs(openapi-resource-gen): document non-JSON response handling (text/blob)
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent a335333 commit 2166744

1 file changed

Lines changed: 59 additions & 0 deletions

File tree

tools/openapi-resource-gen/README.md

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -617,6 +617,65 @@ export type UpsertResourceResponse =
617617
The `httpResource<UpsertResourceResponse>` call site receives a value that is the
618618
union of all possible success shapes.
619619

620+
### Non-JSON responses (text and binary)
621+
622+
When a 2xx response has a non-JSON content type, the generator picks the correct
623+
`httpResource` variant automatically — no manual file maintenance needed:
624+
625+
| Content type | `httpResource` call | `XxxResponse` type |
626+
|---|---|---|
627+
| `application/json` (default) | `httpResource<XxxResponse>()` | derived from `paths[…]['application/json']` |
628+
| `text/plain`, `text/*` | `httpResource.text()` | `string` |
629+
| `image/*`, `video/*`, `audio/*`, `application/octet-stream`, `application/pdf`, … | `httpResource.blob()` | `Blob` |
630+
631+
```typescript
632+
// GET /reports/{id} — response: { '200': { content: { 'text/plain': … } } }
633+
export type GetReportResponse = string;
634+
635+
export const GET_REPORT = new InjectionToken<
636+
(id: string) => ReturnType<typeof httpResource.text>
637+
>('GET_REPORT');
638+
639+
export function provideGetReport(): FactoryProvider {
640+
return {
641+
provide: GET_REPORT,
642+
useFactory: () => {
643+
const base = inject(MYAPI_BASE_URL);
644+
return (id: string) =>
645+
httpResource.text(() => ({ url: `${base}/reports/${id}` }));
646+
},
647+
};
648+
}
649+
```
650+
651+
```typescript
652+
// GET /images/{id} — response: { '200': { content: { 'image/png': … } } }
653+
export type GetImageResponse = Blob;
654+
655+
export const GET_IMAGE = new InjectionToken<
656+
(id: string) => ReturnType<typeof httpResource.blob>
657+
>('GET_IMAGE');
658+
```
659+
660+
`XxxResponse` is always emitted so consumers have a stable type to import regardless
661+
of the content kind. Use it to type component state:
662+
663+
```typescript
664+
readonly report = this.getReport('q4-2025');
665+
readonly reportText = computed(() => this.report.value() ?? '');
666+
667+
readonly image = this.getImage('logo');
668+
readonly imageUrl = computed(() => {
669+
const blob = this.image.value();
670+
return blob ? URL.createObjectURL(blob) : null;
671+
});
672+
```
673+
674+
> **`httpResource.arrayBuffer`** is available in Angular 22 but the generator does
675+
> not emit it — `Blob` covers all practical binary use cases (downloads, object URLs,
676+
> streaming). Add `x-response-type: arraybuffer` as a vendor extension in the spec
677+
> if you need raw byte access; the generator will recognise it in a future release.
678+
620679
### Typed error aliases
621680

622681
For 4xx/5xx responses that carry JSON bodies, the generator emits an `XxxError` type alias

0 commit comments

Comments
 (0)