Skip to content

Commit

Permalink
refactor(http): 添加HTTP拦截器并配置代理
Browse files Browse the repository at this point in the history
  • Loading branch information
Encaik committed Sep 28, 2024
1 parent d917574 commit 071b83f
Show file tree
Hide file tree
Showing 11 changed files with 87 additions and 3 deletions.
3 changes: 3 additions & 0 deletions angular.json
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,9 @@
"buildTarget": "wanjie:build:development"
}
},
"options": {
"proxyConfig": "proxy.conf.js"
},
"defaultConfiguration": "development"
},
"extract-i18n": {
Expand Down
8 changes: 8 additions & 0 deletions proxy.conf.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module.exports = {
'/api': {
target: 'https://wanjie-api.vercel.app',
secure: false,
changeOrigin: true,
logLevel: 'debug'
}
};
7 changes: 6 additions & 1 deletion src/app/app.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Component, inject, OnInit } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { NavigationEnd, Router, RouterOutlet } from '@angular/router';
import { EventType, SystemEventOperate } from '@models';
import { EventService } from '@services';
import { EventService, KimiService } from '@services';
import { NzLayoutModule } from 'ng-zorro-antd/layout';
import { NzModalModule, NzModalService } from 'ng-zorro-antd/modal';
import { NzSegmentedModule } from 'ng-zorro-antd/segmented';
Expand Down Expand Up @@ -34,7 +34,12 @@ export class AppComponent implements OnInit {
segmentedRoutes: string[] = ['/home', '/method', '/universe', '/challenge', '/shop', '/statistics'];
currentSegmented: number = 0;

private kimi = inject(KimiService);

ngOnInit() {
// this.kimi.getCharacterList(5).subscribe(res => {
// console.log(res);
// });
this.event
.sendEvent({
type: EventType.System,
Expand Down
5 changes: 3 additions & 2 deletions src/app/app.config.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { registerLocaleData } from '@angular/common';
import { provideHttpClient } from '@angular/common/http';
import { provideHttpClient, withInterceptors } from '@angular/common/http';
import zh from '@angular/common/locales/zh';
import { ApplicationConfig, provideZoneChangeDetection, importProvidersFrom } from '@angular/core';
import { FormsModule } from '@angular/forms';
Expand All @@ -9,6 +9,7 @@ import { zh_CN, provideNzI18n } from 'ng-zorro-antd/i18n';
import { NzModalService } from 'ng-zorro-antd/modal';

import { routes } from './app.routes';
import { apiInterceptor } from './interceptors/api-interceptor.interceptor';

const ZORRO_SERVICES = [NzModalService];

Expand All @@ -21,7 +22,7 @@ export const appConfig: ApplicationConfig = {
provideNzI18n(zh_CN),
importProvidersFrom(FormsModule),
provideAnimationsAsync(),
provideHttpClient(),
provideHttpClient(withInterceptors([apiInterceptor])),
...ZORRO_SERVICES
]
};
16 changes: 16 additions & 0 deletions src/app/interceptors/api-interceptor.interceptor.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { TestBed } from '@angular/core/testing';
import { HttpInterceptorFn } from '@angular/common/http';

import { apiInterceptor } from './api-interceptor.interceptor';

describe('apiInterceptor', () => {
const interceptor: HttpInterceptorFn = (req, next) => TestBed.runInInjectionContext(() => apiInterceptor(req, next));

beforeEach(() => {
TestBed.configureTestingModule({});
});

it('should be created', () => {
expect(interceptor).toBeTruthy();
});
});
11 changes: 11 additions & 0 deletions src/app/interceptors/api-interceptor.interceptor.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { HttpInterceptorFn } from '@angular/common/http';
import { environment } from 'environments/environment';

export const apiInterceptor: HttpInterceptorFn = (req, next) => {
if (req.url.startsWith('/api')) {
const newUrl = `${environment.apiUrl}${req.url}`;
const modifiedReq = req.clone({ url: newUrl });
return next(modifiedReq);
}
return next(req);
};
1 change: 1 addition & 0 deletions src/app/services/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ export * from './battle.service'; // 战斗管理
export * from './character.service'; // 角色管理
export * from './env.service'; // 环境管理
export * from './event.service'; // 事件管理
export * from './kimi.service'; // kimi
export * from './log.service'; // 日志管理
export * from './runtime.service'; // 运行时管理
export * from './task.service'; // 任务管理
Expand Down
16 changes: 16 additions & 0 deletions src/app/services/kimi.service.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { TestBed } from '@angular/core/testing';

import { KimiService } from './kimi.service';

describe('KimiService', () => {
let service: KimiService;

beforeEach(() => {
TestBed.configureTestingModule({});
service = TestBed.inject(KimiService);
});

it('should be created', () => {
expect(service).toBeTruthy();
});
});
15 changes: 15 additions & 0 deletions src/app/services/kimi.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { HttpClient } from '@angular/common/http';
import { inject, Injectable } from '@angular/core';

@Injectable({
providedIn: 'root'
})
export class KimiService {
private http = inject(HttpClient);

getCharacterList(length: number) {
return this.http.get('/api/generate/character', {
params: { length }
});
}
}
4 changes: 4 additions & 0 deletions src/environments/environment.prod.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export const environment = {
production: true,
apiUrl: 'https://wanjie-api.vercel.app'
};
4 changes: 4 additions & 0 deletions src/environments/environment.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export const environment = {
production: false,
apiUrl: ''
};

1 comment on commit 071b83f

@vercel
Copy link

@vercel vercel bot commented on 071b83f Sep 28, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

wanjie – ./

wanjie-encaiks-projects.vercel.app
wanjie-git-main-encaiks-projects.vercel.app
wanjie.vercel.app

Please sign in to comment.