Created
September 29, 2019 02:50
-
-
Save mopcweb/12786816463360e0de0ab506dd944ca6 to your computer and use it in GitHub Desktop.
Creates structure for data (specifically for swagger UI)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* ------------------------------------------------------------------- */ | |
/** | |
* 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