Skip to content

Commit

Permalink
feat: SO-141 Problem+Json for error messages SO-141 (#270)
Browse files Browse the repository at this point in the history
* chore: update deps

* chore: update typescript

* feat: create problemJson constructor

* feat: create serializer for problem/json

* feat: return problem jsons for route errors

* refactor: move problemjson to http package

* test: check with error title

* refactor: use typescript inferring and fluent reply

* refactor: prefer unknown

* feat: replace 400 error for missing invalid response with problem/json

* feat: replace 400 error for missing invalid response with problem/json

* refactor: from template static function

* test: refactor test to use template functin

* refactor: correctly type problem+json

* refactor: no any

* refactor: avoid casts

* refactor: use tsling-stoplight rules

* chore: graphite update

* chore: .x.x

* refactor: do not be too specific on what's wrong

* fix: correct status code is 422

* refactor: use template ProblemJson

Co-Authored-By: XVincentX <vincenz.chianese@icloud.com>

* refactor: UNPROCESSABLE_ENTITY

* refactor: use https link errors

* refactor: remove custom serialiser

* chore: caret

* style: format

* refactor: detail is always set

* refactor: bring url logic in the class
  • Loading branch information
XVincentX authored May 8, 2019
1 parent de32fb0 commit a5a3a67
Show file tree
Hide file tree
Showing 70 changed files with 1,311 additions and 1,518 deletions.
23 changes: 11 additions & 12 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,17 @@
"release": "lerna version prerelease --conventional-commit"
},
"devDependencies": {
"@stoplight/types": "4.x.x",
"@types/chance": "1.0.x",
"@types/jest": "24.x.x",
"@types/node": "11.x.x",
"chance": "1.0.x",
"jest": "24.x.x",
"lerna": "3.13.3",
"ts-jest": "24.x.x",
"tslint": "5.16.0",
"tslint-config-stoplight": "1.2.x",
"tslint-eslint-rules": "5.4.x",
"typescript": "3.x.x"
"@stoplight/types": "^4.0.0",
"@types/chance": "^1.0.0",
"@types/jest": "^24.0.0",
"@types/node": "^12.0.0",
"chance": "^1.0.0",
"jest": "^24.0.0",
"lerna": "^3.0.0",
"ts-jest": "^24.0.0",
"tslint": "^5.0.0",
"tslint-config-stoplight": "^1.0.0",
"typescript": "^3.0.0"
},
"prettier": {
"printWidth": 100,
Expand Down
24 changes: 12 additions & 12 deletions packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,22 @@
},
"bugs": "https://github.com/stoplightio/prism/issues",
"dependencies": {
"@oclif/command": "1.x.x",
"@oclif/config": "1.12.12",
"@oclif/plugin-help": "2.x.x",
"@oclif/command": "^1.0.0",
"@oclif/config": "^1.12.12",
"@oclif/plugin-help": "^2.0.0",
"@stoplight/prism-core": "^3.0.0-pre.2",
"@stoplight/prism-http-server": "^3.0.0-pre.2",
"tslib": "1.x.x"
"tslib": "^1.0.0"
},
"devDependencies": {
"@oclif/dev-cli": "1.22.0",
"@oclif/tslint": "3.x.x",
"@types/chai": "4.x.x",
"@types/node": "11.13.4",
"globby": "9.2.0",
"ts-node": "8.1.0",
"tsconfig-paths": "^3.8.0",
"typescript": "^3.4.2"
"@oclif/dev-cli": "^1.0.0",
"@oclif/tslint": "^3.0.0",
"@types/chai": "^4.0.0",
"@types/node": "^12.0.0",
"globby": "^9.0.0",
"ts-node": "^8.0.0",
"tsconfig-paths": "^3.0.0",
"typescript": "^3.0.5"
},
"engines": {
"node": ">=8.0.0"
Expand Down
7 changes: 2 additions & 5 deletions packages/cli/src/util/__tests__/createServer.unit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,15 @@ describe('server command', () => {
test('starts filesystem server variant', async () => {
createServer('/path/to', { mock: true });

expect(createPrismServer).toHaveBeenLastCalledWith(
{ path: '/path/to' },
{ config: { mock: true } }
);
expect(createPrismServer).toHaveBeenLastCalledWith({ path: '/path/to' }, { config: { mock: true } });
});

test('starts http server variant', async () => {
createServer('http://path.to/spec.oas2.yaml', { mock: false });

expect(createPrismServer).toHaveBeenLastCalledWith(
{ url: 'http://path.to/spec.oas2.yaml' },
{ components: { loader: expect.any(HttpLoader) }, config: { mock: false } }
{ components: { loader: expect.any(HttpLoader) }, config: { mock: false } },
);
});
});
12 changes: 6 additions & 6 deletions packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@
"node": ">=8"
},
"dependencies": {
"@stoplight/graphite": "5.2.0",
"axios": "0.x.x",
"lodash": "4.x.x",
"mobx": "5.x.x",
"tslib": "1.9.x"
"@stoplight/graphite": "^6.3.1",
"axios": "^0.18.0",
"lodash": "^4.0.0",
"mobx": "^5.0.0",
"tslib": "^1.9.0"
},
"devDependencies": {
"@types/lodash": "4.14.123"
"@types/lodash": "^4.14.123"
},
"publishConfig": {
"access": "public"
Expand Down
27 changes: 13 additions & 14 deletions packages/core/src/factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,20 @@ import { IPrism, IPrismComponents, IPrismConfig, IPrismDiagnostic } from './type

export function factory<Resource, Input, Output, Config, LoadOpts>(
defaultConfig: PrismConfig<Config, Input>,
defaultComponents: Partial<IPrismComponents<Resource, Input, Output, Config, LoadOpts>>
defaultComponents: Partial<IPrismComponents<Resource, Input, Output, Config, LoadOpts>>,
): (
customConfig?: PartialPrismConfig<Config, Input>,
customComponents?: Partial<IPrismComponents<Resource, Input, Output, Config, LoadOpts>>
customComponents?: Partial<IPrismComponents<Resource, Input, Output, Config, LoadOpts>>,
) => IPrism<Resource, Input, Output, Config, LoadOpts> {
const prism = (
customConfig?: PartialPrismConfig<Config, Input>,
customComponents?: Partial<IPrismComponents<Resource, Input, Output, Config, LoadOpts>>
customComponents?: Partial<IPrismComponents<Resource, Input, Output, Config, LoadOpts>>,
) => {
const components: Partial<
IPrismComponents<Resource, Input, Output, Config, LoadOpts>
> = Object.assign({}, defaultComponents, customComponents);
const components: Partial<IPrismComponents<Resource, Input, Output, Config, LoadOpts>> = Object.assign(
{},
defaultComponents,
customComponents,
);

// our loaded resources (HttpOperation objects, etc)
let resources: Resource[] = [];
Expand All @@ -39,10 +41,7 @@ export function factory<Resource, Input, Output, Config, LoadOpts>(
// find the correct resource
let resource: Resource | undefined;
if (components.router) {
resource = components.router.route(
{ resources, input, config: configObj },
defaultComponents.router
);
resource = components.router.route({ resources, input, config: configObj }, defaultComponents.router);
}

// validate input
Expand All @@ -54,7 +53,7 @@ export function factory<Resource, Input, Output, Config, LoadOpts>(
input,
config: configObj,
},
defaultComponents.validator
defaultComponents.validator,
);
}

Expand All @@ -68,7 +67,7 @@ export function factory<Resource, Input, Output, Config, LoadOpts>(
input: { validations: { input: inputValidations }, data: input },
config: configObj,
},
defaultComponents.mocker
defaultComponents.mocker,
);
} else if (components.forwarder) {
// forward request and set output from response
Expand All @@ -78,7 +77,7 @@ export function factory<Resource, Input, Output, Config, LoadOpts>(
input: { validations: { input: inputValidations }, data: input },
config: configObj,
},
defaultComponents.forwarder
defaultComponents.forwarder,
);
}

