Skip to content

Instantly share code, notes, and snippets.

@mopcweb
Created September 29, 2019 02:50
Show Gist options
  • Save mopcweb/12786816463360e0de0ab506dd944ca6 to your computer and use it in GitHub Desktop.
Save mopcweb/12786816463360e0de0ab506dd944ca6 to your computer and use it in GitHub Desktop.
Creates structure for data (specifically for swagger UI)
/* ------------------------------------------------------------------- */
/**
* Creates structure for data (specifically for swagger)
*
* @param data - Original data object, for which there would
* be created a structure
*/
/* ------------------------------------------------------------------- */
export const createDataStructure = (data: any) => {
const structure: any = { };
if (data == null)
structure.type = 'string';
else if (typeof data === 'object' && data != null)
if (Array.isArray(data)) {
structure.type = 'array';
structure.items = createDataStructure(data[0]);
}
else {
structure.type = typeof data;
structure.properties = { };
for (const key of Object.keys(data))
structure.properties[key] = createDataStructure(data[key]);
}
else
structure.type = typeof data;
return structure;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment