feat: init and fork some code from angular-auth-oidc-client
Some checks are pending
Build, Lint & Test Lib / Built, Lint and Test Library (push) Waiting to run
Build, Lint & Test Lib / Angular latest (push) Blocked by required conditions
Build, Lint & Test Lib / Angular latest & Schematics Job (push) Blocked by required conditions
Build, Lint & Test Lib / Angular latest Standalone & Schematics Job (push) Blocked by required conditions
Build, Lint & Test Lib / Angular 16 & RxJs 6 (push) Blocked by required conditions
Build, Lint & Test Lib / Angular V16 (push) Blocked by required conditions
Docs / Build and Deploy Docs Job (push) Waiting to run
Docs / Close Pull Request Job (push) Waiting to run

This commit is contained in:
2025-01-18 01:05:00 +08:00
parent 276d9fbda8
commit 983254164b
201 changed files with 35689 additions and 0 deletions

View File

@@ -0,0 +1,192 @@
import {
HttpHeaders,
provideHttpClient,
withInterceptorsFromDi,
} from '@angular/common/http';
import {
HttpTestingController,
provideHttpClientTesting,
} from '@angular/common/http/testing';
import { TestBed, waitForAsync } from '@angular/core/testing';
import { DataService } from './data.service';
import { HttpBaseService } from './http-base.service';
describe('Data Service', () => {
let dataService: DataService;
let httpMock: HttpTestingController;
beforeEach(() => {
TestBed.configureTestingModule({
imports: [],
providers: [
DataService,
HttpBaseService,
provideHttpClient(withInterceptorsFromDi()),
provideHttpClientTesting(),
],
});
});
beforeEach(() => {
dataService = TestBed.inject(DataService);
httpMock = TestBed.inject(HttpTestingController);
});
it('should create', () => {
expect(dataService).toBeTruthy();
});
describe('get', () => {
it('get call sets the accept header', waitForAsync(() => {
const url = 'testurl';
dataService
.get(url, { configId: 'configId1' })
.subscribe((data: unknown) => {
expect(data).toBe('bodyData');
});
const req = httpMock.expectOne(url);
expect(req.request.method).toBe('GET');
expect(req.request.headers.get('Accept')).toBe('application/json');
req.flush('bodyData');
httpMock.verify();
}));
it('get call with token the accept header and the token', waitForAsync(() => {
const url = 'testurl';
const token = 'token';
dataService
.get(url, { configId: 'configId1' }, token)
.subscribe((data: unknown) => {
expect(data).toBe('bodyData');
});
const req = httpMock.expectOne(url);
expect(req.request.method).toBe('GET');
expect(req.request.headers.get('Accept')).toBe('application/json');
expect(req.request.headers.get('Authorization')).toBe('Bearer ' + token);
req.flush('bodyData');
httpMock.verify();
}));
it('call without ngsw-bypass param by default', waitForAsync(() => {
const url = 'testurl';
dataService
.get(url, { configId: 'configId1' })
.subscribe((data: unknown) => {
expect(data).toBe('bodyData');
});
const req = httpMock.expectOne(url);
expect(req.request.method).toBe('GET');
expect(req.request.headers.get('Accept')).toBe('application/json');
expect(req.request.params.get('ngsw-bypass')).toBeNull();
req.flush('bodyData');
httpMock.verify();
}));
it('call with ngsw-bypass param', waitForAsync(() => {
const url = 'testurl';
dataService
.get(url, { configId: 'configId1', ngswBypass: true })
.subscribe((data: unknown) => {
expect(data).toBe('bodyData');
});
const req = httpMock.expectOne(url + '?ngsw-bypass=');
expect(req.request.method).toBe('GET');
expect(req.request.headers.get('Accept')).toBe('application/json');
expect(req.request.params.get('ngsw-bypass')).toBe('');
req.flush('bodyData');
httpMock.verify();
}));
});
describe('post', () => {
it('call sets the accept header when no other params given', waitForAsync(() => {
const url = 'testurl';
dataService
.post(url, { some: 'thing' }, { configId: 'configId1' })
.subscribe();
const req = httpMock.expectOne(url);
expect(req.request.method).toBe('POST');
expect(req.request.headers.get('Accept')).toBe('application/json');
req.flush('bodyData');
httpMock.verify();
}));
it('call sets custom headers ONLY (No ACCEPT header) when custom headers are given', waitForAsync(() => {
const url = 'testurl';
let headers = new HttpHeaders();
headers = headers.set('X-MyHeader', 'Genesis');
dataService
.post(url, { some: 'thing' }, { configId: 'configId1' }, headers)
.subscribe();
const req = httpMock.expectOne(url);
expect(req.request.method).toBe('POST');
expect(req.request.headers.get('X-MyHeader')).toEqual('Genesis');
expect(req.request.headers.get('X-MyHeader')).not.toEqual('Genesis333');
req.flush('bodyData');
httpMock.verify();
}));
it('call without ngsw-bypass param by default', waitForAsync(() => {
const url = 'testurl';
dataService
.post(url, { some: 'thing' }, { configId: 'configId1' })
.subscribe();
const req = httpMock.expectOne(url);
expect(req.request.method).toBe('POST');
expect(req.request.headers.get('Accept')).toBe('application/json');
expect(req.request.params.get('ngsw-bypass')).toBeNull();
req.flush('bodyData');
httpMock.verify();
}));
it('call with ngsw-bypass param', waitForAsync(() => {
const url = 'testurl';
dataService
.post(
url,
{ some: 'thing' },
{ configId: 'configId1', ngswBypass: true }
)
.subscribe();
const req = httpMock.expectOne(url + '?ngsw-bypass=');
expect(req.request.method).toBe('POST');
expect(req.request.headers.get('Accept')).toBe('application/json');
expect(req.request.params.get('ngsw-bypass')).toBe('');
req.flush('bodyData');
httpMock.verify();
}));
});
});

