@mvxiv/http-service
Advanced tools
Comparing version 1.1.1 to 1.1.2
import { AxiosInstance, AxiosRequestConfig } from 'axios'; | ||
export class BaseService { | ||
constructor(httpService: AxiosInstance); | ||
setHeader(key: string, value: string): void; | ||
get(url: string, config?: AxiosRequestConfig): Promise<any>; | ||
delete(url: string, config?: AxiosRequestConfig): Promise<any>; | ||
head(url: string, config?: AxiosRequestConfig): Promise<any>; | ||
options(url: string, config?: AxiosRequestConfig): Promise<any>; | ||
post(url: string, payload: unknown, config?: AxiosRequestConfig): Promise<any>; | ||
put(url: string, payload: unknown, config?: AxiosRequestConfig): Promise<any>; | ||
patch(url: string, payload: unknown, config?: AxiosRequestConfig): Promise<any>; | ||
request(config: AxiosRequestConfig): Promise<any>; | ||
private readonly httpService: AxiosInstance; | ||
constructor(httpService: AxiosInstance); | ||
setHeader(key: string, value: string): void; | ||
get(url: string, config?: AxiosRequestConfig): Promise<any>; | ||
delete(url: string, config?: AxiosRequestConfig): Promise<any>; | ||
head(url: string, config?: AxiosRequestConfig): Promise<any>; | ||
options(url: string, config?: AxiosRequestConfig): Promise<any>; | ||
post(url: string, payload: unknown, config?: AxiosRequestConfig): Promise<any>; | ||
put(url: string, payload: unknown, config?: AxiosRequestConfig): Promise<any>; | ||
patch(url: string, payload: unknown, config?: AxiosRequestConfig): Promise<any>; | ||
request(config: AxiosRequestConfig): Promise<any>; | ||
private readonly httpService: AxiosInstance; | ||
} | ||
@@ -20,20 +20,20 @@ | ||
export function useHttpService( | ||
config: AxiosRequestConfig, | ||
interceptors?: { request: any[], response: any[] } | undefined | ||
config: AxiosRequestConfig, | ||
interceptors?: { request: any[]; response: any[] } | undefined | ||
): { | ||
httpService: AxiosInstance; | ||
defineService: (constructor: IConstructor, props: any[]) => () => IServiceInstance; | ||
setHeader: (key: string, value: string) => void; | ||
httpService: AxiosInstance; | ||
defineService: (constructor: IConstructor, props: any[]) => () => IServiceInstance; | ||
setHeader: (key: string, value: string) => void; | ||
}; | ||
export interface IServiceInstance { | ||
setHeader(key: string, value: string): void; | ||
get(url: string, config?: AxiosRequestConfig): Promise<any>; | ||
delete(url: string, config?: AxiosRequestConfig): Promise<any>; | ||
head(url: string, config?: AxiosRequestConfig): Promise<any>; | ||
options(url: string, config?: AxiosRequestConfig): Promise<any>; | ||
post(url: string, payload: unknown, config?: AxiosRequestConfig): Promise<any>; | ||
put(url: string, payload: unknown, config?: AxiosRequestConfig): Promise<any>; | ||
patch(url: string, payload: unknown, config?: AxiosRequestConfig): Promise<any>; | ||
request(config: AxiosRequestConfig): Promise<any>; | ||
setHeader(key: string, value: string): void; | ||
get(url: string, config?: AxiosRequestConfig): Promise<any>; | ||
delete(url: string, config?: AxiosRequestConfig): Promise<any>; | ||
head(url: string, config?: AxiosRequestConfig): Promise<any>; | ||
options(url: string, config?: AxiosRequestConfig): Promise<any>; | ||
post(url: string, payload: unknown, config?: AxiosRequestConfig): Promise<any>; | ||
put(url: string, payload: unknown, config?: AxiosRequestConfig): Promise<any>; | ||
patch(url: string, payload: unknown, config?: AxiosRequestConfig): Promise<any>; | ||
request(config: AxiosRequestConfig): Promise<any>; | ||
} | ||
@@ -44,4 +44,5 @@ | ||
export function createApiServiceContainer( | ||
constructorsDict: Record<string, IConstructor>, | ||
httpConfig: AxiosRequestConfig, | ||
httpInterceptors?: object | undefined): any; | ||
constructorsDict: Record<string, IConstructor | [IConstructor, ...unknown[]]>, | ||
httpConfig: AxiosRequestConfig, | ||
httpInterceptors?: object | undefined | ||
): any; |
export { createApiServiceContainer } from './lib/ioc.js'; | ||
export { useHttpService } from './lib/http-service.js'; | ||
export { BaseService } from './lib/BaseService.js'; |
@@ -0,0 +0,0 @@ export class BaseService { |
@@ -48,4 +48,4 @@ import { default as axios, AxiosInstance } from 'axios'; | ||
const defineService = (constructor, props = []) => { | ||
const constructorArgs = [httpService, ...props]; | ||
const defineService = (constructor, args = []) => { | ||
const constructorArgs = [httpService, ...args]; | ||
const instance = Reflect.construct(constructor, constructorArgs); | ||
@@ -52,0 +52,0 @@ return () => instance; |
@@ -33,8 +33,14 @@ import { useHttpService } from './http-service.js'; | ||
); | ||
const container = Object.entries(constructorsDict).reduce((container, [key, Service]) => { | ||
const instanceKey = key.charAt(0).toLowerCase() + key.slice(1); | ||
const service = defineService(Service)(); | ||
container[instanceKey] = autobindDecorator(service); | ||
return container; | ||
}, {}); | ||
const container = Object.entries(constructorsDict).reduce( | ||
(container, [key, constructorProps]) => { | ||
const [constructor, ...args] = Array.isArray(constructorProps) | ||
? constructorProps | ||
: [constructorProps]; | ||
const instanceKey = key.charAt(0).toLowerCase() + key.slice(1); | ||
const service = defineService(constructor, args)(); | ||
container[instanceKey] = autobindDecorator(service); | ||
return container; | ||
}, | ||
{} | ||
); | ||
@@ -51,19 +57,3 @@ Object.defineProperty(container, 'setHeader', { | ||
// Object.defineProperty(container, 'addService', { | ||
// configurable: false, | ||
// enumerable: false, | ||
// writable: false, | ||
// value: Service => { | ||
// if (constructors.has(Service)) { | ||
// console.error(new Error(`The service ${Service.name} already has an instance`)); | ||
// return; | ||
// } | ||
// const key = Service.name[0].toLowerCase() + Service.name.slice(1); | ||
// const service = defineService(Service)(); | ||
// container[key] = autobindDecorator(service); | ||
// constructors.add(constructor); | ||
// }, | ||
// }); | ||
return container; | ||
}; |
{ | ||
"name": "@mvxiv/http-service", | ||
"version": "1.1.1", | ||
"version": "1.1.2", | ||
"description": "Http service for frontend build upon axios", | ||
@@ -5,0 +5,0 @@ "repository": { |
@@ -90,8 +90,18 @@ # HTTP Service | ||
import { UserService } from '../services/user.service.js'; | ||
import { ProductService } from '../services/product.service.js'; | ||
import { UserService } from '../services/UserService.js'; | ||
import { ProductService } from '../services/ProductService.js'; | ||
import { PaymentService } from '../services/PaymentService.js'; | ||
class FooDependencyService {}; | ||
class BarDependencyService {}; | ||
const constructorsDict = { | ||
UserService, | ||
productService: ProductService | ||
productService: ProductService, | ||
// additional syntax (from v.1.1.2) first place - constructor, rest - arguments | ||
paymentService: [ | ||
PaymentService, | ||
new FooDependencyService(), | ||
new BarDependencyService() | ||
], | ||
}; | ||
@@ -98,0 +108,0 @@ ``` |
Sorry, the diff of this file is not supported yet
151
12169
171