const allFields = [
{
id:'name',
name:'Name',
description:'Name of the pokemon',
type:'text'
},
{
id:'type',
name:'Type',
description:'Type of the pokemon',
type:'text'
},
{
id:'abilities',
name:'Abilities',
description:'All abilities of the pokemon',
type:'text',
},
{
id:'height',
name:'Height',
description:'Height of the pokemon',
type:'number',
},
{
id:'weight',
name:'Weight',
description:'Weight of the pokemon',
type:'number'
},
{
id:'base_experience',
name:'Base Experience',
description:'Base Experience of the pokemon',
type:'number'
},
{
id:'hp',
name:'HP',
description:'HP of the pokemon',
type:'number'
},
{
id:'attack',
name:'Attack',
description:'Attacking abilities of the pokemon',
type:'number'
},
{
id:'defense',
name:'Defense',
description:'Defensive Abilities of the pokemon',
type:'number'
},
{
id:'special_attack',
name:'Special Attack',
description:'Special Attacking Abilities of the pokemon',
type:'number'
},
{
id:'special_defense',
name:'Special Defense',
description:'Name of the pokemon',
type:'number'
},
{
id:'speed',
name:'Speed',
description:'Speed of the pokemon',
type:'number'
}
]


const cc = DataStudioApp.createCommunityConnector();


// The code in the following lines are for authentication purposes. We will be using Google OAuth for this,
function getAuthType() {
const AuthTypes = cc.AuthType;
return cc
.newAuthTypeResponse()
.setAuthType(AuthTypes.NONE)
.build();
}


//This is the first main function that's built.
function getConfig(){
return cc.getConfig().build();
}


function getFields() {
const fields = cc.getFields();
allFields.forEach((field)=>{
switch(field.type){
case 'text':
fields.newDimension()
.setId(field.id)
.setName(field.name)
.setDescription(field.description)
.setType(cc.FieldType.TEXT);
break;
case 'number':
fields.newMetric()
.setId(field.id)
.setName(field.name)
.setDescription(field.description)
.setType(cc.FieldType.NUMBER);
break;
}
})
return fields;
}


function getSchema() {
return cc.newGetSchemaResponse()
.setFields(getFields())
.build();
}


function getData(request) {
const requestedFieldIds = request.fields.map((field)=>field.name);
let pokemonData = JSON.parse(getPokemonData());
const pokemonRows = pokemonData.map((item)=>{
return requestedFieldIds.map((field)=>item[field]);
});
return cc.newGetDataResponse()
.setFields(getFields().forIds(requestedFieldIds))
.addAllRows(pokemonRows)
.build();
}