Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ configureOpenApiClient(client, {
Authorization: `Bearer ${token}`
},
onError: (err, options) => {
if (err instanceof OpenApiError && err.response.status === 401) {
if (err instanceof OpenApiError && err.response?.status === 401) {
redirectToLogin();
return null; // the request promise will never settle
}
Expand Down Expand Up @@ -168,14 +168,14 @@ Called when the server returns an error response. Receives an `OpenApiError` and
configureOpenApiClient(client, {
onError: (err, options) => {
// Throw a different error
if (err.response.status === 403) {
if (err.response?.status === 403) {
throw new ForbiddenError(err.message);
}

// Return null to silently discard the error — the request promise
// will never resolve or reject, so the caller hangs and does nothing.
// Useful when you're redirecting the user away from the page entirely.
if (err.response.status === 401) {
if (err.response?.status === 401) {
redirectToLogin();
return null;
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@zyno-io/openapi-client-codegen",
"version": "2.9.0",
"version": "2.9.1",
"description": "OpenAPI client generator for TypeScript apps",
"license": "MIT",
"author": {
Expand Down
10 changes: 5 additions & 5 deletions src/client/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,16 +36,16 @@ export interface OpenApiRequestOptions {
[key: string]: unknown;
}

export type RequestResult<TData = unknown, TError = unknown, ThrowOnError extends boolean = boolean> = ThrowOnError extends true
? Promise<{ data: TData; request: Request; response: Response }>
: Promise<
({ data: TData; error: undefined } | { data: undefined; error: TError }) & {
export type RequestResult<TData = unknown, TError = unknown, ThrowOnError extends boolean = boolean> = Promise<
ThrowOnError extends true
? { data: TData; request: Request; response: Response }
: ({ data: TData; error: undefined } | { data: undefined; error: TError }) & {
// request/response may be undefined, because the error may originate from
// building the request object itself or from a network error
request?: Request;
response?: Response;
}
>;
>;

export interface OpenApiClient {
setConfig(config: Record<string, unknown>): unknown;
Expand Down
14 changes: 12 additions & 2 deletions test/e2e/e2e.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import assert from 'node:assert/strict';
import { existsSync, rmSync } from 'node:fs';
import { existsSync, readFileSync, rmSync } from 'node:fs';
import { createServer, type IncomingMessage, type ServerResponse } from 'node:http';
import path from 'node:path';
import { describe, it, before } from 'node:test';
Expand Down Expand Up @@ -46,10 +46,20 @@ describe('E2E: OpenAPI Client Codegen', () => {
const { client } = await import('./generated/client.gen.js');
configureOpenApiClient(client, {
headers: { 'X-Test': 'value' },
onError: () => null
onError: () => null,
wrapper: async (options, request) => {
return await request(options);
}
});
});

it('uses JSON when an optional multipart upload is omitted', () => {
const sdk = readFileSync(path.join(OUT_PATH, 'sdk.gen.ts'), 'utf8');
const createRequestMethod = sdk.slice(sdk.indexOf('public static createRequest'));

assert.match(createRequestMethod, /'Content-Type': 'application\/json'/);
});

it('converts native Blob and File values to Deepkit multipart payloads', async () => {
const blob = new Blob(['blob-content'], { type: 'text/plain' });
const file = new File(['file-content'], 'file.txt', { type: 'text/plain' });
Expand Down
36 changes: 36 additions & 0 deletions test/e2e/petstore.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,24 @@ paths:
$ref: '#/components/schemas/Pet'
'404':
description: Pet not found
/requests:
post:
operationId: createRequest
tags:
- requests
summary: Create a request with an optional photo
requestBody:
required: true
content:
multipart/form-data:
schema:
$ref: '#/components/schemas/CreateRequestRequest'
application/json:
schema:
$ref: '#/components/schemas/CreateRequestRequest'
responses:
'204':
description: Request created
components:
schemas:
Pet:
Expand Down Expand Up @@ -93,3 +111,21 @@ components:
type: string
name:
type: string
CreateRequestRequest:
type: object
required:
- description
- serviceAddress
properties:
description:
type: string
serviceAddress:
type: object
required:
- city
properties:
city:
type: string
photo:
type: string
format: binary
Loading