Skip to content

Commit

Permalink
Merge pull request #15 from IKatsuba/feature/timeout
Browse files Browse the repository at this point in the history
Feature/timeout
  • Loading branch information
IKatsuba authored Jan 12, 2021
2 parents c75734a + 2c7c723 commit 0c187ae
Show file tree
Hide file tree
Showing 24 changed files with 388 additions and 62 deletions.
69 changes: 51 additions & 18 deletions angular.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,7 @@
"output": "assets/taiga-ui/icons"
}
],
"styles": [
"apps/rickandmorty/src/styles.less"
],
"styles": ["apps/rickandmorty/src/styles.less"],
"scripts": []
},
"configurations": {
Expand Down Expand Up @@ -90,9 +88,7 @@
},
"test": {
"builder": "@nrwl/jest:jest",
"outputs": [
"coverage/apps/rickandmorty"
],
"outputs": ["coverage/apps/rickandmorty"],
"options": {
"jestConfig": "apps/rickandmorty/jest.config.js",
"passWithNoTests": true
Expand Down Expand Up @@ -178,9 +174,7 @@
"lint": {
"builder": "@nrwl/linter:eslint",
"options": {
"lintFilePatterns": [
"apps/rickandmorty-e2e/**/*.{js,ts}"
]
"lintFilePatterns": ["apps/rickandmorty-e2e/**/*.{js,ts}"]
}
}
}
Expand Down Expand Up @@ -214,9 +208,7 @@
},
"test": {
"builder": "@nrwl/jest:jest",
"outputs": [
"coverage/libs/ngx-ssr/cache"
],
"outputs": ["coverage/libs/ngx-ssr/cache"],
"options": {
"jestConfig": "libs/ngx-ssr/cache/jest.config.js",
"passWithNoTests": true
Expand Down Expand Up @@ -261,9 +253,7 @@
},
"test": {
"builder": "@nrwl/jest:jest",
"outputs": [
"coverage/libs/rickandmorty/api"
],
"outputs": ["coverage/libs/rickandmorty/api"],
"options": {
"jestConfig": "libs/rickandmorty/api/jest.config.js",
"passWithNoTests": true
Expand Down Expand Up @@ -296,15 +286,58 @@
},
"test": {
"builder": "@nrwl/jest:jest",
"outputs": [
"coverage/libs/rickandmorty/utils"
],
"outputs": ["coverage/libs/rickandmorty/utils"],
"options": {
"jestConfig": "libs/rickandmorty/utils/jest.config.js",
"passWithNoTests": true
}
}
}
},
"ngx-ssr-timeout": {
"projectType": "library",
"root": "libs/ngx-ssr/timeout",
"sourceRoot": "libs/ngx-ssr/timeout/src",
"prefix": "ngx-ssr",
"architect": {
"build": {
"builder": "@nrwl/angular:package",
"options": {
"tsConfig": "libs/ngx-ssr/timeout/tsconfig.lib.json",
"project": "libs/ngx-ssr/timeout/ng-package.json"
},
"configurations": {
"production": {
"tsConfig": "libs/ngx-ssr/timeout/tsconfig.lib.prod.json"
}
}
},
"lint": {
"builder": "@nrwl/linter:eslint",
"options": {
"lintFilePatterns": [
"libs/ngx-ssr/timeout/src/**/*.ts",
"libs/ngx-ssr/timeout/src/**/*.html"
]
}
},
"test": {
"builder": "@nrwl/jest:jest",
"outputs": ["coverage/libs/ngx-ssr/timeout"],
"options": {
"jestConfig": "libs/ngx-ssr/timeout/jest.config.js",
"passWithNoTests": true
}
},
"deploy": {
"builder": "@ng-builders/semrel:release",
"options": {
"npm": {
"pkgRoot": "dist/libs/ngx-ssr/timeout"
}
}
}
}
}
},
"cli": {
Expand Down
8 changes: 7 additions & 1 deletion apps/rickandmorty/src/app/app.server.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,15 @@ import { UNIVERSAL_ANIMATION_FRAME } from '@ng-web-apis/universal';

import { AppModule } from './app.module';
import { AppComponent } from './app.component';
import { NgxSsrTimeoutModule } from '@ngx-ssr/timeout';

@NgModule({
imports: [AppModule, ServerModule, ServerTransferStateModule],
imports: [
AppModule,
ServerModule,
ServerTransferStateModule,
NgxSsrTimeoutModule.forRoot({ timeout: 500 }),
],
bootstrap: [AppComponent],
providers: [UNIVERSAL_ANIMATION_FRAME],
})
Expand Down
1 change: 1 addition & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@ module.exports = {
'<rootDir>/libs/ngx-ssr/cache',
'<rootDir>/libs/rickandmorty/api',
'<rootDir>/libs/rickandmorty/utils',
'<rootDir>/libs/ngx-ssr/timeout',
],
};
8 changes: 1 addition & 7 deletions libs/ngx-ssr/cache/README.md
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
2 changes: 1 addition & 1 deletion libs/ngx-ssr/cache/src/lib/ngx-ssr-cache.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export class NgxSsrCacheModule {
provide: LRU_CACHE_OPTIONS,
useValue: options,
}
: null,
: [],
],
};
}
Expand Down
29 changes: 29 additions & 0 deletions libs/ngx-ssr/timeout/.eslintrc.json
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": {}
}
]
}
1 change: 1 addition & 0 deletions libs/ngx-ssr/timeout/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# @ngx-ssr/timeout
23 changes: 23 additions & 0 deletions libs/ngx-ssr/timeout/jest.config.js
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',
],
};
7 changes: 7 additions & 0 deletions libs/ngx-ssr/timeout/ng-package.json
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"
}
}
24 changes: 24 additions & 0 deletions libs/ngx-ssr/timeout/package.json
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"
}
}
2 changes: 2 additions & 0 deletions libs/ngx-ssr/timeout/src/index.ts
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';
34 changes: 34 additions & 0 deletions libs/ngx-ssr/timeout/src/lib/ngx-ssr-timeout.module.ts
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,
}
: [],
],
};
}
}
88 changes: 88 additions & 0 deletions libs/ngx-ssr/timeout/src/lib/timeout.interceptor.spec.ts
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();
});
});
Loading

0 comments on commit 0c187ae

Please sign in to comment.