UNPKG

@elastic.io/odata-library

Version:
110 lines (99 loc) 3.92 kB
module.exports = class MetadataBuilder { constructor(csdlConverter, referenceResolveLevel) { this.csdlConverter = csdlConverter; this.referenceResolveLevel = referenceResolveLevel; } getObjectsNamingList() { const list = {}; this.csdlConverter.getResourcesList() /* eslint no-return-assign:0 prefer-destructuring:0 */ .forEach(rs => list[rs.name] = rs.name.match('[a-zA-Z]+$')[0]); return list; } getTriggerMetadata(objectType) { const objectDescription = this.csdlConverter.convertCsdlString(objectType); return { out: this.csdlConverter .resolveMetadataRefs(objectDescription.jsonSchema, this.referenceResolveLevel), }; } getByIdMetadata(objectType) { const objectDescription = this.csdlConverter.convertCsdlString(objectType); const jsonSchema = this.csdlConverter .resolveMetadataRefs(objectDescription.jsonSchema, this.referenceResolveLevel); jsonSchema.properties[objectDescription.keys[0].name].required = true; const idMetadata = { type: 'object', properties: { [objectDescription.keys[0].name]: jsonSchema.properties[objectDescription.keys[0].name], }, }; return { in: idMetadata, out: jsonSchema, }; } getPostMetadata(objectType) { const objectDescription = this.csdlConverter.convertCsdlString(objectType); const jsonSchema = this.csdlConverter .resolveMetadataRefs(objectDescription.jsonSchema, this.referenceResolveLevel); const jsonSchemaInput = JSON.parse(JSON.stringify(jsonSchema)); delete jsonSchemaInput.properties[objectDescription.keys[0].name]; MetadataBuilder.turnRequiredFalseNonKeyVariables(jsonSchemaInput, objectDescription.keys); jsonSchema.properties[objectDescription.keys[0].name].required = true; return { in: jsonSchemaInput, out: jsonSchema, }; } getPutMetadata(objectType) { const objectDescription = this.csdlConverter.convertCsdlString(objectType); const jsonSchema = this.csdlConverter .resolveMetadataRefs(objectDescription.jsonSchema, this.referenceResolveLevel); const jsonSchemaInput = JSON.parse(JSON.stringify(jsonSchema)); jsonSchemaInput.properties[objectDescription.keys[0].name].required = true; MetadataBuilder.turnRequiredFalseNonKeyVariables(jsonSchemaInput, objectDescription.keys); return { in: jsonSchemaInput, out: jsonSchema, }; } getUpsertMetadata(objectType) { const objectDescription = this.csdlConverter.convertCsdlString(objectType); const jsonSchema = this.csdlConverter .resolveMetadataRefs(objectDescription.jsonSchema, this.referenceResolveLevel); const jsonSchemaInput = JSON.parse(JSON.stringify(jsonSchema)); jsonSchemaInput.properties[objectDescription.keys[0].name].required = false; MetadataBuilder.turnRequiredFalseNonKeyVariables(jsonSchemaInput, objectDescription.keys); return { in: jsonSchemaInput, out: jsonSchema, }; } getDeleteMetadata(objectType) { const objectDescription = this.csdlConverter.convertCsdlString(objectType); const jsonSchema = this.csdlConverter .resolveMetadataRefs(objectDescription.jsonSchema, this.referenceResolveLevel); const idMetadata = { type: 'object', properties: {}, }; objectDescription.keys.forEach((key) => { idMetadata.properties[key.name] = jsonSchema.properties[key.name]; idMetadata.properties[key.name].required = true; }); return { in: idMetadata, out: idMetadata, }; } static turnRequiredFalseNonKeyVariables(jsonSchema, keys) { const keyList = keys.map(k => k.name); Object.keys(jsonSchema.properties).forEach((key) => { if (keyList.indexOf(key) === -1) { jsonSchema.properties[key].required = false; // eslint-disable-line no-param-reassign } }); return jsonSchema; } };