Skip to content

Commit 5e2afeb

Browse files
committed
fix: correctly handle string enum
1 parent 7426a01 commit 5e2afeb

2 files changed

Lines changed: 76 additions & 21 deletions

File tree

scripts/gen-client.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -490,6 +490,10 @@ function schemaToTypescriptTypes(
490490
return "boolean" as const;
491491
}
492492
case "string": {
493+
if (schema.enum) {
494+
const literals = schema.enum.map((v) => JSON.stringify(v)).join(" | ");
495+
return `(${literals})` as const;
496+
}
493497
return "string" as const;
494498
}
495499
case "number":

src/client/index.ts

Lines changed: 72 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,23 @@ export class Lichess {
6161
/**
6262
* Get one leaderboard
6363
*/
64-
async playerTopNbPerfType(params: { nb: number; perfType: string }) {
64+
async playerTopNbPerfType(params: {
65+
nb: number;
66+
perfType:
67+
| "ultraBullet"
68+
| "bullet"
69+
| "blitz"
70+
| "rapid"
71+
| "classical"
72+
| "chess960"
73+
| "crazyhouse"
74+
| "antichess"
75+
| "atomic"
76+
| "horde"
77+
| "kingOfTheHill"
78+
| "racingKings"
79+
| "threeCheck";
80+
}) {
6581
const path = `/api/player/top/${params.nb}/${params.perfType}` as const;
6682
return await this.requestor.get(
6783
{ path },
@@ -155,8 +171,8 @@ export class Lichess {
155171
*/
156172
async apiPuzzleNext(params: {
157173
angle?: string;
158-
difficulty?: string;
159-
color?: string;
174+
difficulty?: "easiest" | "easier" | "normal" | "harder" | "hardest";
175+
color?: "white" | "black";
160176
}) {
161177
const path = "/api/puzzle/next" as const;
162178
return await this.requestor.get(
@@ -170,9 +186,9 @@ export class Lichess {
170186
*/
171187
async apiPuzzleBatchSelect(
172188
params: { angle: string } & {
173-
difficulty?: string;
189+
difficulty?: "easiest" | "easier" | "normal" | "harder" | "hardest";
174190
nb?: number;
175-
color?: string;
191+
color?: "white" | "black";
176192
},
177193
) {
178194
const path = `/api/puzzle/batch/${params.angle}` as const;
@@ -493,7 +509,7 @@ export class Lichess {
493509
vs?: string;
494510
rated?: boolean;
495511
perfType?: schemas.PerfType;
496-
color?: string;
512+
color?: "white" | "black";
497513
analysed?: boolean;
498514
moves?: boolean;
499515
pgnInJson?: boolean;
@@ -508,7 +524,7 @@ export class Lichess {
508524
literate?: boolean;
509525
lastFen?: boolean;
510526
withBookmarked?: boolean;
511-
sort?: string;
527+
sort?: "dateAsc" | "dateDesc";
512528
},
513529
) {
514530
const path = `/api/games/user/${params.username}` as const;
@@ -737,7 +753,7 @@ export class Lichess {
737753
division?: boolean;
738754
literate?: boolean;
739755
lastFen?: boolean;
740-
sort?: string;
756+
sort?: "dateAsc" | "dateDesc";
741757
}) {
742758
const path = "/api/games/export/bookmarks" as const;
743759
return await this.requestor.get(
@@ -1857,13 +1873,13 @@ export class Lichess {
18571873
async apiStudyPost(params: {
18581874
body: {
18591875
name: string;
1860-
visibility: string;
1876+
visibility: "public" | "unlisted" | "private";
18611877
computer: schemas.StudyUserSelection;
18621878
explorer: schemas.StudyUserSelection;
18631879
cloneable: schemas.StudyUserSelection;
18641880
shareable: schemas.StudyUserSelection;
18651881
chat: schemas.StudyUserSelection;
1866-
sticky?: string;
1882+
sticky?: "true" | "false";
18671883
};
18681884
}) {
18691885
const path = "/api/study" as const;
@@ -1884,7 +1900,7 @@ export class Lichess {
18841900
body: {
18851901
pgn: string;
18861902
name?: string;
1887-
orientation?: string;
1903+
orientation?: "white" | "black";
18881904
variant?: schemas.VariantKey;
18891905
};
18901906
},
@@ -2777,7 +2793,7 @@ export class Lichess {
27772793
async boardGameChatPost(
27782794
params: { gameId: string } & {
27792795
body: {
2780-
room: string;
2796+
room: "player" | "spectator";
27812797
text: string;
27822798
};
27832799
},
@@ -2975,7 +2991,7 @@ export class Lichess {
29752991
async botGameChat(
29762992
params: { gameId: string } & {
29772993
body: {
2978-
room: string;
2994+
room: "player" | "spectator";
29792995
text: string;
29802996
};
29812997
},
@@ -3113,7 +3129,12 @@ export class Lichess {
31133129
variant?: schemas.VariantKey;
31143130
fen?: schemas.FromPositionFEN;
31153131
keepAliveStream?: boolean;
3116-
rules?: string;
3132+
rules?:
3133+
| "noAbort"
3134+
| "noRematch"
3135+
| "noGiveTime"
3136+
| "noClaimWin"
3137+
| "noEarlyDraw";
31173138
};
31183139
},
31193140
) {
@@ -3141,7 +3162,9 @@ export class Lichess {
31413162
/**
31423163
* Accept a challenge
31433164
*/
3144-
async challengeAccept(params: { challengeId: string } & { color?: string }) {
3165+
async challengeAccept(
3166+
params: { challengeId: string } & { color?: "white" | "black" },
3167+
) {
31453168
const path = `/api/challenge/${params.challengeId}/accept` as const;
31463169
return await this.requestor.post(
31473170
{ path, query: { color: params.color } },
@@ -3156,7 +3179,22 @@ export class Lichess {
31563179
* Decline a challenge
31573180
*/
31583181
async challengeDecline(
3159-
params: { challengeId: string } & { body: { reason?: string } },
3182+
params: { challengeId: string } & {
3183+
body: {
3184+
reason?:
3185+
| "generic"
3186+
| "later"
3187+
| "tooFast"
3188+
| "tooSlow"
3189+
| "timeControl"
3190+
| "rated"
3191+
| "casual"
3192+
| "standard"
3193+
| "variant"
3194+
| "noBot"
3195+
| "onlyBot";
3196+
};
3197+
},
31603198
) {
31613199
const path = `/api/challenge/${params.challengeId}/decline` as const;
31623200
return await this.requestor.post(
@@ -3240,7 +3278,12 @@ export class Lichess {
32403278
variant?: schemas.VariantKey;
32413279
fen?: schemas.FromPositionFEN;
32423280
name?: string;
3243-
rules?: string;
3281+
rules?:
3282+
| "noRematch"
3283+
| "noGiveTime"
3284+
| "noClaimWin"
3285+
| "noEarlyDraw"
3286+
| "noAbort";
32443287
users?: string;
32453288
expiresAt?: number;
32463289
};
@@ -3297,7 +3340,12 @@ export class Lichess {
32973340
variant?: schemas.VariantKey;
32983341
fen?: schemas.FromPositionFEN;
32993342
message?: string;
3300-
rules?: string;
3343+
rules?:
3344+
| "noAbort"
3345+
| "noRematch"
3346+
| "noGiveTime"
3347+
| "noClaimWin"
3348+
| "noEarlyDraw";
33013349
};
33023350
}) {
33033351
const path = "/api/bulk-pairing" as const;
@@ -3719,12 +3767,12 @@ export class Lichess {
37193767
*/
37203768
async openingExplorerPlayer(params: {
37213769
player: string;
3722-
color: string;
3770+
color: "white" | "black";
37233771
variant?: schemas.VariantKey;
37243772
fen?: string;
37253773
play?: string;
37263774
speeds?: schemas.Speed[];
3727-
modes?: string[];
3775+
modes?: ("casual" | "rated")[];
37283776
since?: string;
37293777
until?: string;
37303778
moves?: number;
@@ -3753,7 +3801,10 @@ export class Lichess {
37533801
/**
37543802
* Tablebase lookup
37553803
*/
3756-
async tablebaseStandard(params: { fen: string; dtc?: string }) {
3804+
async tablebaseStandard(params: {
3805+
fen: string;
3806+
dtc?: "never" | "auxiliary" | "always";
3807+
}) {
37573808
const path = "/standard" as const;
37583809
const baseUrl = "https://tablebase.lichess.org";
37593810
return await this.requestor.get(

0 commit comments

Comments
 (0)