Skip to content

Commit 21940f2

Browse files
Rework packaging and use native fetch
1 parent 8f531e3 commit 21940f2

10 files changed

Lines changed: 162 additions & 130 deletions

File tree

CHANGELOG.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,13 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## [Unreleased]
99

10+
## [6.0.0]
11+
### Changed
12+
- Modify the library packaging to make ECMAScript modules (ESM) optional.
13+
- Substitute the 'ky' dependency with native fetch API.
14+
- Reduce the default timeout setting to 5 seconds.
15+
- Implement retries, up to two times, with exponential backoff in the event of a timeout.
16+
1017
## [5.0.2] - 2024-03-27
1118
### Added
1219
- Add missing fields `size` and `status` to type _AutonomousSystemPrefix_.
@@ -165,7 +172,8 @@ https://github.com/ipregistry/ipregistry-javascript#configuring-cache-max-age
165172
## [0.9.1] - 2019-07-23
166173
- First public release.
167174

168-
[Unreleased]: https://github.com/ipregistry/ipregistry-javascript/compare/5.0.2...HEAD
175+
[Unreleased]: https://github.com/ipregistry/ipregistry-javascript/compare/6.0.0...HEAD
176+
[6.0.0]: https://github.com/ipregistry/ipregistry-javascript/compare/6.0.0...5.0.2
169177
[5.0.2]: https://github.com/ipregistry/ipregistry-javascript/compare/5.0.1...5.0.2
170178
[5.0.1]: https://github.com/ipregistry/ipregistry-javascript/compare/5.0.0...5.0.1
171179
[5.0.0]: https://github.com/ipregistry/ipregistry-javascript/compare/4.5.0...5.0.0

integration_test/ipregistry.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import {
2525
IpregistryOptions,
2626
LookupError,
2727
NoCache,
28-
} from '../src/index.js'
28+
} from '../dist/index.mjs'
2929

3030
import { describe, it } from 'node:test'
3131
import { expect } from 'chai'

integration_test/package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"type": "module"
3+
}

package.json

