Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove HTTP client #138

Merged
merged 1 commit into from
Jun 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 8 additions & 60 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,66 +12,6 @@ See [docs](/docs) for further instructions on how to use.

## Overview

## HTTP Client

The library provides methods to implement the client side of HTTP protocols. Public methods available are:

- `buildClient()`, which returns a [Client](https://undici.nodejs.org/#/docs/api/Client) instance and should be called before any of the following methods with parameters:
- `baseUrl`;
- `clientOptions` – set of [ClientOptions](https://undici.nodejs.org/#/docs/api/Client?id=parameter-clientoptions) (optional). If none are provided, the following default options will be used to instantiate the client:
```ts
keepAliveMaxTimeout: 300_000,
keepAliveTimeout: 4000,
```
- `sendGet()`;
- `sendPost()`;
- `sendPut()`;
- `sendPutBinary()`;
- `sendDelete()`;
- `sendPatch()`.

All _send_ methods accept a type parameter and the following arguments:

- `client`, the return value of `buildClient()`;
- `path`;
- `options` – (optional). Possible values are:

- `headers`;
- `query`, query string params to be embedded in the request URL;
- `timeout`, the timeout after which a request will time out, in milliseconds. Default is 30 seconds. Pass `undefined` if you prefer to have no timeout;
- `throwOnError`;`
- `reqContext`;
- `safeParseJson`, used when the response content-type is `application/json`. If `true`, the response body will be parsed as JSON and a `ResponseError` will be thrown in case of syntax errors. If `false`, errors are not handled;
- `blobResponseBody`, used when the response body should be returned as Blob;
- `requestLabel`, this string will be returned together with any thrown or returned Error to provide additional context about what request was being executed when the error has happened;
- `disableKeepAlive`;`
- `retryConfig`, defined by:
- `maxAttempts`, the maximum number of times a request should be retried;
- `delayBetweenAttemptsInMsecs`;
- `statusCodesToRetry`, the status codes that trigger a retry;
- `retryOnTimeout`;
- `clientOptions`;
- `responseSchema`, used both for inferring the response type of the call, and also (if `validateResponse` is `true`) for validating the response structure;
- `validateResponse`;

The following options are applied by default:

```ts
validateResponse: true,
throwOnError: true,
timeout: 30000,
retryConfig: {
maxAttemps: 1,
delayBetweenAttemptsInMsecs: 0,
statusCodesToRetry: [],
retryOnTimeout: false,
}
```

Additionally, `sendPost()`, `sendPut()`, `sendPutBinary()`, and `sendPatch()` also accept a `body` parameter.

The response of any _send_ method will be resolved to always have `result` set, but only have `error` set in case something went wrong. See [Either](#either) for more information.

## Default Logging Configuration

The library provides methods to resolve the default logging configuration. Public methods available are:
Expand Down Expand Up @@ -239,3 +179,11 @@ expect(someEventEmitter.emittedEvents.length).toBe(1)
## Hashing

- `HashUtils` - utils for hashing using sha256/sha512 algorithms

## Checksum

- `ChecksumUtils` - utils for insecure hashing using the MD5 algorithm

## Streams

- `StreamUtils` - utils for temporary persisting of streams for length calculation and reuse
35 changes: 13 additions & 22 deletions index.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,3 @@
export {
sendPut,
sendPutBinary,
sendDelete,
sendPatch,
sendGet,
sendPost,
sendPostBinary,
httpClient,
buildClient,
type RequestOptions,
type Response,
type HttpRequestContext,
type ResponseSchema,
JSON_HEADERS,
} from './src/http/httpClient'

export {
PublicNonRecoverableError,
type PublicNonRecoverableErrorParams,
Expand All @@ -25,8 +8,7 @@ export {
type ErrorDetails,
type InternalErrorParams,
} from './src/errors/InternalError'
export { ResponseStatusError } from './src/errors/ResponseStatusError'
export { isResponseStatusError, isEntityGoneError } from './src/errors/errorTypeGuards'
export { isEntityGoneError } from './src/errors/errorTypeGuards'

export { ConfigScope } from './src/config/ConfigScope'
export { ensureClosingSlashTransformer } from './src/config/configTransformers'
Expand Down Expand Up @@ -110,7 +92,16 @@ export {

export { waitAndRetry } from './src/utils/waitUtils'

export * from './src/observability/observabilityTypes'
export type { TransactionObservabilityManager } from './src/observability/observabilityTypes'

export * from './src/utils/checksumUtils'
export * from './src/utils/streamUtils'
export {
generateChecksumForReadable,
generateChecksumForObject,
generateChecksumForBufferOrString,
} from './src/utils/checksumUtils'
export { FsReadableProvider } from './src/utils/streamUtils'
export type {
PersistToFsOptions,
ReadableProvider,
FsReadableProviderOptions,
} from './src/utils/streamUtils'
10 changes: 4 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,21 +37,19 @@
"dot-prop": "6.0.1",
"pino": "^9.1.0",
"tslib": "^2.6.2",
"undici": "^6.18.2",
"undici-retry": "^5.0.3",
"zod": "^3.23.8"
},
"devDependencies": {
"@types/node": "^20.12.8",
"@types/node": "^20.14.2",
"@types/tmp": "^0.2.6",
"@typescript-eslint/eslint-plugin": "^7.8.0",
"@typescript-eslint/parser": "^7.8.0",
"@typescript-eslint/eslint-plugin": "^7.12.0",
"@typescript-eslint/parser": "^7.12.0",
"@vitest/coverage-v8": "1.6.0",
"auto-changelog": "^2.4.0",
"eslint": "^8.57.0",
"eslint-plugin-import": "^2.29.1",
"eslint-plugin-vitest": "0.4.1",
"prettier": "^3.2.5",
"prettier": "^3.3.1",
"tmp": "^0.2.3",
"typescript": "^5.4.5",
"vitest": "1.6.0"
Expand Down
26 changes: 0 additions & 26 deletions src/errors/ResponseStatusError.ts

This file was deleted.

23 changes: 2 additions & 21 deletions src/errors/errorTypeGuards.spec.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,10 @@
import { describe } from 'vitest'
import { describe, expect, it } from 'vitest'

import { InternalError } from './InternalError'
import { ResponseStatusError } from './ResponseStatusError'
import { isEntityGoneError, isResponseStatusError } from './errorTypeGuards'
import { isEntityGoneError } from './errorTypeGuards'
import { EntityGoneError, EntityNotFoundError } from './publicErrors'

describe('errorTypeGuards', () => {
describe('isResponseStatusError', () => {
it('Returns true for ResponseStatusError', () => {
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument,@typescript-eslint/no-explicit-any
const error = new ResponseStatusError({} as any, 'label')

expect(isResponseStatusError(error)).toBe(true)
})

it('Returns false for not a ResponseStatusError', () => {
const error = new InternalError({
message: 'message',
errorCode: 'CODE',
})

expect(isResponseStatusError(error)).toBe(false)
})
})

describe('isEntityGoneError', () => {
it('Returns true for EntityGoneError', () => {
const error = new EntityGoneError({
Expand Down
5 changes: 0 additions & 5 deletions src/errors/errorTypeGuards.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
import { isPublicNonRecoverableError } from '../utils/typeUtils'

import type { ResponseStatusError } from './ResponseStatusError'
import type { EntityGoneError } from './publicErrors'

export function isResponseStatusError(entity: unknown): entity is ResponseStatusError {
return 'isResponseStatusError' in (entity as ResponseStatusError)
}

export function isEntityGoneError(entity: unknown): entity is EntityGoneError {
return isPublicNonRecoverableError(entity) && entity.httpStatusCode === 410
}
Loading
Loading