UNPKG

bitbucket

Version:

Bitbucket API client for Browser and Node.js

3 lines (2 loc) 10.8 kB
import{Singular as e}from"before-after-hook";import t from"deepmerge";import r from"is-plain-object";import n from"url-template";import o from"node-fetch";var a={method:"GET",baseUrl:"".concat("https","://").concat("api.bitbucket.org").concat("/2.0"),headers:{accept:"application/json","user-agent":"bitbucket.js/".concat("2.12.0")}};function u(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}function i(e){return(i=Object.setPrototypeOf?Object.getPrototypeOf:function(e){return e.__proto__||Object.getPrototypeOf(e)})(e)}function c(e,t){return(c=Object.setPrototypeOf||function(e,t){return e.__proto__=t,e})(e,t)}function s(){if("undefined"==typeof Reflect||!Reflect.construct)return!1;if(Reflect.construct.sham)return!1;if("function"==typeof Proxy)return!0;try{return Date.prototype.toString.call(Reflect.construct(Date,[],(function(){}))),!0}catch(e){return!1}}function f(e,t,r){return(f=s()?Reflect.construct:function(e,t,r){var n=[null];n.push.apply(n,t);var o=new(Function.bind.apply(e,n));return r&&c(o,r.prototype),o}).apply(null,arguments)}function l(e){var t="function"==typeof Map?new Map:void 0;return(l=function(e){if(null===e||(r=e,-1===Function.toString.call(r).indexOf("[native code]")))return e;var r;if("function"!=typeof e)throw new TypeError("Super expression must either be null or a function");if(void 0!==t){if(t.has(e))return t.get(e);t.set(e,n)}function n(){return f(e,arguments,i(this).constructor)}return n.prototype=Object.create(e.prototype,{constructor:{value:n,enumerable:!1,writable:!0,configurable:!0}}),c(n,e)})(e)}function d(e,t){if(null==e)return{};var r,n,o=function(e,t){if(null==e)return{};var r,n,o={},a=Object.keys(e);for(n=0;n<a.length;n++)r=a[n],t.indexOf(r)>=0||(o[r]=e[r]);return o}(e,t);if(Object.getOwnPropertySymbols){var a=Object.getOwnPropertySymbols(e);for(n=0;n<a.length;n++)r=a[n],t.indexOf(r)>=0||Object.prototype.propertyIsEnumerable.call(e,r)&&(o[r]=e[r])}return o}function p(e){if(void 0===e)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return e}function h(e,t){return!t||"object"!=typeof t&&"function"!=typeof t?p(e):t}function v(e,t){return function(e){if(Array.isArray(e))return e}(e)||function(e,t){if("undefined"==typeof Symbol||!(Symbol.iterator in Object(e)))return;var r=[],n=!0,o=!1,a=void 0;try{for(var u,i=e[Symbol.iterator]();!(n=(u=i.next()).done)&&(r.push(u.value),!t||r.length!==t);n=!0);}catch(e){o=!0,a=e}finally{try{n||null==i.return||i.return()}finally{if(o)throw a}}return r}(e,t)||y(e,t)||function(){throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}()}function y(e,t){if(e){if("string"==typeof e)return b(e,t);var r=Object.prototype.toString.call(e).slice(8,-1);return"Object"===r&&e.constructor&&(r=e.constructor.name),"Map"===r||"Set"===r?Array.from(e):"Arguments"===r||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(r)?b(e,t):void 0}}function b(e,t){(null==t||t>e.length)&&(t=e.length);for(var r=0,n=new Array(t);r<t;r++)n[r]=e[r];return n}function m(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{};return Object.keys(e).reduce((function(t,r){return t[r.toLowerCase()]=e[r],t}),{})}function g(e,n,o){if("string"==typeof n){var a=v(n.split(" "),2),u=a[0],i=a[1];o=Object.assign(i?{method:u,url:i}:{url:u},o)}else o=n||{};return o.headers=m(o.headers),t.all([e,o].filter(Boolean),{isMergeableObject:r})}function w(e){var t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{},r=e.includes("?")?"&":"?",n=Object.keys(t).filter((function(e){return void 0!==t[e]}));return 0===n.length?e:"".concat(e).concat(r).concat(n.map((function(e){return"q"===e?"q=".concat(t.q.split(" ").map(encodeURIComponent).join("+")):Array.isArray(t[e])?t[e].map((function(t){return"".concat(e,"=").concat(encodeURIComponent(t))})).join("&"):"".concat(e,"=").concat(encodeURIComponent(t[e]))})).join("&"))}var O=/\{[^}]+\}/g;function j(e){return e.replace(/^\W+|\W+$/g,"").split(/,/)}var q="multipart/form-data",E="application/x-www-form-urlencoded",S="application/json; charset=utf-8";function k(e){var t=e.accepts,r=void 0===t?[]:t,o=e.method,a=e.baseUrl,u=e.url,i=e.headers,c=e.request,s=d(e,["accepts","method","baseUrl","url","headers","request"]),f=o.toUpperCase(),l=function(e){var t=e.match(O);return t?t.map(j).reduce((function(e,t){return e.concat(t)}),[]):[]}(u),p=n.parse(u).expand(s);/^http/.test(p)||(p="".concat(a).concat(p));var h,v=Object.keys(s).reduce((function(e,t){return l.includes(t)||(e[t]=s[t]),e}),{}),y=v._body,b=d(v,["_body"]),m=!1;if(["GET","DELETE"].includes(f))p=w(p,b);else if(void 0!==y){if((m=/form-?data/i.test((h=y).constructor.name))&&r.includes(q))for(var g=0,k=Object.keys(b);g<k.length;g++){var T=k[g];h.append(T,b[T])}}else Object.keys(b).length&&(h=b);return m||(r.includes(E)?(h=w("",h).substring(1),i["content-type"]=E):(h=JSON.stringify(h),i["content-type"]=S)),void 0===h&&delete i["content-type"],{method:f,url:p,body:h,headers:i,request:c}}function T(e,t,r){return k(g(e,t,r))}var x=function e(t,r){var n=g(t,r),o=T.bind(null,n);return o.DEFAULTS=n,o.defaults=e.bind(null,n),o.merge=g.bind(null,n),o.parse=k,o}(null,a),P=function(e){!function(e,t){if("function"!=typeof t&&null!==t)throw new TypeError("Super expression must either be null or a function");e.prototype=Object.create(t&&t.prototype,{constructor:{value:e,writable:!0,configurable:!0}}),t&&c(e,t)}(o,l(Error));var t,r,n=(t=o,r=s(),function(){var e,n=i(t);if(r){var o=i(this).constructor;e=Reflect.construct(n,arguments,o)}else e=n.apply(this,arguments);return h(this,e)});function o(e,t){var r,a=arguments.length>2&&void 0!==arguments[2]?arguments[2]:{};return u(this,o),r=n.call(this,e),Error.captureStackTrace&&Error.captureStackTrace(p(r),r.constructor),r.name="HTTPError",r.error=a.error,r.headers=a.headers,r.request=a.request,r.status=t,r}return o}();function A(e){var t=e.headers.get("content-type");return t.includes("application/json")?e.json():!t||/^text\/|charset=utf-8$/.test(t)?e.text():e.arrayBuffer()}function I(e){var t,r,n,a=e.method,u=e.url,i=e.headers,c=e.body,s=e.request,f=Object.assign({method:a,body:c,headers:i},s),l={};return(null!==(t=s.fetch)&&void 0!==t?t:o)(u,f).then((function(t){r=t.status,n=t.url;var o,a=function(e){if("undefined"==typeof Symbol||null==e[Symbol.iterator]){if(Array.isArray(e)||(e=y(e))){var t=0,r=function(){};return{s:r,n:function(){return t>=e.length?{done:!0}:{done:!1,value:e[t++]}},e:function(e){throw e},f:r}}throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}var n,o,a=!0,u=!1;return{s:function(){n=e[Symbol.iterator]()},n:function(){var e=n.next();return a=e.done,e},e:function(e){u=!0,o=e},f:function(){try{a||null==n.return||n.return()}finally{if(u)throw o}}}}(t.headers);try{for(a.s();!(o=a.n()).done;){var u=v(o.value,2),i=u[0],c=u[1];l[i]=c}}catch(e){a.e(e)}finally{a.f()}return t.status>=400||[304].includes(t.status)?A(t).then((function(n){throw new P(t.statusText,r,{error:n,headers:l,request:e})})):A(t)})).then((function(e){return{data:e,headers:l,status:r,url:n}})).catch((function(t){if(t instanceof P)throw t;throw new P(t.message,500,{headers:l,request:e})}))}var N=function e(t,r){var n=t.defaults(r);function o(e,t){var r=n.merge(e,t);return r.request&&r.request.hook?r.request.hook((function(e){return I(n.parse(e))}),r):I(n.parse(r))}return o.defaults=e.bind(null,n),o.endpoint=n,o}(x,{});function U(e,n){e.headers=m(e.headers);var o,a=t({headers:{},request:{}},(o=e,["baseUrl","headers","request"].reduce((function(e,t){return void 0!==o[t]&&(e[t]=o[t]),e}),{})),{isMergeableObject:r});return a.request.hook=n,a}function R(t){var r=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{},n=new e,o={request:N.defaults(U(r,n)),requestHook:n};return t.forEach((function(e){e(o,r)})),o}function B(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:[],t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:[],r=e.slice(0);return t.forEach((function(e){r.includes(e)||r.push(e)})),C(r)}function C(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:[],t=R.bind(null,e);return t.plugins=B.bind(null,e),t}var _=C();function D(e,t){if("token"in e.auth)t.headers.authorization="Bearer ".concat(e.auth.token);else if(e.auth.username){var r=(n="".concat(e.auth.username,":").concat(e.auth.password),"undefined"!=typeof Buffer?Buffer.from(n).toString("base64"):btoa(n));t.headers.authorization="Basic ".concat(r)}var n}function J(e,t,r){var n=r[t];if(!n)throw new P("not found: ".concat(t," page"),404);return e.request({method:"GET",url:n})}function L(e,t){for(var r=function(){var r=o[n];e[r]||(e[r]={});for(var a=function(){var n=i[u],o=t[r][n];if(o.alias){var a=v(o.alias.split("."),2),c=a[0],s=a[1];o=t[c][s]}var f=["accepts","method","url","headers"].reduce((function(e,t){return t in o&&(e[t]=o[t]),e}),{});f.request={validate:o.params};var l=e.request.defaults(f);if(o.deprecated)return e[r][n]=function(){return console.log(" %s  %s ","DEPRECATION WARNING:","".concat(f.method," ").concat(f.url)),e[r][n]=l,l.apply(void 0,arguments)},"continue";e[r][n]=l},u=0,i=Object.keys(t[r]);u<i.length;u++)a()},n=0,o=Object.keys(t);n<o.length;n++)r()}function M(e,t){var r=t.request.validate;if(r)for(var n=0,o=Object.keys(r);n<o.length;n++){var a=o[n],u=r[a],i=u.type,c=t[a],s=void 0!==c;if(u.required||s){if(u.required&&!s)throw new P("parameter required: '".concat(a,"'"),400);if("integer"===i){var f=c;if(c=parseInt(c,10),isNaN(c))throw new P("invalid value for parameter '".concat(a,"': ").concat(JSON.stringify(f)," is NaN"),400)}if("boolean"===i)if(!("boolean"==typeof c))throw new P("invalid value for parameter '".concat(a,"': ").concat(JSON.stringify(c)),400);if(u.enum&&!u.enum.includes(c))throw new P("invalid value for parameter '".concat(a,"': ").concat(JSON.stringify(c)),400)}}}var G=[function(e,t){var r=t.notice;(void 0===r||r)&&console.log(" %s  %s ","BITBUCKET CLOUD API LATEST UPDATES:","https://developer.atlassian.com/cloud/bitbucket")},function(e,t){if(t.auth){!function(e){if(!("token"in e||e.username&&e.password))throw new Error('Invalid "auth" option: '.concat(JSON.stringify(e)))}(t.auth);var r={client:e,auth:t.auth};e.requestHook.before(D.bind(null,r))}},function(e){e.hasNextPage=function(e){var t=e.next;return Boolean(t)},e.getNextPage=J.bind(null,e,"next"),e.hasPreviousPage=function(e){var t=e.previous;return Boolean(t)},e.getPreviousPage=J.bind(null,e,"previous")},function(e){e.registerEndpoints=L.bind(null,e)},function(e){e.requestHook.before(M.bind(null,e))}],H=_.plugins(G);export{H as Bitbucket}; //# sourceMappingURL=minimal.js.map