Skip to content

Commit

Permalink
refactor(types): put headers into a single type for reuse
Browse files Browse the repository at this point in the history
  • Loading branch information
nfroidure committed Dec 19, 2020
1 parent 78298d9 commit b06da51
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 16 deletions.
7 changes: 4 additions & 3 deletions packages/whook-http-router/src/validation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import type { OpenAPIV3 } from 'openapi-types';
import type {
DereferencedParameterObject,
WhookOperation,
WhookHeaders,
} from '@whook/http-transaction';
import { parseReentrantNumber, parseBoolean } from 'strict-qs';

Expand Down Expand Up @@ -362,8 +363,8 @@ export function _validateParameter(

export function filterHeaders(
parameters: DereferencedParameterObject[],
headers: Record<string, string | string[]>,
): Record<string, string | string[]> {
headers: WhookHeaders,
): WhookHeaders {
return (parameters || [])
.filter((parameter) => 'header' === parameter.in)
.reduce((filteredHeaders, parameter) => {
Expand All @@ -379,7 +380,7 @@ export function castParameters<
T = boolean | boolean[] | string | string[] | number | number[]
>(
parameters: DereferencedParameterObject[],
values: Record<string, string | string[]>,
values: WhookHeaders,
): Record<string, T> {
return (parameters || []).reduce((filteredValues, parameter) => {
const parameterName =
Expand Down
9 changes: 4 additions & 5 deletions packages/whook-http-transaction/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,9 @@ export type WhookOperation<
'x-whook'?: T;
};

export type WhookRequest<
H = Record<string, string | string[]>,
B = JsonValue | Readable
> = {
export type WhookHeaders = Record<string, string | string[]>;

export type WhookRequest<H = WhookHeaders, B = JsonValue | Readable> = {
url: string;
method: string;
headers: H;
Expand All @@ -81,7 +80,7 @@ export type WhookRequest<

export type WhookResponse<
S = number,
H = Record<string, string | string[]>,
H = WhookHeaders,
B = JsonValue | Readable
> = {
status: S;
Expand Down
20 changes: 12 additions & 8 deletions packages/whook-http-transaction/src/services/obfuscator.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { autoService } from 'knifecycle';
import type { LogService } from 'common-services';
import type { JsonValue } from 'type-fest';
import type { WhookHeaders } from '..';

export type SensibleValueDescriptor = {
name: string;
Expand All @@ -18,10 +20,11 @@ export type ObfuscatorDependencies = {
} & ObfuscatorConfig;
export type ObfuscatorService = {
obfuscate: (secret: string) => string;
obfuscateSensibleProps: (propValue: any, propName?: string) => any;
obfuscateSensibleHeaders: (
headers: Record<string, string | string[]>,
) => Record<string, string | string[]>;
obfuscateSensibleProps: (
propValue: JsonValue,
propName?: string,
) => JsonValue;
obfuscateSensibleHeaders: (headers: WhookHeaders) => WhookHeaders;
};

export default autoService(initObfuscator);
Expand Down Expand Up @@ -160,9 +163,7 @@ async function initObfuscator({
: obfuscate(value);
}

function obfuscateSensibleHeaders(
headers: Record<string, string | string[]>,
): Record<string, string | string[]> {
function obfuscateSensibleHeaders(headers: WhookHeaders): WhookHeaders {
return Object.keys(headers).reduce((finalHeaders, headerName) => {
const sensibleHeader = SENSIBLE_HEADERS.find(
(sensibleHeader) =>
Expand All @@ -181,7 +182,10 @@ async function initObfuscator({
}, {});
}

function obfuscateSensibleProps(propValue: any, propName = '_') {
function obfuscateSensibleProps(
propValue: JsonValue,
propName = '_',
): JsonValue {
if (propValue instanceof Array) {
return propValue.map((value) => obfuscateSensibleProps(value, propName));
} else if (typeof propValue === 'object' && propValue !== null) {
Expand Down
2 changes: 2 additions & 0 deletions packages/whook/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ import type {
import type {
WhookOperation,
WhookRequest,
WhookHeaders,
WhookResponse,
WhookHandler,
WhookHandlerFunction,
Expand Down Expand Up @@ -131,6 +132,7 @@ export type {
WhookConfig,
WhookOperation,
WhookRequest,
WhookHeaders,
WhookResponse,
WhookHandler,
WhookHandlerFunction,
Expand Down

0 comments on commit b06da51

Please sign in to comment.