本文整理汇总了TypeScript中request-promise-native.default方法的典型用法代码示例。如果您正苦于以下问题:TypeScript request-promise-native.default方法的具体用法?TypeScript request-promise-native.default怎么用?TypeScript request-promise-native.default使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类request-promise-native
的用法示例。
在下文中一共展示了request-promise-native.default方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。
示例1: switch
private _request<T = any>(
httpMethod: string,
url: string,
params: any,
token: Token | string | undefined,
): Promise<T> {
const header = this.authenticator.getHeader(token);
const options: HTTPRequest = {
method: httpMethod,
url,
headers: header,
json: true,
resolveWithFullResponse: true,
simple: false,
};
switch (httpMethod) {
case 'POST':
case 'PUT':
options.body = params;
break;
default:
options.qs = params;
}
return request(options).then(response => {
if (response.statusCode < 200 || response.statusCode >= 300) {
return Promise.reject(new Error(JSON.stringify(response.body)));
}
return response.body;
});
}
示例2: startServer
test('Response data can be deduplicated with graphql-deduplicator', async t => {
const { uri, data: { book } } = await startServer(t)
const query = `
query {
books {
__typename
id
name
author {
__typename
id
name
lastName
}
}
}
`
const body = await request({
uri,
method: 'POST',
json: true,
body: { query },
}).promise()
const deduplicated = await request({
uri,
method: 'POST',
json: true,
body: { query },
headers: {
'X-GraphQL-Deduplicate': true
}
}).promise()
t.deepEqual(body, {
data: {
books: Array(5).fill(book)
}
})
t.deepEqual(deduplicated, {
data: {
books: [
book,
...Array(4).fill({
__typename: book.__typename,
id: book.id
})
]
}
})
t.deepEqual(body.data, inflate(deduplicated.data))
})
示例3: rpn
http.createServer((req, resp) => {
if (req.url === '/doodle.png') {
const x = rpn('http://mysite.com/doodle.png');
req.pipe(x);
x.pipe(resp);
}
});
示例4: request
/**
* @param {string} url
* @param {{}} form
* @param {Token?} token
*/
postForm<T = any>(
url: string,
form:
| FormData
| {
file: UploadFileStream;
path: string;
uploadToken: string | null;
} & Partial<UploadFileRequest>,
token?: Token | undefined,
): Promise<T> {
const header = this.authenticator.getHeader(token);
const options = {
method: 'POST',
url,
formData: form,
headers: header,
json: true,
};
return request(options).then(
response => {
if (response.statusCode < 200 || response.statusCode >= 300) {
return Promise.reject(response.body);
}
return response;
},
error => {
return Promise.reject(error);
},
);
}
示例5: resolve
public async resolve(value: any): Promise<IObject> {
if (value == null) {
throw new Error('resolvee is null (or undefined)');
}
if (typeof value !== 'string') {
return value;
}
if (this.history.has(value)) {
throw new Error('cannot resolve already resolved one');
}
this.history.add(value);
const object = await request({
url: value,
headers: {
Accept: 'application/activity+json, application/ld+json'
},
json: true
});
if (object === null || (
Array.isArray(object['@context']) ?
!object['@context'].includes('https://www.w3.org/ns/activitystreams') :
object['@context'] !== 'https://www.w3.org/ns/activitystreams'
)) {
log(`invalid response: ${value}`);
throw new Error('invalid response');
}
return object;
}
示例6: httpRequest
async function httpRequest(args: { path: string, method: string, body?: any, formData?: any, query?: any, json?: boolean }) {
const options: any = {
uri: `${Configuration.baseUrl}${args.path}`,
method: args.method,
resolveWithFullResponse: true
};
if (args.formData) {
options.formData = args.formData;
} else if (args.body) {
options.body = args.body;
} else if (args.query) {
options.qs = args.query;
}
if (args.json) {
options.json = true;
}
// console.log(options);
try {
return await request(options);
} finally {
// console.log(`request sent ${options.method} ${options.uri}`);
}
}
示例7:
return new Promise<null>((resolve, reject) => {
let params: any = {
timestamp: tsPost,
commentTimestamp: tsComment
};
let reqPath = path.join('/v1/server/posts', tsPost.toString(), 'comments', tsComment.toString());
let url = postAuthor + reqPath;
if(idtoken && sigtoken) {
params.idToken = idtoken;
let signature = utils.computeSignature('DELETE', url, params, sigtoken);
url += '?idToken=' + idtoken
url += '&signature=' + signature;
}
// We'll use HTTP only for localhost
if(url.indexOf('localhost') < 0 && !commons.settings.forceHttp) url = 'https://' + url;
else url = 'http://' + url
log.debug('Requesting DELETE', url);
request({
method: 'DELETE',
uri: url,
timeout: commons.settings.timeout
})
.then((response) => {
log.debug('Deleted a comment on', postAuthor.toString());
resolve(response);
}).catch(reject);
});
示例8: getSitemap
private async getSitemap() {
try {
if (helper.isUrl(this.url) && helper.isXml(this.url)) {
return await request(this.url);
}
} catch (error) {
console.error(`Error on Request Catch : ${error}`);
throw error;
}
}
示例9: request
export async function getTrainPredictions(station: Wmata.Station): Promise<Array<Wmata.TrainPrediction>> {
let options = {
url: `https://api.wmata.com/StationPrediction.svc/json/GetPrediction/${station.Code}`,
headers: {
api_key: wmataApiKey
},
json: true
};
return request(options)
.then(response => response.Trains);
};