2 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
13 * or implied. See the License for the specific language governing
14 * permissions and limitations under the License.
23 const initQueue = () => {
27 queue['destroy'] = [];
30 const handleOperation = (handler, options) => {
31 if(typeof handler === 'function') {
32 return Promise.resolve(handler(options));
35 return Promise.resolve(handler);
41 fetch(baseUrl, options) {
42 const {fetch} = queue;
44 throw new Error(`Fetch operation was called without proper handler. baseUrl: '${baseUrl}' options: '${options}'`);
46 return handleOperation(fetch.shift(), {options, baseUrl});
49 put(baseUrl, data, options) {
52 throw new Error(`put operation was called without proper handler. baseUrl: '${baseUrl}' options: '${options}'`);
54 return handleOperation(put.shift(), {data, options, baseUrl});
57 post(baseUrl, data, options) {
60 throw new Error(`post operation was called without proper handler. baseUrl: '${baseUrl}' options: '${options}'`);
62 return handleOperation(post.shift(), {data, options, baseUrl});
65 destroy(baseUrl, options) {
66 const {destroy} = queue;
68 throw new Error(`Destroy operation was called without proper handler. baseUrl: '${baseUrl}' options: '${options}'`);
70 return handleOperation(destroy.shift(), {options, baseUrl});
73 addHandler(operation, handler) {
74 queue[operation].push(handler);
83 let message = 'Check following calls: ';
84 for (let operationType in queue) {
85 if (queue[operationType].length > 0) {
87 message += operationType;
91 throw new Error('Queue is not empty, ' + message);