Skip to content

Commit 413d791

Browse files
authored
Merge pull request #5 from PilotConway/feature/crud
Added CI
2 parents 66981b1 + e750c14 commit 413d791

File tree

12 files changed

+178
-10
lines changed

12 files changed

+178
-10
lines changed

.eslintrc.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
module.exports = {
2+
parser: 'babel-eslint',
3+
env: {
4+
browser: true,
5+
es6: true,
6+
jest: true,
7+
},
8+
extends: 'eslint:recommended',
9+
globals: {
10+
Atomics: 'readonly',
11+
SharedArrayBuffer: 'readonly',
12+
},
13+
parserOptions: {
14+
ecmaFeatures: {
15+
jsx: true,
16+
},
17+
ecmaVersion: 2018,
18+
sourceType: 'module',
19+
},
20+
plugins: ['react'],
21+
rules: {},
22+
};

.github/main.workflow

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,5 @@ workflow "Link on Push" {
44
}
55

66
action "eslint" {
7-
uses = "gimenete/eslint-action@1.0"
7+
uses = "stefanoeb/eslint-action@master"
88
}

.prettierrc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"printWidth": 80,
2+
"printWidth": 100,
33
"tabWidth": 2,
44
"useTabs": false,
55
"semi": true,
@@ -8,4 +8,4 @@
88
"bracketSpacing": true,
99
"jsxBracketSameLine": false,
1010
"fluid": false
11-
}
11+
}

.travis.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
language: node_js
2+
node_js:
3+
- '10'
4+
after_success:
5+
- yarn codecov

README.md

Lines changed: 48 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
[![Build Status](https://travis-ci.com/PilotConway/rxjs-api-client-prototype.svg?branch=master)](https://travis-ci.com/PilotConway/rxjs-api-client-prototype) [![codecov](https://codecov.io/gh/PilotConway/rxjs-api-client-prototype/branch/master/graph/badge.svg)](https://codecov.io/gh/PilotConway/rxjs-api-client-prototype)
2+
13
# rxjs-api-client-prototype
24

35
This is a prototype project to feel out ways I can use RxJS to replace the apps current server.js
@@ -24,7 +26,7 @@ import ClientProvider from 'src/ClientProvider';
2426
function Root() {
2527
const client = createClient('https://localhost:3000/api/v1');
2628
return (
27-
<ClientProvider client={client}>
29+
<ClientProvider value={client}>
2830
<App />
2931
</ClientProvider>
3032
);
@@ -42,6 +44,8 @@ the url as provided to make the call.
4244
</Request>
4345
```
4446

47+
By default, if you do not provide a client using the `ClientProvider` and use a realtive endpoint in `Request` or `useEndpointData` then a default client using `window.location.origin` as the base url will be used.
48+
4549
### Render Prop Method
4650

4751
The `<Request>` component provides a render prop method of performing a GET Request to retrieve
@@ -71,7 +75,7 @@ import React from 'react';
7175
import useEndpointData from 'src/useEndpointData`;
7276
7377
export default function MyComponent() {
74-
const [ data, loading, error, links, client ] = useEndpointHooks('/users');
78+
const [ data, loading, error, links, client ] = useEndpointHooks('/users', { params: { per_page: 5 }});
7579
return (
7680
... your component jsx
7781
)
@@ -82,12 +86,51 @@ See [`useEndpointData`](#useEndpointData) for details on the hook parameters and
8286
8387
## API
8488
85-
### `Client`
89+
### `client`
90+
91+
This is base object that is used to make requests to the server. It is recommended that you do not create this object but instead use `createClient` to create a new client.
92+
93+
Both `Request` and `useEndpointData` will also provide the client object so you can use it to make other requests to the server such as a put/post/delete.
94+
95+
#### Functions
96+
97+
The `client` object contains 4 functions that can be used to communicate with the server.
98+
99+
- get
100+
- put
101+
- post
102+
- delete
103+
104+
##### get()
105+
106+
Performs a get request to the server.
86107
87108
### `createClient`
88109
110+
This function creates a new client. It's preferred over using `client` itself as you can set the base URL and headers using `createClient` where with `client` you will have to pass full URLs to every request.
111+
89112
### `<ClientProvider>`
90113

114+
The ClientProvider allows for a client to be set as the default client for all requests in the component tree below the provider. You can use [`createClient`](#createClient) to create a client with a specific base url then use relative endpoints in all `Request` or `useEndpointData` calls to get information from the server.
115+
116+
In addition to being able to set the base url, you can also set headers and other information for every request to use.
117+
118+
```js
119+
...
120+
import { createClient } from 'src/client';
121+
import ClientProvider from 'src/ClientProvider';
122+
...
123+
124+
function Root() {
125+
const client = createClient('https://localhost:3000/api/v1');
126+
return (
127+
<ClientProvider value={client}>
128+
<App />
129+
</ClientProvider>
130+
);
131+
}
132+
```
133+
91134
### `useEndpointData`
92135

93136
This is a hook to perform a GET request to retrieve data.
@@ -145,6 +188,8 @@ The render function is called with a single parameter object that has the follow
145188

146189
### Request Error
147190

191+
TODO: define request error and ensure all errors follow this format.
192+
148193
### Pagination
149194

150195
When an endpoint supports Pagination, the response will contain a link object. This is passed

codecov.yml

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
codecov:
2+
notify:
3+
require_ci_to_pass: yes
4+
5+
coverage:
6+
precision: 2
7+
round: down
8+
range: '50...90'
9+
status:
10+
patch: off
11+
project:
12+
default:
13+
target: auto
14+
threshold: null
15+
base: pr
16+
ignore:
17+
- '**/*.spec.*'
18+
- '**/*.stories.*'
19+
- 'src/components/**/*'
20+
21+
parsers:
22+
gcov:
23+
branch_detection:
24+
conditional: yes
25+
loop: yes
26+
method: no
27+
macro: no
28+
29+
comment:
30+
layout: 'header, diff'
31+
behavior: default
32+
require_changes: no

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
"rxjs": "6.5.2"
1616
},
1717
"devDependencies": {
18+
"codecov": "^3.5.0",
1819
"typescript": "3.3.3"
1920
},
2021
"scripts": {

src/client/client.spec.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
import client from './client';
2+
// Require get request import for mocks to work
3+
// eslint-disable-next-line no-unused-vars
24
import getRequest from './getRequest';
35

46
// SEE __mocks__/getRequest for which endpoint paths result in what types of

src/client/createClient.spec.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
import createClient from './createClient';
2+
// Require get request import for mocks to work
3+
// eslint-disable-next-line no-unused-vars
24
import getRequest from './getRequest';
35

46
// SEE __mocks__/getRequest for which endpoint paths result in what types of

src/client/getRequest.js

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,9 @@ const getRequest = path =>
88
if (response === null) {
99
return throwError('API Timed out', response);
1010
}
11-
console.log('api response: ', response);
1211
return response;
1312
}),
1413
catchError(error => {
15-
console.log(error);
16-
console.error('api error: ', error.response);
1714
return of(error);
1815
}),
1916
);

0 commit comments

Comments
 (0)