Skip to content

Commit 2ad99a4

Browse files
author
Mauro Gabriel Titimoli
committed
chore: Initialize repository
0 parents  commit 2ad99a4

19 files changed

+10375
-0
lines changed

.envrc

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
layout node

.eslintrc.js

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
module.exports = {
2+
root: true,
3+
parser: "@typescript-eslint/parser",
4+
parserOptions: {
5+
ecmaVersion: 2020,
6+
sourceType: "module"
7+
},
8+
env: {
9+
node: true
10+
},
11+
ignorePatterns: ["dist"],
12+
extends: [
13+
"eslint:recommended",
14+
"plugin:@typescript-eslint/eslint-recommended",
15+
"plugin:@typescript-eslint/recommended",
16+
"plugin:import/errors",
17+
"plugin:import/warnings",
18+
"plugin:import/typescript",
19+
"prettier",
20+
"prettier/@typescript-eslint",
21+
"plugin:prettier/recommended"
22+
],
23+
settings: {
24+
"import/resolver": {
25+
typescript: {}
26+
}
27+
},
28+
rules: {
29+
"@typescript-eslint/ban-ts-comment": ["error", {"ts-expect-error": false}],
30+
"@typescript-eslint/explicit-module-boundary-types": "off"
31+
}
32+
};

.gitignore

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.
2+
3+
# dependencies
4+
/node_modules
5+
6+
# production
7+
/dist
8+
9+
yarn-debug.log*
10+
yarn-error.log*

.npmignore

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
.*
2+
*.config.js
3+
*.json
4+
jest*
5+
readmeTemplate.ejs
6+
src

