Skip to content

Commit f57bf96

Browse files
committed
Add core files.
1 parent 2ff70c6 commit f57bf96

14 files changed

+638
-0
lines changed

.eslintrc.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
module.exports = {
2+
root: true,
3+
extends: ['digitalbazaar'],
4+
env: {
5+
browser: true,
6+
node: true
7+
}
8+
};

.github/workflows/main.yml

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
name: Node.js CI
2+
3+
on: [push]
4+
5+
jobs:
6+
test-node:
7+
runs-on: ubuntu-latest
8+
strategy:
9+
matrix:
10+
node-version: [10.x, 12.x, 14.x]
11+
steps:
12+
- uses: actions/checkout@v2
13+
- name: Use Node.js ${{ matrix.node-version }}
14+
uses: actions/setup-node@v1
15+
with:
16+
node-version: ${{ matrix.node-version }}
17+
- run: npm install
18+
- name: Run test with Node.js ${{ matrix.node-version }}
19+
run: npm run test-node
20+
env:
21+
CI: true
22+
test-karma:
23+
runs-on: ubuntu-latest
24+
strategy:
25+
matrix:
26+
node-version: [14.x]
27+
steps:
28+
- uses: actions/checkout@v2
29+
- name: Use Node.js ${{ matrix.node-version }}
30+
uses: actions/setup-node@v1
31+
with:
32+
node-version: ${{ matrix.node-version }}
33+
- run: npm install
34+
- name: Run karma tests
35+
run: npm run test-karma
36+
env:
37+
CI: true
38+
lint:
39+
runs-on: ubuntu-latest
40+
strategy:
41+
matrix:
42+
node-version: [14.x]
43+
steps:
44+
- uses: actions/checkout@v2
45+
- name: Use Node.js ${{ matrix.node-version }}
46+
uses: actions/setup-node@v1
47+
with:
48+
node-version: ${{ matrix.node-version }}
49+
- run: npm install
50+
- name: Run eslint
51+
run: npm run lint
52+
coverage:
53+
needs: [test-node, test-karma]
54+
runs-on: ubuntu-latest
55+
strategy:
56+
matrix:
57+
node-version: [14.x]
58+
steps:
59+
- uses: actions/checkout@v2
60+
- name: Use Node.js ${{ matrix.node-version }}
61+
uses: actions/setup-node@v1
62+
with:
63+
node-version: ${{ matrix.node-version }}
64+
- run: npm install
65+
- name: Generate coverage report
66+
run: npm run coverage-ci
67+
env:
68+
CI: true
69+
- name: Upload coverage to Codecov
70+
uses: codecov/codecov-action@v1
71+
with:
72+
file: ./coverage/lcov.info
73+
fail_ci_if_error: true

.gitignore

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
*.log
2+
*.sw[nop]
3+
*~
4+
.cache
5+
.nyc_output
6+
.project
7+
.settings
8+
.vscode
9+
TAGS
10+
coverage
11+
dist
12+
node_modules
13+
reports

CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# @digitalbazaar/http-client ChangeLog
2+
3+
## 1.0.0 - TBD
4+
5+
### Added
6+
- Add core files.
7+
8+
- See git history for changes previous to this release.

LICENSE

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
Copyright (c) 2020, Digital Bazaar, Inc.
2+
All rights reserved.
3+
4+
Redistribution and use in source and binary forms, with or without
5+
modification, are permitted provided that the following conditions are met:
6+
7+
* Redistributions of source code must retain the above copyright notice, this
8+
list of conditions and the following disclaimer.
9+
10+
* Redistributions in binary form must reproduce the above copyright notice,
11+
this list of conditions and the following disclaimer in the documentation
12+
and/or other materials provided with the distribution.
13+
14+
* Neither the name of the copyright holder nor the names of its
15+
contributors may be used to endorse or promote products derived from
16+
this software without specific prior written permission.
17+
18+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19+
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20+
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21+
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
22+
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23+
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24+
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
25+
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
26+
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27+
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

README.md

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,68 @@
11
# http-client
22
An opinionated, isomorphic HTTP client.
3+
4+
### Usage
5+
6+
#### Import httpClient
7+
```js
8+
import {httpClient} from '@digitalbazaar/http-client';
9+
```
10+
11+
#### GET a JSON response in the browser
12+
```js
13+
try {
14+
result = await httpClient.get('http://httpbin.org/json');
15+
return result.data;
16+
} catch(e) {
17+
// status is HTTP status code
18+
// data is JSON error from the server
19+
const {data, status} = e;
20+
throw e;
21+
}
22+
```
23+
24+
#### GET a JSON response in Node with a HTTP Agent
25+
```js
26+
import https from 'https';
27+
// use an agent to avoid self-signed certificate errors
28+
const agent = new https.agent({rejectUnauthorized: false});
29+
try {
30+
result = await httpClient.get('http://httpbin.org/json', {agent});
31+
return result.data;
32+
} catch(e) {
33+
// status is HTTP status code
34+
// data is JSON error from the server if available
35+
const {data, status} = e;
36+
throw e;
37+
}
38+
```
39+
40+
#### GET HTML by overriding default headers
41+
```js
42+
const headers = {Accept: 'text/html'};
43+
try {
44+
result = await httpClient.get('http://httpbin.org/json', {headers});
45+
// see: https://developer.mozilla.org/en-US/docs/Web/API/Body#Methods
46+
return result.response.text();
47+
} catch(e) {
48+
// status is HTTP status code
49+
// any message from the server can be parsed from the response if present
50+
const {response, status} = e;
51+
throw e;
52+
}
53+
```
54+
55+
#### POST a JSON payload
56+
```js
57+
try {
58+
result = await httpClient.post('http://httpbin.org/json', {
59+
json: {some: 'data'}
60+
});
61+
return result.data;
62+
} catch(e) {
63+
// status is HTTP status code
64+
// data is JSON error from the server
65+
const {data, status} = e;
66+
throw e;
67+
}
68+
```

