@ har- sdk/ 核心
基包可用于导入规格文件(即HAR,OAS和Postman Collection)并检测其类型.
设置
npm i --save @har-sdk/core
使用量
要导入一个规格, 您只需要创建一个实例 SpecImporter 并称为 import 方法通过数据文件。 进口商进行综合分析,并分析所提供的文件.
import { SpecImporter } from '@har-sdk/core';
const result = await new SpecImporter().import(sourceAsString);
console.log(result);
// {
// type: 'postman',
// format: 'json',
// doc: {
// info: {
// name: 'Postman Sample',
// schema: 'https://schema.getpostman.com/json/collection/v2.1.0/collection.json'
// },
// item: [
// // ...
// ]
// },
// name: 'Postman Sample.json'
// }
要配置导入者列表, 您可以把它们作为数组传递到构建器 .
import { SpecImporter, HarImporter } from '@har-sdk/core';
const explorer = new SpecImporter([new HarImporter()]);
要通过添加一个新的自定义导入器来扩展探索者,您可以轻松执行 Importer 接口.
import { Importer, Doc, Spec, Importer } from '@har-sdk/core';
class RamlImporter implements Importer<'raml'> {
get type(): 'raml' {
return 'raml';
}
async import(content: string): Promise<Spec<'raml'>> {
// your code
return {
// other fields
type: this.type,
format: 'yaml'
};
}
}
这套软件包还包含一套有用的公用事业,例如: normalizeUrl数字 :
import { normalizeUrl } from '@har-sdk/core';
normalizeUrl('HTTP://example.COM////foo////dummy/../bar/?'); // http://example.com/foo/bar