UNPKG

9.7 kBJavaScriptView Raw
1import{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";import a from"btoa-lite";var u={method:"GET",baseUrl:"".concat("https","://").concat("api.bitbucket.org").concat("/2.0"),headers:{accept:"application/json","user-agent":"bitbucket.js/".concat("2.0.3")}};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(e,t,r){return(s=function(){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}}()?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 s(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 f(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 d(e){if(void 0===e)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return e}function p(e,t){return function(e){if(Array.isArray(e))return e}(e)||function(e,t){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)||function(){throw new TypeError("Invalid attempt to destructure non-iterable instance")}()}function h(){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 v(e,n,o){if("string"==typeof n){var a=p(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=h(o.headers),t.all([e,o].filter(Boolean),{isMergeableObject:r})}function b(e){var t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{},r=/\?/.test(e)?"&":"?",n=Object.keys(t);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("+")):"".concat(e,"=").concat(encodeURIComponent(t[e]))}).join("&"))}var m=/\{[^}]+\}/g;function y(e){return e.replace(/^\W+|\W+$/g,"").split(/,/)}var g={formData:"multipart/form-data",urlEncoded:"application/x-www-form-urlencoded",json:"application/json; charset=utf-8"};function w(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=f(e,["accepts","method","baseUrl","url","headers","request"]),l=o.toUpperCase(),d=function(e){var t=e.match(m);return t?t.map(y).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 d.includes(t)||(e[t]=s[t]),e},{}),w=v._body,O=f(v,["_body"]),j=!1;if(["GET","DELETE"].includes(l))p=b(p,O);else if(void 0!==w){if((j=/form-?data/i.test((h=w).constructor.name))&&r.includes(g.formData))for(var E=0,k=Object.keys(O);E<k.length;E++){var q=k[E];h.append(q,O[q])}}else Object.keys(O).length&&(h=O);return j||(r.includes(g.urlEncoded)?(h=b("",h).substring(1),i["content-type"]=g.urlEncoded):(h=JSON.stringify(h),i["content-type"]=g.json)),void 0===h&&delete i["content-type"],{method:l,url:p,body:h,headers:i,request:c}}var O=function e(t,r){var n=v(t,r),o=function(e,t,r){return w(v(e,t,r))}.bind(null,n);return o.DEFAULTS=n,o.defaults=e.bind(null,n),o.merge=v.bind(null,n),o.parse=w,o}(null,u),j=function(e){function t(e,r){var n,o,a,u=arguments.length>2&&void 0!==arguments[2]?arguments[2]:{};return function(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}(this,t),o=this,n=!(a=i(t).call(this,e))||"object"!=typeof a&&"function"!=typeof a?d(o):a,Error.captureStackTrace&&Error.captureStackTrace(d(n),n.constructor),n.name="HTTPError",n.error=u.error,n.headers=u.headers,n.request=u.request,n.status=r,n}return 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)}(t,l(Error)),t}();function E(e){var t=e.headers.get("content-type");return/application\/json/.test(t)?e.json():!t||/^text\/|charset=utf-8$/.test(t)?e.text():e.arrayBuffer()}function k(e){var t,r,n=e.method,a=e.url,u=e.headers,i=e.body,c=e.request,s=Object.assign({method:n,body:i,headers:u},c),l={};return(c.fetch||o)(a,s).then(function(n){t=n.status,r=n.url;var o=!0,a=!1,u=void 0;try{for(var i,c=n.headers[Symbol.iterator]();!(o=(i=c.next()).done);o=!0){var s=p(i.value,2),f=s[0],d=s[1];l[f]=d}}catch(e){a=!0,u=e}finally{try{o||null==c.return||c.return()}finally{if(a)throw u}}return n.status>=400||[304].includes(n.status)?E(n).then(function(r){throw new j(n.statusText,t,{error:r,headers:l,request:e})}):E(n)}).then(function(e){return{data:e,headers:l,status:t,url:r}}).catch(function(t){if(t instanceof j)throw t;throw new j(t.message,500,{headers:l,request:e})})}var q=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 k(n.parse(e))},r):k(n.parse(r))}return o.defaults=e.bind(null,n),o.endpoint=n,o}(O,{});function T(e,n){e.headers=h(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 I(){var t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:[],r=function(t){var r=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{},n=new e,o={request:q.defaults(T(r,n)),requestHook:n};return t.forEach(function(e){e(o,r)}),o}.bind(null,t);return r.plugins=function(){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)}),I(r)}.bind(null,t),r}function N(e,t,r){var n=r[t];if(!n)throw new j("not found: ".concat(t," page"),404);return e.request({method:"GET",url:n})}var P=[function(e,t){var r=t.notice;(void 0===r||r)&&(console.log(" %s  %s ","BITBUCKET CLOUD API CHANGING NOTICE:","https://developer.atlassian.com/cloud/bitbucket/bitbucket-api-changes-gdpr"),console.log(" %s  %s ","BITBUCKET CLOUD API MIGRATION GUIDE:","https://developer.atlassian.com/cloud/bitbucket/bbc-gdpr-api-migration-guide"),console.log(" %s  %s ","BITBUCKET CLOUD API REPO URL CHANGE:","https://developer.atlassian.com/cloud/bitbucket/bitbucket-api-changes-workspaces"))},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(function(e,t){if("token"in e.auth)t.headers.authorization="Bearer ".concat(e.auth.token);else if(e.auth.username){var r=a("".concat(e.auth.username,":").concat(e.auth.password));t.headers.authorization="Basic ".concat(r)}}.bind(null,r))}},function(e){e.hasNextPage=function(e){var t=e.next;return Boolean(t)},e.getNextPage=N.bind(null,e,"next"),e.hasPreviousPage=function(e){var t=e.previous;return Boolean(t)},e.getPreviousPage=N.bind(null,e,"previous")},function(e){e.registerEndpoints=function(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=p(o.alias.split("."),2),c=a[0],s=a[1];o=t[c][s]}var l=["accepts","method","url","headers"].reduce(function(e,t){return t in o&&(e[t]=o[t]),e},{});l.request={validate:o.params};var f=e.request.defaults(l);if(o.deprecated)return e[r][n]=function(){return console.log(" %s  %s ","DEPRECATION WARNING:","".concat(l.method," ").concat(l.url)),e[r][n]=f,f.apply(void 0,arguments)},"continue";e[r][n]=f},u=0,i=Object.keys(t[r]);u<i.length;u++)a()},n=0,o=Object.keys(t);n<o.length;n++)r()}.bind(null,e)},function(e){e.requestHook.before(function(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 j("parameter required: '".concat(a,"'"),400);if("integer"===i){var l=c;if(c=parseInt(c,10),isNaN(c))throw new j("invalid value for parameter '".concat(a,"': ").concat(JSON.stringify(l)," is NaN"),400)}if("boolean"===i&&"boolean"!=typeof c)throw new j("invalid value for parameter '".concat(a,"': ").concat(JSON.stringify(c)),400);if(u.enum&&!u.enum.includes(c))throw new j("invalid value for parameter '".concat(a,"': ").concat(JSON.stringify(c)),400)}}}.bind(null,e))}],x=I().plugins(P);export{x as Bitbucket};
2//# sourceMappingURL=minimal.js.map