Created
April 3, 2020 20:54
-
-
Save ottomata/03aa88cfd4122b4deaf226c4f01295a7 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
'use strict'; | |
const got = require('got'); | |
const jsTools = require('@wikimedia/jsonschema-tools'); | |
const yaml = require('js-yaml'); | |
/** | |
* Tests if obj is an Object (not an array). | |
* @param {[type]} obj [description] | |
* @return {Boolean} [description] | |
*/ | |
function isObject( obj ) { | |
return Object.prototype.toString.call( obj ) === '[object Object]'; | |
} | |
/** | |
* Recursively convert an object's string keys to lower case. | |
* Adapted from https://gist.github.com/orcaman/dd40b04e0e42eed841d7. | |
* | |
* @private | |
* @param {Object} obj | |
* @return {Object} | |
*/ | |
function keysToLowerCase( obj ) { | |
var | |
key, | |
value, | |
lowerCaseKey, | |
result = {}; | |
if ( !obj || !isObject( obj ) ) { | |
return obj; | |
} | |
for ( key in obj ) { | |
if ( typeof key === 'string' ) { | |
value = obj[ key ]; | |
lowerCaseKey = key.toLowerCase(); | |
// if the node is an object, perform the same process over that node | |
if ( typeof value === 'object' ) { | |
result[ lowerCaseKey ] = keysToLowerCase( value ); | |
} else { | |
result[ lowerCaseKey ] = value; | |
} | |
} else { | |
result[ key ] = value; | |
} | |
} | |
return result; | |
} | |
function eventLoggingSchemaNameToMetaUrl(schemaName) { | |
return `https://meta.wikimedia.org/w/api.php?action=jsonschema&format=json&formatversion=2&title=${schemaName}`; | |
} | |
function eventLoggingSchemaNameToTitle(schemaName) { | |
return `legacy/eventlogging/${schemaName.toLowerCase()}`; | |
} | |
function eventLoggingSchemaNameToRelativePath(schemaName) { | |
return '/' + eventLoggingSchemaNameToTitle(schemaName); | |
} | |
function eventLoggingSchemaNameToFileUri(schemaName, filename) { | |
return eventLoggingSchemaNameToRelativePath(schemaName) + `/${filename}`; | |
} | |
function eventLoggingSchemaNameToCurrentFile(schemaName) { | |
return eventLoggingSchemaNameToCurrentFile(schemaName, 'current.yaml'); | |
} | |
async function getEventLoggingSchema(schemaName) { | |
const schemaUrl = eventLoggingSchemaNameToMetaUrl(schemaName); | |
return JSON.parse((await got(schemaUrl)).body); | |
} | |
const capsuleSchemaUri = '/fragment/legacy/eventlogging/eventcapsule/1.0.0'; | |
let capsuleSchema; | |
async function convertEventLoggingSchema(schemaName) { | |
if (!capsuleSchema) { | |
capsuleSchema = await jsTools.getSchemaById(capsuleSchemaUri); | |
} | |
const eventSubSchema = keysToLowerCase(await getEventLoggingSchema(schemaName)); | |
const fullSchema = Object.assign({}, capsuleSchema); | |
fullSchema.properties.event = eventSubSchema; | |
fullSchema.properties.event.type = 'object'; | |
fullSchema.title = eventLoggingSchemaNameToTitle(schemaName); | |
fullSchema.description = eventSubSchema.description | |
fullSchema.$id = eventLoggingSchemaNameToFileUri(schemaName, '1.0.0'); | |
fullSchema.$schema = 'https://json-schema.org/draft-07/schema#'; | |
delete fullSchema.properties.event.title; | |
delete fullSchema.properties.event.description; | |
return fullSchema; | |
} | |
async function main() { | |
const s = await convertEventLoggingSchema('SearchSatisfaction'); | |
console.log(yaml.dump(s)); | |
} | |
main(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment