Skip to content

Commit

Permalink
feat: drop urijs (#167)
Browse files Browse the repository at this point in the history
  • Loading branch information
P0lip committed Mar 23, 2022
1 parent 998cbf5 commit 9ebc100
Show file tree
Hide file tree
Showing 81 changed files with 2,070 additions and 4,635 deletions.
43 changes: 25 additions & 18 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,42 +28,49 @@
"test": "jest"
},
"dependencies": {
"@stoplight/json": "^3.10.2",
"@stoplight/json": "^3.17.2",
"@stoplight/types": "^12.4.0",
"@types/swagger-schema-official": "~2.0.21",
"@types/to-json-schema": "^0.2.0",
"@types/type-is": "^1.6.3",
"@types/urijs": "~1.19.9",
"json-schema": "^0.4.0",
"json-schema-generator": "^2.0.6",
"lodash": "^4.17.15",
"lodash": "^4.17.21",
"lodash.isequal": "^4.5.0",
"lodash.omit": "^4.5.0",
"lodash.pick": "^4.4.0",
"lodash.pickby": "^4.6.0",
"openapi3-ts": "^2.0.1",
"postman-collection": "^4.1.0",
"type-is": "^1.6.18",
"urijs": "~1.19.2"
"tslib": "^2.3.1",
"type-is": "^1.6.18"
},
"devDependencies": {
"@stoplight/eslint-config": "^1.1.0",
"@stoplight/eslint-config": "^3.0.0",
"@stoplight/scripts": "^8.2.1",
"@stoplight/test-utils": "^0.0.1",
"@types/jest": "26.0.3",
"@types/json-schema": "7.0.5",
"@types/lodash": "4.14.157",
"@types/lodash": "^4.14.180",
"@types/lodash.isequal": "^4.5.5",
"@types/lodash.omit": "^4.5.6",
"@types/lodash.pick": "^4.4.6",
"@types/lodash.pickby": "^4.6.6",
"@types/postman-collection": "^3.5.3",
"@typescript-eslint/eslint-plugin": "^3.5.0",
"@typescript-eslint/parser": "^3.5.0",
"eslint": "^7.3.1",
"eslint-plugin-import": "^2.22.0",
"eslint-plugin-jest": "^23.17.1",
"eslint-plugin-prettier": "^3.1.4",
"eslint-plugin-react": "^7.20.3",
"eslint-plugin-react-hooks": "^4.0.5",
"eslint-plugin-simple-import-sort": "^5.0.3",
"@typescript-eslint/eslint-plugin": "^5.15.0",
"@typescript-eslint/parser": "^5.15.0",
"eslint": "^8.11.0",
"eslint-plugin-import": "^2.25.4",
"eslint-plugin-jest": "^26.1.1",
"eslint-plugin-prettier": "^4.0.0",
"eslint-plugin-react": "^7.29.4",
"eslint-plugin-react-hooks": "^4.3.0",
"eslint-plugin-simple-import-sort": "^7.0.0",
"jest": "26.1.0",
"prettier": "^2.0.5",
"prettier": "^2.6.0",
"ts-jest": "26.1.1",
"ts-node": "^8.10.2",
"typescript": "3.9.6",
"typescript": "4.6.2",
"utility-types": "^3.10.0"
},
"lint-staged": {
Expand Down
120 changes: 1 addition & 119 deletions src/__tests__/utils.test.ts
Original file line number Diff line number Diff line change
@@ -1,122 +1,4 @@
import * as urijs from 'urijs';

import { maybeResolveLocalRef, URI } from '../utils';

describe('URI()', () => {
it('instantiates from string', () => {
const url = 'test:///path/to/api.yaml#/the/pointer';

expect(URI(url).toString()).toEqual(url);
});

it('instantiates from urijs', () => {
const url = 'test:///path/to/api.yaml#/the/pointer';

expect(URI(urijs(url)).toString()).toEqual(url);
});

describe('type()', () => {
it('sets type', () => {
const url = 'test:///path/to/api.yaml#/the/pointer';

expect(URI(url).scheme('test2').toString()).toEqual('test2:///path/to/api.yaml#/the/pointer');
});
});

describe('host()', () => {
it('sets host', () => {
const url = 'test:///path/to/api.yaml#/the/pointer';

expect(URI(url).host('test2.com').toString()).toEqual('test://test2.com/path/to/api.yaml#/the/pointer');
});

it('sets host and port', () => {
const url = 'test:///path/to/api.yaml#/the/pointer';

expect(URI(url).host('test2:3000').toString()).toEqual('test://test2:3000/path/to/api.yaml#/the/pointer');
});

it('sets host and port (non-number port does not error)', () => {
const url = 'test:///path/to/api.yaml#/the/pointer';

expect(URI(url).host('test2:{$$.env.host}').toString()).toEqual(
'test://test2:{$$.env.host}/path/to/api.yaml#/the/pointer',
);
});
});

describe('port()', () => {
it('sets port', () => {
const url = 'test:///path/to/api.yaml#/the/pointer';

expect(URI(url).host('test2').port('3000').toString()).toEqual('test://test2:3000/path/to/api.yaml#/the/pointer');
});

it('does not error on invalid port', () => {
const url = 'test:///path/to/api.yaml#/the/pointer';

expect(URI(url).host('test2').port('invalid').toString()).toEqual('test://test2/path/to/api.yaml#/the/pointer');
});
});

describe('path()', () => {
it('sets path', () => {
const url = 'test:///path/to/api.yaml#/the/pointer';

expect(URI(url).path('/path/to/other/api.yaml').toString()).toEqual(
'test:///path/to/other/api.yaml#/the/pointer',
);
});
});

describe('pointer()', () => {
describe('pointer is a string', () => {
it('sets pointer', () => {
const url = 'test:///path/to/api.yaml#/the/pointer';

expect(URI(url).pointer('#/new/pointer').toString()).toEqual('test:///path/to/api.yaml#/new/pointer');
});
});

describe('pointer is a path', () => {
it('sets pointer', () => {
const url = 'test:///path/to/api.yaml#/the/pointer';

expect(URI(url).pointer(['new', 'pointer']).toString()).toEqual('test:///path/to/api.yaml#/new/pointer');
});
});
});

describe('append()', () => {
describe('append pointer to uri having a pointer', () => {
it('replaces pointer', () => {
expect(URI('file:///path/to/api.yaml#/some/pointer').append('#/some/other/pointer').toString()).toEqual(
'file:///path/to/api.yaml#/some/other/pointer',
);
});
});

describe('append element to uri having a pointer', () => {
it('appends element to pointer', () => {
expect(URI('file:///path/to/api.yaml#/some/pointer').append('inner').toString()).toEqual(
'file:///path/to/api.yaml#/some/pointer/inner',
);
});
});

describe('append pointer to uri not having a pointer', () => {
it('appends pointer to uri', () => {
expect(URI('file:///path/to/api.yaml').append('#/some').toString()).toEqual('file:///path/to/api.yaml#/some');
});
});

describe('append element to uri not having a pointer', () => {
it('appends element to uri', () => {
expect(URI('file:///path/to').append('api.yaml').toString()).toEqual('file:///path/to/api.yaml');
});
});
});
});
import { maybeResolveLocalRef } from '../utils';

describe('maybeResolveLocalRef()', () => {
it('follows $refs', () => {
Expand Down
9 changes: 9 additions & 0 deletions src/context.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { TransformerContext } from './types';
import { maybeResolveLocalRef } from './utils';

export function createContext<T extends Record<string, unknown>>(document: T): TransformerContext<T> {
return {
document,
maybeResolveLocalRef: maybeResolveLocalRef.bind(null, document),
};
}
15 changes: 15 additions & 0 deletions src/guards.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
export function isNonNullable<T>(value: T): value is NonNullable<T> {
return value !== undefined && value !== null;
}

export function isBoolean(input: unknown): input is boolean {
return typeof input === 'boolean';
}

export function isString(value: unknown): value is string {
return typeof value === 'string';
}

export function isSerializablePrimitive(value: unknown): value is string | number | boolean | null {
return isBoolean(value) || isString(value) || typeof value === 'number' || value === null;
}
5 changes: 5 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export * from './oas';
export * from './oas2';
export * from './oas3';
export * from './postman';
export * from './types';
2 changes: 1 addition & 1 deletion src/merge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
IServer,
} from '@stoplight/types';
import type { JSONSchema7 as JSONSchema } from 'json-schema';
import { isEqual } from 'lodash';
import isEqual = require('lodash.isequal');

function isExclusivelyAnyOfSchema(schema: JSONSchema): schema is { anyOf: JSONSchema[] } {
return !!(schema.anyOf && Object.keys(schema).length === 1);
Expand Down
54 changes: 20 additions & 34 deletions src/oas/__tests__/accessors.test.ts
Original file line number Diff line number Diff line change
@@ -1,45 +1,46 @@
import { getOasTags, getValidOasParameters } from '../accessors';
import { getValidOasParameters } from '../accessors';

describe('getOasParameters', () => {
it('should return empty array', () => {
expect(getValidOasParameters({}, undefined, undefined)).toEqual([]);
expect(getValidOasParameters({}, 'oas2', undefined, undefined)).toEqual([]);
});

it('should fallback to operation parameters', () => {
expect(
getValidOasParameters(
{},
'oas2',
[
{ name: 'n1', in: 'i1' },
{ name: 'n2', in: 'i2' },
{ name: 'n1', in: 'header' },
{ name: 'n2', in: 'query' },
],
undefined,
),
).toEqual([
{
in: 'i1',
in: 'header',
name: 'n1',
},
{
in: 'i2',
in: 'query',
name: 'n2',
},
]);
});

it('should fallback to path parameters', () => {
expect(
getValidOasParameters({}, undefined, [
{ name: 'n1', in: 'i1' },
{ name: 'n2', in: 'i2' },
getValidOasParameters({}, 'oas2', undefined, [
{ name: 'n1', in: 'header' },
{ name: 'n2', in: 'query' },
]),
).toEqual([
{
in: 'i1',
in: 'header',
name: 'n1',
},
{
in: 'i2',
in: 'query',
name: 'n2',
},
]);
Expand All @@ -49,45 +50,30 @@ describe('getOasParameters', () => {
expect(
getValidOasParameters(
{},
'oas3',
[
{ name: 'n1', in: 'n1', type: 'array' },
{ name: 'no2', in: 'io2' },
{ name: 'n1', in: 'query', type: 'array' },
{ name: 'no2', in: 'header' },
],
[
{ name: 'n1', in: 'n1', type: 'string' },
{ name: 'np3', in: 'ip3' },
{ name: 'n1', in: 'query', type: 'string' },
{ name: 'np3', in: 'header' },
],
),
).toEqual([
{
in: 'n1',
in: 'query',
name: 'n1',
type: 'array',
},
{
in: 'io2',
in: 'header',
name: 'no2',
},
{
in: 'ip3',
in: 'header',
name: 'np3',
},
]);
});
});

describe('getOasTags', () => {
describe.each([2, null, {}, '', 0])('when tags property is not an array', tags => {
it('should return empty array', () => {
expect(getOasTags(tags)).toEqual([]);
});
});

it('should filter out invalid values', () => {
expect(getOasTags([{}, null, 'foo'])).toEqual(['foo']);
});

it('should normalize values', () => {
expect(getOasTags([0, 'foo', true])).toEqual(['0', 'foo', 'true']);
});
});
48 changes: 25 additions & 23 deletions src/oas/__tests__/operation.test.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,26 @@
import { OpenAPIObject } from 'openapi3-ts';
import { Spec } from 'swagger-schema-official';
// import { OpenAPIObject } from 'openapi3-ts';
// import { Spec } from 'swagger-schema-official';
//
// import { transformOas2Operations } from '../../oas2/operation';
// import { transformOas3Operations } from '../../oas3/operation';
//
// const oas2KitchenSinkJson: Spec = require('./__fixtures__/oas2-kitchen-sink.json');
// const oas3KitchenSinkJson: OpenAPIObject = require('./__fixtures__/oas3-kitchen-sink.json');
//
// describe('oas operation', () => {
// it('openapi v2', () => {
// const result = transformOas2Operations(oas2KitchenSinkJson);
//
// expect(result).toHaveLength(5);
// expect(result).toMatchSnapshot();
// });
//
// it('openapi v3', () => {
// const result = transformOas3Operations(oas3KitchenSinkJson);
//
// expect(result).toHaveLength(3);
// expect(result).toMatchSnapshot();
// });
// });

import { transformOas2Operations } from '../../oas2/operation';
import { transformOas3Operations } from '../../oas3/operation';

const oas2KitchenSinkJson: Spec = require('./fixtures/oas2-kitchen-sink.json');
const oas3KitchenSinkJson: OpenAPIObject = require('./fixtures/oas3-kitchen-sink.json');

describe('oas operation', () => {
it('openapi v2', () => {
const result = transformOas2Operations(oas2KitchenSinkJson);

expect(result).toHaveLength(5);
expect(result).toMatchSnapshot();
});

it('openapi v3', () => {
const result = transformOas3Operations(oas3KitchenSinkJson);

expect(result).toHaveLength(3);
expect(result).toMatchSnapshot();
});
});
test.todo('acx');
Loading

0 comments on commit 9ebc100

Please sign in to comment.