Skip to content

Commit de46ee3

Browse files
authored
Support ESM and CJS output; remove node-fetch dependency (#138)
* remove use of node-fetch * remove not-used tsconfig.test.json * auto-update .vscode/settings.json * export everything from the top level, not submodules * update all packages * make package a module, change to dual esm/cjs output, use rollup * bump minor version number * Don't use the old node module resolution setting, use Bundler * use makerx eslint and prettier config
1 parent cb55dc3 commit de46ee3

23 files changed

+655
-668
lines changed

.eslintrc.cjs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
module.exports = {
2+
root: true,
3+
parser: '@typescript-eslint/parser',
4+
plugins: ['@typescript-eslint', 'node', 'prettier'],
5+
parserOptions: {
6+
tsconfigRootDir: __dirname,
7+
project: ['./tsconfig.json'],
8+
sourceType: 'module',
9+
},
10+
env: {
11+
node: true,
12+
},
13+
extends: ['@makerx/eslint-config'],
14+
rules: {
15+
'@typescript-eslint/no-explicit-any': 'off',
16+
'@typescript-eslint/no-non-null-assertion': 'off',
17+
'@typescript-eslint/consistent-type-imports': 'error',
18+
},
19+
}

.eslintrc.js

Lines changed: 0 additions & 30 deletions
This file was deleted.

.prettierrc

Lines changed: 0 additions & 7 deletions
This file was deleted.

.prettierrc.cjs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module.exports = {
2+
...require('@makerx/prettier-config'),
3+
}

.vscode/settings.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
22
"editor.formatOnSave": true,
33
"editor.defaultFormatter": "esbenp.prettier-vscode",
44
"editor.codeActionsOnSave": {
5-
"source.fixAll.eslint": true
5+
"source.fixAll.eslint": "explicit"
66
}
7-
}
7+
}

README.md

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ const logger: Logger = {
5757

5858
### HttpClient
5959

60-
`HttpClient` is a class wrapping `node-fetch` to make calling Web API endpoints slightly easier:
60+
`HttpClient` is a class wrapping `fetch` to make calling Web API endpoints slightly easier:
6161

6262
- supports setting a base URL so relative paths can be used
6363
- supports providing an function to set an authorization header on every request
@@ -114,36 +114,34 @@ export const createServices = (context: BaseContext): Services => {
114114
If the `HttpClient` class is too opinionated for your use case, or you simply want a stateless wrapper for the fetch api with some sensible defaults; we export the function `makeHttpRequest` which is what `HttpClient` uses internally. This function takes care of request logging but leaves request encoding and response decoding to the consumer which offers a higher degree of flexibility.
115115

116116
```ts
117-
import { makeHttpRequest } from "./http";
117+
import { makeHttpRequest } from './http'
118118

119119
const usersResponse = await makeHttpRequest({
120120
url: 'https://localhost:8080/api/users',
121121
method: 'GET',
122122
headers: {
123-
'Authorization': 'Bearer abc123def'
123+
Authorization: 'Bearer abc123def',
124124
},
125125
ensureSuccessStatusCode: false,
126126
logger: myLogger,
127127
requestLogLovel: 'debug',
128128
logContext: {
129129
service: 'My Service Name',
130-
version: '1.0.0'
130+
version: '1.0.0',
131131
},
132132
accept: 'application/json',
133133
fetchInit: {
134-
redirect: 'manual'
135-
}
134+
redirect: 'manual',
135+
},
136136
})
137137

138-
if(usersResponse.ok) {
138+
if (usersResponse.ok) {
139139
const users = await usersResponse.json()
140-
} else if(usersResponse.status === 302) {
140+
} else if (usersResponse.status === 302) {
141141
const redirectLocation = usersResponse.headers.get('location')
142142
} else {
143143
// Do something with error code???
144144
}
145-
146-
147145
```
148146

149147
### HttpResponseError
@@ -161,6 +159,10 @@ const response = await fetch('https://broken.io/error', {
161159
if (!response.ok) throw await HttpResponseError.create(response, 'POST failed')
162160
```
163161

162+
### Polyfilling fetch for NodeJS v16
163+
164+
When using NodeJS v16 or below, you must polyfill the global `fetch` function. We recommend using [node-fetch](https://github.com/node-fetch/node-fetch?tab=readme-ov-file#providing-global-access) as a drop-in polyfill choice. **Note:** Due to node 16 and earlier versions no longer being maintained, we also strongly recommend the project be updated to the current LTS version.
165+
164166
## Authorisation
165167

166168
A number of authorisation functions and `HttpAuthFactory` wrappers are exported:

jest.config.ts

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,5 @@ const config: Config.InitialOptions = {
44
preset: 'ts-jest',
55
testEnvironment: 'node',
66
testMatch: ['**/**/*.spec.ts'],
7-
transform: {
8-
'<regex_match_files>': [
9-
'ts-jest',
10-
{
11-
tsconfig: 'tsconfig.test.json',
12-
},
13-
],
14-
},
157
}
168
export default config

0 commit comments

Comments
 (0)