Skip to content
Open
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
35 changes: 35 additions & 0 deletions packages/openapi-ts/src/ir/operation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,9 +145,13 @@ export const operationResponsesMap = (
// store default response to be evaluated last
let defaultResponse: IR.ResponseObject | undefined;

const errorVariants: IR.SchemaObject[] = [];


for (const name in operation.responses) {
const response = operation.responses[name]!;


switch (statusCodeToGroup({ statusCode: name })) {
case '1XX':
case '3XX':
Expand All @@ -159,6 +163,23 @@ export const operationResponsesMap = (
case '4XX':
case '5XX':
errors.properties[name] = response.schema;

// TODO cleanup
const statusCode = Number(name);

const variant: IR.SchemaObject = {
type: 'object',
properties: {
status: {
type: 'integer',
const: statusCode,
},
error: response.schema,
},
required: ['status', 'error'],
};

errorVariants.push(variant);
break;
case 'default':
defaultResponse = response;
Expand Down Expand Up @@ -223,6 +244,20 @@ export const operationResponsesMap = (
if (Object.keys(errorUnion).length && errorUnion.type !== 'unknown') {
result.error = errorUnion;
}


if (errorVariants.length) {

let errorsUnion = addItemsToSchema({
items: errorVariants,
mutateSchemaOneItem: true,
schema: {},
});
errorsUnion = deduplicateSchema({ schema: errorsUnion });

// TODO keep it backwards compatible, do not break existing errors type
result.errors = errorsUnion;
}
}

const responseKeys = Object.keys(responses.properties);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,6 @@ export type {
RequestOptions,
RequestResult,
TDataShape,
AxiosErrorWithTypedStatus,
} from './types';
export { createConfig } from './utils';
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,14 @@ export interface ClientOptions {
throwOnError?: boolean;
}

export type AxiosErrorWithTypedStatus<U> =
U extends { error: infer E; status: infer S extends number }
? AxiosError<E> & {
response: AxiosResponse<E> & { status: S };
status?: S;
}
: never;

export type RequestResult<
TData = unknown,
TError = unknown,
Expand All @@ -106,7 +114,7 @@ export type RequestResult<
| (AxiosResponse<
TData extends Record<string, unknown> ? TData[keyof TData] : TData
> & { error: undefined })
| (AxiosError<
| (AxiosErrorWithTypedStatus<
TError extends Record<string, unknown> ? TError[keyof TError] : TError
> & {
data: undefined;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ export const operationToType = ({
const { error, errors, response, responses } =
operationResponsesMap(operation);

// TODO here
if (errors) {
const symbolErrors = plugin.registerSymbol({
exported: true,
Expand Down Expand Up @@ -217,9 +218,7 @@ export const operationToType = ({
.alias(symbol.placeholder)
.export(symbol.exported)
.type(
$.type(symbolErrors.placeholder).idx(
$.type(symbolErrors.placeholder).keyof(),
),
$.type(symbolErrors.placeholder).idx($.type.literal("error")),
);
plugin.setSymbolValue(symbol, node);
}
Expand Down
4 changes: 2 additions & 2 deletions packages/openapi-ts/src/plugins/@pinia/colada/useType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export const useTypeError = ({
category: 'type',
resource: 'operation',
resourceId: operation.id,
role: 'error',
role: 'errors',
});

let typeErrorName: string | undefined = symbolErrorType?.placeholder;
Expand All @@ -39,7 +39,7 @@ export const useTypeError = ({
if (client.name === '@hey-api/client-axios') {
const symbol = plugin.referenceSymbol({
category: 'external',
resource: 'axios.AxiosError',
resource: 'client',
});
typeErrorName = `${symbol.placeholder}<${typeErrorName}>`;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,17 @@ export const useTypeError = ({
}): string => {
const client = getClientPlugin(plugin.context.config);

// TODO use the errors type here
const symbolErrorType = plugin.querySymbol({
category: 'type',
resource: 'operation',
resourceId: operation.id,
role: 'error',
role: 'errors',
});

let typeErrorName: string | undefined = symbolErrorType?.placeholder;
if (!typeErrorName) {
// TODO fix the default error
const symbol = plugin.referenceSymbol({
category: 'external',
resource: `${plugin.name}.DefaultError`,
Expand All @@ -43,7 +45,7 @@ export const useTypeError = ({
if (client.name === '@hey-api/client-axios') {
const symbol = plugin.referenceSymbol({
category: 'external',
resource: 'axios.AxiosError',
resource: 'AxiosErrorWithTypedStatus',
});
typeErrorName = `${symbol.placeholder}<${typeErrorName}>`;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,14 +66,15 @@ export const handlerV5: PluginHandler = ({ plugin }) => {
},
name: 'useQuery',
});
// TODO what is the proper way?
plugin.registerSymbol({
external: 'axios',
external: '../client/types.gen',
kind: 'type',
meta: {
category: 'external',
resource: 'axios.AxiosError',
resource: 'AxiosErrorWithTypedStatus',
},
name: 'AxiosError',
name: 'AxiosErrorWithTypedStatus',
});

const sdkPlugin = plugin.getPluginOrThrow('@hey-api/sdk');
Expand Down
Loading