|
1 | 1 | import type { |
2 | | - APIGatewayProxyEventV2, APIGatewayProxyStructuredResultV2, |
3 | | - APIGatewayProxyEventQueryStringParameters, APIGatewayProxyEventPathParameters, |
| 2 | + APIGatewayProxyEventPathParameters, |
| 3 | + APIGatewayProxyEventQueryStringParameters, |
4 | 4 | APIGatewayProxyEventV2WithLambdaAuthorizer, |
| 5 | + APIGatewayProxyStructuredResultV2, |
5 | 6 | Context |
6 | 7 | } from 'aws-lambda' |
7 | 8 |
|
8 | 9 | import createDebug from 'debug' |
9 | 10 |
|
10 | 11 | import type { AxiosRequestConfig, AxiosResponse } from 'axios' |
11 | | -import type { Runner, Operation, UnknownContext } from 'openapi-client-axios' |
12 | | -import { invokeLambda } from './lambda-invoker' |
13 | 12 | import { params } from 'bath/params' |
14 | | -import { safeParseJson } from './util' |
| 13 | +import type { Operation, Runner, UnknownContext } from 'openapi-client-axios' |
15 | 14 | import { v4 as uuidv4 } from 'uuid' |
| 15 | +import { invokeLambda } from './lambda-invoker' |
| 16 | +import { safeParseJson } from './util' |
16 | 17 |
|
17 | 18 | const debug = createDebug('openapi-lambda-adapter') |
18 | 19 |
|
@@ -43,17 +44,26 @@ export const convertAxiosToApiGw = (config: AxiosRequestConfig, operation: Opera |
43 | 44 | ...parsedParams |
44 | 45 | } |
45 | 46 |
|
46 | | - // extract query params -> convert each value to ta string |
| 47 | + // extract query params -> convert each value to a string |
47 | 48 | const queryParams = Object.entries(config.params ?? {}).filter(entryValueExists).reduce<APIGatewayProxyEventQueryStringParameters>((queryParams, [key, val]) => { |
48 | | - queryParams[key] = val?.toString() |
| 49 | + if (Array.isArray(val)) { |
| 50 | + // Use valueExists to filter out undefined/null |
| 51 | + queryParams[key] = val.filter(valueExists).map(v => v.toString()).join(',') |
| 52 | + } else { |
| 53 | + queryParams[key] = val?.toString() |
| 54 | + } |
49 | 55 | return queryParams |
50 | 56 | }, {}) |
51 | 57 |
|
52 | 58 | const queryString: string[] = [] |
53 | 59 | Object.entries(config.params ?? {}).filter(entryValueExists).forEach(([key, val]) => { |
54 | | - if (val && Array.isArray(val)) { |
| 60 | + if (Array.isArray(val)) { |
| 61 | + const filtered = val.filter(valueExists) |
55 | 62 | // eslint-disable-next-line @typescript-eslint/restrict-template-expressions |
56 | | - queryString.push(...val.map((entry) => `${key}=${entry.toString()}`)) |
| 63 | + queryString.push(...filtered.map((entry) => `${key}=${entry.toString()}`)) |
| 64 | + if (filtered.length === 0) { |
| 65 | + queryString.push(`${key}=`) |
| 66 | + } |
57 | 67 | } else { |
58 | 68 | queryString.push(`${key}=${val.toString()}`) |
59 | 69 | } |
@@ -134,7 +144,9 @@ export const convertApiGwToAxios = (resp: APIGatewayProxyStructuredResultV2, axi |
134 | 144 | return axiosResp |
135 | 145 | } |
136 | 146 |
|
137 | | -const entryValueExists = (entry: [string, unknown]): boolean => entry[1] !== null && entry[1] !== undefined |
| 147 | +const entryValueExists = (entry: [string, unknown]): boolean => valueExists(entry[1]) |
| 148 | + |
| 149 | +const valueExists = (value: unknown): boolean => value !== null && value !== undefined |
138 | 150 |
|
139 | 151 | class AxiosError extends Error { |
140 | 152 | public readonly code: string |
|
0 commit comments