@odg/message
v1.15.1
Published
Interface request fo inversion of control
Readme
Table of Contents
🎇 Benefits
- 👀 Inversion of control (IoC)
- 🎇 Dependency Injection (DI)
- 🚨 Over 800 rules for pattern, possible errors and errors in Linter
📗 Libraries
📁 Dependencies
- Node.js 16 or later
- Yarn Optional/Recommended
- ODG TsConfig Last Version
- ODG Exception Last Version
⏩ Get Started
🔘 Add dependencies
yarn add @odg/message💻 Usage
for use axios implementation click here
import { type MessageInterface, type MessageResponse } from "@odg/message";
class Test {
public constructor(private readonly requester: MessageInterface) {
}
public async example(): Promise<MessageResponse<any, any>> {
return this.requester.request({
url: "https://google.com",
});
}
}📰 Example Implements
import axios, {
type AxiosInstance,
type AxiosResponse,
} from "axios";
import {
type HttpHeadersInterface,
type InterceptorsInterface,
type MessageInterface,
type RequestInterface,
type ResponseInterface,
MessageException,
MessageResponse,
} from "@odg/message";
export class AxiosMessage<RequestData, ResponseData> implements MessageInterface<RequestData, ResponseData> {
public interceptors: {
request: InterceptorManager<RequestInterface<RequestData>>;
response: InterceptorManager<MessageResponse<RequestData, ResponseData>>;
}
private readonly client: AxiosInstance;
private defaultOptions: Partial<RequestInterface<RequestData>> = {};
public constructor(client?: AxiosInstance) {
this.client = client ?? axios.create();
}
public getDefaultOptions(): Partial<RequestInterface<RequestData>> {
return { ...this.defaultOptions };
}
public setDefaultOptions(config: Partial<RequestInterface<RequestData>>): this {
this.defaultOptions = { ...this.defaultOptions, ...config };
return this;
}
public async request<
RequestD = RequestData,
ResponseD = ResponseData,
>(config: RequestInterface<RequestD>): Promise<MessageResponse<RequestD, ResponseD>> {
try {
const response = await this.client.request<ResponseD, AxiosResponse<ResponseD, RequestD>, RequestD>(config);
const body: ResponseInterface<ResponseD> = {
data: response.data,
status: response.status,
headers: response.headers as HttpHeadersInterface,
};
return new MessageResponse(
response.config as RequestInterface<RequestD>,
body,
);
} catch (error: unknown) {
throw new MessageException("Example");
}
}
}🧪 Teste Code
To Test execute this command
yarn test
# or
yarn test:watch