本文整理汇总了TypeScript中axios.request函数的典型用法代码示例。如果您正苦于以下问题:TypeScript request函数的具体用法?TypeScript request怎么用?TypeScript request使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了request函数的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。
示例1: addBasepath
async function AJAX<T = any>(
{
url,
method = 'GET',
data = {},
params = {},
headers = {},
auth = null,
}: RequestParams,
excludeBasepath = false
): Promise<(T) | AxiosResponse<T>> {
try {
url = addBasepath(url, excludeBasepath)
const response = await axios.request<T>({
url,
method,
data,
params,
headers,
auth,
})
return response
} catch (error) {
const {response} = error
throw response
}
}
示例2: Promise
return new Promise((resolve, reject) => {
let partialBody = '';
return axios
.request<IncomingMessage>(config)
.then(response => {
if (response.status !== 200) {
return reject(`Request failed with status code "${response.status}".`);
}
const contentType = response.headers['content-type'] || '';
const isHtmlContentType = contentType.match(/.*text\/html/);
if (!isHtmlContentType) {
reject(`Unhandled format for open graph generation ('${contentType}')`);
}
response.data.on('data', buffer => {
const chunk = buffer.toString();
partialBody += chunk;
if (chunk.match('</head>') || partialBody.length > contentLimit) {
cancelSource.cancel();
resolve(partialBody);
}
});
response.data.on('error', reject);
response.data.on('end', () => resolve(partialBody));
})
.catch(error => (axios.isCancel(error) ? Promise.resolve('') : Promise.reject(error)));
});
示例3: async
const fetchImageAsBase64 = async (url: string): Promise<string | undefined> => {
const IMAGE_SIZE_LIMIT = 5e6; // 5MB
const axiosConfig: AxiosRequestConfig = {
headers: {
'User-Agent': USER_AGENT,
},
maxContentLength: IMAGE_SIZE_LIMIT,
method: 'get',
responseType: 'arraybuffer',
url,
};
const response = await axios.request<Buffer>(axiosConfig);
if (response.status !== 200) {
throw new Error(`Request failed with status code "${response.status}".`);
}
const contentType = response.headers['content-type'] || '';
const isImageContentType = contentType.match(/.*image\/.*/);
if (!isImageContentType) {
throw new Error(`Unhandled format for open graph image ('${contentType}')`);
}
return bufferToBase64(response.data, contentType);
};
示例4: makeRequest
function makeRequest(request: AxiosRequestConfig) {
return axios.request({
...request,
headers: {
'Content-Type': 'application/json',
},
});
}
示例5: getUserCanAccessResource
async function getUserCanAccessResource (authToken: string, account: string, userEmail: string, productCode: string, resourceCode: string): Promise<boolean> {
const url = `http://${account}.vtexcommercestable.com.br/api/license-manager/pvt/accounts/${account}/products/${productCode}/logins/${userEmail}/resources/${resourceCode}/granted`
const req = await axios.request({
headers: {
'Authorization': authToken,
},
method: 'get',
url,
})
return req.data
}
示例6: sendRequest
protected sendRequest(method, url, params?): Promise<AxiosResponse> {
return Axios.request({
method,
baseURL: `${this.rabbitmqConfig.mgtHttpTheme}://${this.rabbitmqConfig.mgtHttpHost}:${this.rabbitmqConfig.mgtHttpPort}/api/`,
url,
params,
headers: {'content-type': 'application/json'},
auth: {
username: this.rabbitmqConfig.username,
password: this.rabbitmqConfig.password,
},
});
}
示例7: getUserEmail
async function getUserEmail (authToken: string, vtexIdToken: string): Promise<string | void> {
const url = `vtexid.vtex.com.br/api/vtexid/pub/authenticated/user?authToken=${vtexIdToken}`
const req = await axios.request({
headers: {
'Accept': 'application/json',
'Proxy-Authorization': authToken,
'X-VTEX-Proxy-To': `https://${url}`,
},
method: 'get',
url: `http://${url}`,
})
if (!req.data) {
return undefined
}
return req.data.user
}
示例8: getSubjectDetails
public getSubjectDetails(subjectIds: string): Promise<Subject[]> {
const config = {
...this.axiosConfig,
method: "get",
params: {
"index": "gnd",
// lookfor: "[" + contributorIds + "]",
"method": "getSubjects",
"overrideIds[]": subjectIds,
"searcher": "ElasticSearch",
"type": "DEFAULT",
},
};
return Axios.request(config)
.then((response: AxiosResponse) => {
return response.data.data as Subject[];
});
}
示例9: getOrganisationDetails
public getOrganisationDetails(organisationIds: string): Promise<Organisation[]> {
const config = {
...this.axiosConfig,
method: "get",
params: {
"index": "lsb",
// lookfor: "[" + contributorIds + "]",
"method": "getOrganisations",
"overrideIds[]": organisationIds,
"searcher": "ElasticSearch",
"type": "organisation",
},
};
return Axios.request(config)
.then((response: AxiosResponse) => {
return response.data.data as Organisation[];
});
}