
Security News
Meet Socket at Black Hat and DEF CON 2025 in Las Vegas
Meet Socket at Black Hat & DEF CON 2025 for 1:1s, insider security talks at Allegiant Stadium, and a private dinner with top minds in software supply chain security.
axios-typescript-response
Advanced tools
Get axios AJAX response in typescript class objects
- npm install axios-typescript-response
http.get(url: string, type: (new (arg: any) => T), useConstructor: Boolean, config?: AxiosRequestConfig | undefined)
- url: API url
- type: Expected data type to be returned from the API
- useConstructor: Indicates if the class constructor should be used (false by default)
- config: Axios configuration if needed
http.post(url: string, payload: any, type: (new (arg: any) => T), useConstructor: Boolean, config?: AxiosRequestConfig | undefined): Promise
- url: API url
- payload: Data that will be put in the request body
- type: Expected data type to be returned from the API
- useConstructor: Indicates if the class constructor should be used (false by default)
- config: Axios configuration if needed
class Post{
id: number = 0;
userId: number = 0;
title: string = '';
body: string = '';
}
import Http from 'axios-typescript-response';
...
http
.get<Post>("https://jsonplaceholder.typicode.com/posts/1", Post)
.then((response) => console.log(response instanceof Post)) //output: true
//Calling the function without type parameter returns axios default function
http
.get("https://jsonplaceholder.typicode.com/posts/1")
.then((response) => console.log(response.data instanceof Post)) //output: false
class Post{
constructor(obj:any = {}){
this.id = obj.id;
this.userId = obj.userId;
this.title = obj.title;
this.body = obj.body;
}
id: number;
userId: number;
title: string;
body: string;
}
import Http from 'axios-typescript-response';
...
http
.get<Post>("https://jsonplaceholder.typicode.com/posts/1", Post, true)
.then((response) => console.log(response instanceof Post)) //output: true
//Calling the function without type parameter returns axios default function
http
.get("https://jsonplaceholder.typicode.com/posts/1")
.then((response) => console.log(response.data instanceof Post)) //output: false
import store from '../store/store';
import router from '../router/router';
import Http from 'axios-typescript-response';
Http.axiosInstance.interceptors.request.use(config => {
debugger
config.baseURL = store.getters.baseUrl;
//Auth token
const token = store.state.user!.token;
if (token) {
config.headers.Authorization = `Bearer ${token}`;
}
//Culture
const lang = store.getters.Language;
config.headers["Accept-Language"] = lang || 'bg';
config.headers["Accept"] = '*/*';
return config;
});
// Add a 401 response interceptor
Http.axiosInstance.interceptors.response.use(response => {
return response;
}, function (error) {
if (401 === error.response.status) {
store.dispatch('logout').then(() => {
router.push('/login');
});
} else {
return Promise.reject(error);
}
});
export default Http;
FAQs
Get axios AJAX response in typescript class objects
The npm package axios-typescript-response receives a total of 3 weekly downloads. As such, axios-typescript-response popularity was classified as not popular.
We found that axios-typescript-response demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Meet Socket at Black Hat & DEF CON 2025 for 1:1s, insider security talks at Allegiant Stadium, and a private dinner with top minds in software supply chain security.
Security News
CAI is a new open source AI framework that automates penetration testing tasks like scanning and exploitation up to 3,600× faster than humans.
Security News
Deno 2.4 brings back bundling, improves dependency updates and telemetry, and makes the runtime more practical for real-world JavaScript projects.