From 0c7bbb3b4f9a0f5d29af5cc147594f12e9fc11c1 Mon Sep 17 00:00:00 2001 From: Danilo Alonso Date: Tue, 19 Mar 2024 14:47:05 -0400 Subject: [PATCH] =?UTF-8?q?feat:=20=F0=9F=8E=B8=20improve=20yar=20vals=20a?= =?UTF-8?q?nd=20flash=20typings?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/index.d.ts | 18 +++++++++---- test/types/index.ts | 62 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 75 insertions(+), 5 deletions(-) diff --git a/lib/index.d.ts b/lib/index.d.ts index 13c91fe..5f252c7 100644 --- a/lib/index.d.ts +++ b/lib/index.d.ts @@ -1,6 +1,12 @@ import { Id, PolicyOptions } from '@hapi/catbox'; import { CachePolicyOptions, Plugin, Request, ResponseToolkit, Server, ServerOptionsCache } from '@hapi/hapi'; +export interface YarValues {} +export interface YarFlashes {} + +type YarValKeys = keyof YarValues; +type YarFlashKeys = keyof YarFlashes; + declare namespace yar { interface YarOptions { /** @@ -109,20 +115,20 @@ declare namespace yar { /** * - assigns a value (string, object, etc) to a given key which will persist across requests. Returns the value. */ - set(key: string, value: T): T; + set(key: T, value: YarValues[T]): T; /** * assigns values to multiple keys using each 'keysObject' top-level property. Returns the keysObject. */ - set(keysObject: T): T; + set(keysObject: Partial): T; /** * retrieve value using a key. If 'clear' is 'true', key is cleared on return. */ - get(key: string, clear?: boolean): any; + get (key: T, clear?: boolean): YarValues[T]; /** * clears key. */ - clear(key: string): void; + clear (key: T): void; /** * Manually notify the session of changes (when using get() * and changing the content of the returned reference directly without calling set()). @@ -136,7 +142,7 @@ declare namespace yar { * 'isOverride' used to indicate that the message provided should replace * any existing value instead of being appended to it (defaults to false). */ - flash(type?: string, message?: any, isOverride?: boolean): any[]; + flash (type?: T, message?: YarFlashes[T], isOverride?: boolean): any[]; /** * if set to 'true', enables lazy mode. @@ -160,7 +166,9 @@ declare namespace yar { revoke(id: Id): Promise; } } + declare const yar: Plugin; + export = yar; declare module '@hapi/hapi' { diff --git a/test/types/index.ts b/test/types/index.ts index 6ef0bfe..387ae09 100644 --- a/test/types/index.ts +++ b/test/types/index.ts @@ -1,6 +1,29 @@ +import { types } from '@hapi/lab'; import { Request, Server } from '@hapi/hapi'; import * as yar from '../..'; +declare module '../..' { + + interface YarValues { + + test1: string; + test2: { + a: true; + b: string; + }, + + example: { + key: string; + }; + } + + interface YarFlashes { + + success: { title: string; message: string; }; + error: { title: string; message: string; }; + } +} + async function boot() { const server = new Server(); await server.register({ @@ -28,4 +51,43 @@ async function boot() { }; }, }); + + server.route({ + + path: '/test', + method: 'post', + handler(request: Request) { + + + types.expect.error(request.yar.set('abc123', true)); + request.yar.set('test1', '123'); + request.yar.set('test2', { + a: true, + b: '123', + }); + + types.expect.error(request.yar.set('test1', 123)) + types.expect.error(request.yar.set('test2', 123)) + + request.yar.set({ + test1: '123', + }); + + request.yar.flash('error', { title: 'Error', message: 'This is an error' }); + request.yar.flash('success', { title: 'Success', message: 'This is a success' }); + + types.expect.error( + request.yar.flash('test', { title: 'Success', message: 'This is a success' }) + ) + + types.expect.error( + request.yar.flash('success', 'message') + ); + + + return { + id: request.yar.id, + }; + }, + }); } \ No newline at end of file