From 1557cefe0c8487c8531f62698a70a1fc8b68d05e Mon Sep 17 00:00:00 2001 From: Alexander James Phillips Date: Thu, 11 May 2017 11:10:05 +0100 Subject: [PATCH] Include @theefer's work in #16416 --- types/hapi/index.d.ts | 18 +++++++------ types/hapi/test/route/additional-options.ts | 30 +++++++++++++++++++++ types/hapi/v15/index.d.ts | 30 +++++++++++++-------- 3 files changed, 59 insertions(+), 19 deletions(-) diff --git a/types/hapi/index.d.ts b/types/hapi/index.d.ts index d2619acef146c1..ccf39dde5f877f 100644 --- a/types/hapi/index.d.ts +++ b/types/hapi/index.d.ts @@ -826,7 +826,7 @@ export interface RouteAuthAccessConfiguationObject { /** * For context see RouteAdditionalConfigurationOptions > cache */ -export interface RouteCacheOptions { +export type RouteCacheOptions = { /** * determines the privacy flag included in client-side caching using the 'Cache-Control' header. Values are: * * 'default' - no privacy flag. This is the default setting. @@ -834,15 +834,17 @@ export interface RouteCacheOptions { * * 'private' - mark the response as suitable only for private caching. */ privacy?: 'default' | 'public' | 'private'; - /** relative expiration expressed in the number of milliseconds since the item was saved in the cache. Cannot be used together with expiresAt. */ - expiresIn?: number; - /** time of day expressed in 24h notation using the 'HH:MM' format, at which point all cache records for the route expire. Cannot be used together with expiresIn. */ - expiresAt?: string; - /** an array of HTTP response status codes (e.g. 200) which are allowed to include a valid caching directive. Defaults to [200]. */ - statuses?: [number]; + statuses?: number[]; /** a string with the value of the 'Cache-Control' header when caching is disabled. Defaults to 'no-cache'. */ otherwise?: string; -} +} & ({ + /** relative expiration expressed in the number of milliseconds since the item was saved in the cache. Cannot be used together with expiresAt. */ + expiresIn: number; + } | { + /** time of day expressed in 24h notation using the 'HH:MM' format, at which point all cache records for the route expire. Cannot be used together with expiresIn. */ + expiresAt: string; + } | {} +); /** * For context see RouteAdditionalConfigurationOptions > cors diff --git a/types/hapi/test/route/additional-options.ts b/types/hapi/test/route/additional-options.ts index 2e517877424967..35715abfbc4081 100644 --- a/types/hapi/test/route/additional-options.ts +++ b/types/hapi/test/route/additional-options.ts @@ -14,3 +14,33 @@ const user: Hapi.RouteAdditionalConfigurationOptions = { return reply({ name: 'John' }); } }; + + +// Add in addition to examples in docs + +var cache: Hapi.RouteCacheOptions = { + privacy: 'default', + statuses: [200, 201], + expiresIn: 5000, +}; + +cache = { + privacy: 'default', + statuses: [200, 201], + expiresAt: '22:44', +}; + +cache = { + privacy: 'default', + statuses: [200, 201], + /* typings should cause this to error, but currently does not + expiresAt: 5000, + */ +}; + +/* typings should cause this to error, but currently does not +var cache: Hapi.RouteCacheOptions = { + expiresIn: 5000, + expiresAt: '22:44', +}; +*/ diff --git a/types/hapi/v15/index.d.ts b/types/hapi/v15/index.d.ts index 2a59422c695fc3..86b993bfb58334 100644 --- a/types/hapi/v15/index.d.ts +++ b/types/hapi/v15/index.d.ts @@ -470,17 +470,7 @@ export interface IRouteAdditionalConfigurationOptions { /** an object passed back to the provided handler (via this) when called. */ bind?: any; /** if the route method is 'GET', the route can be configured to include caching directives in the response using the following options */ - cache?: { - /** mines the privacy flag included in clientside caching using the 'Cache-Control' header.Values are: - fault'no privacy flag.This is the default setting. - 'public'mark the response as suitable for public caching. - 'private'mark the response as suitable only for private caching. */ - privacy: string; - /** relative expiration expressed in the number of milliseconds since the item was saved in the cache.Cannot be used together with expiresAt. */ - expiresIn: number; - /** time of day expressed in 24h notation using the 'HH:MM' format, at which point all cache records for the route expire.Cannot be used together with expiresIn. */ - expiresAt: string; - }; + cache?: IRouteAdditionalConfigurationCache; /** the Cross- Origin Resource Sharing protocol allows browsers to make cross- origin API calls.CORS is required by web applications running inside a browser which are loaded from a different domain than the API server.CORS headers are disabled by default. To enable, set cors to true, or to an object with the following options: */ cors?: { /** a strings array of allowed origin servers ('Access-Control-Allow-Origin').The array can contain any combination of fully qualified origins along with origin strings containing a wildcard '' character, or a single `''origin string. Defaults to any origin['*']`. */ @@ -728,6 +718,24 @@ export interface IRouteAdditionalConfigurationAuthAccess { entity?: string; } +export type IRouteAdditionalConfigurationCache = { + /** determines the privacy flag included in clientside caching using the 'Cache-Control' header. Values are: + 'Default': no privacy flag.This is the default setting. + 'public': mark the response as suitable for public caching. + 'private': mark the response as suitable only for private caching. */ + privacy?: 'default' | 'public' | 'private'; + /** an array of HTTP response status codes (e.g. 200) which are allowed to include a valid caching directive. Defaults to [200]. */ + statuses?: number[]; + /** a string with the value of the 'Cache-Control' header when caching is disabled. Defaults to 'no-cache'. */ + otherwise?: string; +} & ({ + /** relative expiration expressed in the number of milliseconds since the item was saved in the cache. Cannot be used together with expiresAt. */ + expiresIn: number; +} | { + /** time of day expressed in 24h notation using the 'HH:MM' format, at which point all cache records for the route expire. Cannot be used together with expiresIn. */ + expiresAt: string; +} | {}); + /** server.realm http://hapijs.com/api#serverrealm The realm object contains server-wide or plugin-specific state that can be shared across various methods. For example, when calling server.bind(), the active realm settings.bind property is set which is then used by routes and extensions added at the same level (server root or plugin).