Skip to content

Commit 6c0d8bc

Browse files
committed
feat(request): support custom baseUrl
1 parent bd39473 commit 6c0d8bc

File tree

3 files changed

+38
-7
lines changed

3 files changed

+38
-7
lines changed

src/model/xivapi-options.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,11 @@ export interface XivapiOptions {
4343
*/
4444
staging?: boolean;
4545

46+
/**
47+
* A custom xivapi instance to request instead of `xivapi.com`. This is prior to `staging`.
48+
*/
49+
baseUrl?: string;
50+
4651
/**
4752
* Servers for the market endpoint.
4853
*/

src/xivapi.service.ts

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -239,14 +239,19 @@ export class XivapiService {
239239

240240
protected request<T>(endpoint: string, params?: XivapiOptions): Observable<T> {
241241
let queryParams: HttpParams = this.prepareQueryString(params);
242-
let baseUrl: string;
243-
if (params !== undefined) {
244-
baseUrl = params.staging ? XivapiService.STAGING_API_BASE_URL : XivapiService.API_BASE_URL;
245-
if (params.staging) {
246-
queryParams = queryParams.delete('staging');
242+
let baseUrl = XivapiService.API_BASE_URL;
243+
if (params) {
244+
if (params.baseUrl) {
245+
baseUrl = params.baseUrl;
246+
} else if (params.staging) {
247+
baseUrl = XivapiService.STAGING_API_BASE_URL;
247248
}
248-
} else {
249-
baseUrl = XivapiService.API_BASE_URL;
249+
250+
['staging', 'baseUrl'].forEach(key => {
251+
if (params.hasOwnProperty(key)) {
252+
queryParams.delete(key);
253+
}
254+
})
250255
}
251256
return this.doGet<any>(`${baseUrl}${endpoint}`, queryParams);
252257
}

test/xivapi.service.spec.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,4 +64,25 @@ describe('Client tests', () => {
6464
expect(req.request.params.get('string')).toEqual('legendary');
6565
expect(req.request.params.get('columns')).toEqual('Context,Source,SourceID,Text,Data.Icon');
6666
});
67+
68+
it('Should use custom baseUrl when `baseUrl` is provided', () => {
69+
const service: XivapiService = new XivapiService(httpClient);
70+
service.get(XivapiEndpoint.Item, 12087, {
71+
baseUrl: 'https://example.org'
72+
}).subscribe();
73+
const req: TestRequest = <TestRequest>httpMock.match({method: 'GET'})
74+
.find(row => row.request.url === 'https://example.org/Item/12087');
75+
expect(req).not.toBeUndefined();
76+
});
77+
78+
it('Should use custom baseUrl when both baseUrl and staging are provided', () => {
79+
const service: XivapiService = new XivapiService(httpClient);
80+
service.get(XivapiEndpoint.Item, 12087, {
81+
baseUrl: 'https://example.org',
82+
staging: true
83+
}).subscribe();
84+
const req: TestRequest = <TestRequest>httpMock.match({method: 'GET'})
85+
.find(row => row.request.url === 'https://example.org/Item/12087');
86+
expect(req).not.toBeUndefined();
87+
});
6788
});

0 commit comments

Comments
 (0)