From 1e7e455a8958e679b293ce0de7282e27c05229b3 Mon Sep 17 00:00:00 2001 From: Yuliia Krashanovska Date: Fri, 10 Jan 2025 23:00:15 +0200 Subject: [PATCH 1/2] ykrashanovska Typescript Essentials --- index.ts | 147 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 147 insertions(+) create mode 100644 index.ts diff --git a/index.ts b/index.ts new file mode 100644 index 0000000..ed089de --- /dev/null +++ b/index.ts @@ -0,0 +1,147 @@ +enum HttpMethod { + POST = 'POST', + GET = 'GET' +} + +enum HttpStatus { + OK = 200, + INTERNAL_SERVER_ERROR = 500 +} + +type Handlers = { + next?: (value: any) => void; + error?: (error: any) => void; + complete?: () => void; +}; + +type RequestType = { method: string, host: string, path: string, body?: any, params?: any }; + +class Observer { + + private handlers: Handlers; + private isUnsubscribed: boolean; + private _unsubscribe: () => void; + + constructor(handlers: Handlers) { + this.handlers = handlers; + this.isUnsubscribed = false; + this._unsubscribe = () => {}; + } + + next(value: any): void { + if (this.handlers.next && !this.isUnsubscribed) { + this.handlers.next(value); + } + } + + error(error: any): void { + if (!this.isUnsubscribed) { + if (this.handlers.error) { + this.handlers.error(error); + } + + this.unsubscribe(); + } + } + + complete(): void { + if (!this.isUnsubscribed) { + if (this.handlers.complete) { + this.handlers.complete(); + } + + this.unsubscribe(); + } + } + + unsubscribe(): void { + this.isUnsubscribed = true; + + if (this._unsubscribe) { + this._unsubscribe(); + } + } + } + + class Observable { + private _subscribe: (observer: Observer) => () => void; + constructor(subscribe: (observer: Observer) => () => void) { + this._subscribe = subscribe; + } + + static from(values: RequestType[]): Observable { + return new Observable((observer) => { + values.forEach((value) => observer.next(value)); + + observer.complete(); + + return () => { + console.log('unsubscribed'); + }; + }); + } + + subscribe(obs: Handlers): { unsubscribe: () => void } { + const observer = new Observer(obs); + + // NOTE: not sure why this code is here, if we are meaning that _unsubscribe is a private method, we should not be able to access it from outside the class + observer._unsubscribe = this._subscribe(observer); + + return ({ + unsubscribe() { + observer.unsubscribe(); + } + }); + } + } + + const userMock = { + name: 'User Name', + age: 26, + roles: [ + 'user', + 'admin' + ], + createdAt: new Date(), + isDeleated: false, + }; + + const requestsMock = [ + { + method: HttpMethod.POST, + host: 'service.example', + path: 'user', + body: userMock, + params: {}, + }, + { + method: HttpMethod.GET, + host: 'service.example', + path: 'user', + params: { + id: '3f5h67s4s' + }, + } + ]; + + + const handleRequest = (request: any) => { + // handling of request + return {status: HttpStatus.OK}; + }; + const handleError = (error: any) => { + // handling of error + return {status: HttpStatus.INTERNAL_SERVER_ERROR}; + }; + + const handleComplete: () => void = () => console.log('complete'); + + const requests$: Observable = Observable.from(requestsMock); + + const subscription: { unsubscribe: () => void } = requests$.subscribe({ + next: handleRequest, + error: handleError, + complete: handleComplete + }); + + subscription.unsubscribe(); From 67a6915610f51648a03361e233426dac23c7e2e1 Mon Sep 17 00:00:00 2001 From: Yuliia Krashanovska Date: Wed, 26 Feb 2025 17:41:01 +0200 Subject: [PATCH 2/2] fix for any types --- index.ts | 28 +++++++++++++++++++++------- 1 file changed, 21 insertions(+), 7 deletions(-) diff --git a/index.ts b/index.ts index ed089de..8c9807f 100644 --- a/index.ts +++ b/index.ts @@ -9,12 +9,26 @@ enum HttpStatus { } type Handlers = { - next?: (value: any) => void; - error?: (error: any) => void; + next?: (value: RequestType) => void; + error?: (error: string) => void; complete?: () => void; }; -type RequestType = { method: string, host: string, path: string, body?: any, params?: any }; +type RequestType = { + method: string, + host: string, + path: string, + body?: { + name: string, + age: number, + roles: string[], + createdAt: Date, + isDeleated: boolean, + }, + params?: { + id?: undefined | string, + }, +}; class Observer { @@ -28,13 +42,13 @@ class Observer { this._unsubscribe = () => {}; } - next(value: any): void { + next(value: RequestType): void { if (this.handlers.next && !this.isUnsubscribed) { this.handlers.next(value); } } - error(error: any): void { + error(error: string): void { if (!this.isUnsubscribed) { if (this.handlers.error) { this.handlers.error(error); @@ -125,11 +139,11 @@ class Observer { ]; - const handleRequest = (request: any) => { + const handleRequest = (request: RequestType) => { // handling of request return {status: HttpStatus.OK}; }; - const handleError = (error: any) => { + const handleError = (error: string) => { // handling of error return {status: HttpStatus.INTERNAL_SERVER_ERROR}; };