65
src/api/data.service.ts Normal file
View File

@@ -0,0 +1,65 @@
import { HttpHeaders, HttpParams } from '@ngify/http';
import { Injectable, inject } from 'injection-js';
import { Observable } from 'rxjs';
import { OpenIdConfiguration } from '../config/openid-configuration';
import { HttpBaseService } from './http-base.service';
const NGSW_CUSTOM_PARAM = 'ngsw-bypass';
@Injectable()
export class DataService {
private readonly httpClient = inject(HttpBaseService);
get<T>(
url: string,
config: OpenIdConfiguration,
token?: string
): Observable<T> {
const headers = this.prepareHeaders(token);
const params = this.prepareParams(config);
return this.httpClient.get<T>(url, {
headers,
params,
});
}
post<T>(
url: string | null,
body: unknown,
config: OpenIdConfiguration,
headersParams?: HttpHeaders
): Observable<T> {
const headers = headersParams || this.prepareHeaders();
const params = this.prepareParams(config);
return this.httpClient.post<T>(url ?? '', body, { headers, params });
}
private prepareHeaders(token?: string): HttpHeaders {
let headers = new HttpHeaders();
headers = headers.set('Accept', 'application/json');
if (!!token) {
headers = headers.set(
'Authorization',
'Bearer ' + decodeURIComponent(token)
);
}
return headers;
}
private prepareParams(config: OpenIdConfiguration): HttpParams {
let params = new HttpParams();
const { ngswBypass } = config;
if (ngswBypass) {
params = params.set(NGSW_CUSTOM_PARAM, '');
}
return params;
}
}

View File

@@ -0,0 +1,22 @@
import { HttpClient } from '@ngify/http';
import { Injectable, inject } from 'injection-js';
import { Observable } from 'rxjs';
@Injectable()
export class HttpBaseService {
constructor() {}
private readonly http = inject(HttpClient);
get<T>(url: string, params?: { [key: string]: unknown }): Observable<T> {
return this.http.get<T>(url, params);
}
post<T>(
url: string,
body: unknown,
params?: { [key: string]: unknown }
): Observable<T> {
return this.http.post<T>(url, body, params);
}
}