tornapi-typescript 2.0.2 → 3.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.ts CHANGED
@@ -22,6 +22,7 @@ export type UserCrimeUniquesRewardAmmoEnum = "standard" | "special";
22
22
  export type RaceStatusEnum = "open" | "in_progress" | "finished";
23
23
  export type TornHofCategory = "level" | "busts" | "rank" | "traveltime" | "workstats" | "networth" | "revives" | "defends" | "offences" | "attacks" | "awards" | "racingwins" | "racingpoints" | "racingskill";
24
24
  export type TornFactionHofCategory = "respect" | "chains" | "rank";
25
+ export type TornOrganizedCrimePositionId = "P1" | "P2" | "P3" | "P4" | "P5" | "P6";
25
26
  export type FactionTerritoryWarResultEnum = "success_assault" | "fail_assault" | "end_with_nap" | "end_with_destroy_attack" | "end_with_destroy_defense" | "end_with_peace_treaty";
26
27
  export type FactionAttackResult = "None" | "Attacked" | "Mugged" | "Hospitalized" | "Arrested" | "Looted" | "Lost" | "Stalemate" | "Assist" | "Escape" | "Timeout" | "Special" | "Bounty" | "Interrupted";
27
28
  export type RaceCarUpgradeCategory = "Aerodynamics" | "Brakes" | "Engine" | "Exhaust and Induction" | "Fuel" | "Safety" | "Suspension" | "Transmission" | "Weight Reduction" | "Wheels and Tyres";
@@ -359,6 +360,7 @@ export interface UserPropertyDetailsExtendedRented extends UserPropertyBasicDeta
359
360
  cost_per_day: number;
360
361
  rental_period: number;
361
362
  rental_period_remaining: number;
363
+ rented_by: BasicUser;
362
364
  }
