Skip to content

Commit

Permalink
Make content encoder and options explicitly typed and extensible
Browse files Browse the repository at this point in the history
  • Loading branch information
kanongil authored and Marsup committed Mar 19, 2024
1 parent 4206c21 commit 4426c5a
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 19 deletions.
17 changes: 3 additions & 14 deletions lib/types/route.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { ObjectSchema, ValidationOptions, SchemaMap, Schema } from 'joi';

import { PluginSpecificConfiguration} from './plugin';
import { MergeType, ReqRef, ReqRefDefaults, MergeRefs, AuthMode } from './request';
import { RouteRequestExtType, RouteExtObject, Server } from './server';
import { ContentDecoders, ContentEncoders, RouteRequestExtType, RouteExtObject, Server } from './server';
import { Lifecycle, Json, HTTP_METHODS_PARTIAL } from './utils';

/**
Expand Down Expand Up @@ -208,11 +208,6 @@ export interface RouteOptionsCors {
*/
export type PayloadOutput = 'data' | 'stream' | 'file';

/**
* [See docs](https://github.com/hapijs/hapi/blob/master/API.md#-routeoptionspayloadcompression)
*/
export type PayloadCompressionDecoderSettings = object;

/**
* Determines how the request payload is processed.
* [See docs](https://github.com/hapijs/hapi/blob/master/API.md#-routeoptionspayload)
Expand All @@ -237,7 +232,7 @@ export interface RouteOptionsPayload {
* An object where each key is a content-encoding name and each value is an object with the desired decoder settings. Note that encoder settings are set in compression.
* [See docs](https://github.com/hapijs/hapi/blob/master/API.md#-routeoptionspayloadcompression)
*/
compression?: Record<string, PayloadCompressionDecoderSettings> | undefined;
compression?: { [P in keyof ContentDecoders]?: Parameters<ContentDecoders[P]>[0] } | undefined;

/**
* @default 'application/json'.
Expand Down Expand Up @@ -643,12 +638,6 @@ export interface RouteOptionsValidate {
state?: RouteOptionsResponseSchema | undefined;
}

/**
* For context [See docs](https://github.com/hapijs/hapi/blob/master/API.md#-routeoptionscompression)
* For context [See docs](https://github.com/hapijs/hapi/blob/master/API.md#-serverencoderencoding-encoder)
*/
export type RouteCompressionEncoderSettings = object;

export interface CommonRouteProperties<Refs extends ReqRef = ReqRefDefaults> {
/**
* Application-specific route configuration state. Should not be used by plugins which should use options.plugins[name] instead.
Expand Down Expand Up @@ -683,7 +672,7 @@ export interface CommonRouteProperties<Refs extends ReqRef = ReqRefDefaults> {
* An object where each key is a content-encoding name and each value is an object with the desired encoder settings. Note that decoder settings are set in compression.
* [See docs](https://github.com/hapijs/hapi/blob/master/API.md#-routeoptionscompression)
*/
compression?: Record<string, RouteCompressionEncoderSettings> | undefined;
compression?: { [P in keyof ContentEncoders]?: Parameters<ContentEncoders[P]>[0] } | undefined;

/**
* @default false (no CORS headers).
Expand Down
19 changes: 19 additions & 0 deletions lib/types/server/encoders.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { createDeflate, createGunzip, createGzip, createInflate } from 'zlib';

/**
* Available [content encoders](https://github.com/hapijs/hapi/blob/master/API.md#-serverencoderencoding-encoder).
*/
export interface ContentEncoders {

deflate: typeof createDeflate;
gzip: typeof createGzip;
}

/**
* Available [content decoders](https://github.com/hapijs/hapi/blob/master/API.md#-serverdecoderencoding-decoder).
*/
export interface ContentDecoders {

deflate: typeof createInflate;
gzip: typeof createGunzip;
}
1 change: 1 addition & 0 deletions lib/types/server/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export * from './auth';
export * from './cache';
export * from './encoders';
export * from './events';
export * from './ext';
export * from './info';
Expand Down
11 changes: 6 additions & 5 deletions lib/types/server/server.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as http from 'http';
import * as zlib from 'zlib';
import { Stream } from 'stream';

import { Root } from 'joi';
import { Mimos } from '@hapi/mimos';
Expand All @@ -25,15 +25,14 @@ import {
} from '../request';
import { ResponseToolkit } from '../response';
import {
PayloadCompressionDecoderSettings,
RouteCompressionEncoderSettings,
RulesOptions,
RulesProcessor,
ServerRoute
} from '../route';
import { HTTP_METHODS, Lifecycle } from '../utils';
import { ServerAuth } from './auth';
import { ServerCache } from './cache';
import { ContentDecoders, ContentEncoders } from './encoders';
import { ServerEventsApplication, ServerEvents } from './events';
import {
ServerExtEventsObject,
Expand Down Expand Up @@ -291,7 +290,8 @@ export class Server<A = ServerApplicationState> {
* @return Return value: none.
* [See docs](https://github.com/hapijs/hapi/blob/master/API.md#-serverdecoderencoding-decoder)
*/
decoder(encoding: string, decoder: ((options: PayloadCompressionDecoderSettings) => zlib.Gunzip)): void;
decoder<T extends keyof ContentDecoders>(encoding: T, decoder: ContentDecoders[T]): void;
decoder(encoding: string, decoder: ((options?: object) => Stream)): void;

/**
* Extends various framework interfaces with custom methods where:
Expand Down Expand Up @@ -340,7 +340,8 @@ export class Server<A = ServerApplicationState> {
* @return Return value: none.
* [See docs](https://github.com/hapijs/hapi/blob/master/API.md#-serverencoderencoding-encoder)
*/
encoder(encoding: string, encoder: ((options: RouteCompressionEncoderSettings) => zlib.Gzip)): void;
encoder<T extends keyof ContentEncoders>(encoding: T, encoder: ContentEncoders[T]): void;
encoder(encoding: string, encoder: ((options?: object) => Stream)): void;

/**
* Used within a plugin to expose a property via server.plugins[name] where:
Expand Down

0 comments on commit 4426c5a

Please sign in to comment.