Skip to content

Commit ca940a0

Browse files
committed
allow adding only mods or resource packs - fixes #53
1 parent ef5e938 commit ca940a0

File tree

4 files changed

+35
-7
lines changed

4 files changed

+35
-7
lines changed

src/api/cfclient.nim

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,20 @@ const
1919
## used for retrieving mods by their slug, which isn't possible with the curse api
2020
addonsSlugBaseUrl = "https://curse.nikky.moe/graphql"
2121

22-
proc fetchAddonsByQuery*(query: string, category = CfAddonGameCategory.Mod): Future[seq[CfAddon]] {.async.} =
22+
proc fetchAddonsByQuery*(query: string, category: Option[CfAddonGameCategory]): Future[seq[CfAddon]] {.async.} =
2323
## retrieves all addons that match the given `query` search and `category`.
2424
let encodedQuery = encodeUrl(query, usePlus = false)
25-
let url = addonsBaseUrl & "/v1/mods/search?gameId=432&classId=" & $ord(category) & "&pageSize=50&sortField=6&sortOrder=desc&searchFilter=" & encodedQuery
25+
var url = addonsBaseUrl & "/v1/mods/search?gameId=432&pageSize=50&sortField=6&sortOrder=desc&searchFilter=" & encodedQuery
26+
if category.isSome:
27+
url = url & "&classId=" & $ord(category.get())
2628
try:
2729
return get(url.Url).await.parseJson["data"].addonsFromForgeSvc
2830
except HttpRequestError:
2931
return @[]
3032

33+
proc fetchAddonsByQuery*(query: string): Future[seq[CfAddon]] {.async.} =
34+
return await fetchAddonsByQuery(query, category = none[CfAddonGameCategory]())
35+
3136
proc fetchAddon*(projectId: int): Future[Option[CfAddon]] {.async.} =
3237
## get the addon with the given `projectId`.
3338
let url = addonsBaseUrl & "/v1/mods/" & $projectId

src/cmd/add.nim

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ proc strScan(input: string, strVal: var string, start: int): int =
3838
inc result
3939
strVal = input.substr(start, start+result-1)
4040

41-
proc paxAdd*(input: string, noDepends: bool, strategy: string): void =
41+
proc paxAdd*(input: string, noDepends: bool, strategy: string, addonType: string): void =
4242
## add a new mod
4343
requirePaxProject()
4444

@@ -116,7 +116,14 @@ proc paxAdd*(input: string, noDepends: bool, strategy: string): void =
116116

117117
else:
118118
## Just search normally
119-
let mcMods = waitFor(fetchAddonsByQuery(input))
119+
let addonType: Option[CfAddonGameCategory] = case addonType:
120+
of "mod":
121+
some(CfAddonGameCategory.Mod)
122+
of "resourcepack":
123+
some(CfAddonGameCategory.Resourcepack)
124+
else:
125+
none[CfAddonGameCategory]()
126+
let mcMods = waitFor(fetchAddonsByQuery(input, addonType))
120127
let mcModOption = manifest.promptAddonChoice(mcMods, selectInstalled = false)
121128
if mcModOption.isNone:
122129
echoError "No mods found for your search."

src/pax.nim

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,10 @@ let addCmd = (
6060
noDepends: newCountArg(@["--no-deps"],
6161
help = "don't install dependencies"
6262
),
63+
addonType: newStringArg(@["-t", "--type"],
64+
choices = @["mod", "resourcepack"],
65+
help = "restrict search to a certain addon type"
66+
),
6367
strategy: commonArgs.strategy,
6468
yes: commonArgs.yes,
6569
noColor: commonArgs.noColor,
@@ -209,8 +213,12 @@ if spec.init.seen:
209213
elif spec.list.seen:
210214
paxList(status = listCmd.status.seen, info = listCmd.info.seen)
211215
elif spec.add.seen:
212-
paxAdd(input = addCmd.input.value, noDepends = addCmd.noDepends.seen,
213-
strategy = addCmd.strategy.value)
216+
paxAdd(
217+
input = addCmd.input.value,
218+
noDepends = addCmd.noDepends.seen,
219+
strategy = addCmd.strategy.value,
220+
addonType = addCmd.addonType.value
221+
)
214222
elif spec.remove.seen:
215223
paxRemove(name = removeCmd.name.value, strategy = removeCmd.strategy.value)
216224
elif spec.pin.seen:

tests/api/tcfclient.nim

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,22 @@ discard """
22
cmd: "nim $target --hints:on -d:testing -d:ssl --nimblePath:tests/deps $options $file"
33
"""
44

5-
import asyncdispatch, options, sequtils, sugar
5+
import asyncdispatch, options, sequtils, strutils, sugar
66
import api/cfclient, api/cfcore
77

88
block: # fetch by query
99
let mods = waitFor(fetchAddonsByQuery("jei"))
1010
doAssert mods[0].projectId == 238222
1111
doAssert mods[0].name == "Just Enough Items (JEI)"
1212

13+
block: # fetch mods by query
14+
let mods = waitFor(fetchAddonsByQuery("jei", some(CfAddonGameCategory.Mod)))
15+
doAssert mods.all(m => m.websiteUrl.contains("/mc-mods/"))
16+
17+
block: # fetch resource packs by query
18+
let mods = waitFor(fetchAddonsByQuery("jei", some(CfAddonGameCategory.Resourcepack)))
19+
doAssert mods.all(m => m.websiteUrl.contains("/texture-packs/"))
20+
1321
block: # fetch mod by id
1422
let mcMod = waitFor(fetchAddon(220318)).get()
1523
doAssert mcMod.projectId == 220318

0 commit comments

Comments
 (0)