UNPKG

2.46 kBJavaScriptView Raw
1// @flow
2
3import type { RequestorInterface } from '../types/Requestor';
4import type { Response, ResponseMergerStaticsInterface } from '../types/Responses';
5
6const BlueprintManager = require('./BlueprintManager');
7const HttpRequestor = require('./HttpRequestor');
8const JsonPathReplacer = require('./JsonPathReplacer');
9const MultipartRelatedResponse = require('./MultipartRelatedResponse');
10const ResponseMergerBase = require('./ResponseMergerBase');
11const SubrequestsManager = require('./SubrequestsManager');
12
13/**
14 * Execute a blueprint of requests using a requestor, get a single response.
15 *
16 * @param {string} blueprint
17 * The tree of requests in the expected blueprint format.
18 * @param {RequestorInterface} requestor
19 * The wrapped HTTP library to execute all communications.
20 * @param {function} MergerClass
21 * The class with static methods to merge all the subresponses into one.
22 *
23 * @return {Promise.<string>}
24 * The promise of a single response.
25 */
26function request(
27 blueprint: string,
28 requestor: RequestorInterface = new HttpRequestor(),
29 MergerClass: ResponseMergerStaticsInterface = MultipartRelatedResponse
30): Promise<Response> {
31 // Parse the blueprint to build the request tree.
32 const tree = BlueprintManager.parse(blueprint);
33 // Make all the sequential requests that, in turn, contain parallel requests.
34 return SubrequestsManager.request(tree, requestor)
35 // Merge all the responses into a single response string.
36 .then(subresponses => MergerClass.mergeResponses(subresponses));
37}
38
39/**
40 * Execute a blueprint of requests using a requestor, get an array of responses.
41 *
42 * @param {string} blueprint
43 * The tree of requests in the expected blueprint format.
44 * @param {RequestorInterface} requestor
45 * The wrapped HTTP library to execute all communications.
46 *
47 * @return {Promise.<string>}
48 * The promise of a single response.
49 */
50function requestArray(
51 blueprint: string,
52 requestor: RequestorInterface = new HttpRequestor()
53): Promise<Array<Response>> {
54 // Parse the blueprint to build the request tree.
55 const tree = BlueprintManager.parse(blueprint);
56 // Make all the sequential requests that, in turn, contain parallel requests.
57 return SubrequestsManager.request(tree, requestor);
58}
59
60module.exports = {
61 request,
62 requestArray,
63 lib: {
64 BlueprintManager,
65 HttpRequestor,
66 JsonPathReplacer,
67 MultipartRelatedResponse,
68 ResponseMergerBase,
69 SubrequestsManager,
70 },
71};