Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 5fd4d98

Browse files
Added a new feature: getBadges()
- The equivalent of [/badges](https://api.stackexchange.com/docs/badges) - Method of the `StackExchange` class
1 parent 57dfde8 commit 5fd4d98

File tree

7 files changed

+128
-1
lines changed

7 files changed

+128
-1
lines changed

‎src/index.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import {Answer as _Answer} from './objects/Answer';
2+
import {Badge as _Badge} from './objects/Badge';
23
import {BadgeCount as _BadgeCount} from './objects/BadgeCount';
34
import {ClosedDetails as _ClosedDetails} from './objects/ClosedDetails';
45
import {Comment as _Comment} from './objects/Comment';
@@ -17,6 +18,7 @@ import {Wrapper as _Wrapper} from './objects/Wrapper';
1718

1819
export {
1920
_Answer as Answer,
21+
_Badge as Badge,
2022
_BadgeCount as BadgeCount,
2123
_ClosedDetails as ClosedDetails,
2224
_Comment as Comment,
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
export interface GetBadgesOptions {
2+
fromDate?: Date;
3+
inName?: string;
4+
max?: 'gold' | 'silver' | 'bronze';
5+
min?: 'gold' | 'silver' | 'bronze';
6+
order?: 'desc' | 'asc';
7+
page?: number;
8+
pageSize?: number;
9+
site: string;
10+
sort?: 'rank' | 'name' | 'type';
11+
toDate?: Date;
12+
}

‎src/objects/Badge.ts‎

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import {ShallowUser} from './ShallowUser';
2+
3+
import {TypeBadge} from '../result-types/TypeBadge';
4+
5+
export class Badge {
6+
7+
public awardCount: number;
8+
9+
public badgeId: number;
10+
11+
public badgeType: 'named' | 'tag_based';
12+
13+
public description?: string;
14+
15+
public link: string;
16+
17+
public name: string;
18+
19+
public rank: 'gold' | 'silver' | 'bronze';
20+
21+
public user?: ShallowUser;
22+
23+
public constructor (badge: TypeBadge) {
24+
this.awardCount = badge.award_count;
25+
this.badgeId = badge.badge_id;
26+
this.badgeType = badge.badge_type;
27+
this.description = badge.description ?? null;
28+
this.link = badge.link;
29+
this.name = badge.name;
30+
this.rank = badge.rank;
31+
if (typeof badge.user === 'undefined') {
32+
this.user = null;
33+
} else {
34+
this.user = new ShallowUser(badge.user);
35+
}
36+
}
37+
}

‎src/objects/StackExchange.ts‎

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {Wrapper} from '../objects/Wrapper';
55
import {AdvancedSearchOptions} from '../method-options/StackExchange/AdvancedSearchOptions';
66
import {GetAnswersOptions} from '../method-options/StackExchange/GetAnswersOptions';
77
import {GetAnswersByIdsOptions} from '../method-options/StackExchange/GetAnswersByIdsOptions';
8+
import {GetBadgesOptions} from '../method-options/StackExchange/GetBadgesOptions';
89
import {GetCommentsOptions} from '../method-options/StackExchange/GetCommentsOptions';
910
import {GetCommentsByIdsOptions} from '../method-options/StackExchange/GetCommentsByIdsOptions';
1011
import {GetCommentsOnAnswersOptions} from '../method-options/StackExchange/GetCommentsOnAnswersOptions';
@@ -255,6 +256,59 @@ export class StackExchange {
255256
}
256257

257258

259+
/*
260+
* A method for the /badges endpoint: https://api.stackexchange.com/docs/badges
261+
* Returns all the badges in the system.
262+
* This method returns an array of badges (Badge[]) wrapped in a Wrapper.
263+
*/
264+
public static async getBadges (
265+
options: GetBadgesOptions
266+
): Promise<Wrapper> {
267+
const getBadgesUrl = new URL('/badges', this.baseUrl);
268+
269+
if (options.fromDate) {
270+
getBadgesUrl.searchParams.append('fromdate', this.dateHandler(options.fromDate));
271+
}
272+
if (options.inName) {
273+
getBadgesUrl.searchParams.append('inname', options.inName);
274+
}
275+
if (options.max) {
276+
getBadgesUrl.searchParams.append('max', options.max);
277+
}
278+
if (options.min) {
279+
getBadgesUrl.searchParams.append('min', options.min);
280+
}
281+
if (options.order) {
282+
getBadgesUrl.searchParams.append('order', options.order);
283+
}
284+
if (options.page) {
285+
getBadgesUrl.searchParams.append('page', options.page.toString());
286+
}
287+
if (options.pageSize) {
288+
getBadgesUrl.searchParams.append('pagesize', options.pageSize.toString());
289+
}
290+
getBadgesUrl.searchParams.append('site', options.site);
291+
if (options.sort) {
292+
getBadgesUrl.searchParams.append('sort', options.sort);
293+
}
294+
if (options.toDate) {
295+
getBadgesUrl.searchParams.append('todate', this.dateHandler(options.toDate));
296+
}
297+
298+
return new Wrapper(
299+
await rp.get(
300+
getBadgesUrl.href, {
301+
headers: {
302+
'accept-encoding': 'gzip',
303+
},
304+
gzip: true,
305+
json: true,
306+
}
307+
), 'Badge'
308+
);
309+
}
310+
311+
258312
/*
259313
* A method for the /comments endpoint: https://api.stackexchange.com/docs/comments
260314
* Gets all the comments on the site.

‎src/objects/Wrapper.ts‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
import {Answer} from './Answer';
2+
import {Badge} from './Badge';
23
import {Comment} from './Comment';
34
import {Privilege} from './Privilege';
45
import {Question} from './Question';
56
import {Site} from './Site';
67
import {Tag} from './Tag';
78

89
import {TypeAnswer} from '../result-types/TypeAnswer';
10+
import {TypeBadge} from '../result-types/TypeBadge';
911
import {TypeComment} from '../result-types/TypeComment';
1012
import {TypePrivilege} from '../result-types/TypePrivilege';
1113
import {TypeQuestion} from '../result-types/TypeQuestion';
@@ -15,7 +17,7 @@ import {TypeTag} from '../result-types/TypeTag';
1517
import {TypeWrapper} from '../result-types/TypeWrapper';
1618

1719

18-
type ClassType = 'Answer' | 'Comment' | 'Privilege' | 'Question' | 'Site' | 'Tag';
20+
type ClassType = 'Answer' | 'Badge'|'Comment' | 'Privilege' | 'Question' | 'Site' | 'Tag';
1921

2022
export class Wrapper {
2123

@@ -31,6 +33,7 @@ export class Wrapper {
3133

3234
public items:
3335
| Answer[]
36+
| Badge[]
3437
| Comment[]
3538
| Privilege[]
3639
| Question[]
@@ -61,6 +64,11 @@ export class Wrapper {
6164
= [...wrapper.items]
6265
.map((item: TypeAnswer) => new Answer(item));
6366
break;
67+
case 'Badge':
68+
this.items
69+
= [...wrapper.items]
70+
.map((item: TypeBadge) => new Badge(item));
71+
break;
6472
case 'Comment':
6573
this.items
6674
= [...wrapper.items]

‎src/result-types/TypeBadge.ts‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import {TypeShallowUser} from './TypeShallowUser';
2+
3+
export interface TypeBadge {
4+
award_count: number;
5+
badge_id: number;
6+
badge_type: 'named' | 'tag_based';
7+
description?: string;
8+
link: string;
9+
name: string;
10+
rank: 'gold' | 'silver' | 'bronze';
11+
user?: TypeShallowUser;
12+
}

‎src/result-types/TypeWrapper.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import {TypeAnswer} from './TypeAnswer';
2+
import {TypeBadge} from './TypeBadge';
23
import {TypeComment} from './TypeComment';
34
import {TypePrivilege} from './TypePrivilege';
45
import {TypeQuestion} from './TypeQuestion';
@@ -13,6 +14,7 @@ export interface TypeWrapper {
1314
has_more: boolean;
1415
items:
1516
| TypeAnswer[]
17+
| TypeBadge[]
1618
| TypeComment[]
1719
| TypePrivilege[]
1820
| TypeQuestion[]

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /