@zthun/webigail-http 0.2.0 → 1.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +95 -0
- package/package.json +2 -2
package/README.md
ADDED
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
# Webigail Http
|
|
2
|
+
|
|
3
|
+
There's many libraries for invoking http requests such as axios, node-fetch, and the built in fetch api. These work, but
|
|
4
|
+
don't make it easy to do dependency injection.
|
|
5
|
+
|
|
6
|
+
The webigail-http package uses an interface + class based approach to http requests which makes dependency injection
|
|
7
|
+
much easier.
|
|
8
|
+
|
|
9
|
+
## Build Status
|
|
10
|
+
|
|
11
|
+
[](https://dl.circleci.com/status-badge/redirect/gh/zthun/webigail/tree/latest)
|
|
12
|
+
|
|
13
|
+
## Usage
|
|
14
|
+
|
|
15
|
+
Webigail is built in TypeScript and it exports both ESM and CJS modules.
|
|
16
|
+
|
|
17
|
+
```sh
|
|
18
|
+
# NPM
|
|
19
|
+
npm install @zthun/webigail-http
|
|
20
|
+
# Yarn
|
|
21
|
+
yarn add @zthun/webigail-http
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
```ts
|
|
25
|
+
import { ZUrlBuilder } from '@zthun/webigail-url';
|
|
26
|
+
import { IZHttpService, ZHttpRequestBuilder } from '@zthun/webigail-http';
|
|
27
|
+
import { Product } from '../models/product';
|
|
28
|
+
|
|
29
|
+
export class ProductService {
|
|
30
|
+
public constructor(private _http: IZHttpService) {}
|
|
31
|
+
|
|
32
|
+
public static endpoint(): string {
|
|
33
|
+
const url = new ZUrlBuilder().api(location).path('users').build();
|
|
34
|
+
return url;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
public list(): Promise<Product> {
|
|
38
|
+
const request = new ZHttpRequestBuilder().url(ProductService.endpoint()).get().build();
|
|
39
|
+
const { data } = this._http.request<Product>(request);
|
|
40
|
+
return data;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
public create(product: Product): Promise<Product> {
|
|
44
|
+
const request = new ZHttpRequestBuilder().url(ProductService.endpoint()).post(product).build();
|
|
45
|
+
const { data } = this._http.request<Product>(request);
|
|
46
|
+
return data;
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
// Somewhere at the root of your application.
|
|
51
|
+
import { ZHttpService } from '@zthun/webigail-http';
|
|
52
|
+
|
|
53
|
+
const product = new ProductService(new ZHttpService());
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
## Testing
|
|
57
|
+
|
|
58
|
+
One challenge with dealing with http services and functions in tests is mocking API calls. You can override functions
|
|
59
|
+
like get, post, but if you want to respond to different URL invocations, then it can become a bit of a pain to do so.
|
|
60
|
+
Instead of having to create a mock and shuffle the http service implementations, this package also includes a mock
|
|
61
|
+
implementation of the http service.
|
|
62
|
+
|
|
63
|
+
```ts
|
|
64
|
+
import { createGuid } from '@zthun/helpful-fn';
|
|
65
|
+
import { ZHttpServiceMock } from '@zthun/webigail-http';
|
|
66
|
+
|
|
67
|
+
describe('ProductService', () => {
|
|
68
|
+
let http: ZHttpServiceMock;
|
|
69
|
+
|
|
70
|
+
const createTestTarget = () => new ProductService(http);
|
|
71
|
+
|
|
72
|
+
beforeEach(() => {
|
|
73
|
+
http = new ZHttpServiceMock();
|
|
74
|
+
|
|
75
|
+
http.set(
|
|
76
|
+
ProductService.endpoint(),
|
|
77
|
+
ZHttpMethod.Get,
|
|
78
|
+
new ZHttpResultBuilder<User>().data([createMockExistingUser(), createMockExistingUser()]).build()
|
|
79
|
+
);
|
|
80
|
+
|
|
81
|
+
http.set(ProductService.endpoint(), ZHttpMethod.Post, (r) =>
|
|
82
|
+
new ZHttpResultBuilder<User>().data(Object.assign({}, r.body!, { id: createGuid() })).build()
|
|
83
|
+
);
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
it('should retrieve the list of users', async () => {
|
|
87
|
+
// Arrange.
|
|
88
|
+
const target = createTestTarget();
|
|
89
|
+
// Act.
|
|
90
|
+
const actual = await target.list();
|
|
91
|
+
// Assert.
|
|
92
|
+
expect(actual.length).toEqual(2);
|
|
93
|
+
});
|
|
94
|
+
});
|
|
95
|
+
```
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@zthun/webigail-http",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "1.1.0",
|
|
4
4
|
"description": "Http service implementation with an equivalent mock for testing.",
|
|
5
5
|
"author": "Anthony Bonta",
|
|
6
6
|
"license": "MIT",
|
|
@@ -30,5 +30,5 @@
|
|
|
30
30
|
"dependencies": {
|
|
31
31
|
"axios": "^1.4.0"
|
|
32
32
|
},
|
|
33
|
-
"gitHead": "
|
|
33
|
+
"gitHead": "ffceee0f80b78f31703dca2cdf3309a9f785258d"
|
|
34
34
|
}
|