Lines changed: 18 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
11
{
22
"name": "@ipregistry/client",
33
"description": "Official Ipregistry Javascript Library.",
4-
"version": "5.0.2",
5-
"type": "module",
4+
"version": "6.0.0",
5+
"browser": "./dist/index.global.js",
66
"main": "./dist/index.js",
7-
"module": "./dist/esm/index.js",
7+
"module": "./dist/index.mjs",
88
"types": "./dist/index.d.ts",
99
"exports": {
10-
"import": "./dist/esm/index.js",
11-
"require": "./dist/index.js"
10+
".": {
11+
"import": "./dist/index.mjs",
12+
"require": "./dist/index.js",
13+
"types": "./dist/index.d.ts"
14+
}
1215
},
1316
"homepage": "https://ipregistry.co",
1417
"license": "Apache-2.0",
@@ -43,10 +46,7 @@
4346
"url": "https://github.com/ipregistry/ipregistry-javascript/issues"
4447
},
4548
"scripts": {
46-
"build:browser": "rollup -c",
47-
"build:cjs": "./node_modules/.bin/tsc -p tsconfig.cjs.json",
48-
"build:mjs": "./node_modules/.bin/tsc -d --outDir dist/esm",
49-
"build": "npm run lint && npm run format && npm run build:mjs && npm run build:cjs && npm run build:browser",
49+
"build": "tsup",
5050
"clean": "rm -rf dist/",
5151
"format": "prettier --ignore-path .gitignore --write \"**/*.+(js|ts|json)\"",
5252
"lint": "./node_modules/.bin/eslint src/*",
@@ -57,26 +57,20 @@
5757
},
5858
"author": "Ipregistry <support@ipregistry.co>",
5959
"devDependencies": {
60-
"@rollup/plugin-commonjs": "^25.0.7",
61-
"@rollup/plugin-node-resolve": "^15.2.3",
62-
"@rollup/plugin-terser": "^0.4.4",
63-
"@rollup/plugin-typescript": "^11.1.6",
64-
"@typescript-eslint/eslint-plugin": "^7.4.0",
65-
"@typescript-eslint/parser": "^7.4.0",
66-
"chai": "^5.1.0",
67-
"eslint": "^8.57.0",
68-
"eslint-config-prettier": "^9.1.0",
69-
"prettier": "^3.2.5",
70-
"rollup": "^4.13.0",
60+
"@typescript-eslint/eslint-plugin": "^7.12.0",
61+
"@typescript-eslint/parser": "^7.12.0",
62+
"chai": "^5.1.1",
63+
"prettier": "^3.3.0",
64+
"terser": "^5.31.0",
7165
"ts-node": "^10.9.2",
7266
"tsdoc": "^0.0.4",
7367
"tslib": "^2.6.2",
74-
"typedoc": "^0.25.12",
75-
"typescript": "^5.4.3"
68+
"tsup": "^8.1.0",
69+
"typedoc": "^0.25.13",
70+
"typescript": "^5.4.5"
7671
},
7772
"dependencies": {
78-
"ky": "^1.2.3",
79-
"lru-cache": "^10.2.0"
73+
"lru-cache": "^10.2.2"
8074
},
8175
"files": [
8276
"dist/**"

rollup.config.js

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

src/fetch.ts

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import { ApiError, ClientError } from './errors.js'
2+
3+
interface Options extends RequestInit {
4+
retry?: {
5+
backoffLimit: number,
6+
delay: (attemptCount: number) => number,
7+
limit: number
8+
}
9+
timeout?: number
10+
}
11+
12+
const DEFAULT_OPTIONS = {
13+
retry: {
14+
backoffLimit: 5000,
15+
delay: (attemptCount: number) => 0.3 * (2 ** (attemptCount - 1)) * 500,
16+
limit: 2
17+
},
18+
timeout: 5000
19+
}
20+
21+
export async function customFetch(url: string, providedOptions: Options): Promise<Response> {
22+
let options ={
23+
...DEFAULT_OPTIONS,
24+
...providedOptions
25+
}
26+
27+
let retryCount = 0;
28+
29+
while (retryCount <= options.retry.limit) {
30+
const controller = new AbortController();
31+
const timeoutId = setTimeout(() => controller.abort(), options.timeout);
32+
33+
try {
34+
const response = await fetch(url, {
35+
...options,
36+
signal: controller.signal
37+
});
38+
39+
if (!response.ok) {
40+
const data = await response.json();
41+
throw new ApiError(data.code, data.message, data.resolution);
42+
}
43+
44+
return response;
45+
46+
} catch (error: any) {
47+
if (error?.name === 'AbortError') {
48+
retryCount++;
49+
const retryDelay = Math.min(options.retry.delay(retryCount), options.retry.backoffLimit);
50+
await new Promise(resolve => setTimeout(resolve, retryDelay));
51+
} else {
52+
throw error;
53+
}
54+
} finally {
55+
clearTimeout(timeoutId);
56+
}
57+
}
58+
59+
throw new ClientError(`Request failed after ${options.retry.limit} retries`);
60+
}

src/index.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,9 @@ export class IpregistryConfig {
4848
public readonly baseUrl: string = 'https://api.ipregistry.co'
4949

5050
/**
51-
* The timeout (in milliseconds) for API requests. Defaults to 15000.
51+
* The timeout (in milliseconds) for API requests. Defaults to 5000.
5252
*/
53-
public readonly timeout: number = 15000
53+
public readonly timeout: number = 5000
5454

5555
/**
5656
* Constructs a new `IpregistryConfig` instance.
@@ -81,7 +81,7 @@ export class IpregistryConfigBuilder {
8181

8282
private baseUrl: string = 'https://api.ipregistry.co'
8383

84-
private timeout: number = 15000
84+
private timeout: number = 5000
8585

8686
constructor(apiKey: string) {
8787
this.apiKey = apiKey

0 commit comments

Comments
 (0)