Skip to content

Commit

Permalink
feat(core): new utilities (#91)
Browse files Browse the repository at this point in the history
closes #90
  • Loading branch information
pmstss authored Jan 12, 2022
1 parent c29d745 commit f1ddeeb
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 14 deletions.
19 changes: 13 additions & 6 deletions packages/core/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,23 +14,22 @@ To import a specification you just need to create an instance of `SpecImporter`

```ts
import { SpecImporter } from '@har-sdk/core';
import petstore from './petstore.collection.json';

const result = await new SpecImporter().tryToImportSpec(petstore);
const result = await new SpecImporter().import(sourceAsString);
console.log(result);
// {
// type: 'postman',
// format: 'json',
// doc: {
// info: {
// name: 'Swagger Petstore',
// name: 'Postman Sample',
// schema: 'https://schema.getpostman.com/json/collection/v2.1.0/collection.json'
// },
// item: [
// // ...
// ]
// },
// name: 'Swagger Petstore.json'
// name: 'Postman Sample.json'
// }
```

Expand All @@ -54,12 +53,20 @@ class RamlImporter implements Importer<'raml'> {

async import(content: string): Promise<Spec<'raml'>> {
// your code

return {
// other fields
type: this.type,
format: 'yaml'
}
};
}
}
```

The package also contains a set of useful utilities like `normalizeUrl`:

```ts
import { normalizeUrl } from '@har-sdk/core';

normalizeUrl('HTTP://example.COM////foo////dummy/../bar/?'); // http://example.com/foo/bar
```
4 changes: 1 addition & 3 deletions packages/core/src/utils/url.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import type { URL as URLType } from 'url';

const URL =
typeof (global as any).window !== 'undefined'
? (global as any).window.URL
Expand Down Expand Up @@ -55,4 +53,4 @@ export const normalizeUrl = (value: string): string => {
return urlString;
};

export const parseUrl = (value: string): URLType => new URL(value);
export const parseUrl = (value: string): typeof URL => new URL(value);
10 changes: 5 additions & 5 deletions packages/core/tests/normalize-url.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import 'chai/register-should';
import { normalizeUrl } from '../src/utils';
import { normalizeUrl } from '../src';

describe('normalizeUrl', () => {
[
Expand All @@ -11,10 +11,6 @@ describe('normalizeUrl', () => {
input: 'http://example.com/pets/?offset=10&limit=10',
output: 'http://example.com/pets?limit=10&offset=10'
},
{
input: 'http://example.com/pets/?offset=10&limit=10',
output: 'http://example.com/pets?limit=10&offset=10'
},
{
input: 'HTTP://example.com',
output: 'http://example.com'
Expand All @@ -38,6 +34,10 @@ describe('normalizeUrl', () => {
{
input: 'https://user:[email protected]',
output: 'https://user:[email protected]'
},
{
input: 'HTTP://example.COM////foo////dummy/../bar/?',
output: 'http://example.com/foo/bar'
}
].forEach(({ input, output }) =>
it(`should return ${output} for ${input}`, () => {
Expand Down

0 comments on commit f1ddeeb

Please sign in to comment.