@fastrack/starter-http 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +14 -0
- package/dist/client.d.ts +20 -0
- package/dist/client.d.ts.map +1 -0
- package/dist/client.js +41 -0
- package/dist/client.js.map +1 -0
- package/dist/index.d.ts +8 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +6 -0
- package/dist/index.js.map +1 -0
- package/dist/plugin.d.ts +14 -0
- package/dist/plugin.d.ts.map +1 -0
- package/dist/plugin.js +16 -0
- package/dist/plugin.js.map +1 -0
- package/package.json +35 -0
- package/src/client.ts +62 -0
- package/src/index.ts +7 -0
- package/src/plugin.ts +29 -0
- package/tsconfig.json +6 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Fastrack
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
# @fastrack/starter-http
|
|
2
|
+
|
|
3
|
+
HTTP client wrapper (fetch/undici, Node 18+): timeouts, retries for idempotent methods only, correlation ID propagation. No ad-hoc HTTP clients (design rule).
|
|
4
|
+
|
|
5
|
+
Use a **singleton** client; pass the current request's correlation ID at **request time** so it is propagated on outbound calls:
|
|
6
|
+
|
|
7
|
+
```ts
|
|
8
|
+
// In a route handler (starter-web sets request.correlationId)
|
|
9
|
+
const res = await app.httpClient.fetch("https://api.example.com/data", undefined, {
|
|
10
|
+
correlationId: request.correlationId,
|
|
11
|
+
});
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
See [docs/starters.md](../../docs/starters.md).
|
package/dist/client.d.ts
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
export interface CreateHttpClientOptions {
|
|
2
|
+
timeoutMs?: number;
|
|
3
|
+
retries?: number;
|
|
4
|
+
}
|
|
5
|
+
/**
|
|
6
|
+
* Per-call options: pass the current request's correlation ID so it is propagated
|
|
7
|
+
* on the outbound request. Use singleton client and pass at request time.
|
|
8
|
+
*/
|
|
9
|
+
export interface HttpRequestOptions {
|
|
10
|
+
correlationId?: string;
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* Create an HTTP client with timeout and optional retries (idempotent only).
|
|
14
|
+
* Uses native fetch (Node 18+). Keep a singleton instance; pass correlationId
|
|
15
|
+
* at request time so it is propagated on each outbound call (e.g. from request.correlationId).
|
|
16
|
+
*/
|
|
17
|
+
export declare function createHttpClient(options?: CreateHttpClientOptions): {
|
|
18
|
+
fetch(url: string | URL, init?: RequestInit, requestOptions?: HttpRequestOptions): Promise<Response>;
|
|
19
|
+
};
|
|
20
|
+
//# sourceMappingURL=client.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"client.d.ts","sourceRoot":"","sources":["../src/client.ts"],"names":[],"mappings":"AAAA,MAAM,WAAW,uBAAuB;IACtC,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,OAAO,CAAC,EAAE,MAAM,CAAC;CAClB;AAED;;;GAGG;AACH,MAAM,WAAW,kBAAkB;IACjC,aAAa,CAAC,EAAE,MAAM,CAAC;CACxB;AAED;;;;GAIG;AACH,wBAAgB,gBAAgB,CAAC,OAAO,GAAE,uBAA4B,GAAG;IACvE,KAAK,CAAC,GAAG,EAAE,MAAM,GAAG,GAAG,EAAE,IAAI,CAAC,EAAE,WAAW,EAAE,cAAc,CAAC,EAAE,kBAAkB,GAAG,OAAO,CAAC,QAAQ,CAAC,CAAC;CACtG,CAyCA"}
|
package/dist/client.js
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Create an HTTP client with timeout and optional retries (idempotent only).
|
|
3
|
+
* Uses native fetch (Node 18+). Keep a singleton instance; pass correlationId
|
|
4
|
+
* at request time so it is propagated on each outbound call (e.g. from request.correlationId).
|
|
5
|
+
*/
|
|
6
|
+
export function createHttpClient(options = {}) {
|
|
7
|
+
const { timeoutMs = 30_000, retries = 0 } = options;
|
|
8
|
+
async function doFetch(url, init, requestOptions, attempt = 0) {
|
|
9
|
+
const controller = new AbortController();
|
|
10
|
+
const timeout = setTimeout(() => controller.abort(), timeoutMs);
|
|
11
|
+
const headers = new Headers(init?.headers);
|
|
12
|
+
if (requestOptions?.correlationId) {
|
|
13
|
+
headers.set("x-correlation-id", requestOptions.correlationId);
|
|
14
|
+
}
|
|
15
|
+
try {
|
|
16
|
+
const res = await fetch(url, {
|
|
17
|
+
...init,
|
|
18
|
+
signal: controller.signal,
|
|
19
|
+
headers,
|
|
20
|
+
});
|
|
21
|
+
clearTimeout(timeout);
|
|
22
|
+
const method = (init?.method ?? "GET").toUpperCase();
|
|
23
|
+
const isIdempotent = ["GET", "HEAD", "PUT", "DELETE", "OPTIONS", "TRACE"].includes(method);
|
|
24
|
+
if (!res.ok && isIdempotent && attempt < retries) {
|
|
25
|
+
return doFetch(url, init, requestOptions, attempt + 1);
|
|
26
|
+
}
|
|
27
|
+
return res;
|
|
28
|
+
}
|
|
29
|
+
catch (e) {
|
|
30
|
+
clearTimeout(timeout);
|
|
31
|
+
if (attempt < retries) {
|
|
32
|
+
return doFetch(url, init, requestOptions, attempt + 1);
|
|
33
|
+
}
|
|
34
|
+
throw e;
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
return {
|
|
38
|
+
fetch: (url, init, requestOptions) => doFetch(url, init, requestOptions),
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
//# sourceMappingURL=client.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"client.js","sourceRoot":"","sources":["../src/client.ts"],"names":[],"mappings":"AAaA;;;;GAIG;AACH,MAAM,UAAU,gBAAgB,CAAC,UAAmC,EAAE;IAGpE,MAAM,EAAE,SAAS,GAAG,MAAM,EAAE,OAAO,GAAG,CAAC,EAAE,GAAG,OAAO,CAAC;IAEpD,KAAK,UAAU,OAAO,CACpB,GAAiB,EACjB,IAAkB,EAClB,cAAmC,EACnC,OAAO,GAAG,CAAC;QAEX,MAAM,UAAU,GAAG,IAAI,eAAe,EAAE,CAAC;QACzC,MAAM,OAAO,GAAG,UAAU,CAAC,GAAG,EAAE,CAAC,UAAU,CAAC,KAAK,EAAE,EAAE,SAAS,CAAC,CAAC;QAChE,MAAM,OAAO,GAAG,IAAI,OAAO,CAAC,IAAI,EAAE,OAAO,CAAC,CAAC;QAC3C,IAAI,cAAc,EAAE,aAAa,EAAE,CAAC;YAClC,OAAO,CAAC,GAAG,CAAC,kBAAkB,EAAE,cAAc,CAAC,aAAa,CAAC,CAAC;QAChE,CAAC;QACD,IAAI,CAAC;YACH,MAAM,GAAG,GAAG,MAAM,KAAK,CAAC,GAAG,EAAE;gBAC3B,GAAG,IAAI;gBACP,MAAM,EAAE,UAAU,CAAC,MAAM;gBACzB,OAAO;aACR,CAAC,CAAC;YACH,YAAY,CAAC,OAAO,CAAC,CAAC;YACtB,MAAM,MAAM,GAAG,CAAC,IAAI,EAAE,MAAM,IAAI,KAAK,CAAC,CAAC,WAAW,EAAE,CAAC;YACrD,MAAM,YAAY,GAAG,CAAC,KAAK,EAAE,MAAM,EAAE,KAAK,EAAE,QAAQ,EAAE,SAAS,EAAE,OAAO,CAAC,CAAC,QAAQ,CAAC,MAAM,CAAC,CAAC;YAC3F,IAAI,CAAC,GAAG,CAAC,EAAE,IAAI,YAAY,IAAI,OAAO,GAAG,OAAO,EAAE,CAAC;gBACjD,OAAO,OAAO,CAAC,GAAG,EAAE,IAAI,EAAE,cAAc,EAAE,OAAO,GAAG,CAAC,CAAC,CAAC;YACzD,CAAC;YACD,OAAO,GAAG,CAAC;QACb,CAAC;QAAC,OAAO,CAAC,EAAE,CAAC;YACX,YAAY,CAAC,OAAO,CAAC,CAAC;YACtB,IAAI,OAAO,GAAG,OAAO,EAAE,CAAC;gBACtB,OAAO,OAAO,CAAC,GAAG,EAAE,IAAI,EAAE,cAAc,EAAE,OAAO,GAAG,CAAC,CAAC,CAAC;YACzD,CAAC;YACD,MAAM,CAAC,CAAC;QACV,CAAC;IACH,CAAC;IAED,OAAO;QACL,KAAK,EAAE,CAAC,GAAiB,EAAE,IAAkB,EAAE,cAAmC,EAAE,EAAE,CACpF,OAAO,CAAC,GAAG,EAAE,IAAI,EAAE,cAAc,CAAC;KACrC,CAAC;AACJ,CAAC"}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fastrack/starter-http — fetch/undici wrapper: timeouts, retries, correlation propagation.
|
|
3
|
+
*/
|
|
4
|
+
export { httpStarter } from "./plugin.js";
|
|
5
|
+
export { createHttpClient } from "./client.js";
|
|
6
|
+
export type { HttpStarterConfig, HttpClient } from "./plugin.js";
|
|
7
|
+
export type { CreateHttpClientOptions, HttpRequestOptions } from "./client.js";
|
|
8
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,OAAO,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC;AAC1C,OAAO,EAAE,gBAAgB,EAAE,MAAM,aAAa,CAAC;AAC/C,YAAY,EAAE,iBAAiB,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AACjE,YAAY,EAAE,uBAAuB,EAAE,kBAAkB,EAAE,MAAM,aAAa,CAAC"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,OAAO,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC;AAC1C,OAAO,EAAE,gBAAgB,EAAE,MAAM,aAAa,CAAC"}
|
package/dist/plugin.d.ts
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { type Static } from "@sinclair/typebox";
|
|
2
|
+
import type { FastrackStarter } from "@fastrack/core";
|
|
3
|
+
export declare const HttpStarterConfigSchema: import("@sinclair/typebox").TObject<{
|
|
4
|
+
timeoutMs: import("@sinclair/typebox").TOptional<import("@sinclair/typebox").TNumber>;
|
|
5
|
+
retries: import("@sinclair/typebox").TOptional<import("@sinclair/typebox").TNumber>;
|
|
6
|
+
}>;
|
|
7
|
+
export type HttpStarterConfig = Static<typeof HttpStarterConfigSchema>;
|
|
8
|
+
export interface HttpClient {
|
|
9
|
+
fetch(url: string | URL, init?: RequestInit, requestOptions?: {
|
|
10
|
+
correlationId?: string;
|
|
11
|
+
}): Promise<Response>;
|
|
12
|
+
}
|
|
13
|
+
export declare const httpStarter: FastrackStarter<HttpStarterConfig>;
|
|
14
|
+
//# sourceMappingURL=plugin.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"plugin.d.ts","sourceRoot":"","sources":["../src/plugin.ts"],"names":[],"mappings":"AAAA,OAAO,EAAQ,KAAK,MAAM,EAAE,MAAM,mBAAmB,CAAC;AAEtD,OAAO,KAAK,EAAE,eAAe,EAAmB,MAAM,gBAAgB,CAAC;AAGvE,eAAO,MAAM,uBAAuB;;;EAGlC,CAAC;AAEH,MAAM,MAAM,iBAAiB,GAAG,MAAM,CAAC,OAAO,uBAAuB,CAAC,CAAC;AAEvE,MAAM,WAAW,UAAU;IACzB,KAAK,CACH,GAAG,EAAE,MAAM,GAAG,GAAG,EACjB,IAAI,CAAC,EAAE,WAAW,EAClB,cAAc,CAAC,EAAE;QAAE,aAAa,CAAC,EAAE,MAAM,CAAA;KAAE,GAC1C,OAAO,CAAC,QAAQ,CAAC,CAAC;CACtB;AAED,eAAO,MAAM,WAAW,EAAE,eAAe,CAAC,iBAAiB,CAQ1D,CAAC"}
|
package/dist/plugin.js
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { Type } from "@sinclair/typebox";
|
|
2
|
+
import { createHttpClient } from "./client.js";
|
|
3
|
+
export const HttpStarterConfigSchema = Type.Object({
|
|
4
|
+
timeoutMs: Type.Optional(Type.Number({ default: 30_000 })),
|
|
5
|
+
retries: Type.Optional(Type.Number({ default: 0 })),
|
|
6
|
+
});
|
|
7
|
+
export const httpStarter = {
|
|
8
|
+
name: "http",
|
|
9
|
+
configSchema: HttpStarterConfigSchema,
|
|
10
|
+
defaults: { timeoutMs: 30_000, retries: 0 },
|
|
11
|
+
register(app, _ctx, config) {
|
|
12
|
+
const client = createHttpClient({ timeoutMs: config.timeoutMs ?? 30_000, retries: config.retries ?? 0 });
|
|
13
|
+
app.decorate("httpClient", client);
|
|
14
|
+
},
|
|
15
|
+
};
|
|
16
|
+
//# sourceMappingURL=plugin.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"plugin.js","sourceRoot":"","sources":["../src/plugin.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,EAAe,MAAM,mBAAmB,CAAC;AAGtD,OAAO,EAAE,gBAAgB,EAAE,MAAM,aAAa,CAAC;AAE/C,MAAM,CAAC,MAAM,uBAAuB,GAAG,IAAI,CAAC,MAAM,CAAC;IACjD,SAAS,EAAE,IAAI,CAAC,QAAQ,CAAC,IAAI,CAAC,MAAM,CAAC,EAAE,OAAO,EAAE,MAAM,EAAE,CAAC,CAAC;IAC1D,OAAO,EAAE,IAAI,CAAC,QAAQ,CAAC,IAAI,CAAC,MAAM,CAAC,EAAE,OAAO,EAAE,CAAC,EAAE,CAAC,CAAC;CACpD,CAAC,CAAC;AAYH,MAAM,CAAC,MAAM,WAAW,GAAuC;IAC7D,IAAI,EAAE,MAAM;IACZ,YAAY,EAAE,uBAAuB;IACrC,QAAQ,EAAE,EAAE,SAAS,EAAE,MAAM,EAAE,OAAO,EAAE,CAAC,EAAE;IAC3C,QAAQ,CAAC,GAAoB,EAAE,IAAqB,EAAE,MAAyB;QAC7E,MAAM,MAAM,GAAG,gBAAgB,CAAC,EAAE,SAAS,EAAE,MAAM,CAAC,SAAS,IAAI,MAAM,EAAE,OAAO,EAAE,MAAM,CAAC,OAAO,IAAI,CAAC,EAAE,CAAC,CAAC;QACzG,GAAG,CAAC,QAAQ,CAAC,YAAY,EAAE,MAAM,CAAC,CAAC;IACrC,CAAC;CACF,CAAC"}
|
package/package.json
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@fastrack/starter-http",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"main": "dist/index.js",
|
|
6
|
+
"types": "dist/index.d.ts",
|
|
7
|
+
"exports": {
|
|
8
|
+
".": {
|
|
9
|
+
"types": "./dist/index.d.ts",
|
|
10
|
+
"import": "./dist/index.js",
|
|
11
|
+
"default": "./dist/index.js"
|
|
12
|
+
}
|
|
13
|
+
},
|
|
14
|
+
"dependencies": {
|
|
15
|
+
"@sinclair/typebox": "^0.34.34",
|
|
16
|
+
"fastify": "^5.1.0",
|
|
17
|
+
"@fastrack/core": "0.1.0"
|
|
18
|
+
},
|
|
19
|
+
"devDependencies": {
|
|
20
|
+
"typescript": "^5.6.3"
|
|
21
|
+
},
|
|
22
|
+
"publishConfig": {
|
|
23
|
+
"access": "public",
|
|
24
|
+
"registry": "https://registry.npmjs.org/"
|
|
25
|
+
},
|
|
26
|
+
"engines": {
|
|
27
|
+
"node": ">=18"
|
|
28
|
+
},
|
|
29
|
+
"scripts": {
|
|
30
|
+
"build": "tsc -p tsconfig.json",
|
|
31
|
+
"clean": "node -e \"require('fs').rmSync('dist',{recursive:true,force:true})\"",
|
|
32
|
+
"lint": "echo ok",
|
|
33
|
+
"test": "echo ok"
|
|
34
|
+
}
|
|
35
|
+
}
|
package/src/client.ts
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
export interface CreateHttpClientOptions {
|
|
2
|
+
timeoutMs?: number;
|
|
3
|
+
retries?: number;
|
|
4
|
+
}
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Per-call options: pass the current request's correlation ID so it is propagated
|
|
8
|
+
* on the outbound request. Use singleton client and pass at request time.
|
|
9
|
+
*/
|
|
10
|
+
export interface HttpRequestOptions {
|
|
11
|
+
correlationId?: string;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Create an HTTP client with timeout and optional retries (idempotent only).
|
|
16
|
+
* Uses native fetch (Node 18+). Keep a singleton instance; pass correlationId
|
|
17
|
+
* at request time so it is propagated on each outbound call (e.g. from request.correlationId).
|
|
18
|
+
*/
|
|
19
|
+
export function createHttpClient(options: CreateHttpClientOptions = {}): {
|
|
20
|
+
fetch(url: string | URL, init?: RequestInit, requestOptions?: HttpRequestOptions): Promise<Response>;
|
|
21
|
+
} {
|
|
22
|
+
const { timeoutMs = 30_000, retries = 0 } = options;
|
|
23
|
+
|
|
24
|
+
async function doFetch(
|
|
25
|
+
url: string | URL,
|
|
26
|
+
init?: RequestInit,
|
|
27
|
+
requestOptions?: HttpRequestOptions,
|
|
28
|
+
attempt = 0
|
|
29
|
+
): Promise<Response> {
|
|
30
|
+
const controller = new AbortController();
|
|
31
|
+
const timeout = setTimeout(() => controller.abort(), timeoutMs);
|
|
32
|
+
const headers = new Headers(init?.headers);
|
|
33
|
+
if (requestOptions?.correlationId) {
|
|
34
|
+
headers.set("x-correlation-id", requestOptions.correlationId);
|
|
35
|
+
}
|
|
36
|
+
try {
|
|
37
|
+
const res = await fetch(url, {
|
|
38
|
+
...init,
|
|
39
|
+
signal: controller.signal,
|
|
40
|
+
headers,
|
|
41
|
+
});
|
|
42
|
+
clearTimeout(timeout);
|
|
43
|
+
const method = (init?.method ?? "GET").toUpperCase();
|
|
44
|
+
const isIdempotent = ["GET", "HEAD", "PUT", "DELETE", "OPTIONS", "TRACE"].includes(method);
|
|
45
|
+
if (!res.ok && isIdempotent && attempt < retries) {
|
|
46
|
+
return doFetch(url, init, requestOptions, attempt + 1);
|
|
47
|
+
}
|
|
48
|
+
return res;
|
|
49
|
+
} catch (e) {
|
|
50
|
+
clearTimeout(timeout);
|
|
51
|
+
if (attempt < retries) {
|
|
52
|
+
return doFetch(url, init, requestOptions, attempt + 1);
|
|
53
|
+
}
|
|
54
|
+
throw e;
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
return {
|
|
59
|
+
fetch: (url: string | URL, init?: RequestInit, requestOptions?: HttpRequestOptions) =>
|
|
60
|
+
doFetch(url, init, requestOptions),
|
|
61
|
+
};
|
|
62
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fastrack/starter-http — fetch/undici wrapper: timeouts, retries, correlation propagation.
|
|
3
|
+
*/
|
|
4
|
+
export { httpStarter } from "./plugin.js";
|
|
5
|
+
export { createHttpClient } from "./client.js";
|
|
6
|
+
export type { HttpStarterConfig, HttpClient } from "./plugin.js";
|
|
7
|
+
export type { CreateHttpClientOptions, HttpRequestOptions } from "./client.js";
|
package/src/plugin.ts
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { Type, type Static } from "@sinclair/typebox";
|
|
2
|
+
import type { FastifyInstance } from "fastify";
|
|
3
|
+
import type { FastrackStarter, FastrackContext } from "@fastrack/core";
|
|
4
|
+
import { createHttpClient } from "./client.js";
|
|
5
|
+
|
|
6
|
+
export const HttpStarterConfigSchema = Type.Object({
|
|
7
|
+
timeoutMs: Type.Optional(Type.Number({ default: 30_000 })),
|
|
8
|
+
retries: Type.Optional(Type.Number({ default: 0 })),
|
|
9
|
+
});
|
|
10
|
+
|
|
11
|
+
export type HttpStarterConfig = Static<typeof HttpStarterConfigSchema>;
|
|
12
|
+
|
|
13
|
+
export interface HttpClient {
|
|
14
|
+
fetch(
|
|
15
|
+
url: string | URL,
|
|
16
|
+
init?: RequestInit,
|
|
17
|
+
requestOptions?: { correlationId?: string }
|
|
18
|
+
): Promise<Response>;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export const httpStarter: FastrackStarter<HttpStarterConfig> = {
|
|
22
|
+
name: "http",
|
|
23
|
+
configSchema: HttpStarterConfigSchema,
|
|
24
|
+
defaults: { timeoutMs: 30_000, retries: 0 },
|
|
25
|
+
register(app: FastifyInstance, _ctx: FastrackContext, config: HttpStarterConfig) {
|
|
26
|
+
const client = createHttpClient({ timeoutMs: config.timeoutMs ?? 30_000, retries: config.retries ?? 0 });
|
|
27
|
+
app.decorate("httpClient", client);
|
|
28
|
+
},
|
|
29
|
+
};
|