-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #15 from IKatsuba/feature/timeout
Feature/timeout
- Loading branch information
Showing
24 changed files
with
388 additions
and
62 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1 @@ | ||
# ngx-ssr-cache | ||
|
||
This library was generated with [Nx](https://nx.dev). | ||
|
||
## Running unit tests | ||
|
||
Run `nx test ngx-ssr-cache` to execute the unit tests. | ||
# @ngx-ssr/cache |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
{ | ||
"extends": ["../../../.eslintrc.json"], | ||
"ignorePatterns": ["!**/*"], | ||
"overrides": [ | ||
{ | ||
"files": ["*.ts"], | ||
"extends": [ | ||
"plugin:@nrwl/nx/angular", | ||
"plugin:@angular-eslint/template/process-inline-templates" | ||
], | ||
"parserOptions": { "project": ["libs/ngx-ssr/timeout/tsconfig.*?.json"] }, | ||
"rules": { | ||
"@angular-eslint/directive-selector": [ | ||
"error", | ||
{ "type": "attribute", "prefix": "ngx-ssr", "style": "camelCase" } | ||
], | ||
"@angular-eslint/component-selector": [ | ||
"error", | ||
{ "type": "element", "prefix": "ngx-ssr", "style": "kebab-case" } | ||
] | ||
} | ||
}, | ||
{ | ||
"files": ["*.html"], | ||
"extends": ["plugin:@nrwl/nx/angular-template"], | ||
"rules": {} | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
# @ngx-ssr/timeout |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
module.exports = { | ||
displayName: 'ngx-ssr-timeout', | ||
preset: '../../../jest.preset.js', | ||
setupFilesAfterEnv: ['<rootDir>/src/test-setup.ts'], | ||
globals: { | ||
'ts-jest': { | ||
tsConfig: '<rootDir>/tsconfig.spec.json', | ||
stringifyContentPathRegex: '\\.(html|svg)$', | ||
astTransformers: { | ||
before: [ | ||
'jest-preset-angular/build/InlineFilesTransformer', | ||
'jest-preset-angular/build/StripStylesTransformer', | ||
], | ||
}, | ||
}, | ||
}, | ||
coverageDirectory: '../../../coverage/libs/ngx-ssr/timeout', | ||
snapshotSerializers: [ | ||
'jest-preset-angular/build/AngularNoNgAttributesSnapshotSerializer.js', | ||
'jest-preset-angular/build/AngularSnapshotSerializer.js', | ||
'jest-preset-angular/build/HTMLCommentSerializer.js', | ||
], | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"$schema": "../../../node_modules/ng-packagr/ng-package.schema.json", | ||
"dest": "../../../dist/libs/ngx-ssr/timeout", | ||
"lib": { | ||
"entryFile": "src/index.ts" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
{ | ||
"name": "@ngx-ssr/timeout", | ||
"version": "0.0.0-development", | ||
"keywords": [ | ||
"angular", | ||
"angulae ssr", | ||
"universal", | ||
"angular/universal", | ||
"ssr", | ||
"timeout", | ||
"http", | ||
"interceptor" | ||
], | ||
"peerDependencies": { | ||
"@angular/common": "^11.0.6", | ||
"@angular/core": "^11.0.6" | ||
}, | ||
"dependencies": { | ||
"tslib": "^2.0.0" | ||
}, | ||
"publishConfig": { | ||
"access": "public" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from './lib/ngx-ssr-timeout.module'; | ||
export * from './lib/timeout.interceptor'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import { ModuleWithProviders, NgModule } from '@angular/core'; | ||
import { CommonModule } from '@angular/common'; | ||
import { HTTP_INTERCEPTORS } from '@angular/common/http'; | ||
import { | ||
TIMEOUT_OPTIONS, | ||
TimeoutInterceptor, | ||
TimeoutOptions, | ||
} from './timeout.interceptor'; | ||
|
||
@NgModule({ | ||
imports: [CommonModule], | ||
}) | ||
export class NgxSsrTimeoutModule { | ||
static forRoot( | ||
options?: TimeoutOptions | ||
): ModuleWithProviders<NgxSsrTimeoutModule> { | ||
return { | ||
ngModule: NgxSsrTimeoutModule, | ||
providers: [ | ||
{ | ||
provide: HTTP_INTERCEPTORS, | ||
useClass: TimeoutInterceptor, | ||
multi: true, | ||
}, | ||
options | ||
? { | ||
provide: TIMEOUT_OPTIONS, | ||
useValue: options, | ||
} | ||
: [], | ||
], | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
import { fakeAsync, flush, TestBed } from '@angular/core/testing'; | ||
import { | ||
HttpClientTestingModule, | ||
HttpTestingController, | ||
} from '@angular/common/http/testing'; | ||
import { | ||
HTTP_INTERCEPTORS, | ||
HttpClient, | ||
HttpErrorResponse, | ||
} from '@angular/common/http'; | ||
import { Injectable } from '@angular/core'; | ||
import { TIMEOUT_OPTIONS, TimeoutInterceptor } from './timeout.interceptor'; | ||
import { TimeoutError, timer } from 'rxjs'; | ||
|
||
@Injectable() | ||
class ApiService { | ||
constructor(private httpClient: HttpClient) {} | ||
|
||
get() { | ||
return this.httpClient.get('/api/v1/getResources'); | ||
} | ||
|
||
post() { | ||
return this.httpClient.post('/api/v1/getResources', { | ||
params: { | ||
query: '123', | ||
}, | ||
}); | ||
} | ||
} | ||
|
||
describe('TimeoutInterceptor', () => { | ||
let service: ApiService; | ||
let httpController: HttpTestingController; | ||
|
||
beforeEach(() => { | ||
TestBed.configureTestingModule({ | ||
imports: [HttpClientTestingModule], | ||
providers: [ | ||
ApiService, | ||
{ | ||
provide: TIMEOUT_OPTIONS, | ||
useValue: { timeout: 1_000 }, | ||
}, | ||
{ | ||
provide: HTTP_INTERCEPTORS, | ||
useClass: TimeoutInterceptor, | ||
multi: true, | ||
}, | ||
], | ||
}); | ||
|
||
service = TestBed.inject(ApiService); | ||
httpController = TestBed.inject(HttpTestingController); | ||
}); | ||
|
||
it('Если превышается лимит таймаута запрос обрывается', async () => { | ||
const response = service | ||
.get() | ||
.toPromise() | ||
.catch((res) => res); | ||
|
||
httpController.expectOne(`/api/v1/getResources`); | ||
|
||
await timer(2_000).toPromise(); | ||
|
||
expect(await response).toEqual( | ||
new HttpErrorResponse({ | ||
error: expect.any(TimeoutError), | ||
url: '/api/v1/getResources', | ||
}) | ||
); | ||
}); | ||
|
||
it('Если лимит НЕ превышается запрос работает в штатном режиме', async () => { | ||
service.get().subscribe(); | ||
|
||
const req = httpController.expectOne(`/api/v1/getResources`); | ||
|
||
await timer(100).toPromise(); | ||
|
||
req.flush(123); | ||
}); | ||
|
||
afterEach(() => { | ||
httpController.verify(); | ||
}); | ||
}); |
Oops, something went wrong.