Skip to content

Commit

Permalink
feat (DDragon): Add DDragon.Champion.Helpers.getDataById and DDragon.…
Browse files Browse the repository at this point in the history
…Champion.Helpers.getDataByIdWithParentAsId #54
  • Loading branch information
cnguy committed Sep 6, 2018
1 parent e122497 commit 113e08d
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 4 deletions.
5 changes: 4 additions & 1 deletion example.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,10 @@ import test from './examples/es5/turn-free-to-play-into-ddragon-champion-objects

const main = async () => {
try {
const lala = await kayn.DDragon.Realm.list(/* default region */)
const aatrox = await kayn.DDragon.Champion.Helpers.getDataById(
'Aatrox',
).version('8.15.1')
console.log(aatrox)
} catch (ex) {
console.error(ex)
}
Expand Down
63 changes: 63 additions & 0 deletions lib/Endpoints/DDragonEndpoints/DDragonChampionEndpoint.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@ class DDragonChampionEndpoint {
this.get = this.get.bind(this)
this.list = this.list.bind(this)
this.listFull = this.listFull.bind(this)
this.Helpers = {
getDataById: this.getDataById.bind(this),
getDataByIdWithParentAsId: this.getDataByIdWithParentAsId.bind(
this,
),
}
}

get(championName) {
Expand Down Expand Up @@ -39,6 +45,63 @@ class DDragonChampionEndpoint {
METHOD_NAMES.DDRAGON.CHAMPION_LIST_FULL,
)
}

getDataById(championName) {
return new DDragonRequest(
this.config,
`champion/${championName}.json`,
DDragonRequestTypes.CDN.DATA,
METHOD_NAMES.DDRAGON.CHAMPION_GET_DATA_BY_ID,
({ data, ...rest }) => {
return {
...rest,
data: transform(ID_WITH_KEY, data),
}
},
)
}

getDataByIdWithParentAsId(championName) {
return new DDragonRequest(
this.config,
`champion/${championName}.json`,
DDragonRequestTypes.CDN.DATA,
METHOD_NAMES.DDRAGON.CHAMPION_GET_DATA_BY_ID_PARENT_TOO,
({ data, ...rest }) => {
return {
...rest,
data: transform(ID_WITH_KEY_AND_PARENT_AS_ID, data),
}
},
)
}

listById() {}
listByIdWithParentAsIdToo() {}

listFullById() {}
listFullByIdWithParentAsIdToo() {}
}

const swapIdAndKey = data => ({ ...data, id: data.key, key: data.id })
const makeParentId = data => ({
[data.key]: { ...data, id: data.key, key: data.id },
})

const ID_WITH_KEY = 'id_with_key'
const ID_WITH_KEY_AND_PARENT_AS_ID = 'id_with_key_and_parent_as_id'

const transform = (type, data) => {
const nameKey = Object.keys(data)
// There is only 1 key (which is the name).
const object = data[nameKey]
const fn = {
[ID_WITH_KEY]: {
[nameKey]: swapIdAndKey(object),
},
[ID_WITH_KEY_AND_PARENT_AS_ID]: makeParentId(object),
}[type]
return fn
}

export default DDragonChampionEndpoint
10 changes: 10 additions & 0 deletions lib/Enums/method-names.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,18 @@ const CHAMPION = {

const DDRAGON = {
CHAMPION_GET: 'DDRAGON.CHAMPION.GET',
CHAMPION_GET_DATA_BY_ID: 'DDRAGON.CHAMPION.HELPER.GET_DATA_BY_ID', // { Aatrox: { ... id: 5, key: "Aatrox" } }
CHAMPION_GET_DATA_BY_ID_PARENT_TOO:
'DDRAGON.CHAMPION.HELPER.GET_DATA_BY_ID_PARENT_TOO', // { 5: { ... id: 5, key: "Aatrox" } }
CHAMPION_LIST: 'DDRAGON.CHAMPION.LIST',
CHAMPION_LIST_DATA_BY_ID: 'DDRAGON.CHAMPION.HELPER.LIST_DATA_BY_ID', // { Aatrox: { ... id: 5, key: "Aatrox" } }
CHAMPION_LIST_DATA_BY_ID_PARENT_TOO:
'DDRAGON.CHAMPION.HELPER.LIST_DATA_BY_ID_PARENT_TOO', // { 5: { ... id: 5, key: "Aatrox" } }
CHAMPION_LIST_FULL: 'DDRAGON.CHAMPION.LIST_FULL',
CHAMPION_LIST_FULL_DATA_BY_ID:
'DDRAGON.CHAMPION.HELPER.LIST_FULL_DATA_BY_ID', // { Aatrox: { ... id: 5, key: "Aatrox" } }
CHAMPION_LIST_FULL_DATA_BY_ID_PARENT_TOO:
'DDRAGON.CHAMPION.HELPER.LIST_FULL_DATA_BY_ID_PARENT_TOO', // { 5: { ... id: 5, key: "Aatrox" } }
CHAMPION_ITEM_LIST: 'DDRAGON.ITEM.LIST',
LANGUAGE_LIST: 'DDRAGON.LANGUAGE.LIST',
LANGUAGE_STRING_LIST: 'DDRAGON.LANGUAGE_STRING.LIST',
Expand Down
16 changes: 13 additions & 3 deletions lib/RequestClient/DDragonRequest.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,13 @@ export const ddragonRequestTypeToUrl = (type, urlInformation) => {
}
}

function DDragonRequest(config, endpoint, requestType, methodName) {
function DDragonRequest(
config,
endpoint,
requestType,
methodName,
dataTransformer,
) {
this.payload = {
endpoint,
locale: '',
Expand All @@ -70,6 +76,7 @@ function DDragonRequest(config, endpoint, requestType, methodName) {
}
this.config = config
this.methodName = methodName
this.dataTransformer = dataTransformer
}

DDragonRequest.prototype.locale = function(locale) {
Expand Down Expand Up @@ -146,6 +153,9 @@ DDragonRequest.prototype.execute = async function(url, cb) {
debugOptions.loggers.request.incoming.success(debugURL)
}
const blob = JSON.parse(res)
const transformedBlob = this.dataTransformer
? this.dataTransformer(blob)
: blob
if (
cacheOptions.cache &&
cacheOptions.ttls[this.methodName] > 0
Expand All @@ -155,13 +165,13 @@ DDragonRequest.prototype.execute = async function(url, cb) {
key: url,
ttl: cacheOptions.ttls[this.methodName],
},
blob,
transformedBlob,
)
if (debugOptions.isEnabled) {
debugOptions.loggers.cache.set(`${url}`)
}
}
ok(blob)(cb)
ok(transformedBlob)(cb)
} catch (ex) {
// Don't think this is useful?
error({ statusCode: ex.statusCode, url, error: ex })(cb)
Expand Down

0 comments on commit 113e08d

Please sign in to comment.