Skip to content

Commit

Permalink
*BREAKING CHANGE* feature(http): request path interceptors (#202)
Browse files Browse the repository at this point in the history
* create an interface for url comparation and a RegEx implementation of it (IURLCompare)

* created a separate file for IHttpInterceptor and moved into /interceptors

* move unit test into /interceptors

* added interceptor mappings as a new feature and restructured code

* fixed http module to configure new mappings correctly

* fixed index.ts to export correct interfaces

* renamed compare interfaces with matcher and made it more flexible for any type of interceptions

* pass instance of matcher interface on constructor

* remove HttpInterceptorService from provider since its not needed

* validate for empty strings when matching in url-regexp matche

* added unit tests for ** and /path** interceptors

* updated unit tests with more interceptor cases

* added unit tests for url regexp interceptor matcher

* added some docblocks

* added examples for path interception

* fixed docs to use `paths` and as an array

* chages for AoT future support in http.module

* updated http-interceptor.service unit tests with AoT changes

* added Type<IHttpInterceptor> type for interceptor providers in docs and unit tests

* updated docs with AoT changes
  • Loading branch information
emoralesb05 authored and kyleledbetter committed Dec 26, 2016
1 parent 1d59af9 commit 7db377e
Show file tree
Hide file tree
Showing 13 changed files with 913 additions and 340 deletions.
77 changes: 74 additions & 3 deletions src/app/components/components/http/http.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -70,17 +70,31 @@ <h3>Setup:</h3>
}
]]>
</td-highlight>
<p>Then, import the [CovalentHttpModule] using the <code>forRoot()</code> method with the desired interceptors in your NgModule:</p>
<p>Then, import the [CovalentHttpModule] using the <code>forRoot()</code> method with the desired interceptors and paths to intercept in your NgModule:</p>
<td-highlight lang="typescript">
<![CDATA[
import { NgModule, Type } from '@angular/core';
import { HttpModule } from '@angular/http';
import { CovalentHttpModule } from '@covalent/http';
import { CovalentHttpModule, IHttpInterceptor } from '@covalent/http';
import { CustomInterceptor } from 'dir/to/interceptor';

const httpInterceptorProviders: Type<IHttpInterceptor>[] = [
CustomInterceptor,
...
];

@NgModule({
imports: [
HttpModule, /* or CovalentCoreModule.forRoot() */
CovalentHttpModule.forRoot([CustomInterceptor]),
CovalentHttpModule.forRoot({
interceptors: [{
interceptor: CustomInterceptor, paths: ['**'],
}],
}),
...
],
providers: [
httpInterceptorProviders,
...
],
...
Expand All @@ -89,6 +103,63 @@ <h3>Setup:</h3>
]]>
</td-highlight>
<p>After that, just inject [HttpInterceptorService] and use it for your requests.</p>
<h3>Paths:</h3>
<p>The following characters are accepted as a path to intercept:</p>
<td-markdown>
<pre><code>
- `**` is a wildcard for `[a-zA-Z0-9-_]` (including `/`)
- `*` is a wildcard for `[a-zA-Z0-9-_]` (excluding `/`)
- `[a-zA-Z0-9-_]`
</code></pre>
</td-markdown>
<h3>Examples:</h3>
<td-markdown>
<pre><code>
Example 1

`/users/*/groups` intercepts:
- `www.url.com/users/id-of-user/groups`
- `www.url.com/users/id/groups`

`/users/*/groups` DOES NOT intercept:
- `www.url.com/users/id-of-user/groups/path`
- `www.url.com/users/id-of-user/path/groups`
- `www.url.com/users/groups`

Example 2

`/users/**/groups` intercepts:
- `www.url.com/users/id-of-user/groups`
- `www.url.com/users/id/groups`
- `www.url.com/users/id-of-user/path/groups`

`/users/**/groups` DOES NOT intercept:
- `www.url.com/users/id-of-user/groups/path`
- `www.url.com/users/groups`

Example 3

`/users/**` intercepts:
- `www.url.com/users/id-of-user/groups`
- `www.url.com/users/id/groups`
- `www.url.com/users/id-of-user/path/groups`
- `www.url.com/users/id-of-user/groups/path`
- `www.url.com/users/groups`

`/users/**` DOES NOT intercept:
- `www.url.com/users`

Example 4

`/users**` intercepts:
- `www.url.com/users/id-of-user/groups`
- `www.url.com/users/id/groups`
- `www.url.com/users/id-of-user/path/groups`
- `www.url.com/users/id-of-user/groups/path`
- `www.url.com/users/groups`
- `www.url.com/users`
</code></pre>
</td-markdown>
</md-card-content>
</md-card>
<md-card>
Expand Down
74 changes: 71 additions & 3 deletions src/platform/http/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,16 +73,31 @@ export class CustomInterceptor implements IHttpInterceptor {

```

Then, import the [CovalentHttpModule] using the forRoot() method with the desired interceptors in your NgModule:
Then, import the [CovalentHttpModule] using the forRoot() method with the desired interceptors and paths to intercept in your NgModule:

```typescript
import { NgModule, Type } from '@angular/core';
import { HttpModule } from '@angular/http';
import { CovalentHttpModule } from '@covalent/http';
import { CovalentHttpModule, IHttpInterceptor } from '@covalent/http';
import { CustomInterceptor } from 'dir/to/interceptor';

const httpInterceptorProviders: Type<IHttpInterceptor>[] = [
CustomInterceptor,
...
];

@NgModule({
imports: [
HttpModule, /* or CovalentCoreModule.forRoot() */
CovalentHttpModule.forRoot([CustomInterceptor]),
CovalentHttpModule.forRoot({
interceptors: [{
interceptor: CustomInterceptor, paths: ['**'],
}],
}),
...
],
providers: [
httpInterceptorProviders,
...
],
...
Expand All @@ -92,6 +107,59 @@ export class MyModule {}

After that, just inject [HttpInterceptorService] and use it for your requests.

## Paths

The following characters are accepted as a path to intercept
- `**` is a wildcard for `[a-zA-Z0-9-_]` (including `/`)
- `*` is a wildcard for `[a-zA-Z0-9-_]` (excluding `/`)
- `[a-zA-Z0-9-_]`

#### Examples

Example 1

`/users/*/groups` intercepts:
- `www.url.com/users/id-of-user/groups`
- `www.url.com/users/id/groups`

`/users/*/groups` DOES NOT intercept:
- `www.url.com/users/id-of-user/groups/path`
- `www.url.com/users/id-of-user/path/groups`
- `www.url.com/users/groups`

Example 2

`/users/**/groups` intercepts:
- `www.url.com/users/id-of-user/groups`
- `www.url.com/users/id/groups`
- `www.url.com/users/id-of-user/path/groups`

`/users/**/groups` DOES NOT intercept:
- `www.url.com/users/id-of-user/groups/path`
- `www.url.com/users/groups`

Example 3

`/users/**` intercepts:
- `www.url.com/users/id-of-user/groups`
- `www.url.com/users/id/groups`
- `www.url.com/users/id-of-user/path/groups`
- `www.url.com/users/id-of-user/groups/path`
- `www.url.com/users/groups`

`/users/**` DOES NOT intercept:
- `www.url.com/users`

Example 4

`/users**` intercepts:
- `www.url.com/users/id-of-user/groups`
- `www.url.com/users/id/groups`
- `www.url.com/users/id-of-user/path/groups`
- `www.url.com/users/id-of-user/groups/path`
- `www.url.com/users/groups`
- `www.url.com/users`


# RESTService

Expand Down
154 changes: 0 additions & 154 deletions src/platform/http/http-interceptor.service.spec.ts

This file was deleted.

Loading

0 comments on commit 7db377e

Please sign in to comment.