index.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
/*!
2+
* Copyright (c) 2020 Digital Bazaar, Inc. All rights reserved.
3+
*/
4+
'use strict';
5+
6+
// translate `main.js` to CommonJS
7+
require = require('esm')(module);
8+
module.exports = require('./main.js');

karma.conf.js

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
/*
2+
* Copyright (c) 2020 Digital Bazaar, Inc. All rights reserved.
3+
*/
4+
module.exports = function(config) {
5+
6+
config.set({
7+
// base path that will be used to resolve all patterns (eg. files, exclude)
8+
basePath: '',
9+
10+
// frameworks to use
11+
// available frameworks: https://npmjs.org/browse/keyword/karma-adapter
12+
frameworks: ['mocha', 'chai'],
13+
14+
// list of files / patterns to load in the browser
15+
files: [
16+
'tests/test-karma.js',
17+
'tests/*.spec.js'
18+
],
19+
20+
// list of files to exclude
21+
exclude: [],
22+
23+
// preprocess matching files before serving them to the browser
24+
// preprocessors: https://npmjs.org/browse/keyword/karma-preprocessor
25+
preprocessors: {
26+
//'tests/*.js': ['webpack', 'babel', 'sourcemap']
27+
'tests/*.js': ['webpack', 'sourcemap']
28+
},
29+
30+
webpack: {
31+
//mode: 'production',
32+
mode: 'development',
33+
devtool: 'inline-source-map',
34+
module: {
35+
rules: [
36+
/*
37+
{
38+
test: /\.js$/,
39+
include: [{
40+
// exclude node_modules by default
41+
exclude: /(node_modules)/
42+
}],
43+
use: {
44+
loader: 'babel-loader',
45+
options: {
46+
presets: ['@babel/preset-env'],
47+
plugins: [
48+
'@babel/plugin-transform-modules-commonjs',
49+
'@babel/plugin-transform-runtime'
50+
]
51+
}
52+
}
53+
}
54+
*/
55+
]
56+
},
57+
node: {
58+
Buffer: false,
59+
process: false,
60+
crypto: false,
61+
setImmediate: false
62+
}
63+
},
64+
65+
// test results reporter to use
66+
// possible values: 'dots', 'progress'
67+
// available reporters: https://npmjs.org/browse/keyword/karma-reporter
68+
//reporters: ['progress'],
69+
reporters: ['mocha'],
70+
71+
// web server port
72+
port: 9876,
73+
74+
// enable / disable colors in the output (reporters and logs)
75+
colors: true,
76+
77+
// level of logging
78+
// possible values: config.LOG_DISABLE || config.LOG_ERROR ||
79+
// config.LOG_WARN || config.LOG_INFO || config.LOG_DEBUG
80+
logLevel: config.LOG_INFO,
81+
82+
// enable / disable watching file and executing tests whenever any
83+
// file changes
84+
autoWatch: false,
85+
86+
// start these browsers
87+
// browser launchers: https://npmjs.org/browse/keyword/karma-launcher
88+
//browsers: ['ChromeHeadless', 'Chrome', 'Firefox', 'Safari'],
89+
browsers: ['ChromeHeadless'],
90+
91+
customLaunchers: {
92+
IE9: {
93+
base: 'IE',
94+
'x-ua-compatible': 'IE=EmulateIE9'
95+
},
96+
IE8: {
97+
base: 'IE',
98+
'x-ua-compatible': 'IE=EmulateIE8'
99+
}
100+
},
101+
102+
// Continuous Integration mode
103+
// if true, Karma captures browsers, runs the tests and exits
104+
singleRun: true,
105+
106+
// Concurrency level
107+
// how many browser should be started simultaneous
108+
concurrency: Infinity,
109+
110+
// Mocha
111+
client: {
112+
mocha: {
113+
// increase from default 2s
114+
timeout: 10000,
115+
reporter: 'html'
116+
//delay: true
117+
}
118+
},
119+
120+
// Proxied paths
121+
proxies: {}
122+
});
123+
};

0 commit comments

Comments
 (0)