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.04 kB
import{load as t,YAMLException as e}from"js-yaml";export*from"openapi-types";class r{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 s{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 o extends s{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 n extends s{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 i extends r{constructor(){super(new o)}parse(){return JSON.parse(this.source)}isSupportedError(t){return t instanceof SyntaxError}}class a extends r{constructor(){super(new n)}parse(){return t(this.source,{json:!0})}isSupportedError(t){return t instanceof e}}class c{getLoader(t){switch(t){case"json":return new i;case"yaml":return new a}}}class l{constructor(){this.docFormats=["json","yaml"],this.loaderFactory=new c,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 p;!function(t){t.HAR="har",t.OASV3="oasv3",t.OASV2="oasv2",t.POSTMAN="postman"}(p||(p={}));class u extends l{constructor(){super(),this.SUPPORTED_HAR_VERSION=/^1\.\d+$/}get type(){return p.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 h extends l{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 h{constructor(){super(),this.SUPPORTED_SWAGGER_VERSION="2.0"}get type(){return p.OASV2}isSupported(t){const e=null==t?void 0:t.swagger;return"string"==typeof e&&e.trim()===this.SUPPORTED_SWAGGER_VERSION}}class m extends h{constructor(){super(),this.SUPPORTED_OPENAPI_VERSION=/^3\.[01]\.\d+$/}get type(){return p.OASV3}isSupported(t){const e=null==t?void 0:t.openapi;return"string"==typeof e&&this.SUPPORTED_OPENAPI_VERSION.test(e.trim())}}class f extends l{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 p.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 g=t=>t.replace(/\/$/,""),O=t=>t.replace(/^\//,""),S=t=>{const e=new URL(t);if(!E(e))throw new TypeError(`Invalid URL: ${e}`);return e},E=t=>{let e;try{e="string"==typeof t?S(t):t}catch{}return!!(e&&e.hostname&&e.origin&&"null"!==e.origin)},y=t=>{const e=S((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=g(r)),r};class v{constructor(t=[new u,new m,new f,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}}export{u as HARImporter,p as ImporterType,d as OASV2Importer,m as OASV3Importer,f as PostmanImporter,v as SpecImporter,y as normalizeUrl,S as parseUrl,O as removeLeadingSlash,g as removeTrailingSlash,E as validateUrl}; //# sourceMappingURL=bundle.esm5.js.map