Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Switch APIs to v2.0 #10722

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/portal/src/app/app.module.ts
Expand Up @@ -45,6 +45,7 @@ import { LabelsComponent } from './labels/labels.component';
import { ProjectQuotasComponent } from './project-quotas/project-quotas.component';
import { HarborLibraryModule } from "../lib/harbor-library.module";
import { HTTP_INTERCEPTORS } from '@angular/common/http';
import { BaseHrefInterceptService } from "./base-href-intercept.service";

registerLocaleData(zh, 'zh-cn');
registerLocaleData(es, 'es-es');
Expand Down Expand Up @@ -98,6 +99,7 @@ export function getCurrentLanguage(translateService: TranslateService) {
multi: true
},
{ provide: LOCALE_ID, useValue: "en-US" },
{ provide: HTTP_INTERCEPTORS, useClass: BaseHrefInterceptService, multi: true },
{ provide: HTTP_INTERCEPTORS, useClass: InterceptHttpService, multi: true }

],
Expand Down
18 changes: 18 additions & 0 deletions src/portal/src/app/base-href-intercept.service.spec.ts
@@ -0,0 +1,18 @@
import { TestBed } from '@angular/core/testing';
import { BaseHrefInterceptService } from "./base-href-intercept.service";


describe('BaseHrefSwitchService', () => {
beforeEach(() => {
TestBed.configureTestingModule({
providers: [
BaseHrefInterceptService
]
});
});

it('should be created', () => {
const service: BaseHrefInterceptService = TestBed.get(BaseHrefInterceptService);
expect(service).toBeTruthy();
});
});
22 changes: 22 additions & 0 deletions src/portal/src/app/base-href-intercept.service.ts
@@ -0,0 +1,22 @@
import { Injectable } from '@angular/core';
import { HttpInterceptor, HttpHandler, HttpRequest } from '@angular/common/http';
import { Observable } from "rxjs";

const BASE_HREF = '/api';
enum APILevels {
'V1.0' = '',
'V2.0' = '/v2.0'
}
@Injectable()
export class BaseHrefInterceptService implements HttpInterceptor {
intercept(req: HttpRequest<any>, next: HttpHandler): Observable<any> {
let url: string = req.url;
// use API level v2.0
if (url && url.indexOf(BASE_HREF) !== -1 && url.indexOf(BASE_HREF + APILevels["V2.0"]) === -1) {
url = BASE_HREF + APILevels["V2.0"] + url.split(BASE_HREF)[1];
}
const apiReq = req.clone({url});
return next.handle(apiReq);
}
}