Skip to content

Commit

Permalink
feat: 🎸 improve yar vals and flash typings
Browse files Browse the repository at this point in the history
  • Loading branch information
Danilo Alonso authored and damusix committed Mar 19, 2024
1 parent 1e71593 commit 02839f6
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 6 deletions.
19 changes: 14 additions & 5 deletions lib/index.d.ts
Original file line number Diff line number Diff line change
@@ -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 {
/**
Expand Down Expand Up @@ -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<T>(key: string, value: T): T;
set<T extends YarValKeys>(key: T, value: YarValues[T]): T;
/**
* assigns values to multiple keys using each 'keysObject' top-level property. Returns the keysObject.
*/
set<T extends { [key: string]: any }>(keysObject: T): T;
set<T extends YarValues>(keysObject: Partial<T>): T;

/**
* retrieve value using a key. If 'clear' is 'true', key is cleared on return.
*/
get(key: string, clear?: boolean): any;
get <T extends YarValKeys>(key: T, clear?: boolean): YarValues[T] | null;
/**
* clears key.
*/
clear(key: string): void;
clear <T extends YarValKeys>(key: T): void;
/**
* Manually notify the session of changes (when using get()
* and changing the content of the returned reference directly without calling set()).
Expand All @@ -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 <T extends YarFlashKeys>(type?: T, message?: YarFlashes[T], isOverride?: boolean): any[];

/**
* if set to 'true', enables lazy mode.
Expand All @@ -160,7 +166,10 @@ declare namespace yar {
revoke(id: Id): Promise<void>;
}
}

declare const yar: Plugin<yar.YarOptions>;

// @ts-expect-error we know...
export = yar;

declare module '@hapi/hapi' {
Expand Down
69 changes: 68 additions & 1 deletion test/types/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,31 @@
import { types as lab } from '@hapi/lab';
import { Request, Server } from '@hapi/hapi';
import * as yar from '../..';

const { expect: check } = lab;

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({
Expand All @@ -21,10 +46,52 @@ async function boot() {
path: '/test',
method: 'get',
handler(request: Request) {

const example = request.yar.get('example');

const test1 = request.yar.get('test1');
const test2 = request.yar.get('test2');

test1 === '1233';
test2?.a === true;

return {
id: request.yar.id,
key: example?.key,
};
},
});

server.route({

path: '/test',
method: 'post',
handler(request: Request) {


request.yar.set('test1', '123');
request.yar.set('test2', {
a: true,
b: '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'
});


return {
id: request.yar.id,
key: example.key,
};
},
});
Expand Down

0 comments on commit 02839f6

Please sign in to comment.