Skip to content

Fixes of ResponseValidationError class #94

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jan 16, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions src/language/typescript/common/bundled/__tests__/client.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import * as typescript from 'typescript';
import { client } from '../client';

describe('client', () => {
describe('ResponseValidationError class', () => {
it('should produce uniquely identifiable instance of itself', () => {
const exports = {} as any;

const clientCodeTranspiled = typescript.transpile(client, {
target: typescript.ScriptTarget.ESNext,
module: typescript.ModuleKind.None,
});

// eslint-disable-next-line no-eval
eval(clientCodeTranspiled);

expect(exports.ResponseValidationError.create([])).toBeInstanceOf(exports.ResponseValidationError);
expect(exports.ResponseValidationError.create([])).toBeInstanceOf(Error);
});
it('should produce instance that not crashing when its casted to string', () => {
const exports = {} as any;
const clientCodeTranspiled = typescript.transpile(client, {
target: typescript.ScriptTarget.ESNext,
module: typescript.ModuleKind.None,
});

// eslint-disable-next-line no-eval
eval(clientCodeTranspiled);

expect(() => String(exports.ResponseValidationError.create([]))).not.toThrow();
});
});
});
18 changes: 9 additions & 9 deletions src/language/typescript/common/bundled/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,20 @@ import { pipe } from 'fp-ts/lib/pipeable';
export const clientRef = fromString('#/client/client');

//TODO: make WebSocketChannelRequest.query a string after https://github.com/asyncapi/asyncapi/issues/294
const client = `
export const client = `
import { HKT, Kind, Kind2, URIS, URIS2 } from 'fp-ts/lib/HKT';
import { MonadThrow, MonadThrow1, MonadThrow2 } from 'fp-ts/lib/MonadThrow';
import { Errors } from 'io-ts';
import { PathReporter } from 'io-ts/lib/PathReporter';
import { left } from 'fp-ts/lib/Either';

export interface Request {
readonly method: 'GET' | 'POST' | 'PUT' | 'DELETE' | 'PATCH' | 'HEAD' | 'OPTIONS';
readonly url: string;
readonly query?: string;
readonly body?: unknown;
}

export interface HTTPClient<F> extends MonadThrow<F> {
readonly request: (request: Request) => HKT<F, unknown>;
}
Expand All @@ -29,14 +29,14 @@ const client = `
export interface HTTPClient2<F extends URIS2> extends MonadThrow2<F> {
readonly request: (request: Request) => Kind2<F, unknown, unknown>;
}

export interface WebSocketChannelRequest {
readonly method: 'GET' | 'POST';
readonly channel: string;
readonly query?: Record<string, unknown>;
readonly headers?: Record<string, unknown>;
}

export interface WebSocketClient2<F extends URIS2> {
readonly channel: (request: WebSocketChannelRequest) => WebSocketChannel2<F>;
}
Expand All @@ -46,7 +46,7 @@ const client = `
export interface WebSocketClient<F> {
readonly channel: (request: WebSocketChannelRequest) => WebSocketChannel<F>;
}

export interface WebSocketChannel<F> extends MonadThrow<F> {
readonly send: (payload: unknown) => void;
readonly message: HKT<F, unknown>
Expand All @@ -59,16 +59,16 @@ const client = `
readonly send: (payload: unknown) => void;
readonly message: Kind2<F, unknown, unknown>
}

export class ResponseValidationError extends Error {
static create(errors: Errors): ResponseValidationError {
return new ResponseValidationError(errors);
}

constructor(readonly errors: Errors) {
super(PathReporter.report(left(errors)).join('\\n\\n'));
this.name = 'ResponseValidationError';
Object.setPrototypeOf(this, ResponseValidationError);
Object.setPrototypeOf(this, ResponseValidationError.prototype);
}
}
`;
Expand Down