@superlogica/super-http-client-js
v4.2.4
Published
Http Client Lib with segregated interfaces developed with clean architecture concepts by Superlógica Tecnologias S.A
Readme

What is Super Http Client?
Http Client lib for Node.js & TypeScript developed by Superlógica Tecnologias S.A
Getting started
To install the lib just run the command:
npm install @superlogica/super-http-client-jsReady!
Now you can use the available interfaces and the adapter.
How Works?
The most basic possible use is to import default (adapter) and use it, as in the following example:
import SuperHttpClient from '@superlogica/super-http-client-js'
async function getAllCharacters() {
const superHttpClient = new SuperHttpClient({
baseURL: 'https://rickandmortyapi.com/api'
})
const characters = await superHttpClient.get({
url: '/character'
})
return characters
}You can also make named imports to use interfaces, that is, abstractions instead of injecting concrete classes, as in the following example:
First of all, it is possible to create a type to define what the response will be, for example, of a get request.
// characters-dto.ts
export type Info = {
count: number
pages: number
next: string
prev?: string
}
export type Character = {
id: number
name: string
status: 'alive' | 'dead' | 'unknown'
species: string
type: string
gender: 'male' | 'female' | 'genderless'
origin: {
name: string
url: string
}
location: {
name: string
url: string
}
image: string
episode: string[]
url: string
created: Date
}Let's create a simple use case that basically lists all characters for a given API. For this, we will use the existing abstraction in the @superlogica/super-http-client-js lib, which is HttpGetClient
Note that when calling the get method, a parameter is passed to indicate the method's response type.
// get-all-characters-use-case.ts
import { HttpGetClient } from '@superlogica/super-http-client-js'
import { Info, Character } from './characters-dto'
export type Response = Info & Character
export class GetAllCharactersUseCase {
// abstract dependency
constructor(private readonly getCharacters: HttpGetClient) {}
async execute() {
return this.getCharacters.get<Response>({
url: '/character'
})
}
}Finally, in index file, for example, you can create a lib instance to inject the dependency into the use case constructor
// index.ts
const superHttpClient = new SuperHttpClientAdapter({
baseURL: 'https://rickandmortyapi.com/api'
})
// inject dependency
const getAllCharactersUseCase = new GetAllCharactersUseCase(superHttpClient)
getAllCharactersUseCase.execute().then((response) => {
console.log(response)
})Cache
The lib supports caching with a pluggable driver system. Currently, the Redis driver is available.
Basic usage with Redis driver
import SuperHttpClient from '@superlogica/super-http-client-js'
const superHttpClient = new SuperHttpClient(
{ baseURL: 'https://rickandmortyapi.com/api' },
{
storage: {
driver: 'redis',
config: { host: 'localhost', port: 6379 }
}
}
)
// GET responses are cached automatically with a default TTL of 5 minutes
const characters = await superHttpClient.get({ url: '/character' })Custom TTL and cached methods
const superHttpClient = new SuperHttpClient(
{ baseURL: 'https://rickandmortyapi.com/api' },
{
ttl: 1000 * 60 * 10, // 10 minutes
methods: ['get', 'post'], // cache GET and POST responses
storage: {
driver: 'redis',
config: { host: 'localhost', port: 6379 }
}
}
)Cache context: GLOBAL vs REQUEST
By default the cache context is GLOBAL, which means caching is enabled for all requests automatically. You can set the context to REQUEST to control caching per request:
const superHttpClient = new SuperHttpClient(
{ baseURL: 'https://rickandmortyapi.com/api' },
{
context: 'REQUEST',
storage: {
driver: 'redis',
config: { host: 'localhost', port: 6379 }
}
}
)
// Cache enabled for this specific request
const characters = await superHttpClient.get(
{ url: '/character' },
{ useCache: true }
)
// Cache disabled for this request
const freshCharacters = await superHttpClient.get(
{ url: '/character' },
{ useCache: false }
)Per-request cache control
You can override cache behavior on individual requests, regardless of the global configuration:
// Force cache refresh (bypass existing cache)
const freshData = await superHttpClient.get(
{ url: '/character' },
{ override: true }
)
// Custom TTL for a specific request
const shortLivedData = await superHttpClient.get(
{ url: '/character' },
{ ttl: 1000 * 30 } // 30 seconds
)Custom storage with Redis key options
import { makeRedisDriverStorage } from '@superlogica/super-http-client-js'
const redisStorage = makeRedisDriverStorage({
host: 'localhost',
port: 6379,
keyOptions: {
prefix: 'my-app',
suffix: 'v1',
delimiter: ':'
}
})
// Keys will be formatted as: "my-app:<cache-key>:v1"
const superHttpClient = new SuperHttpClient(
{ baseURL: 'https://rickandmortyapi.com/api' },
{ storage: redisStorage }
)Using a custom cache implementation
You can provide your own cache implementation by implementing the HttpCache interface:
import SuperHttpClient, { HttpCache } from '@superlogica/super-http-client-js'
const myCustomCache: HttpCache = {
async set(key, value) { /* your logic */ },
async find(key) { /* your logic */ },
async remove(key) { /* your logic */ }
}
const superHttpClient = new SuperHttpClient(
{ baseURL: 'https://rickandmortyapi.com/api' },
{ storage: myCustomCache }
)