Skip to content

Commit d8a7184

Browse files
committed
feat: added support for staging requests via request options
1 parent 78216da commit d8a7184

File tree

2 files changed

+27
-14
lines changed

2 files changed

+27
-14
lines changed

src/model/xivapi-options.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,9 @@ export interface XivapiOptions {
3737
* A GitHub issue to add support for this to TypeScript can be found at https://github.com/Microsoft/TypeScript/issues/6579
3838
*/
3939
tags?: string[];
40+
41+
/**
42+
* Should this request be fired against staging environment? NOT SUITED FOR PRODUCTION !!!
43+
*/
44+
staging?: boolean;
4045
}

src/xivapi.service.ts

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ export class XivapiService {
2121
* Base url of xivapi.
2222
*/
2323
public static readonly API_BASE_URL: string = 'https://xivapi.com';
24+
public static readonly STAGING_API_BASE_URL: string = 'https://staging.xivapi.com';
2425

2526
constructor(@Inject(XIVAPI_KEY) protected readonly apiKey: string, private http: HttpClient) {
2627
}
@@ -33,7 +34,7 @@ export class XivapiService {
3334
* @param options The options of the request, optional.
3435
*/
3536
public get<T = any>(endpoint: XivapiEndpoint, id: number, options?: XivapiRequestOptions): Observable<T> {
36-
return this.request<T>(`${XivapiService.API_BASE_URL}/${endpoint}/${id}`, options);
37+
return this.request<T>(`/${endpoint}/${id}`, options);
3738
}
3839

3940
/**
@@ -43,7 +44,7 @@ export class XivapiService {
4344
* @param options The options of the request, optional.
4445
*/
4546
public getList<T = any>(endpoint: XivapiEndpoint, options?: XivapiRequestOptions): Observable<XivapiList<T>> {
46-
return this.request<XivapiList<T>>(`${XivapiService.API_BASE_URL}/${endpoint}`, options);
47+
return this.request<XivapiList<T>>(`/${endpoint}`, options);
4748
}
4849

4950
/**
@@ -62,7 +63,7 @@ export class XivapiService {
6263
if (this.apiKey !== undefined) {
6364
queryParams = queryParams.set('key', this.apiKey);
6465
}
65-
return this.http.get<any>(`${XivapiService.API_BASE_URL}/Search`, {params: queryParams});
66+
return this.http.get<any>(`/Search`, {params: queryParams});
6667
}
6768

6869
/**
@@ -74,14 +75,14 @@ export class XivapiService {
7475
*/
7576
public getCharacter(lodestoneId: number, options?: XivapiCharacterOptions,
7677
details?: 'Friends' | 'Achievements' | 'Gearsets' | 'Record' | 'FreeCompany'): Observable<CharacterResponse> {
77-
return this.request<any>(`${XivapiService.API_BASE_URL}/Character/${lodestoneId}${details ? '/' + details : ''}`, options);
78+
return this.request<any>(`/Character/${lodestoneId}${details ? '/' + details : ''}`, options);
7879
}
7980

8081
/**
8182
* Gets the current list of available servers. Useful for character search queries.
8283
*/
8384
public getServerList(): Observable<string[]> {
84-
return this.request<string[]>(`${XivapiService.API_BASE_URL}/servers`);
85+
return this.request<string[]>(`/servers`);
8586
}
8687

8788
/**
@@ -97,7 +98,7 @@ export class XivapiService {
9798
* @param page Search or move to a specific page.
9899
*/
99100
public searchCharacter(name: string, server?: string, page?: number): Observable<CharacterSearchResult> {
100-
let url: string = `${XivapiService.API_BASE_URL}/character/search?name=${name}`;
101+
let url: string = `/character/search?name=${name}`;
101102
if (server !== undefined) {
102103
url += `&server=${server}`;
103104
}
@@ -114,7 +115,7 @@ export class XivapiService {
114115
* @param options Options of the request.
115116
*/
116117
public verifyCharacter(lodestoneId: number, options?: XivapiCharacterOptions): Observable<CharacterVerification> {
117-
return this.request<any>(`${XivapiService.API_BASE_URL}/Character/${lodestoneId}/Verification`, options);
118+
return this.request<any>(`/Character/${lodestoneId}/Verification`, options);
118119
}
119120

120121

@@ -127,7 +128,7 @@ export class XivapiService {
127128
*/
128129
public getFreeCompany(lodestoneId: number, options?: XivapiOptions,
129130
details?: 'members' | 'record'): Observable<any> {
130-
return this.request<any>(`${XivapiService.API_BASE_URL}/FreeCompany/${lodestoneId}${details ? '/' + details : ''}`, options);
131+
return this.request<any>(`/FreeCompany/${lodestoneId}${details ? '/' + details : ''}`, options);
131132
}
132133

133134
/**
@@ -139,29 +140,36 @@ export class XivapiService {
139140
*/
140141
public getLinkshell(lodestoneId: number, options?: XivapiOptions,
141142
details?: 'record'): Observable<any> {
142-
return this.request<any>(`${XivapiService.API_BASE_URL}/Linkshell/${lodestoneId}${details ? '/' + details : ''}`, options);
143+
return this.request<any>(`/Linkshell/${lodestoneId}${details ? '/' + details : ''}`, options);
143144
}
144145

145146
/**
146147
* Gets a PvP team based on its lodestone id (string)
147148
*
148149
* @param id the id of the team to get.
150+
* @param options Options of the request
149151
*/
150-
public getPvPTeam(id: string): Observable<PvpTeam> {
151-
return this.request<PvpTeam>(`${XivapiService.API_BASE_URL}/PvPTeam/${id}`);
152+
public getPvPTeam(id: string, options?: XivapiOptions): Observable<PvpTeam> {
153+
return this.request<PvpTeam>(`/PvPTeam/${id}`, options);
152154
}
153155

154156
/**
155157
* Gets the list of patches using the /PatchList endpoint.
156158
* @param options Options of the request.
157159
*/
158-
public getPatchList(options: XivapiOptions): Observable<any> {
159-
return this.request<any>(`${XivapiService.API_BASE_URL}/PatchList`, options);
160+
public getPatchList(options?: XivapiOptions): Observable<any> {
161+
return this.request<any>(`/PatchList`, options);
160162
}
161163

162164
protected request<T>(endpoint: string, params?: XivapiOptions): Observable<T> {
163165
const queryParams: HttpParams = this.prepareQueryString(params);
164-
return this.http.get<any>(endpoint, {params: queryParams});
166+
let baseUrl: string;
167+
if (params !== undefined) {
168+
baseUrl = params.staging ? XivapiService.STAGING_API_BASE_URL : XivapiService.API_BASE_URL;
169+
} else {
170+
baseUrl = XivapiService.API_BASE_URL;
171+
}
172+
return this.http.get<any>(`${baseUrl}${endpoint}`, {params: queryParams});
165173
}
166174

167175
private prepareQueryString(options?: XivapiOptions): HttpParams {

0 commit comments

Comments
 (0)