UNPKG

@har-sdk/core

Version:

The base package can be used to import specification files (i.e. HAR, OAS and Postman Collection) and detect their type.

3 lines (2 loc) 6.5 kB
var t,e;t=this,e=function(t,e,r){class s{constructor(t){this.syntaxErrorDetailsExtractor=t}load(t){this.source=t,this.error=void 0;try{return this.parse()}catch(t){throw t instanceof Error&&(this.error=t),t}}getSyntaxErrorDetails(){return this.error&&(this.isSupportedError(this.error)?this.syntaxErrorDetailsExtractor.extract(this.error,this.source):{message:this.error.message})}}class o{get lineOffsets(){if(!this._lineOffsets){let t=0;this._lineOffsets=[0,...(this.source||"").split("\n").map((e=>t+=e.length+1))]}return this._lineOffsets}extract(t,e){this.source=e,this._lineOffsets=void 0;const r=this.extractMessage(t),s=this.extractOffset(t),o=this.extractSnippet(t);return{message:r,...void 0!==s?{offset:s}:{},...void 0!==o?{snippet:o}:{}}}extractSnippet(t){}extractMessage(t){return t.message}calculateOffset(t,e){return t<=this.lineOffsets.length?this.lineOffsets[t-1]+e-1:this.source.length}}class n extends o{constructor(){super(...arguments),this.LOCATION_PATTERNS=[{pattern:/at position (\d+)$/,positionExtractor:([,t])=>+t},{pattern:/at line (\d+) column (\d+)/,positionExtractor:([,t,e])=>({lineNumber:+t,columnNumber:+e})}]}extractOffset(t){const e="lineNumber"in t&&"columnNumber"in t?t:this.extractByLocationPatterns(t.message);return"number"==typeof e?e:e&&this.calculateOffset(e.lineNumber,e.columnNumber)}extractMessage(t){return t.message.replace(/^JSON.parse: /,"").replace(/ at line \d+ column \d+ of the JSON data$/,"").replace(/ at position \d+$/,"")}extractByLocationPatterns(t){for(let e=0;e<this.LOCATION_PATTERNS.length;++e){const r=this.LOCATION_PATTERNS[e],s=t.match(r.pattern);if(s)return r.positionExtractor(s)}}}class i extends o{constructor(){super(...arguments),this.LOCATION_PATTERN=/\((\d+):(\d+)\)$/}extractMessage(t){const[e]=t.message.split("\n");return e.replace(/^YAMLException: /,"").replace(this.LOCATION_PATTERN,"").trim()}extractOffset(t){const[e]=t.message.split("\n"),r=e.match(this.LOCATION_PATTERN);return r?this.calculateOffset(+r[1],+r[2]):void 0}extractSnippet(t){return t.message.split("\n").slice(2).join("\n")}}class a extends s{constructor(){super(new n)}parse(){return JSON.parse(this.source)}isSupportedError(t){return t instanceof SyntaxError}}class c extends s{constructor(){super(new i)}parse(){return e.load(this.source,{json:!0})}isSupportedError(t){return t instanceof e.YAMLException}}class l{getLoader(t){switch(t){case"json":return new a;case"yaml":return new c}}}class p{constructor(){this.docFormats=["json","yaml"],this.loaderFactory=new l,this.loaders=new Map}async import(t,e){const r=this.load(t.trim(),e);if(r&&this.isSupported(r.doc)){const{format:t,doc:e}=r;return{doc:e,name:this.fileName({format:t,doc:e}),format:t,type:this.type}}}getErrorDetails(t){var e;return null===(e=this.getLoader(t))||void 0===e?void 0:e.getSyntaxErrorDetails()}fileName(t){}load(t,e){return this.initLoaders(),this.docFormats.reduce(((r,s)=>{if(r||e&&e!==s)return r;try{const e=this.getLoader(s).load(t);if(e)return{doc:e,format:s}}catch(t){}}),void 0)}initLoaders(){this.loaders.clear(),this.docFormats.forEach((t=>this.loaders.set(t,this.loaderFactory.getLoader(t))))}getLoader(t){return this.loaders.get(t)}}var u;t.ImporterType=void 0,(u=t.ImporterType||(t.ImporterType={})).HAR="har",u.OASV3="oasv3",u.OASV2="oasv2",u.POSTMAN="postman";class h extends p{constructor(){super(),this.SUPPORTED_HAR_VERSION=/^1\.\d+$/}get type(){return t.ImporterType.HAR}isSupported(t){var e;const r=null===(e=null==t?void 0:t.log)||void 0===e?void 0:e.version;return!("string"!=typeof r||!this.SUPPORTED_HAR_VERSION.test(r.trim())||!Array.isArray(t.log.entries))}}class m extends p{constructor(){super()}fileName({doc:t,format:e}){var r;if("string"==typeof(null===(r=null==t?void 0:t.info)||void 0===r?void 0:r.title)&&"string"==typeof t.info.version)return`${[t.info.title,t.info.version].map((t=>t.trim())).join(" ")}.${e}`}}class d extends m{constructor(){super(),this.SUPPORTED_SWAGGER_VERSION="2.0"}get type(){return t.ImporterType.OASV2}isSupported(t){const e=null==t?void 0:t.swagger;return"string"==typeof e&&e.trim()===this.SUPPORTED_SWAGGER_VERSION}}class f extends m{constructor(){super(),this.SUPPORTED_OPENAPI_VERSION=/^3\.[01]\.\d+$/}get type(){return t.ImporterType.OASV3}isSupported(t){const e=null==t?void 0:t.openapi;return"string"==typeof e&&this.SUPPORTED_OPENAPI_VERSION.test(e.trim())}}class g extends p{constructor(){super(),this.POSTMAN_SCHEMAS=new Set(["https://schema.getpostman.com/json/collection/v2.0.0/collection.json","https://schema.getpostman.com/json/collection/v2.1.0/collection.json"])}get type(){return t.ImporterType.POSTMAN}isSupported(t){var e;const r=null===(e=null==t?void 0:t.info)||void 0===e?void 0:e.schema;return"string"==typeof r&&this.POSTMAN_SCHEMAS.has(r.trim().toLowerCase())}fileName({doc:t,format:e}){var r;return"string"==typeof(null===(r=null==t?void 0:t.info)||void 0===r?void 0:r.name)?`${t.info.name.trim()}.${e}`:void 0}}const y=t=>t.replace(/\/$/,""),O=t=>{const e=new URL(t);if(!S(e))throw new TypeError(`Invalid URL: ${e}`);return e},S=t=>{let e;try{e="string"==typeof t?O(t):t}catch{}return!!(e&&e.hostname&&e.origin&&"null"!==e.origin)};t.HARImporter=h,t.OASV2Importer=d,t.OASV3Importer=f,t.PostmanImporter=g,t.SpecImporter=class{constructor(t=[new h,new f,new g,new d]){this.importers=t}async import(t,e){let r;try{const s=this.importers.map((r=>r.import(t,e)));r=await((t,e)=>{const r=t.map((t=>new Promise(((r,s)=>t.then((t=>!!e(t)&&r(t)),s)))));return r.push(Promise.all(t).then((()=>{}))),Promise.race(r)})(s,(t=>!!t))}catch{}return r}},t.normalizeUrl=t=>{const e=O((t=>(!/^\/{2}/.test(t)&&/^\.+\//.test(t)||(t=t.replace(/^(?!(?:\w+:)?\/\/)|^\/\//,"https://")),t))(t));try{e.pathname=e.pathname.replace(/((?!:).|^)\/{2,}/g,((t,e)=>/^(?!\/)/g.test(e)?`${e}/`:"/"))}catch{}e.searchParams.sort(),e.hostname=e.hostname.replace(/\.$/,"");let r=e.toString();return"/"===e.pathname&&""===e.hash&&(r=y(r)),r},t.parseUrl=O,t.removeLeadingSlash=t=>t.replace(/^\//,""),t.removeTrailingSlash=y,t.validateUrl=S,Object.keys(r).forEach((function(e){"default"===e||t.hasOwnProperty(e)||Object.defineProperty(t,e,{enumerable:!0,get:function(){return r[e]}})}))},"object"==typeof exports&&"undefined"!=typeof module?e(exports,require("js-yaml"),require("openapi-types")):"function"==typeof define&&define.amd?define(["exports","js-yaml","openapi-types"],e):e((t="undefined"!=typeof globalThis?globalThis:t||self)["@har-sdk/core"]={},t.jsYaml,t.openapiTypes); //# sourceMappingURL=bundle.umd.js.map