Skip to content

Commit b484a12

Browse files
committed
refactor: API base URLs use InjectionTokens for better configurability
- Introduced API_BASE_URL and RATINGS_API_BASE_URL InjectionTokens in api-config.ts to centralize API base URL management. - Updated HordeStatusService, Client-Agent Interceptor, Rate Limit Interceptor, RatingsApiService, SharedKeyService, StyleService, and TeamService to use the new InjectionTokens. - Modified tests in respective service spec files to utilize API_BASE and RATINGS_API_BASE from api-test-helpers for consistency. - Adjusted environment files to include ratingsApiBaseUrl for development and local environments.
1 parent 17e5052 commit b484a12

32 files changed

Lines changed: 326 additions & 450 deletions

angular.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@
117117
}
118118
},
119119
"cli": {
120-
"schematicCollections": ["angular-eslint"]
120+
"schematicCollections": ["angular-eslint"],
121+
"analytics": false
121122
}
122123
}

src/app/services/admin-filter.service.spec.ts

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { of } from 'rxjs';
99
import { AdminFilterService } from './admin-filter.service';
1010
import { AuthService } from './auth.service';
1111
import { HordeApiCacheService } from './horde-api-cache.service';
12+
import { API_BASE } from '../testing/api-test-helpers';
1213

