Skip to content

Commit

Permalink
Update PostmanToOpenAPIConverter.tsx
Browse files Browse the repository at this point in the history
  • Loading branch information
wise4rmgod committed Oct 27, 2024
1 parent 580a3c4 commit 76bd5d3
Showing 1 changed file with 73 additions and 6 deletions.
79 changes: 73 additions & 6 deletions src/components/PostmanToOpenAPIConverter.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,17 @@ type PostmanFormData = {
value: string;
}

// Add response types
type PostmanResponse = {
name: string;
originalRequest?: PostmanRequest;
status?: string;
code?: number;
_postman_previewlanguage?: string;
header?: PostmanHeader[];
body?: string;
}

type PostmanRequest = {
url?: {
raw?: string;
Expand Down Expand Up @@ -53,6 +64,19 @@ type PostmanCollection = {
item?: PostmanItem[];
}

// Updated OpenAPI types to include response content
interface ResponseContent {
description: string;
content?: {
[key: string]: {
schema: {
type: string;
example?: unknown;
};
};
};
}

// New OpenAPI related interfaces
interface Operation {
tags?: string[];
Expand Down Expand Up @@ -337,6 +361,54 @@ const toYAML = (obj: Record<string, unknown>, indent = 0): string => {
return yaml;
};

// Add new function to process response examples
const processResponses = (item: PostmanItem): Record<string, ResponseContent> => {
const responses: Record<string, ResponseContent> = {
'200': {
description: 'Successful response'
}
};

if (item.response && item.response.length > 0) {
item.response.forEach(response => {
const statusCode = response.code?.toString() || '200';
let contentType = 'application/json';

// Get content type from response headers
if (response.header) {
const ctHeader = response.header.find(h => h.key.toLowerCase() === 'content-type');
if (ctHeader) {
contentType = ctHeader.value;
}
}

let example: unknown = undefined;
if (response.body) {
try {
example = JSON.parse(response.body);
} catch {
example = response.body;
}
}

responses[statusCode] = {
description: response.name || `${statusCode} response`,
content: example ? {
[contentType]: {
schema: {
type: typeof example === 'object' ? 'object' : 'string',
example
}
}
} : undefined
};
});
}

return responses;
};

// Update the convertToOpenAPI function to include response processing
const convertToOpenAPI = (collection: PostmanCollection): OpenAPISpec => {
const openapi: OpenAPISpec = {
openapi: '3.0.3',
Expand Down Expand Up @@ -384,11 +456,7 @@ const convertToOpenAPI = (collection: PostmanCollection): OpenAPISpec => {
description: item.request.description || '',
operationId: `${method}${path.replace(/\W+/g, '')}`,
parameters: processParameters(item.request),
responses: {
'200': {
description: 'Successful response'
}
}
responses: processResponses(item)
};

if (!['get', 'head', 'delete'].includes(method)) {
Expand Down Expand Up @@ -423,7 +491,6 @@ const convertToOpenAPI = (collection: PostmanCollection): OpenAPISpec => {
return openapi;
};


const PostmanToOpenAPIConverter = () => {
const [input, setInput] = useState('');
const [output, setOutput] = useState('');
Expand Down

0 comments on commit 76bd5d3

Please sign in to comment.