Skip to content

Commit

Permalink
feat: support refs
Browse files Browse the repository at this point in the history
  • Loading branch information
gajus committed Sep 17, 2017
1 parent d56931e commit c0f520b
Showing 1 changed file with 41 additions and 6 deletions.
47 changes: 41 additions & 6 deletions src/bin/generate-types.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,40 @@ import {
} from 'lodash';

const typeMap = {
ImagePathType: (data) => {
return data.definitions['image-path'].type;
},
MovieCastCreditType: (data) => {
return data.paths['/movie/{movie_id}/credits'].get.responses['200'].schema.properties.cast.items.properties;
},
MovieCrewCreditType: (data) => {
return data.paths['/movie/{movie_id}/credits'].get.responses['200'].schema.properties.crew.items.properties;
},
MovieType: (data) => {
return data.paths['/movie/{movie_id}'].get.responses['200'].schema.properties;
}
};

const definitionMap = {
'#/definitions/image-path': 'ImagePathType'
};

const typeNames = Object.keys(typeMap);

const createFlowType = (typeDefinition: mixed) => {
if (Array.isArray(typeDefinition)) {
return typeDefinition.map(createFlowType).join(' | ');
} else if (typeof typeDefinition === 'string') {
// eslint-disable-next-line no-use-before-define
return getPrimitiveFlowType(typeDefinition);
} else if (typeof typeDefinition === 'object' && typeDefinition !== null) {
// eslint-disable-next-line no-use-before-define
return createFlowObject(typeDefinition);
}

throw new Error('Unexpected type definition.');
};

// eslint-disable-next-line flowtype/no-weak-types
const createFlowObject = (resource: Object) => {
const propertyNames = Object.keys(resource);
Expand All @@ -32,7 +59,7 @@ const createFlowObject = (resource: Object) => {
types.push('+' + camelCase(propertyName) + ': ' + getFlowType(property));
}

return '{|\n' + types.join(',\n') + '\n|};';
return '{|\n' + types.join(',\n') + '\n|}';
};

const getPrimitiveFlowType = (typeName: string) => {
Expand All @@ -52,9 +79,17 @@ const getPrimitiveFlowType = (typeName: string) => {
};

const getFlowType = (property: *) => {
// @todo Support $ref
if (property.$ref) {
return 'any';
const mappedType = definitionMap[property.$ref];

if (!mappedType) {
// eslint-disable-next-line no-console
console.log('property.$ref', property.$ref);

throw new Error('Unmapped definition.');
}

return mappedType;
}

if (Array.isArray(property.type)) {
Expand Down Expand Up @@ -84,14 +119,14 @@ const run = async () => {
throw new Error('Unexpected state.');
}

const properties = resourceResolver(oas);
const typeDefinition = resourceResolver(oas);

if (!properties) {
if (!typeDefinition) {
throw new Error('Unexpected state.');
}

// eslint-disable-next-line no-console
console.log('type ' + typeName + ' = ' + createFlowObject(properties));
console.log('export type ' + typeName + ' = ' + createFlowType(typeDefinition) + ';');
}
};

Expand Down

0 comments on commit c0f520b

Please sign in to comment.