1314
describe('AdminFilterService', () => {
1415
let service: AdminFilterService;
@@ -119,7 +120,7 @@ describe('AdminFilterService', () => {
119120
const payload = { regex: 'bad.*word', filter_type: 10 };
120121
service.createFilter(payload as never).subscribe();
121122

122-
const req = httpTesting.expectOne('https://aihorde.net/api/v2/filters');
123+
const req = httpTesting.expectOne(`${API_BASE}/filters`);
123124
expect(req.request.method).toBe('PUT');
124125
expect(req.request.headers.get('apikey')).toBe('mod-key');
125126
req.flush({ id: 'new-id' });
@@ -146,9 +147,7 @@ describe('AdminFilterService', () => {
146147
service
147148
.updateFilter('f1', { description: 'updated' } as never)
148149
.subscribe();
149-
const req = httpTesting.expectOne(
150-
'https://aihorde.net/api/v2/filters/f1',
151-
);
150+
const req = httpTesting.expectOne(`${API_BASE}/filters/f1`);
152151
expect(req.request.method).toBe('PATCH');
153152
req.flush({ id: 'f1' });
154153
expect(mockCache.invalidate).toHaveBeenCalled();
@@ -170,9 +169,7 @@ describe('AdminFilterService', () => {
170169
it('sends DELETE and returns true on success', () => {
171170
let result: unknown;
172171
service.deleteFilter('f1').subscribe((r) => (result = r));
173-
const req = httpTesting.expectOne(
174-
'https://aihorde.net/api/v2/filters/f1',
175-
);
172+
const req = httpTesting.expectOne(`${API_BASE}/filters/f1`);
176173
expect(req.request.method).toBe('DELETE');
177174
req.flush(null);
178175
expect(result).toBe(true);
@@ -194,7 +191,7 @@ describe('AdminFilterService', () => {
194191
describe('testPrompt()', () => {
195192
it('sends POST with prompt data', () => {
196193
service.testPrompt({ prompt: 'hello world' } as never).subscribe();
197-
const req = httpTesting.expectOne('https://aihorde.net/api/v2/filters');
194+
const req = httpTesting.expectOne(`${API_BASE}/filters`);
198195
expect(req.request.method).toBe('POST');
199196
expect(req.request.body).toEqual({ prompt: 'hello world' });
200197
req.flush({ suspicion: 0 });

src/app/services/admin-filter.service.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import {
1111
TestPromptRequest,
1212
} from '../types/filter';
1313
import { HordeApiCacheService, CacheTTL } from './horde-api-cache.service';
14+
import { API_BASE_URL } from './api-config';
1415

1516
/**
1617
* Service for managing AI Horde filters.
@@ -24,7 +25,7 @@ export class AdminFilterService {
2425
private readonly httpClient = inject(HttpClient);
2526
private readonly auth = inject(AuthService);
2627
private readonly cache = inject(HordeApiCacheService);
27-
private readonly baseUrl = 'https://aihorde.net/api/v2';
28+
private readonly baseUrl = inject(API_BASE_URL);
2829

2930
/**
3031
* Get authorization headers for API requests.

src/app/services/admin-operations.service.spec.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,9 @@ import { of } from 'rxjs';
99
import { AdminOperationsService } from './admin-operations.service';
1010
import { AuthService } from './auth.service';
1111
import { HordeApiCacheService } from './horde-api-cache.service';
12+
import { API_BASE } from '../testing/api-test-helpers';
1213

13-
const BASE = 'https://aihorde.net/api/v2';
14+
const BASE = API_BASE;
1415

1516
describe('AdminOperationsService', () => {
1617
let svc: AdminOperationsService;

src/app/services/admin-operations.service.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
SimpleResponse,
1010
} from '../types/ip-operations';
1111
import { HordeApiCacheService, CacheTTL } from './horde-api-cache.service';
12+
import { API_BASE_URL } from './api-config';
1213

1314
/**
1415
* Service for managing IP operations (timeouts and blocks).
@@ -22,7 +23,7 @@ export class AdminOperationsService {
2223
private readonly httpClient = inject(HttpClient);
2324
private readonly auth = inject(AuthService);
2425
private readonly cache = inject(HordeApiCacheService);
25-
private readonly baseUrl = 'https://aihorde.net/api/v2';
26+
private readonly baseUrl = inject(API_BASE_URL);
2627

2728
/**
2829
* Get authorization headers for API requests.

src/app/services/admin-user.service.spec.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import { of } from 'rxjs';
1010
import { AdminUserService } from './admin-user.service';
1111
import { AuthService } from './auth.service';
1212
import { HordeApiCacheService } from './horde-api-cache.service';
13+
import { API_BASE } from '../testing/api-test-helpers';
1314

1415
describe('AdminUserService', () => {
1516
let service: AdminUserService;
@@ -116,7 +117,7 @@ describe('AdminUserService', () => {
116117
it('sends PUT with apikey and invalidates cache', () => {
117118
service.updateUser(42, { trusted: true }).subscribe();
118119

119-
const req = httpTesting.expectOne('https://aihorde.net/api/v2/users/42');
120+
const req = httpTesting.expectOne(`${API_BASE}/users/42`);
120121
expect(req.request.method).toBe('PUT');
121122
expect(req.request.headers.get('apikey')).toBe('admin-key');
122123
expect(req.request.body).toEqual({ trusted: true });
@@ -141,35 +142,35 @@ describe('AdminUserService', () => {
141142

142143
it('setTrusted sends { trusted } payload', () => {
143144
service.setTrusted(42, true).subscribe();
144-
const req = httpTesting.expectOne('https://aihorde.net/api/v2/users/42');
145+
const req = httpTesting.expectOne(`${API_BASE}/users/42`);
145146
expect(req.request.body).toEqual({ trusted: true });
146147
req.flush({});
147148
});
148149

149150
it('setFlagged sends { flagged } payload', () => {
150151
service.setFlagged(42, false).subscribe();
151-
const req = httpTesting.expectOne('https://aihorde.net/api/v2/users/42');
152+
const req = httpTesting.expectOne(`${API_BASE}/users/42`);
152153
expect(req.request.body).toEqual({ flagged: false });
153154
req.flush({});
154155
});
155156

156157
it('resetSuspicion sends { reset_suspicion: true }', () => {
157158
service.resetSuspicion(42).subscribe();
158-
const req = httpTesting.expectOne('https://aihorde.net/api/v2/users/42');
159+
const req = httpTesting.expectOne(`${API_BASE}/users/42`);
159160
expect(req.request.body).toEqual({ reset_suspicion: true });
160161
req.flush({});
161162
});
162163

163164
it('setVpnAccess sends { vpn } payload', () => {
164165
service.setVpnAccess(42, true).subscribe();
165-
const req = httpTesting.expectOne('https://aihorde.net/api/v2/users/42');
166+
const req = httpTesting.expectOne(`${API_BASE}/users/42`);
166167
expect(req.request.body).toEqual({ vpn: true });
167168
req.flush({});
168169
});
169170

170171
it('setWorkerInvites sends { worker_invite } payload', () => {
171172
service.setWorkerInvites(42, 5).subscribe();
172-
const req = httpTesting.expectOne('https://aihorde.net/api/v2/users/42');
173+
const req = httpTesting.expectOne(`${API_BASE}/users/42`);
173174
expect(req.request.body).toEqual({ worker_invite: 5 });
174175
req.flush({});
175176
});

src/app/services/admin-user.service.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
import { SharedKeyDetails } from '../types/shared-key';
1010
import { AuthService } from './auth.service';
1111
import { HordeApiCacheService, CacheTTL } from './horde-api-cache.service';
12+
import { API_BASE_URL } from './api-config';
1213

1314
@Injectable({
1415
providedIn: 'root',
@@ -17,7 +18,7 @@ export class AdminUserService {
1718
private readonly httpClient = inject(HttpClient);
1819
private readonly auth = inject(AuthService);
1920
private readonly cache = inject(HordeApiCacheService);
20-
private readonly baseUrl = 'https://aihorde.net/api/v2';
21+
private readonly baseUrl = inject(API_BASE_URL);
2122

2223
/**
2324
* Get user by ID with full admin details

src/app/services/admin-worker.service.spec.ts

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ import { AdminWorkerService } from './admin-worker.service';
99
import { AuthService } from './auth.service';
1010
import { HordeApiCacheService } from './horde-api-cache.service';
1111
import { HordeWorker } from '../types/horde-worker';
12+
import { API_BASE } from '../testing/api-test-helpers';
13+
14+
const BASE = API_BASE;
1215

1316
describe('AdminWorkerService', () => {
1417
let service: AdminWorkerService;
@@ -182,9 +185,7 @@ describe('AdminWorkerService', () => {
182185
it('sends PUT with apikey and invalidates cache', () => {
183186
service.updateWorker('w1', { paused: true }).subscribe();
184187

185-
const req = httpTesting.expectOne(
186-
'https://aihorde.net/api/v2/workers/w1',
187-
);
188+
const req = httpTesting.expectOne(`${BASE}/workers/w1`);
188189
expect(req.request.method).toBe('PUT');
189190
expect(req.request.headers.get('apikey')).toBe('admin-key');
190191
req.flush({ paused: true });
@@ -210,9 +211,7 @@ describe('AdminWorkerService', () => {
210211
it('sends maintenance flag with message', () => {
211212
service.setMaintenance('w1', true, 'GPU update').subscribe();
212213

213-
const req = httpTesting.expectOne(
214-
'https://aihorde.net/api/v2/workers/w1',
215-
);
214+
const req = httpTesting.expectOne(`${BASE}/workers/w1`);
216215
expect(req.request.body).toEqual({
217216
maintenance: true,
218217
maintenance_msg: 'GPU update',
@@ -223,9 +222,7 @@ describe('AdminWorkerService', () => {
223222
it('clears message when disabling maintenance', () => {
224223
service.setMaintenance('w1', false).subscribe();
225224

226-
const req = httpTesting.expectOne(
227-
'https://aihorde.net/api/v2/workers/w1',
228-
);
225+
const req = httpTesting.expectOne(`${BASE}/workers/w1`);
229226
expect(req.request.body.maintenance).toBe(false);
230227
expect(req.request.body.maintenance_msg).toBe('');
231228
req.flush({});
@@ -236,9 +233,7 @@ describe('AdminWorkerService', () => {
236233
it('sends paused payload', () => {
237234
service.setPaused('w1', true).subscribe();
238235

239-
const req = httpTesting.expectOne(
240-
'https://aihorde.net/api/v2/workers/w1',
241-
);
236+
const req = httpTesting.expectOne(`${BASE}/workers/w1`);
242237
expect(req.request.body).toEqual({ paused: true });
243238
req.flush({});
244239
});
@@ -253,9 +248,7 @@ describe('AdminWorkerService', () => {
253248
let result: unknown;
254249
service.deleteWorker('w1').subscribe((r) => (result = r));
255250

256-
const req = httpTesting.expectOne(
257-
'https://aihorde.net/api/v2/workers/w1',
258-
);
251+
const req = httpTesting.expectOne(`${BASE}/workers/w1`);
259252
expect(req.request.method).toBe('DELETE');
260253
req.flush({ deleted_id: 'w1', deleted_name: 'test' });
261254

src/app/services/admin-worker.service.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
} from '../types/horde-worker';
1010
import { AuthService } from './auth.service';
1111
import { HordeApiCacheService, CacheTTL } from './horde-api-cache.service';
12+
import { API_BASE_URL } from './api-config';
1213

1314
@Injectable({
1415
providedIn: 'root',
@@ -17,7 +18,7 @@ export class AdminWorkerService {
1718
private readonly httpClient = inject(HttpClient);
1819
private readonly auth = inject(AuthService);
1920
private readonly cache = inject(HordeApiCacheService);
20-
private readonly baseUrl = 'https://aihorde.net/api/v2';
21+
private readonly baseUrl = inject(API_BASE_URL);
2122

2223
/**
2324
* Get all workers in the horde

0 commit comments

Comments
 (0)