363
365
  export interface UserPropertyDetailsExtendedForRent extends UserPropertyBasicDetails {
364
366
  used_by: BasicUser[];
@@ -1807,6 +1809,8 @@ export interface FactionCrimeReward {
1807
1809
  }
1808
1810
  export interface FactionCrimeSlot {
1809
1811
  position: string;
1812
+ position_id: TornOrganizedCrimePositionId;
1813
+ position_number: number;
1810
1814
  item_requirement: {
1811
1815
  id: ItemId;
1812
1816
  /** Shows if the item is reusable or consumed during the crime. */
@@ -2048,6 +2052,11 @@ export interface KeyInfoResponse {
2048
2052
  forum: ForumSelectionName[];
2049
2053
  key: KeySelectionName[];
2050
2054
  };
2055
+ user: {
2056
+ faction_id: FactionId | null;
2057
+ company_id: CompanyId | null;
2058
+ id: UserId;
2059
+ };
2051
2060
  access: {
2052
2061
  level: number;
2053
2062
  type: ApiKeyAccessTypeEnum;
@@ -2339,7 +2348,7 @@ export interface TornOrganizedCrimeScope {
2339
2348
  return: number;
2340
2349
  }
2341
2350
  export interface TornOrganizedCrimeSlot {
2342
- id: string;
2351
+ id: TornOrganizedCrimePositionId;
2343
2352
  name: string;
2344
2353
  required_item: TornOrganizedCrimeRequiredItem | null;
2345
2354
  }
@@ -2444,7 +2453,7 @@ export interface TornCalendarResponse {
2444
2453
  events: TornCalendarActivity[];
2445
2454
  };
2446
2455
  }
2447
- export interface TornHof {
2456
+ export interface TornHofBasic {
2448
2457
  id: UserId;
2449
2458
  username: string;
2450
2459
  faction_id: FactionId;
@@ -2458,6 +2467,10 @@ export interface TornHof {
2458
2467
  value: number | string | number;
2459
2468
  rank: string;
2460
2469
  }
2470
+ export interface TornHofWithOffenses extends TornHofBasic {
2471
+ criminal_offenses: number;
2472
+ }
2473
+ export type TornHof = TornHofBasic | TornHofWithOffenses;
2461
2474
  export interface TornHofResponse {
2462
2475
  hof: TornHof[];
2463
2476
  _metadata: RequestMetadataWithLinks;
@@ -3297,6 +3310,10 @@ export interface UserV1 extends BaseSchema {
3297
3310
  bazaarcustomers: number;
3298
3311
  bazaarprofit: number;
3299
3312
  bazaarsales: number;
3313
+ itemmarketsales: number;
3314
+ itemmarketcustomers: number;
3315
+ itemmarketrevenue: number;
3316
+ itemmarketfees: number;
3300
3317
  bestactivestreak: number;
3301
3318
  bestdamage: number;
3302
3319
  bestkillstreak: number;
@@ -6398,10 +6415,10 @@ export type SectionV1 = keyof SectionsV1Map;
6398
6415
  export type SectionV2 = keyof SectionsV2Map;
6399
6416
  export type SelectionV1<Sec extends SectionV1> = keyof SectionsV1Map[Sec]["selections"];
6400
6417
  export type SelectionV2<Sec extends SectionV2> = keyof SectionsV2Map[Sec]["selections"];
6401
- export type ParamsV1<Sec extends SectionV1, Sel extends keyof SectionsV1Map[Sec]["selections"]> = SectionsV1Map[Sec]["selections"][Sel] extends {
6418
+ export type ParamsV1<Sec extends SectionV1, Sel extends SelectionV1<Sec>> = SectionsV1Map[Sec]["selections"][Sel] extends {
6402
6419
  params: any;
6403
6420
  } ? SectionsV1Map[Sec]["selections"][Sel]["params"] : never;
6404
- export type ParamsV2<Sec extends SectionV2, Sel extends keyof SectionsV2Map[Sec]["selections"]> = SectionsV2Map[Sec]["selections"][Sel] extends {
6421
+ export type ParamsV2<Sec extends SectionV2, Sel extends SelectionV2<Sec>> = SectionsV2Map[Sec]["selections"][Sel] extends {
6405
6422
  params: any;
6406
6423
  } ? SectionsV2Map[Sec]["selections"][Sel]["params"] : never;
6407
6424
  export type GetArgumentV1<Sec extends SectionV1, Sel extends keyof SectionsV1Map[Sec]["selections"]> = ParamsV1<Sec, Sel> extends never ? {
@@ -6459,11 +6476,44 @@ export type CacheV2<Sec extends SectionV2, Sel extends keyof SectionsV2Map[Sec][
6459
6476
  set: (key: GetArgumentV2<Sec, Sel>, value: GetResponseV2<Sec, Sel>, expiry: number) => void | Promise<void>;
6460
6477
  };
6461
6478
  type UnionToIntersection<U> = (U extends any ? (x: U) => void : never) extends (x: infer I) => void ? I : never;
6462
- export declare function tornApiGetV1<Sec extends SectionV1, Sel extends SelectionV1<Sec>>({ section, selections, id, params, key, comment, cache, expiry, }: GetArgumentV1<Sec, Sel> & {
6479
+ export interface HTTPClient {
6480
+ getJson(url: URL): Promise<any>;
6481
+ }
6482
+ export declare class FetchHTTPClient implements HTTPClient {
6483
+ getJson(url: URL): Promise<any>;
6484
+ }
6485
+ interface ClientOptions {
6486
+ httpClient?: HTTPClient;
6487
+ defaultComment?: string;
6488
+ }
6489
+ export declare class TornApiClient {
6490
+ private readonly httpClient;
6491
+ private readonly defaultComment?;
6492
+ constructor(options?: ClientOptions);
6493
+ getV1<Sec extends SectionV1, Sel extends SelectionV1<Sec>>({ section, selections, id, params, key, comment, cache, expiry, }: GetArgumentV1<Sec, Sel> & {
6494
+ cache?: CacheV1<Sec, Sel>;
6495
+ expiry?: number;
6496
+ }): Promise<GetResponseV1<Sec, Sel>>;
6497
+ getV2<Sec extends SectionV2, Sel extends SelectionV2<Sec>>({ section, selections, id, params, key, comment, cache, expiry, }: GetArgumentV2<Sec, Sel> & {
6498
+ cache?: CacheV2<Sec, Sel>;
6499
+ expiry?: number;
6500
+ }): Promise<GetResponseV2<Sec, Sel>>;
6501
+ private handleError;
6502
+ private populateUrl;
6503
+ }
6504
+ /**
6505
+ * @deprecated Move to TornApiClient instead of using this. Will be removed in an upcoming version.
6506
+ * @see TornApiClient
6507
+ */
6508
+ export declare function tornApiGetV1<Sec extends SectionV1, Sel extends SelectionV1<Sec>>(options: GetArgumentV1<Sec, Sel> & {
6463
6509
  cache?: CacheV1<Sec, Sel>;
6464
6510
  expiry?: number;
6465
6511
  }): Promise<GetResponseV1<Sec, Sel>>;
6466
- export declare function tornApiGetV2<Sec extends SectionV2, Sel extends SelectionV2<Sec>>({ section, selections, id, params, key, comment, cache, expiry, }: GetArgumentV2<Sec, Sel> & {
6512
+ /**
6513
+ * @deprecated Move to TornApiClient instead of using this. Will be removed in an upcoming version.
6514
+ * @see TornApiClient
6515
+ */
6516
+ export declare function tornApiGetV2<Sec extends SectionV2, Sel extends SelectionV2<Sec>>(options: GetArgumentV2<Sec, Sel> & {
6467
6517
  cache?: CacheV2<Sec, Sel>;
6468
6518
  expiry?: number;
6469
6519
  }): Promise<GetResponseV2<Sec, Sel>>;
package/dist/index.js CHANGED
@@ -34,81 +34,80 @@ export var TornApiError;
34
34
  TornApiError[TornApiError["SELECTION_ONLY_AVAILABLE_API_V2"] = 23] = "SELECTION_ONLY_AVAILABLE_API_V2";
35
35
  TornApiError[TornApiError["CLOSED_TEMPORARILY"] = 24] = "CLOSED_TEMPORARILY";
36
36
  })(TornApiError || (TornApiError = {}));
37
- export async function tornApiGetV1({ section, selections, id, params = {}, key, comment, cache, expiry, }) {
38
- const cached = await cache?.get({
39
- section,
40
- selections,
41
- id,
42
- params,
43
- key,
44
- });
45
- if (cached)
46
- return cached;
47
- const url = new URL(`https://api.torn.com/${section}/${id ?? ""}`);
48
- selections?.length &&
49
- url.searchParams.set("selections", selections.join(","));
50
- url.searchParams.set("key", key);
51
- if (comment)
52
- url.searchParams.set("comment", comment);
53
- Object.entries(params).forEach(([k, v]) => url.searchParams.set(k, v));
54
- return await fetch(url)
55
- .then((res) => res.json())
56
- .then(addToCache)
57
- .catch(handleError);
58
- function handleError(e) {
59
- console.error(e);
60
- return { error: { code: -1, error: generateErrorString(e) } };
61
- function generateErrorString(e) {
62
- switch (typeof e) {
63
- case "string":
64
- return e;
65
- case "object": {
66
- if (e instanceof Error)
67
- return e.message;
68
- return JSON.stringify(e);
69
- }
70
- default:
71
- return e.toString();
72
- }
73
- }
37
+ export class FetchHTTPClient {
38
+ async getJson(url) {
39
+ const response = await fetch(url);
40
+ return await response.json();
74
41
  }
75
- function addToCache(response) {
76
- if ("error" in response)
42
+ }
43
+ export class TornApiClient {
44
+ httpClient;
45
+ defaultComment;
46
+ constructor(options = {}) {
47
+ this.httpClient = options.httpClient ?? new FetchHTTPClient();
48
+ this.defaultComment = options.defaultComment;
49
+ }
50
+ async getV1({ section, selections, id, params = {}, key, comment, cache, expiry, }) {
51
+ const cached = await cache?.get({
52
+ section,
53
+ selections,
54
+ id,
55
+ params,
56
+ key,
57
+ });
58
+ if (cached)
59
+ return cached;
60
+ const url = new URL(`https://api.torn.com/${section}/${id ?? ""}`);
61
+ this.populateUrl(url, key, (selections ?? []), comment, params ?? {});
62
+ return this.httpClient
63
+ .getJson(url)
64
+ .then(addToCache)
65
+ .catch(this.handleError);
66
+ function addToCache(response) {
67
+ if ("error" in response)
68
+ return response;
69
+ cache?.set({
70
+ section,
71
+ selections,
72
+ id,
73
+ params,
74
+ key,
75
+ }, response, expiry ?? Date.now() + 30_000);
77
76
  return response;
78
- cache?.set({
77
+ }
78
+ }
79
+ async getV2({ section, selections, id, params = {}, key, comment, cache, expiry, }) {
80
+ const cached = await cache?.get({
79
81
  section,
80
82
  selections,
81
83
  id,
82
84
  params,
83
85
  key,
84
- }, response, expiry ?? Date.now() + 30_000);
85
- return response;
86
+ });
87
+ if (cached)
88
+ return cached;
89
+ const url = new URL(`https://api.torn.com/v2/${section}/${id ?? ""}`);
90
+ this.populateUrl(url, key, (selections ?? []), comment, params ?? {});
91
+ return this.httpClient
92
+ .getJson(url)
93
+ .then(addToCache)
94
+ .catch(this.handleError);
95
+ function addToCache(response) {
96
+ if ("error" in response)
97
+ return response;
98
+ cache?.set({
99
+ section,
100
+ selections,
101
+ id,
102
+ params,
103
+ key,
104
+ }, response, expiry ?? Date.now() + 30_000);
105
+ return response;
106
+ }
86
107
  }
87
- }
88
- export async function tornApiGetV2({ section, selections, id, params = {}, key, comment, cache, expiry, }) {
89
- const cached = await cache?.get({
90
- section,
91
- selections,
92
- id,
93
- params,
94
- key,
95
- });
96
- if (cached)
97
- return cached;
98
- const url = new URL(`https://api.torn.com/v2/${section}/${id ?? ""}`);
99
- selections?.length &&
100
- url.searchParams.set("selections", selections.join(","));
101
- url.searchParams.set("key", key);
102
- if (comment)
103
- url.searchParams.set("comment", comment);
104
- Object.entries(params).forEach(([k, v]) => url.searchParams.set(k, v));
105
- return await fetch(url)
106
- .then((res) => res.json())
107
- .then(addToCache)
108
- .catch(handleError);
109
- function handleError(e) {
110
- console.error(e);
111
- return { error: { code: -1, error: generateErrorString(e) } };
108
+ handleError(error) {
109
+ console.error(error);
110
+ return { error: { code: -1, error: generateErrorString(error) } };
112
111
  function generateErrorString(e) {
113
112
  switch (typeof e) {
114
113
  case "string":
@@ -123,16 +122,30 @@ export async function tornApiGetV2({ section, selections, id, params = {}, key,
123
122
  }
124
123
  }
125
124
  }
126
- function addToCache(response) {
127
- if ("error" in response)
128
- return response;
129
- cache?.set({
130
- section,
131
- selections,
132
- id,
133
- params,
125
+ populateUrl(url, key, selections, comment, params) {
126
+ const allParams = {
134
127
  key,
135
- }, response, expiry ?? Date.now() + 30_000);
136
- return response;
128
+ comment: comment ?? this.defaultComment,
129
+ selections: selections.length ? selections.join(",") : undefined,
130
+ ...params,
131
+ };
132
+ Object.entries(allParams)
133
+ .filter((entry) => !!entry[1])
134
+ .forEach(([key, value]) => url.searchParams.set(key, value));
137
135
  }
138
136
  }
137
+ const DEFAULT_CLIENT = new TornApiClient();
138
+ /**
139
+ * @deprecated Move to TornApiClient instead of using this. Will be removed in an upcoming version.
140
+ * @see TornApiClient
141
+ */
142
+ export async function tornApiGetV1(options) {
143
+ return DEFAULT_CLIENT.getV1(options);
144
+ }
145
+ /**
146
+ * @deprecated Move to TornApiClient instead of using this. Will be removed in an upcoming version.
147
+ * @see TornApiClient
148
+ */
149
+ export async function tornApiGetV2(options) {
150
+ return DEFAULT_CLIENT.getV2(options);
151
+ }
package/dist/index.ts CHANGED
@@ -4303,6 +4303,14 @@ export type TornHofCategory =
4303
4303
 
4304
4304
  export type TornFactionHofCategory = "respect" | "chains" | "rank";
4305
4305
 
4306
+ export type TornOrganizedCrimePositionId =
4307
+ | "P1"
4308
+ | "P2"
4309
+ | "P3"
4310
+ | "P4"
4311
+ | "P5"
4312
+ | "P6";
4313
+
4306
4314
  export type FactionTerritoryWarResultEnum =
4307
4315
  | "success_assault"
4308
4316
  | "fail_assault"
@@ -5109,6 +5117,7 @@ export interface UserPropertyDetailsExtendedRented
5109
5117
  cost_per_day: number;
5110
5118
  rental_period: number;
5111
5119
  rental_period_remaining: number;
5120
+ rented_by: BasicUser;
5112
5121
  }
5113
5122
 
5114
5123
  export interface UserPropertyDetailsExtendedForRent
@@ -7050,6 +7059,8 @@ export interface FactionCrimeReward {
7050
7059
 
7051
7060
  export interface FactionCrimeSlot {
7052
7061
  position: string;
7062
+ position_id: TornOrganizedCrimePositionId;
7063
+ position_number: number;
7053
7064
  item_requirement: {
7054
7065
  id: ItemId;
7055
7066
  /** Shows if the item is reusable or consumed during the crime. */
@@ -7325,6 +7336,11 @@ export interface KeyInfoResponse {
7325
7336
  forum: ForumSelectionName[];
7326
7337
  key: KeySelectionName[];
7327
7338
  };
7339
+ user: {
7340
+ faction_id: FactionId | null;
7341
+ company_id: CompanyId | null;
7342
+ id: UserId;
7343
+ };
7328
7344
  access: {
7329
7345
  level: number;
7330
7346
  type: ApiKeyAccessTypeEnum;
@@ -7668,7 +7684,7 @@ export interface TornOrganizedCrimeScope {
7668
7684
  }
7669
7685
 
7670
7686
  export interface TornOrganizedCrimeSlot {
7671
- id: string;
7687
+ id: TornOrganizedCrimePositionId;
7672
7688
  name: string;
7673
7689
  required_item: TornOrganizedCrimeRequiredItem | null;
7674
7690
  }
@@ -7791,7 +7807,7 @@ export interface TornCalendarResponse {
7791
7807
  };
7792
7808
  }
7793
7809
 
7794
- export interface TornHof {
7810
+ export interface TornHofBasic {
7795
7811
  id: UserId;
7796
7812
  username: string;
7797
7813
  faction_id: FactionId;
@@ -7806,6 +7822,12 @@ export interface TornHof {
7806
7822
  rank: string;
7807
7823
  }
7808
7824
 
7825
+ export interface TornHofWithOffenses extends TornHofBasic {
7826
+ criminal_offenses: number;
7827
+ }
7828
+
7829
+ export type TornHof = TornHofBasic | TornHofWithOffenses;
7830
+
7809
7831
  export interface TornHofResponse {
7810
7832
  hof: TornHof[];
7811
7833
  _metadata: RequestMetadataWithLinks;
@@ -8668,6 +8690,10 @@ export interface UserV1 extends BaseSchema {
8668
8690
  bazaarcustomers: number;
8669
8691
  bazaarprofit: number;
8670
8692
  bazaarsales: number;
8693
+ itemmarketsales: number;
8694
+ itemmarketcustomers: number;
8695
+ itemmarketrevenue: number;
8696
+ itemmarketfees: number;
8671
8697
  bestactivestreak: number;
8672
8698
  bestdamage: number;
8673
8699
  bestkillstreak: number;
@@ -11902,16 +11928,21 @@ export type SelectionV2<Sec extends SectionV2> =
11902
11928
 
11903
11929
  export type ParamsV1<
11904
11930
  Sec extends SectionV1,
11905
- Sel extends keyof SectionsV1Map[Sec]["selections"],
11906
- > = SectionsV1Map[Sec]["selections"][Sel] extends { params: any }
11931
+ Sel extends SelectionV1<Sec>,
11932
+ > = SectionsV1Map[Sec]["selections"][Sel] extends {
11933
+ params: any;
11934
+ }
11907
11935
  ? SectionsV1Map[Sec]["selections"][Sel]["params"]
11908
11936
  : never;
11909
11937
  export type ParamsV2<
11910
11938
  Sec extends SectionV2,
11911
- Sel extends keyof SectionsV2Map[Sec]["selections"],
11912
- > = SectionsV2Map[Sec]["selections"][Sel] extends { params: any }
11939
+ Sel extends SelectionV2<Sec>,
11940
+ > = SectionsV2Map[Sec]["selections"][Sel] extends {
11941
+ params: any;
11942
+ }
11913
11943
  ? SectionsV2Map[Sec]["selections"][Sel]["params"]
11914
11944
  : never;
11945
+
11915
11946
  export type GetArgumentV1<
11916
11947
  Sec extends SectionV1,
11917
11948
  Sel extends keyof SectionsV1Map[Sec]["selections"],
@@ -12022,123 +12053,145 @@ type UnionToIntersection<U> = (U extends any ? (x: U) => void : never) extends (
12022
12053
  ? I
12023
12054
  : never;
12024
12055
 
12025
- export async function tornApiGetV1<
12026
- Sec extends SectionV1,
12027
- Sel extends SelectionV1<Sec>,
12028
- >({
12029
- section,
12030
- selections,
12031
- id,
12032
- params = {} as any,
12033
- key,
12034
- comment,
12035
- cache,
12036
- expiry,
12037
- }: GetArgumentV1<Sec, Sel> & {
12038
- cache?: CacheV1<Sec, Sel>;
12039
- expiry?: number;
12040
- }): Promise<GetResponseV1<Sec, Sel>> {
12041
- const cached = await cache?.get({
12056
+ export interface HTTPClient {
12057
+ getJson(url: URL): Promise<any>;
12058
+ }
12059
+
12060
+ export class FetchHTTPClient implements HTTPClient {
12061
+ async getJson(url: URL): Promise<any> {
12062
+ const response = await fetch(url);
12063
+
12064
+ return await response.json();
12065
+ }
12066
+ }
12067
+
12068
+ interface ClientOptions {
12069
+ httpClient?: HTTPClient;
12070
+ defaultComment?: string;
12071
+ }
12072
+
12073
+ export class TornApiClient {
12074
+ private readonly httpClient: HTTPClient;
12075
+ private readonly defaultComment?: string;
12076
+
12077
+ constructor(options: ClientOptions = {}) {
12078
+ this.httpClient = options.httpClient ?? new FetchHTTPClient();
12079
+ this.defaultComment = options.defaultComment;
12080
+ }
12081
+
12082
+ async getV1<Sec extends SectionV1, Sel extends SelectionV1<Sec>>({
12042
12083
  section,
12043
12084
  selections,
12044
12085
  id,
12045
- params,
12086
+ params = {} as any,
12046
12087
  key,
12047
- } as GetArgumentV1<Sec, Sel>);
12048
- if (cached) return cached;
12049
-
12050
- const url = new URL(`https://api.torn.com/${section}/${id ?? ""}`);
12051
- selections?.length &&
12052
- url.searchParams.set("selections", selections.join(","));
12053
- url.searchParams.set("key", key);
12054
- if (comment) url.searchParams.set("comment", comment);
12055
- Object.entries<string>(params as any).forEach(([k, v]) =>
12056
- url.searchParams.set(k, v),
12057
- );
12058
-
12059
- return await fetch(url)
12060
- .then((res) => res.json())
12061
- .then(addToCache)
12062
- .catch(handleError);
12063
-
12064
- function handleError(e: unknown) {
12065
- console.error(e);
12066
- return { error: { code: -1, error: generateErrorString(e) } };
12088
+ comment,
12089
+ cache,
12090
+ expiry,
12091
+ }: GetArgumentV1<Sec, Sel> & {
12092
+ cache?: CacheV1<Sec, Sel>;
12093
+ expiry?: number;
12094
+ }): Promise<GetResponseV1<Sec, Sel>> {
12095
+ const cached = await cache?.get({
12096
+ section,
12097
+ selections,
12098
+ id,
12099
+ params,
12100
+ key,
12101
+ } as GetArgumentV1<Sec, Sel>);
12102
+ if (cached) return cached;
12103
+
12104
+ const url = new URL(`https://api.torn.com/${section}/${id ?? ""}`);
12105
+ this.populateUrl(
12106
+ url,
12107
+ key,
12108
+ (selections ?? []) as string[],
12109
+ comment,
12110
+ params ?? {},
12111
+ );
12067
12112
 
12068
- function generateErrorString(e: unknown): string {
12069
- switch (typeof e) {
12070
- case "string":
12071
- return e;
12072
- case "object": {
12073
- if (e instanceof Error) return e.message;
12074
- return JSON.stringify(e);
12075
- }
12076
- default:
12077
- return (e as any).toString();
12078
- }
12113
+ return this.httpClient
12114
+ .getJson(url)
12115
+ .then(addToCache)
12116
+ .catch(this.handleError);
12117
+
12118
+ function addToCache(
12119
+ response: GetResponseV1<Sec, Sel>,
12120
+ ): GetResponseV1<Sec, Sel> {
12121
+ if ("error" in (response as any)) return response;
12122
+ cache?.set(
12123
+ {
12124
+ section,
12125
+ selections,
12126
+ id,
12127
+ params,
12128
+ key,
12129
+ } as GetArgumentV1<Sec, Sel>,
12130
+ response,
12131
+ expiry ?? Date.now() + 30_000,
12132
+ );
12133
+ return response;
12079
12134
  }
12080
12135
  }
12081
12136
 
12082
- function addToCache(
12083
- response: GetResponseV1<Sec, Sel>,
12084
- ): GetResponseV1<Sec, Sel> {
12085
- if ("error" in (response as any)) return response;
12086
- cache?.set(
12087
- {
12088
- section,
12089
- selections,
12090
- id,
12091
- params,
12092
- key,
12093
- } as GetArgumentV1<Sec, Sel>,
12094
- response,
12095
- expiry ?? Date.now() + 30_000,
12096
- );
12097
- return response;
12098
- }
12099
- }
12100
- export async function tornApiGetV2<
12101
- Sec extends SectionV2,
12102
- Sel extends SelectionV2<Sec>,
12103
- >({
12104
- section,
12105
- selections,
12106
- id,
12107
- params = {} as any,
12108
- key,
12109
- comment,
12110
- cache,
12111
- expiry,
12112
- }: GetArgumentV2<Sec, Sel> & {
12113
- cache?: CacheV2<Sec, Sel>;
12114
- expiry?: number;
12115
- }): Promise<GetResponseV2<Sec, Sel>> {
12116
- const cached = await cache?.get({
12137
+ async getV2<Sec extends SectionV2, Sel extends SelectionV2<Sec>>({
12117
12138
  section,
12118
12139
  selections,
12119
12140
  id,
12120
- params,
12141
+ params = {} as any,
12121
12142
  key,
12122
- } as GetArgumentV2<Sec, Sel>);
12123
- if (cached) return cached;
12124
-
12125
- const url = new URL(`https://api.torn.com/v2/${section}/${id ?? ""}`);
12126
- selections?.length &&
12127
- url.searchParams.set("selections", selections.join(","));
12128
- url.searchParams.set("key", key);
12129
- if (comment) url.searchParams.set("comment", comment);
12130
- Object.entries<string>(params as any).forEach(([k, v]) =>
12131
- url.searchParams.set(k, v),
12132
- );
12133
-
12134
- return await fetch(url)
12135
- .then((res) => res.json())
12136
- .then(addToCache)
12137
- .catch(handleError);
12138
-
12139
- function handleError(e: unknown) {
12140
- console.error(e);
12141
- return { error: { code: -1, error: generateErrorString(e) } };
12143
+ comment,
12144
+ cache,
12145
+ expiry,
12146
+ }: GetArgumentV2<Sec, Sel> & {
12147
+ cache?: CacheV2<Sec, Sel>;
12148
+ expiry?: number;
12149
+ }): Promise<GetResponseV2<Sec, Sel>> {
12150
+ const cached = await cache?.get({
12151
+ section,
12152
+ selections,
12153
+ id,
12154
+ params,
12155
+ key,
12156
+ } as GetArgumentV2<Sec, Sel>);
12157
+ if (cached) return cached;
12158
+
12159
+ const url = new URL(`https://api.torn.com/v2/${section}/${id ?? ""}`);
12160
+ this.populateUrl(
12161
+ url,
12162
+ key,
12163
+ (selections ?? []) as string[],
12164
+ comment,
12165
+ params ?? {},
12166
+ );
12167
+
12168
+ return this.httpClient
12169
+ .getJson(url)
12170
+ .then(addToCache)
12171
+ .catch(this.handleError);
12172
+
12173
+ function addToCache(
12174
+ response: GetResponseV2<Sec, Sel>,
12175
+ ): GetResponseV2<Sec, Sel> {
12176
+ if ("error" in (response as any)) return response;
12177
+ cache?.set(
12178
+ {
12179
+ section,
12180
+ selections,
12181
+ id,
12182
+ params,
12183
+ key,
12184
+ } as GetArgumentV2<Sec, Sel>,
12185
+ response,
12186
+ expiry ?? Date.now() + 30_000,
12187
+ );
12188
+ return response;
12189
+ }
12190
+ }
12191
+
12192
+ private handleError(error: unknown) {
12193
+ console.error(error);
12194
+ return { error: { code: -1, error: generateErrorString(error) } };
12142
12195
 
12143
12196
  function generateErrorString(e: unknown): string {
12144
12197
  switch (typeof e) {
@@ -12154,21 +12207,58 @@ export async function tornApiGetV2<
12154
12207
  }
12155
12208
  }
12156
12209
 
12157
- function addToCache(
12158
- response: GetResponseV2<Sec, Sel>,
12159
- ): GetResponseV2<Sec, Sel> {
12160
- if ("error" in (response as any)) return response;
12161
- cache?.set(
12162
- {
12163
- section,
12164
- selections,
12165
- id,
12166
- params,
12167
- key,
12168
- } as GetArgumentV2<Sec, Sel>,
12169
- response,
12170
- expiry ?? Date.now() + 30_000,
12171
- );
12172
- return response;
12210
+ private populateUrl(
12211
+ url: URL,
12212
+ key: string,
12213
+ selections: string[],
12214
+ comment: string | undefined,
12215
+ params: Record<string, string | undefined>,
12216
+ ): void {
12217
+ const allParams: Record<string, string | undefined> = {
12218
+ key,
12219
+ comment: comment ?? this.defaultComment,
12220
+ selections: selections.length ? selections.join(",") : undefined,
12221
+ ...params,
12222
+ };
12223
+
12224
+ Object.entries(allParams)
12225
+ .filter<[string, string]>(
12226
+ (entry): entry is [string, string] => !!entry[1],
12227
+ )
12228
+ .forEach(([key, value]) => url.searchParams.set(key, value));
12173
12229
  }
12174
12230
  }
12231
+
12232
+ const DEFAULT_CLIENT = new TornApiClient();
12233
+
12234
+ /**
12235
+ * @deprecated Move to TornApiClient instead of using this. Will be removed in an upcoming version.
12236
+ * @see TornApiClient
12237
+ */
12238
+ export async function tornApiGetV1<
12239
+ Sec extends SectionV1,
12240
+ Sel extends SelectionV1<Sec>,
12241
+ >(
12242
+ options: GetArgumentV1<Sec, Sel> & {
12243
+ cache?: CacheV1<Sec, Sel>;
12244
+ expiry?: number;
12245
+ },
12246
+ ): Promise<GetResponseV1<Sec, Sel>> {
12247
+ return DEFAULT_CLIENT.getV1(options);
12248
+ }
12249
+
12250
+ /**
12251
+ * @deprecated Move to TornApiClient instead of using this. Will be removed in an upcoming version.
12252
+ * @see TornApiClient
12253
+ */
12254
+ export async function tornApiGetV2<
12255
+ Sec extends SectionV2,
12256
+ Sel extends SelectionV2<Sec>,
12257
+ >(
12258
+ options: GetArgumentV2<Sec, Sel> & {
12259
+ cache?: CacheV2<Sec, Sel>;
12260
+ expiry?: number;
12261
+ },
12262
+ ): Promise<GetResponseV2<Sec, Sel>> {
12263
+ return DEFAULT_CLIENT.getV2(options);
12264
+ }
package/dist/openapi.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "info": {
4
4
  "title": "Torn API",
5
5
  "description": "\n * The development of Torn's API v2 is still ongoing.\n * If selections remain unaltered, they will default to the API v1 version.\n * Unlike API v1, API v2 accepts both selections and IDs as path and query parameters.\n * If any discrepancies or errors are found, please submit a [bug report](https://www.torn.com/forums.php#/p=forums&f=19&b=0&a=0) on the Torn Forums.\n * In case you're using bots to check for changes on openapi.json file, make sure to specificy a custom user-agent header - CloudFlare sometimes prevents requests from default user-agents.",
6
- "version": "2.0.2"
6
+ "version": "3.0.1"
7
7
  },
8
8
  "servers": [
9
9
  {
@@ -11585,6 +11585,17 @@
11585
11585
  "rank"
11586
11586
  ]
11587
11587
  },
11588
+ "TornOrganizedCrimePositionId": {
11589
+ "type": "string",
11590
+ "enum": [
11591
+ "P1",
11592
+ "P2",
11593
+ "P3",
11594
+ "P4",
11595
+ "P5",
11596
+ "P6"
11597
+ ]
11598
+ },
11588
11599
  "FactionTerritoryWarResultEnum": {
11589
11600
  "type": "string",
11590
11601
  "enum": [
@@ -13559,6 +13570,7 @@
13559
13570
  {
13560
13571
  "required": [
13561
13572
  "status",
13573
+ "rented_by",
13562
13574
  "used_by",
13563
13575
  "cost",
13564
13576
  "cost_per_day",
@@ -13593,6 +13605,9 @@
13593
13605
  "rental_period_remaining": {
13594
13606
  "type": "integer",
13595
13607
  "format": "int32"
13608
+ },
13609
+ "rented_by": {
13610
+ "$ref": "#/components/schemas/BasicUser"
13596
13611
  }
13597
13612
  },
13598
13613
  "type": "object"
@@ -20724,6 +20739,8 @@
20724
20739
  "FactionCrimeSlot": {
20725
20740
  "required": [
20726
20741
  "position",
20742
+ "position_id",
20743
+ "position_number",
20727
20744
  "item_requirement",
20728
20745
  "user",
20729
20746
  "checkpoint_pass_rate"
@@ -20732,6 +20749,13 @@
20732
20749
  "position": {
20733
20750
  "type": "string"
20734
20751
  },
20752
+ "position_id": {
20753
+ "$ref": "#/components/schemas/TornOrganizedCrimePositionId"
20754
+ },
20755
+ "position_number": {
20756
+ "type": "integer",
20757
+ "format": "int32"
20758
+ },
20735
20759
  "item_requirement": {
20736
20760
  "description": "Details of item required for the slot, if applicable.",
20737
20761
  "oneOf": [
@@ -21896,7 +21920,8 @@
21896
21920
  "info": {
21897
21921
  "required": [
21898
21922
  "selections",
21899
- "access"
21923
+ "access",
21924
+ "user"
21900
21925
  ],
21901
21926
  "properties": {
21902
21927
  "selections": {
@@ -21969,6 +21994,41 @@
21969
21994
  },
21970
21995
  "type": "object"
21971
21996
  },
21997
+ "user": {
21998
+ "required": [
21999
+ "id",
22000
+ "faction_id",
22001
+ "company_id"
22002
+ ],
22003
+ "properties": {
22004
+ "faction_id": {
22005
+ "description": "Null only if the user is not in a faction.",
22006
+ "oneOf": [
22007
+ {
22008
+ "$ref": "#/components/schemas/FactionId"
22009
+ },
22010
+ {
22011
+ "type": "null"
22012
+ }
22013
+ ]
22014
+ },
22015
+ "company_id": {
22016
+ "description": "Null only if the user is not in a company.",
22017
+ "oneOf": [
22018
+ {
22019
+ "$ref": "#/components/schemas/CompanyId"
22020
+ },
22021
+ {
22022
+ "type": "null"
22023
+ }
22024
+ ]
22025
+ },
22026
+ "id": {
22027
+ "$ref": "#/components/schemas/UserId"
22028
+ }
22029
+ },
22030
+ "type": "object"
22031
+ },
21972
22032
  "access": {
21973
22033
  "required": [
21974
22034
  "level",
@@ -21988,6 +22048,8 @@
21988
22048
  "type": "boolean"
21989
22049
  },
21990
22050
  "faction_id": {
22051
+ "description": "This field is replaced with 'user'.'faction_id' field and will be removed on 1st of September 2025. Populated only if 'faction' field is true.",
22052
+ "deprecated": true,
21991
22053
  "oneOf": [
21992
22054
  {
21993
22055
  "$ref": "#/components/schemas/FactionId"
@@ -22001,6 +22063,8 @@
22001
22063
  "type": "boolean"
22002
22064
  },
22003
22065
  "company_id": {
22066
+ "description": "This field is replaced with 'user'.'company_id' field and will be removed on 1st of September 2025. Populated only if 'company' field is true.",
22067
+ "deprecated": true,
22004
22068
  "oneOf": [
22005
22069
  {
22006
22070
  "$ref": "#/components/schemas/CompanyId"
@@ -23476,7 +23540,7 @@
23476
23540
  ],
23477
23541
  "properties": {
23478
23542
  "id": {
23479
- "type": "string"
23543
+ "$ref": "#/components/schemas/TornOrganizedCrimePositionId"
23480
23544
  },
23481
23545
  "name": {
23482
23546
  "type": "string"
@@ -23982,7 +24046,7 @@
23982
24046
  },
23983
24047
  "type": "object"
23984
24048
  },
23985
- "TornHof": {
24049
+ "TornHofBasic": {
23986
24050
  "required": [
23987
24051
  "id",
23988
24052
  "username",
@@ -24056,6 +24120,35 @@
24056
24120
  },
24057
24121
  "type": "object"
24058
24122
  },
24123
+ "TornHofWithOffenses": {
24124
+ "allOf": [
24125
+ {
24126
+ "$ref": "#/components/schemas/TornHofBasic"
24127
+ },
24128
+ {
24129
+ "required": [
24130
+ "criminal_offenses"
24131
+ ],
24132
+ "properties": {
24133
+ "criminal_offenses": {
24134
+ "type": "integer",
24135
+ "format": "int32"
24136
+ }
24137
+ },
24138
+ "type": "object"
24139
+ }
24140
+ ]
24141
+ },
24142
+ "TornHof": {
24143
+ "oneOf": [
24144
+ {
24145
+ "$ref": "#/components/schemas/TornHofBasic"
24146
+ },
24147
+ {
24148
+ "$ref": "#/components/schemas/TornHofWithOffenses"
24149
+ }
24150
+ ]
24151
+ },
24059
24152
  "TornHofResponse": {
24060
24153
  "required": [
24061
24154
  "hof",
package/package.json CHANGED
@@ -1,13 +1,13 @@
1
1
  {
2
2
  "name": "tornapi-typescript",
3
3
  "type": "module",
4
- "version": "2.0.2",
4
+ "version": "3.0.1",
5
5
  "scripts": {
6
6
  "generate-types": "tsc && node build/index.js"
7
7
  },
8
8
  "devDependencies": {
9
- "@scalar/openapi-types": "^0.3.5",
10
- "@types/node": "^22.16.0",
9
+ "@scalar/openapi-types": "^0.3.6",
10
+ "@types/node": "^22.16.5",
11
11
  "prettier": "^3.6.2",
12
12
  "typeconv": "^2.3.1",
13
13
  "typescript": "^5.8.3"