Skip to content

Commit

Permalink
Merge pull request #442 from hey-api/feat/add-errors-to-operation-map…
Browse files Browse the repository at this point in the history
…pings
  • Loading branch information
jordanshatford authored Apr 19, 2024
2 parents fbf5de7 + 0324ef2 commit 840d9df
Show file tree
Hide file tree
Showing 19 changed files with 466 additions and 34 deletions.
5 changes: 5 additions & 0 deletions .changeset/short-cows-think.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@hey-api/openapi-ts": minor
---

feat: add operation error type mappings
7 changes: 1 addition & 6 deletions packages/openapi-ts/src/openApi/common/interfaces/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,6 @@ export interface Enum {
value: string | number;
}

export interface OperationError {
code: number;
description: string;
}

export interface OperationParameter extends Model {
in: 'path' | 'query' | 'header' | 'formData' | 'body' | 'cookie';
prop: string;
Expand All @@ -39,7 +34,7 @@ export interface OperationResponse extends Model {
export interface Operation extends OperationParameters {
deprecated: boolean;
description: string | null;
errors: OperationError[];
errors: OperationResponse[];
method: 'DELETE' | 'GET' | 'HEAD' | 'OPTIONS' | 'PATCH' | 'POST' | 'PUT';
/**
* Method name. Methods contain the request logic.
Expand Down
17 changes: 6 additions & 11 deletions packages/openapi-ts/src/openApi/common/parser/operation.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import camelCase from 'camelcase';

import { getConfig } from '../../../utils/config';
import type { OperationError, OperationResponse } from '../interfaces/client';
import type { OperationResponse } from '../interfaces/client';
import { reservedWords } from './reservedWords';
import {
sanitizeNamespaceIdentifier,
Expand Down Expand Up @@ -74,13 +74,8 @@ export const getOperationResponseCode = (

export const getOperationErrors = (
operationResponses: OperationResponse[],
): OperationError[] =>
operationResponses
.filter(
(operationResponse) =>
operationResponse.code >= 300 && operationResponse.description,
)
.map((response) => ({
code: response.code,
description: response.description!,
}));
): OperationResponse[] =>
operationResponses.filter(
(operationResponse) =>
operationResponse.code >= 300 && operationResponse.description,
);
19 changes: 18 additions & 1 deletion packages/openapi-ts/src/utils/write/models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,8 +144,9 @@ const processServiceTypes = (services: Service[], onNode: OnNode) => {
service.operations.forEach((operation) => {
const hasReq = operation.parameters.length;
const hasRes = operation.results.length;
const hasErr = operation.errors.length;

if (hasReq || hasRes) {
if (hasReq || hasRes || hasErr) {
let pathMap = pathsMap.get(operation.path);
if (!pathMap) {
pathsMap.set(operation.path, new Map());
Expand Down Expand Up @@ -177,6 +178,22 @@ const processServiceTypes = (services: Service[], onNode: OnNode) => {
resMap.set(result.code, result);
});
}

if (hasErr) {
let resMap = methodMap.get('res');
if (!resMap) {
methodMap.set('res', new Map());
resMap = methodMap.get('res')!;
}

if (Array.isArray(resMap)) {
return;
}

operation.errors.forEach((error) => {
resMap.set(error.code, error);
});
}
}
});
});
Expand Down
6 changes: 2 additions & 4 deletions packages/openapi-ts/src/utils/write/services.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,7 @@ const toOperationParamType = (operation: Operation): FunctionParameter[] => {
const toOperationReturnType = (operation: Operation) => {
const config = getConfig();
const baseTypePath = `${serviceExportedNamespace()}['${operation.path}']['${operation.method.toLocaleLowerCase()}']['res']`;
const results = operation.results.filter(
(result) => result.code >= 200 && result.code < 300,
);
const results = operation.results;
// TODO: we should return nothing when results don't exist
// can't remove this logic without removing request/name config
// as it complicates things
Expand Down Expand Up @@ -157,7 +155,7 @@ const toRequestOptions = (operation: Operation) => {
if (operation.errors.length) {
const errors: Record<number, string> = {};
operation.errors.forEach((err) => {
errors[err.code] = escapeDescription(err.description);
errors[err.code] = escapeDescription(err.description ?? '');
});
obj.errors = errors;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,17 @@ export const $ModelWithString = {
},
} as const;

export const $ModelWithStringError = {
description: 'This is a model with one string property',
type: 'object',
properties: {
prop: {
description: 'This is a simple string property',
type: 'string',
},
},
} as const;

export const $ModelWithNullableString = {
description: 'This is a model with one string property',
type: 'object',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,16 @@ export type ModelWithString = {
prop?: string;
};

/**
* This is a model with one string property
*/
export type ModelWithStringError = {
/**
* This is a simple string property
*/
prop?: string;
};

/**
* This is a model with one string property
*/
Expand Down Expand Up @@ -628,6 +638,18 @@ export type $OpenApiTs = {
* Message for default response
*/
200: ModelWithString;
/**
* Message for 500 error
*/
500: ModelWithStringError;
/**
* Message for 501 error
*/
501: ModelWithStringError;
/**
* Message for 502 error
*/
502: ModelWithStringError;
};
};
put: {
Expand All @@ -644,6 +666,18 @@ export type $OpenApiTs = {
* Message for 202 response
*/
202: ModelThatExtendsExtends;
/**
* Message for 500 error
*/
500: ModelWithStringError;
/**
* Message for 501 error
*/
501: ModelWithStringError;
/**
* Message for 502 error
*/
502: ModelWithStringError;
};
};
};
Expand Down Expand Up @@ -774,6 +808,14 @@ export type $OpenApiTs = {
* Successful response
*/
200: Array<ModelWithString>;
/**
* 400 server error
*/
400: unknown;
/**
* 500 server error
*/
500: unknown;
};
};
};
Expand All @@ -784,6 +826,14 @@ export type $OpenApiTs = {
* Successful response
*/
200: string;
/**
* 400 server error
*/
400: unknown;
/**
* 500 server error
*/
500: unknown;
};
};
};
Expand All @@ -800,6 +850,22 @@ export type $OpenApiTs = {
* Custom message: Successful response
*/
200: unknown;
/**
* Custom message: Internal Server Error
*/
500: unknown;
/**
* Custom message: Not Implemented
*/
501: unknown;
/**
* Custom message: Bad Gateway
*/
502: unknown;
/**
* Custom message: Service Unavailable
*/
503: unknown;
};
};
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,17 @@ export const $ModelWithString = {
},
} as const;

export const $ModelWithStringError = {
description: 'This is a model with one string property',
type: 'object',
properties: {
prop: {
description: 'This is a simple string property',
type: 'string',
},
},
} as const;

export const $Model_From_Zendesk = {
description: `\`Comment\` or \`VoiceComment\`. The JSON object for adding voice comments to tickets is different. See [Adding voice comments to tickets](/documentation/ticketing/managing-tickets/adding-voice-comments-to-tickets)`,
type: 'string',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,16 @@ export type ModelWithString = {
prop?: string;
};

/**
* This is a model with one string property
*/
export type ModelWithStringError = {
/**
* This is a simple string property
*/
prop?: string;
};

/**
* `Comment` or `VoiceComment`. The JSON object for adding voice comments to tickets is different. See [Adding voice comments to tickets](/documentation/ticketing/managing-tickets/adding-voice-comments-to-tickets)
*/
Expand Down Expand Up @@ -1214,6 +1224,18 @@ export type $OpenApiTs = {
* Message for default response
*/
200: ModelWithString;
/**
* Message for 500 error
*/
500: ModelWithStringError;
/**
* Message for 501 error
*/
501: ModelWithStringError;
/**
* Message for 502 error
*/
502: ModelWithStringError;
};
};
put: {
Expand All @@ -1230,6 +1252,18 @@ export type $OpenApiTs = {
* Message for 202 response
*/
202: ModelThatExtendsExtends;
/**
* Message for 500 error
*/
500: ModelWithStringError;
/**
* Message for 501 error
*/
501: ModelWithStringError;
/**
* Message for 502 error
*/
502: ModelWithStringError;
};
};
};
Expand Down Expand Up @@ -1390,6 +1424,14 @@ export type $OpenApiTs = {
* Successful response
*/
200: Array<ModelWithString>;
/**
* 400 `server` error
*/
400: unknown;
/**
* 500 server error
*/
500: unknown;
};
};
};
Expand Down Expand Up @@ -1454,6 +1496,14 @@ export type $OpenApiTs = {
* Successful response
*/
200: string;
/**
* 400 server error
*/
400: unknown;
/**
* 500 server error
*/
500: unknown;
};
};
};
Expand All @@ -1470,6 +1520,22 @@ export type $OpenApiTs = {
* Custom message: Successful response
*/
200: unknown;
/**
* Custom message: Internal Server Error
*/
500: unknown;
/**
* Custom message: Not Implemented
*/
501: unknown;
/**
* Custom message: Bad Gateway
*/
502: unknown;
/**
* Custom message: Service Unavailable
*/
503: unknown;
};
};
};
Expand Down
Loading

0 comments on commit 840d9df

Please sign in to comment.