Skip to content

Commit

Permalink
Optimize dict
Browse files Browse the repository at this point in the history
  • Loading branch information
lit26 committed Mar 11, 2024
1 parent 713c48c commit e5adbe9
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
11 changes: 10 additions & 1 deletion lib/services/mimetype-content-wrapper.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,21 @@
import { ContentObject } from '../interfaces/open-api-spec.interface';

function removeUndefinedKeys(obj: { [x: string]: any }) {
Object.entries(obj).forEach(([key, value]) => {
if (value === undefined) {
delete obj[key];
}
});
return obj;
}

export class MimetypeContentWrapper {
wrap(
mimetype: string[],
obj: Record<string, any>
): Record<'content', ContentObject> {
const content = mimetype.reduce(
(acc, item) => ({ ...acc, [item]: obj }),
(acc, item) => ({ ...acc, [item]: removeUndefinedKeys(obj) }),
{}
);
return { content };
Expand Down
8 changes: 4 additions & 4 deletions lib/services/response-object-mapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export class ResponseObjectMapper {
$ref: getSchemaPath(name)
}
},
...(response.example ? { example: response.example } : {})
example: response.example
})
};
}
Expand All @@ -32,7 +32,7 @@ export class ResponseObjectMapper {
schema: {
$ref: getSchemaPath(name)
},
...(response.example ? { example: response.example } : {})
example: response.example
})
};
}
Expand All @@ -45,8 +45,8 @@ export class ResponseObjectMapper {
return response;
}
const content = this.mimetypeContentWrapper.wrap(produces, {
...(response.schema ? { schema: response.schema } : {}),
...(response.example ? { example: response.example } : {})
schema: response.schema,
example: response.example
});
return {
...omit(response, ['schema', 'example']),
Expand Down

0 comments on commit e5adbe9

Please sign in to comment.