README.md

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# @mgtitimoli/utils-http
2+
3+
> HTTP Utilities
4+
5+
<!-- START doctoc generated TOC please keep comment here to allow auto update -->
6+
<!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE -->
7+
8+
9+
- [Installation](#installation)
10+
- [Using npm](#using-npm)
11+
- [Using yarn](#using-yarn)
12+
- [API](#api)
13+
- [License](#license)
14+
15+
<!-- END doctoc generated TOC please keep comment here to allow auto update -->
16+
17+
## Installation
18+
19+
### Using [npm](https://docs.npmjs.com/cli/npm)
20+
21+
```
22+
$ npm install --save @mgtitimoli/utils-http
23+
```
24+
25+
### Using [yarn](https://yarnpkg.com)
26+
27+
```
28+
$ yarn add @mgtitimoli/utils-http
29+
```
30+
31+
## API
32+
33+
34+
35+
## License
36+
37+
[MIT](LICENSE.txt) :copyright: Mauro Gabriel Titimoli ([email protected])

commitlint.config.js

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
module.exports = {
2+
extends: ["@commitlint/config-conventional"],
3+
rules: {
4+
"subject-case": [2, "always", "sentence-case"]
5+
}
6+
};

husky.config.js

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
module.exports = {
2+
hooks: {
3+
"pre-commit": "lint-staged",
4+
"commit-msg": "commitlint -E HUSKY_GIT_PARAMS"
5+
}
6+
};

jest.config.js

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// eslint-disable-next-line no-undef
2+
module.exports = {
3+
clearMocks: true,
4+
moduleNameMapper: {
5+
"^@/(.*)$": "<rootDir>/src/$1"
6+
},
7+
roots: ["<rootDir>"],
8+
snapshotResolver: "./jestSnapshotResolver",
9+
testMatch: ["**/__tests__/*.test.ts?(x)"],
10+
transform: {
11+
// to be able to use jest setup files with typescript
12+
"^.+\\.tsx?$": "ts-jest"
13+
}
14+
};

jestSnapshotResolver.js

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// eslint-disable-next-line no-undef
2+
module.exports = {
3+
resolveSnapshotPath: (testPath, snapshotExtension) =>
4+
testPath + snapshotExtension,
5+
6+
resolveTestPath: (snapshotFilePath, snapshotExtension) =>
7+
snapshotFilePath.slice(0, -snapshotExtension.length),
8+
9+
// Example test path, used for preflight consistency check of the implementation above
10+
testPathForConsistencyCheck: "some/__tests__/example.test.js"
11+
};

lint-staged.config.js

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module.exports = {
2+
"*.ts": [() => "ttsc --noEmit", "eslint --fix"]
3+
};

package.json

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
{
2+
"name": "@mgtitimoli/utils-http",
3+
"description": "HTTP Utilities",
4+
"version": "0.0.0",
5+
"author": "Mauro Gabriel Titimoli <[email protected]> (https://github.com/mgtitimoli)",
6+
"license": "MIT",
7+
"publishConfig": {
8+
"access": "public"
9+
},
10+
"scripts": {
11+
"build": "run-s build:clean build:code build:readme",
12+
"build:clean": "rm -rf dist",
13+
"build:code": "ttsc --inlineSources --sourceMap --sourceRoot '.'",
14+
"build:readme": "run-s build:readme:from-package build:readme:doctoc",
15+
"build:readme:doctoc": "doctoc README.md",
16+
"build:readme:from-package": "pkg-to-readme --force --template readmeTemplate.ejs",
17+
"debug": "node --require ts-node/register --require tsconfig-paths/register --inspect src/index.ts",
18+
"test": "jest"
19+
},
20+
"devDependencies": {
21+
"@commitlint/cli": "^9.1.1",
22+
"@commitlint/config-conventional": "^9.1.1",
23+
"@commitlint/prompt-cli": "^9.1.1",
24+
"@types/isomorphic-fetch": "^0.0.35",
25+
"@types/jest": "^26.0.9",
26+
"@types/node": "^14.0.27",
27+
"@types/node-fetch": "^2.5.7",
28+
"@typescript-eslint/eslint-plugin": "^3.9.0",
29+
"@typescript-eslint/parser": "^3.9.0",
30+
"@zerollup/ts-transform-paths": "^1.7.18",
31+
"concat-md": "^0.3.5",
32+
"doctoc": "^1.4.0",
33+
"eslint": "^7.6.0",
34+
"eslint-config-prettier": "^6.11.0",
35+
"eslint-import-resolver-typescript": "^2.2.0",
36+
"eslint-plugin-import": "^2.22.0",
37+
"eslint-plugin-prettier": "^3.1.4",
38+
"husky": "^4.2.5",
39+
"jest": "^26.4.0",
40+
"lint-staged": "^10.2.11",
41+
"np": "^6.4.0",
42+
"npm-run-all": "^4.1.5",
43+
"pkg-to-readme": "^1.1.0",
44+
"prettier": "^2.0.5",
45+
"ts-jest": "^26.2.0",
46+
"ts-node": "^8.10.2",
47+
"tsconfig-paths": "^3.9.0",
48+
"ttypescript": "^1.5.10",
49+
"typedoc": "^0.18.0",
50+
"typedoc-plugin-example-tag": "^1.0.2",
51+
"typedoc-plugin-external-module-map": "^1.2.1",
52+
"typedoc-plugin-internal-external": "^2.2.0",
53+
"typedoc-plugin-markdown": "^2.4.0",
54+
"typescript": "^3.9.7"
55+
},
56+
"dependencies": {
57+
"@mgtitimoli/utils-error": "^1.0.2",
58+
"isomorphic-fetch": "^2.2.1",
59+
"tslib": "^2.0.1"
60+
}
61+
}

prettier.config.js

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module.exports = {
2+
arrowParens: "avoid",
3+
bracketSpacing: false,
4+
trailingComma: "none"
5+
};

readmeTemplate.ejs

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# <%= name %>
2+
3+
> <%= description %>
4+
5+
<!-- START doctoc -->
6+
<!-- END doctoc -->
7+
8+
## Installation
9+
10+
### Using [npm](https://docs.npmjs.com/cli/npm)
11+
12+
```
13+
$ npm install --save <%= name %>
14+
```
15+
16+
### Using [yarn](https://yarnpkg.com)
17+
18+
```
19+
$ yarn add <%= name %>
20+
```
21+
22+
## API
23+
24+
<%# include API.md %>
25+
26+
## License
27+
28+
[<%= license %>](LICENSE.txt) :copyright: <%= author.name %> (<%= author.email %>)

src/HttpResponseError.ts

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import type {Response} from "node-fetch";
2+
3+
import {CustomError} from "@mgtitimoli/utils-error";
4+
5+
import * as withHttpStatusCode from "./httpStatusCode";
6+
7+
import type {HttpStatusCode} from "./httpStatusCode";
8+
9+
const getResponseStatusCode = (response: Response): HttpStatusCode =>
10+
response.status as any;
11+
12+
const getMessage = (response: Response) =>
13+
response.statusText ||
14+
withHttpStatusCode.getText(getResponseStatusCode(response));
15+
16+
class HttpResponseError extends CustomError {
17+
response: Response;
18+
19+
constructor(response: Response) {
20+
super(getMessage(response));
21+
22+
this.response = response;
23+
}
24+
}
25+
26+
export default HttpResponseError;

src/httpRequest.ts

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import fetch from "isomorphic-fetch";
2+
3+
import type {RequestInfo, RequestInit, Response} from "node-fetch";
4+
5+
import HttpResponseError from "./HttpResponseError";
6+
7+
type FetchOptionsMode = "cors" | "no-cors" | "same-origin" | "navigate";
8+
9+
type FetchOptions = RequestInit & {mode?: FetchOptionsMode};
10+
11+
type PostJsonOptions = Omit<FetchOptions, "body" | "method"> & {data?: unknown};
12+
13+
type GetJsonOptions = Omit<FetchOptions, "body" | "method">;
14+
15+
const responseToJson = (response: Response) => response.json();
16+
17+
const ensureResponseIsSucceeded = (response: Response) =>
18+
response.ok ? response : Promise.reject(new HttpResponseError(response));
19+
20+
const safeFetch = (url: RequestInfo, options?: FetchOptions) =>
21+
fetch(url, options).then(ensureResponseIsSucceeded);
22+
23+
const postJsonHeaders = {"Content-Type": "application/json"};
24+
25+
const postJsonOptionsToFetchOptionsCommon = ({
26+
data,
27+
...rest
28+
}: PostJsonOptions) => ({
29+
...rest,
30+
method: "POST"
31+
});
32+
33+
const postJsonOptionsToFetchOptionsWithData = (options: PostJsonOptions) => ({
34+
...postJsonOptionsToFetchOptionsCommon(options),
35+
body: JSON.stringify(options.data),
36+
headers: {...options.headers, ...postJsonHeaders}
37+
});
38+
39+
const postJsonOptionsToFetchOptions = (options: PostJsonOptions) =>
40+
options.data !== undefined
41+
? postJsonOptionsToFetchOptionsWithData(options)
42+
: postJsonOptionsToFetchOptionsCommon(options);
43+
44+
const postJson = (url: RequestInfo, options: PostJsonOptions = {}) =>
45+
safeFetch(url, postJsonOptionsToFetchOptions(options)).then(responseToJson);
46+
47+
const getJson = (url: RequestInfo, options?: GetJsonOptions) =>
48+
safeFetch(url, options).then(responseToJson);
49+
50+
export {getJson, postJson, safeFetch};
51+
52+
export type {GetJsonOptions as HttpRequestGetJsonOptions};
53+
export type {PostJsonOptions as HttpRequestPostJsonOptions};

0 commit comments

Comments
 (0)