Skip to content

Commit

Permalink
fix(@whook/server): fix multi headers parsing support
Browse files Browse the repository at this point in the history
  • Loading branch information
nfroidure committed Dec 20, 2022
1 parent 3a17640 commit a38cf72
Show file tree
Hide file tree
Showing 6 changed files with 138 additions and 14 deletions.
14 changes: 13 additions & 1 deletion packages/whook-aws-lambda/src/commands/testHTTPLambda.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,19 @@ async function initTestHTTPLambdaCommand({
headers: ammendedParameters
.filter((p) => p.in === 'header')
.reduce((headerParameters, p) => {
headerParameters[p.name] = parameters[camelCase(p.name)];
headerParameters[p.name] =
parameters[camelCase(p.name)] instanceof Array
? parameters[camelCase(p.name)][0]
: parameters[camelCase(p.name)];
return headerParameters;
}, {}),
multiValueHeaders: ammendedParameters
.filter((p) => p.in === 'header')
.reduce((headerParameters, p) => {
headerParameters[p.name] =
parameters[camelCase(p.name)] instanceof Array
? parameters[camelCase(p.name)]
: [parameters[camelCase(p.name)]];
return headerParameters;
}, {}),
body: hasBody
Expand Down
11 changes: 9 additions & 2 deletions packages/whook-example/src/handlers/getParameters.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@ describe('getParameters', () => {
const getParameters = await initGetParameters({});
const response = await getParameters({
pathParam1: 2,
pathParam2: ['a', 'b'],
pathParam2: 'a',
queryParam: ['a', 'b'],
aHeader: true,
aMultiHeader: [1, 2],
});

expect({
Expand All @@ -17,8 +19,13 @@ describe('getParameters', () => {
"response": {
"body": {
"aHeader": true,
"aMultiHeader": [
1,
2,
],
"pathParam1": 2,
"pathParam2": [
"pathParam2": "a",
"queryParam": [
"a",
"b",
],
Expand Down
50 changes: 47 additions & 3 deletions packages/whook-example/src/handlers/getParameters.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { autoHandler, handler } from 'knifecycle';
import { handler } from 'knifecycle';
import { refersTo } from '@whook/whook';
import type {
WhookAPIParameterDefinition,
Expand All @@ -19,14 +19,30 @@ export const pathParam1Parameter: WhookAPIParameterDefinition<API.GetParameters.
},
},
};

export const pathParam2Parameter: WhookAPIParameterDefinition<API.GetParameters.Parameters.PathParam2> =
{
name: 'pathParam2',
example: ['item1', 'item2'],
example: 'item',
parameter: {
in: 'path',
name: 'pathParam2',
required: true,
description: 'A string item',
schema: {
type: 'string',
},
},
};

export const queryParamParameter: WhookAPIParameterDefinition<API.GetParameters.Parameters.QueryParam> =
{
name: 'queryParam',
example: ['item1', 'item2'],
parameter: {
in: 'query',
name: 'queryParam',
required: true,
description: 'A list of items',
schema: {
type: 'array',
Expand All @@ -52,13 +68,24 @@ export const definition: WhookAPIHandlerDefinition = {
parameters: [
refersTo(pathParam1Parameter),
refersTo(pathParam2Parameter),
refersTo(queryParamParameter),
{
in: 'header',
name: 'aHeader',
schema: {
type: 'boolean',
},
},
{
in: 'header',
name: 'aMultiHeader',
schema: {
type: 'array',
items: {
type: 'number',
},
},
},
],
responses: {
200: {
Expand All @@ -71,10 +98,19 @@ export const definition: WhookAPIHandlerDefinition = {
aHeader: {
type: 'boolean',
},
aMultiHeader: {
type: 'array',
items: {
type: 'number',
},
},
pathParam1: {
type: 'number',
},
pathParam2: {
type: 'string',
},
queryParam: {
type: 'array',
items: {
type: 'string',
Expand All @@ -91,14 +127,22 @@ export const definition: WhookAPIHandlerDefinition = {

async function getParameters(
_,
{ aHeader, pathParam1, pathParam2 }: API.GetParameters.Input,
{
aHeader,
aMultiHeader,
pathParam1,
pathParam2,
queryParam,
}: API.GetParameters.Input,
): Promise<API.GetParameters.Output> {
return {
status: 200,
body: {
aHeader,
aMultiHeader,
pathParam1,
pathParam2,
queryParam,
},
};
}
Expand Down
16 changes: 12 additions & 4 deletions packages/whook-example/src/openAPISchema.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,20 @@ declare namespace API {
export type Output = Responses.$200;
export type Input = {
readonly aHeader?: Parameters.AHeader;
readonly aMultiHeader?: Parameters.AMultiHeader;
readonly pathParam1: Parameters.PathParam1;
readonly pathParam2: Parameters.PathParam2;
readonly queryParam: Parameters.QueryParam;
};
export namespace Responses {
export type $200 = Components.Responses.getParametersResponse200<200>;
}
export namespace Parameters {
export type AHeader = Components.Parameters.GetParameters2;
export type AHeader = Components.Parameters.GetParameters3;
export type AMultiHeader = Components.Parameters.GetParameters4;
export type PathParam1 = Components.Parameters.PathParam1;
export type PathParam2 = Components.Parameters.PathParam2;
export type QueryParam = Components.Parameters.QueryParam;
}
}
export namespace GetTime {
Expand Down Expand Up @@ -73,8 +77,10 @@ declare namespace Components {
export namespace Parameters {
export type Duration = NonNullable<number>;
export type PathParam1 = NonNullable<number>;
export type GetParameters2 = NonNullable<boolean>;
export type PathParam2 = NonNullable<NonNullable<string>[]>;
export type PathParam2 = NonNullable<string>;
export type GetParameters3 = NonNullable<boolean>;
export type QueryParam = NonNullable<NonNullable<string>[]>;
export type GetParameters4 = NonNullable<NonNullable<number>[]>;
}
export namespace Responses {
export type getDelayResponse204<S extends number> = {
Expand Down Expand Up @@ -142,8 +148,10 @@ declare namespace Components {
export type ResponsesgetOpenAPIResponse200Body0 = NonNullable<{}>;
export type ResponsesgetParametersResponse200Body0 = NonNullable<{
aHeader?: NonNullable<boolean>;
aMultiHeader?: NonNullable<NonNullable<number>[]>;
pathParam1?: NonNullable<number>;
pathParam2?: NonNullable<NonNullable<string>[]>;
pathParam2?: NonNullable<string>;
queryParam?: NonNullable<NonNullable<string>[]>;
}>;
export type ResponsesgetPingResponse200Body0 = NonNullable<{
pong?: 'pong';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,19 @@ exports[`API should work 1`] = `
},
},
"pathParam2": {
"description": "A list of items",
"description": "A string item",
"in": "path",
"name": "pathParam2",
"required": true,
"schema": {
"type": "string",
},
},
"queryParam": {
"description": "A list of items",
"in": "query",
"name": "queryParam",
"required": true,
"schema": {
"items": {
"type": "string",
Expand Down Expand Up @@ -467,13 +476,26 @@ exports[`API should work 1`] = `
{
"$ref": "#/components/parameters/pathParam2",
},
{
"$ref": "#/components/parameters/queryParam",
},
{
"in": "header",
"name": "aHeader",
"schema": {
"type": "boolean",
},
},
{
"in": "header",
"name": "aMultiHeader",
"schema": {
"items": {
"type": "number",
},
"type": "array",
},
},
],
"responses": {
"200": {
Expand All @@ -484,10 +506,19 @@ exports[`API should work 1`] = `
"aHeader": {
"type": "boolean",
},
"aMultiHeader": {
"items": {
"type": "number",
},
"type": "array",
},
"pathParam1": {
"type": "number",
},
"pathParam2": {
"type": "string",
},
"queryParam": {
"items": {
"type": "string",
},
Expand Down Expand Up @@ -515,6 +546,18 @@ exports[`API should work 1`] = `
{
"$ref": "#/components/parameters/pathParam2",
},
{
"description": "A list of items",
"in": "query",
"name": "queryParam",
"required": false,
"schema": {
"items": {
"type": "string",
},
"type": "array",
},
},
],
"responses": {
"200": {
Expand Down
16 changes: 13 additions & 3 deletions packages/whook-http-router/src/libs/validation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ import { YError } from 'yerror';
import { YHTTPError } from 'yhttperror';
import Stream from 'stream';
import { pickupOperationSecuritySchemes } from './openAPIUtils.js';
import {
pickAllHeaderValues,
pickFirstHeaderValue,
} from '@whook/http-transaction';
import Ajv from 'ajv';
import { parseReentrantNumber, parseBoolean } from 'strict-qs';
import type { ValidateFunction } from 'ajv';
Expand Down Expand Up @@ -405,11 +409,17 @@ export function castParameters<
return (parameters || []).reduce((filteredValues, parameter) => {
const parameterName =
parameter.in === 'header' ? camelCase(parameter.name) : parameter.name;

if (values[parameterName]) {
const parameterValue =
parameter.in !== 'header'
? values[parameterName]
: parameter.schema?.type === 'array'
? pickAllHeaderValues(parameterName, values)
: pickFirstHeaderValue(parameterName, values);

if (parameterValue) {
filteredValues[parameterName] = castSchemaValue(
parameter.schema,
values[parameterName],
parameterValue,
);
}
return filteredValues;
Expand Down

0 comments on commit a38cf72

Please sign in to comment.