From 49bc64805978323c0fd7b4f2226ccf3fdcf1db57 Mon Sep 17 00:00:00 2001 From: Roshni Mandal Date: Mon, 7 Jul 2025 14:17:47 +0530 Subject: [PATCH 1/2] Add TypeScript Capstone Challenge implementation --- index.ts | 174 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 174 insertions(+) create mode 100644 index.ts diff --git a/index.ts b/index.ts new file mode 100644 index 0000000..42783ae --- /dev/null +++ b/index.ts @@ -0,0 +1,174 @@ + +// Type Definitions +type HttpMethod = 'GET' | 'POST'; +type UserRole = 'user' | 'admin'; + +interface User { + name: string; + age: number; + roles: UserRole[]; + createdAt: Date; + isDeleated: boolean; +} + +interface RequestBase { + method: HttpMethod; + host: string; + path: string; + params?: Record; +} + +interface PostRequest extends RequestBase { + method: 'POST'; + body: User; +} + +interface GetRequest extends RequestBase { + method: 'GET'; +} + +type Request = PostRequest | GetRequest; + +interface Response { + status: number; +} + +interface ObserverHandlers { + next?: (value: T) => void; + error?: (error: unknown) => void; + complete?: () => void; +} + +interface Subscription { + unsubscribe(): void; +} + +// Observer Class +class Observer { + private isUnsubscribed = false; + private _unsubscribe?: () => void; + + constructor(private handlers: ObserverHandlers) {} + + next(value: T): void { + if (!this.isUnsubscribed && this.handlers.next) { + this.handlers.next(value); + } + } + + error(error: unknown): 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(); + } + } + + setUnsubscribe(unsub: () => void) { + this._unsubscribe = unsub; + } +} + +// Observable Class +class Observable { + constructor(private _subscribeFn: (observer: Observer) => () => void) {} + + static from(values: T[]): Observable { + return new Observable((observer: Observer) => { + values.forEach((value) => observer.next(value)); + observer.complete(); + + return () => { + console.log('unsubscribed'); + }; + }); + } + + subscribe(handlers: ObserverHandlers): Subscription { + const observer = new Observer(handlers); + const unsub = this._subscribeFn(observer); + observer.setUnsubscribe(unsub); + + return { + unsubscribe: () => observer.unsubscribe() + }; + } +} + +// Constants +const HTTP_POST_METHOD: HttpMethod = 'POST'; +const HTTP_GET_METHOD: HttpMethod = 'GET'; + +const HTTP_STATUS_OK = 200; +const HTTP_STATUS_INTERNAL_SERVER_ERROR = 500; + +// Mock Data +const userMock: User = { + name: 'User Name', + age: 26, + roles: ['user', 'admin'], + createdAt: new Date(), + isDeleated: false, +}; + +const requestsMock: Request[] = [ + { + method: HTTP_POST_METHOD, + host: 'service.example', + path: 'user', + body: userMock, + params: {}, + }, + { + method: HTTP_GET_METHOD, + host: 'service.example', + path: 'user', + params: { + id: '3f5h67s4s', + }, + } +]; + +// Handlers +const handleRequest = (request: Request): Response => { + // Simulate request handling + console.log('Request handled:', request); + return { status: HTTP_STATUS_OK }; +}; + +const handleError = (error: unknown): Response => { + console.error('Error occurred:', error); + return { status: HTTP_STATUS_INTERNAL_SERVER_ERROR }; +}; + +const handleComplete = (): void => { + console.log('complete'); +}; + +// Usage +const requests$ = Observable.from(requestsMock); +const subscription = requests$.subscribe({ + next: handleRequest, + error: handleError, + complete: handleComplete +}); + +// Unsubscribe explicitly +subscription.unsubscribe(); From 1164d12d901ba15511ac37a9068d4e61350b0580 Mon Sep 17 00:00:00 2001 From: Roshni Mandal Date: Mon, 7 Jul 2025 14:18:31 +0530 Subject: [PATCH 2/2] Add TypeScript Capstone Challenge implementation --- index.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/index.ts b/index.ts index 42783ae..3695555 100644 --- a/index.ts +++ b/index.ts @@ -1,4 +1,3 @@ - // Type Definitions type HttpMethod = 'GET' | 'POST'; type UserRole = 'user' | 'admin';