Expand All @@ -91,7 +90,7 @@ export function factory<Resource, Input, Output, Config, LoadOpts>(
output,
config: configObj,
},
defaultComponents.validator
defaultComponents.validator,
);
}

Expand Down
17 changes: 7 additions & 10 deletions packages/core/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,9 @@ export interface IPrism<Resource, Input, Output, Config, LoadOpts> {

export type PartialPrismConfigFactory<C, I> = (
input: I,
defaultConfig?: PartialPrismConfig<C, I> | PrismConfig<C, I>
defaultConfig?: PartialPrismConfig<C, I> | PrismConfig<C, I>,
) => Partial<C>;
export type PartialPrismConfig<C, I> =
| Partial<C>
| PrismConfigFactory<C, I>
| PartialPrismConfigFactory<C, I>;
export type PartialPrismConfig<C, I> = Partial<C> | PrismConfigFactory<C, I> | PartialPrismConfigFactory<C, I>;

export interface IPrismConfig {
mock?: boolean | object;
Expand All @@ -42,21 +39,21 @@ export interface IHttpLoaderOpts {
export interface IRouter<Resource, Input, Config> {
route: (
opts: { resources: Resource[]; input: Input; config?: Config },
defaultRouter?: IRouter<Resource, Input, Config>
defaultRouter?: IRouter<Resource, Input, Config>,
) => Resource;
}

export interface IForwarder<Resource, Input, Config, Output> {
forward: (
opts: { resource?: Resource; input: IPrismInput<Input>; config?: Config },
defaultForwarder?: IForwarder<Resource, Input, Config, Output>
defaultForwarder?: IForwarder<Resource, Input, Config, Output>,
) => Promise<Output>;
}

export interface IMocker<Resource, Input, Config, Output> {
mock: (
opts: Partial<IMockerOpts<Resource, Input, Config>>,
defaultMocker?: IMocker<Resource, Input, Config, Output>
defaultMocker?: IMocker<Resource, Input, Config, Output>,
) => Promise<Output>;
}

Expand All @@ -69,11 +66,11 @@ export interface IMockerOpts<Resource, Input, Config> {
export interface IValidator<Resource, Input, Config, Output> {
validateInput?: (
opts: { resource: Resource; input: Input; config?: Config },
defaultValidator?: IValidator<Resource, Input, Config, Output>
defaultValidator?: IValidator<Resource, Input, Config, Output>,
) => Promise<IPrismDiagnostic[]>;
validateOutput?: (
opts: { resource: Resource; output?: Output; config?: Config },
defaultValidator?: IValidator<Resource, Input, Config, Output>
defaultValidator?: IValidator<Resource, Input, Config, Output>,
) => Promise<IPrismDiagnostic[]>;
}

Expand Down
6 changes: 3 additions & 3 deletions packages/core/src/utils/__tests__/configMergerFactory.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ describe('configMerger', () => {
x: 1,
y: 2,
},
undefined
undefined,
);

return expect(configMerger(input, defaultConfig)).toMatchSnapshot();
Expand All @@ -51,7 +51,7 @@ describe('configMerger', () => {
c: 4,
},
beta: true,
}
},
);

return expect(configMerger(input, defaultConfig)).toMatchSnapshot();
Expand Down Expand Up @@ -80,7 +80,7 @@ describe('configMerger', () => {
},
alfa: false,
},
configFn
configFn,
);

expect(configMerger(input, defaultConfig)).toMatchSnapshot();
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/utils/__tests__/graphFacade.int.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ describe('graphFacade', () => {
test('httpOperations should return filtered nodes', async () => {
await graphFacade.createRawNode(
JSON.stringify(require('../../../../cli/src/samples/no-refs-petstore.oas2.json')),
{ type: FilesystemNodeType.File, language: 'json' }
{ type: FilesystemNodeType.File, language: 'json' },
);

expect(graphFacade.httpOperations.length).toBeGreaterThan(0);
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/utils/configMergerFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export function configMergerFactory<C, I>(
// remove falsy config props
_compact([baseConfig, ...configs])
// resolve each config (resolveConfig is async)
.map((c: C) => resolveConfig(input, c, defaultConfig))
.map((c: C) => resolveConfig(input, c, defaultConfig)),
);

if (!resolvedConfigs.length) {
Expand Down
7 changes: 2 additions & 5 deletions packages/core/src/utils/graphFacade.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export class GraphFacade {
createOas2Plugin(),
createOas3Plugin(),
createOas2HttpPlugin(),
createOas3HttpPlugin()
createOas3HttpPlugin(),
);
this.fsBackend = createFileSystemBackend(graphite, fs);
}
Expand Down Expand Up @@ -59,10 +59,7 @@ export class GraphFacade {
await this.graphite.scheduler.drain();
}

public async createRawNode(
raw: string,
{ type, language }: Pick<ISourceNode, 'type' | 'language'>
) {
public async createRawNode(raw: string, { type, language }: Pick<ISourceNode, 'type' | 'language'>) {
this.graphite.graph.addNode({
category: NodeCategory.Source,
type,
Expand Down
6 changes: 2 additions & 4 deletions packages/core/src/utils/resolveConfig.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
import { PartialPrismConfig, PartialPrismConfigFactory } from '../types';

function isPrismConfigFactory<C, I>(
val: PartialPrismConfig<C, I>
): val is PartialPrismConfigFactory<C, I> {
function isPrismConfigFactory<C, I>(val: PartialPrismConfig<C, I>): val is PartialPrismConfigFactory<C, I> {
return typeof val === 'function';
}

export function resolveConfig<Config, Input>(
input: Input,
config: PartialPrismConfig<Config, Input>,
defaultConfig?: PartialPrismConfig<Config, Input>
defaultConfig?: PartialPrismConfig<Config, Input>,
): Partial<Config> {
if (isPrismConfigFactory<Config, Input>(config)) {
return config(input, defaultConfig);
Expand Down
4 changes: 2 additions & 2 deletions packages/http-server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"dependencies": {
"@stoplight/prism-core": "^3.0.0-pre.2",
"@stoplight/prism-http": "^3.0.0-pre.2",
"fastify": "2.2.x",
"tslib": "1.9.x"
"fastify": "^2.3.0",
"tslib": "^1.9.0"
}
}
Loading

0 comments on commit a5a3a67

Please sign in to comment.