From b31c0d512aad33692c1edcbcf34f8bf010dcdbf5 Mon Sep 17 00:00:00 2001 From: Simon Schick Date: Sat, 17 Feb 2018 21:22:06 +0100 Subject: [PATCH] feature(types): add types for yar, fix boom references --- types/catbox/index.d.ts | 14 +- types/hapi/definitions/util/lifecycle.d.ts | 2 +- types/yar/index.d.ts | 169 +++++++++++++++++++++ types/yar/tsconfig.json | 23 +++ types/yar/tslint.json | 1 + types/yar/yar-tests.ts | 24 +++ 6 files changed, 225 insertions(+), 8 deletions(-) create mode 100644 types/yar/index.d.ts create mode 100644 types/yar/tsconfig.json create mode 100644 types/yar/tslint.json create mode 100644 types/yar/yar-tests.ts diff --git a/types/catbox/index.d.ts b/types/catbox/index.d.ts index 826f1f7369..9d84f2204f 100644 --- a/types/catbox/index.d.ts +++ b/types/catbox/index.d.ts @@ -6,8 +6,8 @@ import * as Boom from 'boom'; -export type CallBackNoResult = (err?: Boom.BoomError) => void; -export type CallBackWithResult = (err: Boom.BoomError | null | undefined, result: T) => void; +export type CallBackNoResult = (err?: Boom.Boom) => void; +export type CallBackWithResult = (err: Boom.Boom | null | undefined, result: T) => void; /** * Client @@ -50,7 +50,7 @@ export class Client implements ClientApi { /** isReady() - returns true if cache engine determines itself as ready, false if it is not ready. */ isReady(): boolean; /** validateSegmentName(segment) - returns null if the segment name is valid (see below), otherwise should return an instance of Error with an appropriate message. */ - validateSegmentName(segment: string): null | Boom.BoomError; + validateSegmentName(segment: string): null | Boom.Boom; } export type EnginePrototypeOrObject = EnginePrototype | ClientApi; @@ -95,7 +95,7 @@ export interface ClientApi { /** isReady() - returns true if cache engine determines itself as ready, false if it is not ready. */ isReady(): boolean; /** validateSegmentName(segment) - returns null if the segment name is valid (see below), otherwise should return an instance of Error with an appropriate message. */ - validateSegmentName(segment: string): null | Boom.BoomError; + validateSegmentName(segment: string): null | Boom.Boom; } /** @@ -211,7 +211,7 @@ export interface PolicyAPI { * @param cached - null if a valid item was not found in the cache, or IPolicyGetCallbackCachedOptions * @param report - an object with logging information about the generation operation */ -export type PolicyGetCallback = (err: null | Boom.BoomError, value: CacheItem, cached: PolicyGetCallbackCachedOptions, report: PolicyGetCallbackReportLog) => void; +export type PolicyGetCallback = (err: null | Boom.Boom, value: CacheItem, cached: PolicyGetCallbackCachedOptions, report: PolicyGetCallbackReportLog) => void; export interface PolicyGetCallbackCachedOptions { /** item - the cached value. */ @@ -273,7 +273,7 @@ export interface PolicyOptions { * * ttl - the cache ttl value in milliseconds. Set to 0 to skip storing in the cache. Defaults to the cache global policy. * @see {@link https://github.com/hapijs/catbox#policy} */ -export type GenerateFunc = (id: string, next: ((err: null | Boom.BoomError, value: CacheItem, ttl?: number) => void)) => void; +export type GenerateFunc = (id: string, next: ((err: null | Boom.Boom, value: CacheItem, ttl?: number) => void)) => void; /** * An object with logging information about the generation operation containing the following keys (as relevant): @@ -288,7 +288,7 @@ export interface PolicyGetCallbackReportLog { /** ttl - the cache ttl value for the record. */ ttl: number; /** error - lookup error. */ - error?: Boom.BoomError; + error?: Boom.Boom; } /** diff --git a/types/hapi/definitions/util/lifecycle.d.ts b/types/hapi/definitions/util/lifecycle.d.ts index 35f347dcd5..4eda3733bd 100644 --- a/types/hapi/definitions/util/lifecycle.d.ts +++ b/types/hapi/definitions/util/lifecycle.d.ts @@ -36,7 +36,7 @@ export namespace Lifecycle { export type ReturnValueTypes = (null | string | number | boolean) | (Buffer) | - (Error | Boom.BoomError) | + (Error | Boom.Boom) | (stream.Stream) | (object | object[]) | Object | diff --git a/types/yar/index.d.ts b/types/yar/index.d.ts new file mode 100644 index 0000000000..87b830cca3 --- /dev/null +++ b/types/yar/index.d.ts @@ -0,0 +1,169 @@ +// Type definitions for yar 9.0 +// Project: https://github.com/hapijs/yar#readme +// Definitions by: Simon Schick +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped +// TypeScript Version: 2.4 +// From https://github.com/hapijs/yar/blob/master/API.md + +import { + Server, + ServerOptionsCache, + Request, + Plugin, +} from 'hapi'; +declare namespace yar { + interface YarOptions { + /** + * Tells Hapi that it should not respond with a HTTP 400 error if the session cookie cannot decrypt. + * This could happen if the cookie is changed on the client, or more likely, if you change the cookie password in your settings. + * If you want to make this condition send an error like it did in prior versions, change this to `false`, + * but be aware that if you change your cookie password you will cause 400 errors to be returned to end users. + * In that case you should probably change this back to true for a short time to allow session cookies to get reset for the best user experience. + * Defaults to true. + */ + ignoreErrors?: boolean; + + /** + * Tells Hapi that if a session cookie is invalid for any reason, + * to clear it from the browser. + * This prevents Hapi from having to reprocess the bad cookie on future requests. + * In general you'll probably want this on, + * but if you'd prefer that session cookies be dealt with in some + * other way you may set this to false. + * Defaults to true + */ + clearInvalid?: boolean; + + /** + * Determines the name of the cookie used to store session information. + * Defaults to session. + */ + name?: string; + + /** + * maximum cookie size before using server-side storage. + * Defaults to 1K. Set to zero to always use server-side storage. + */ + maxCookieSize?: number; + + /** + * determines whether to store empty session before they've been modified. + * Defaults to true. + */ + storeBlank?: boolean; + + /** + * will cause yar to throw an exception if trying to persist to cache when the cache is unavailable. + * Setting this to false will allow applications using yar to run uninterrupted if the cache is not ready (however sessions will not be saving). + * Defaults to true. + */ + errorOnCacheNotReady?: boolean; + + /** + * hapi cache options which includes (among other options): + */ + cache?: ServerOptionsCache; + + /** + * server-side storage expiration (defaults to 1 day). + */ + expiresIn?: number; + /** + * the configuration for cookie-specific features: + */ + cookieOptions: { + /** + * (Required) used to encrypt and sign the cookie data. + * Must be at least 32 chars. + */ + password: string; + /** + * determines the cookie path. + * Defaults to '/'. + */ + path?: string; + /** + * enables the same-site cookie parameter. + * Default to 'Lax'. + */ + isSameSite?: 'Lax' | 'Strict' | false; + /** + * determines whether or not to transfer using TLS/SSL. + * Defaults to true. + */ + isSecure?: boolean; + /** + * determines whether or not to set HttpOnly option in cookie. + * Defaults to false. + */ + isHttpOnly?: boolean; + /** + * sets the time for the cookie to live in the browser, in milliseconds. + * Defaults to null (session time-life - cookies are deleted when the browser is closed). + */ + ttl?: number; + /** + * an optional function to create custom session IDs. + * Must retun a string and have the signature function (request) where: + * request - (optional) is the original request received from the client. + */ + customSessionIDGenerator?(req: Request): string; + }; + } + + interface Yar { + /** + * clears the session and assigns a new session id. + */ + reset(): void; + /** + * - assigns a value (string, object, etc) to a given key which will persist across requests. Returns the value. + */ + set(key: string, value: T): T; + /** + * assigns values to multiple keys using each 'keysObject' top-level property. Returns the keysObject. + */ + set(keysObject: T): T; + + /** + * retrieve value using a key. If 'clear' is 'true', key is cleared on return. + */ + get(key: string, clear?: boolean): any; + /** + * clears key. + */ + clear(key: string): void; + /** + * Manually notify the session of changes (when using get() + * and changing the content of the returned reference directly without calling set()). + */ + touch(): void; + /** + * stores volatile data - data that should be deleted once read. + * When given no arguments, it will return all of the flash messages and delete the originals. + * When given only a type, it will return all of the flash messages of that type and delete the originals. + * When given a type and a message, it will set or append that message to the given type. + * '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): any[]; + flash(type: string, message: any, isOverride?: boolean): void; + + /** + * if set to 'true', enables lazy mode. + * In lazy mode, request.yar can be modified directly (e.g. setting request.yar.myKey to an object value), + * and those keys will be stored and loaded back. + * Lazy mode isn't as fast as the normal get/set because + * it has to store the session state on every responses regardless of any changes being made. + */ + lazy(enabled: boolean): void; + } +} +declare const yar: Plugin; +export = yar; + +declare module 'hapi/definitions/request/request' { + interface Request { + yar: yar.Yar; + } +} diff --git a/types/yar/tsconfig.json b/types/yar/tsconfig.json new file mode 100644 index 0000000000..c2723ded55 --- /dev/null +++ b/types/yar/tsconfig.json @@ -0,0 +1,23 @@ +{ + "compilerOptions": { + "module": "commonjs", + "lib": [ + "es6" + ], + "noImplicitAny": true, + "noImplicitThis": true, + "strictNullChecks": true, + "strictFunctionTypes": true, + "baseUrl": "../", + "typeRoots": [ + "../" + ], + "types": [], + "noEmit": true, + "forceConsistentCasingInFileNames": true + }, + "files": [ + "index.d.ts", + "yar-tests.ts" + ] +} diff --git a/types/yar/tslint.json b/types/yar/tslint.json new file mode 100644 index 0000000000..3db14f85ea --- /dev/null +++ b/types/yar/tslint.json @@ -0,0 +1 @@ +{ "extends": "dtslint/dt.json" } diff --git a/types/yar/yar-tests.ts b/types/yar/yar-tests.ts new file mode 100644 index 0000000000..997b18f82b --- /dev/null +++ b/types/yar/yar-tests.ts @@ -0,0 +1,24 @@ +import { Server, Request } from 'hapi'; +import * as yar from 'yar'; + +async function boot() { + const server = new Server(); + await server.register({ + plugin: yar, + options: { + cookieOptions: { + password: 'test', + isSecure: true, + }, + }, + }); + + server.route({ + path: '/test', + method: 'get', + handler(request: Request) { + const example = request.yar.get('example'); + return example.key; + }, + }); +}