@forgeframework/http-client
v0.3.0
Published
Multi-protocol HTTP client for the Forge Framework.
Maintainers
Readme
@forgeframework/http-client
Multi-protocol HTTP client for the Forge Framework.
Part of the Forge Framework — a TypeScript framework for backend microservices and web applications.
Installation
npm install @forgeframework/http-clientUsage
import { HttpClientFactory } from '@forgeframework/http-client';
const factory = new HttpClientFactory();
const client = factory.create({
baseUrl: 'https://api.example.com',
timeout: 10000,
headers: {
'X-Request-Source': 'forge-service',
},
auth: {
clientId: 'my-service',
clientSecret: process.env.CLIENT_SECRET!,
tokenUrl: 'https://auth.example.com/oauth/token',
grantType: 'client_credentials',
scopes: ['read', 'write'],
expireTokenBefore: 300,
},
retry: {
maxAttempts: 3,
minDelay: 1000,
maxDelay: 15000,
retryStatusCodes: [502, 503, 504, 429],
},
enableCurl: true,
interceptors: {
request: [correlationIdInterceptor],
},
});
interface User {
id: string;
name: string;
email: string;
}
const response = await client.get<User[]>('/users', {
params: { limit: 10, active: true },
});
const newUser = await client.post<User>('/users', {
name: 'Ada Lovelace',
email: '[email protected]',
});
const cbClient = factory.createWithCircuitBreaker(
{ baseUrl: 'https://unstable-api.example.com' },
{
failureRateThreshold: 50,
slidingWindowSize: 10,
waitDurationInOpenState: 30000,
minimumNumberOfCalls: 5,
},
);
const graphql = factory.createGraphQL({
baseUrl: 'https://api.example.com/graphql',
auth: {
clientId: 'my-service',
clientSecret: process.env.CLIENT_SECRET!,
tokenUrl: 'https://auth.example.com/oauth/token',
grantType: 'client_credentials',
},
});
interface GetUsersResult {
users: User[];
}
const result = await graphql.query<GetUsersResult>(
`query GetUsers($limit: Int!) { users(limit: $limit) { id name email } }`,
{ limit: 10 },
);
await client.close();Documentation
Full API reference and guides: https://carbonforge.io/framework/docs/clients/http-client
(Documentation site launching soon.)
License
MIT © Carbon Forge
