diff --git a/src/language/typescript/common/bundled/__tests__/client.spec.ts b/src/language/typescript/common/bundled/__tests__/client.spec.ts new file mode 100644 index 0000000..071a8d8 --- /dev/null +++ b/src/language/typescript/common/bundled/__tests__/client.spec.ts @@ -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(); + }); + }); +}); diff --git a/src/language/typescript/common/bundled/client.ts b/src/language/typescript/common/bundled/client.ts index 6815cdb..9645b29 100644 --- a/src/language/typescript/common/bundled/client.ts +++ b/src/language/typescript/common/bundled/client.ts @@ -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 extends MonadThrow { readonly request: (request: Request) => HKT; } @@ -29,14 +29,14 @@ const client = ` export interface HTTPClient2 extends MonadThrow2 { readonly request: (request: Request) => Kind2; } - + export interface WebSocketChannelRequest { readonly method: 'GET' | 'POST'; readonly channel: string; readonly query?: Record; readonly headers?: Record; } - + export interface WebSocketClient2 { readonly channel: (request: WebSocketChannelRequest) => WebSocketChannel2; } @@ -46,7 +46,7 @@ const client = ` export interface WebSocketClient { readonly channel: (request: WebSocketChannelRequest) => WebSocketChannel; } - + export interface WebSocketChannel extends MonadThrow { readonly send: (payload: unknown) => void; readonly message: HKT @@ -59,16 +59,16 @@ const client = ` readonly send: (payload: unknown) => void; readonly message: Kind2 } - + 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); } } `;