DRY Axios
Utilize typescript decorators to create declarative http service with axios
Installation
Add to project with your package manager
npm install axios dry-axios
yarn add axios dry-axios
pnpm install axios dry-axios
Set experimental decorators options in your tsconfig.json
{
"compilerOptions": {
"experimentalDecorators": true,
"emitDecoratorMetadata": true
}
}
Example Usage
import Axios from 'axios';
import {
Asserted,
Post,
Get,
Sample,
Jwt,
Http,
} from 'dry-axios;
// runtime is a config passed to certain resolvers when methods are invoked
// ext: jwtResolver, sample validate & apply. See below
type Runtime = {
mode: 'production' | 'development';
};
@Http<Runtime>(Axios, {
// config passed to axios.create
axios: {
baseURL: 'http:
},
runtime: {
mode: 'development',
}
})
class AccountHttpService {
@Get('/identity', {
preserveAxiosResponse: false;
})
@Jwt<Runtime>((runtime) => someJwtGetter(runtime))
async getIdentity(): Promise<IdentityResponse> {
return Asserted<IdentityResponse>();
}
@Post('/sign-up')
@Sample<Runtime>({
resolver: async () => {
const module = await import('./sign-up.sample.json');
return module.default;
},
apply: (runtime) => runtime.mode === 'development',
validate: (runtime) => runtime.mode === 'development',
})
async signUp(@Body _body: SignUpRequestBody): Promise<SignUpResponse> {
return Asserted<SignUpResponse>();
}
}
const accountHttp = new AccountHttpService();
const response = await accountHttp.signUp({ ... });
Todos
Related