@nrfcloud/lambda-helpers 1.1.8 → 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,8 +1,8 @@
1
1
  /**
2
- * Returns a Map of headers with keys in lowercase.
3
- * This is useful for consistent header handling, especially in environments
4
- * where header keys may be case-insensitive.
2
+ * Returns case-insensitive headers (a `Headers` instance).
3
+ *
4
+ * `undefined` values are skipped, and entries that are not valid HTTP headers
5
+ * are ignored instead of throwing.
5
6
  */
6
- export declare const parseHeaders: (headers: Record<string, string | undefined> | null) => ParsedHeaders;
7
- export type ParsedHeaders = Pick<Map<string, string | null>, 'get' | 'has' | 'size' | 'keys' | 'values' | 'entries' | 'forEach'>;
8
- export declare const CaseInsensitiveMap: (map: Map<string, string | null>) => ParsedHeaders;
7
+ export declare const parseHeaders: (headers: Record<string, string | undefined> | null | undefined) => ParsedHeaders;
8
+ export type ParsedHeaders = Pick<Headers, 'get' | 'has' | 'keys' | 'values' | 'entries' | 'forEach'>;
@@ -1,20 +1,17 @@
1
1
  /**
2
- * Returns a Map of headers with keys in lowercase.
3
- * This is useful for consistent header handling, especially in environments
4
- * where header keys may be case-insensitive.
2
+ * Returns case-insensitive headers (a `Headers` instance).
3
+ *
4
+ * `undefined` values are skipped, and entries that are not valid HTTP headers
5
+ * are ignored instead of throwing.
5
6
  */ export const parseHeaders = (headers)=>{
6
- if (headers === null) return new Map();
7
- return CaseInsensitiveMap(Object.entries(headers).reduce((h, [key, value])=>{
8
- h.set(key.toLowerCase(), value ?? null);
9
- return h;
10
- }, new Map()));
7
+ const parsed = new Headers();
8
+ for (const [key, value] of Object.entries(headers ?? {})){
9
+ if (value === undefined || value === null) continue;
10
+ try {
11
+ parsed.set(key, value);
12
+ } catch {
13
+ // Ignore invalid header names or values
14
+ }
15
+ }
16
+ return parsed;
11
17
  };
12
- export const CaseInsensitiveMap = (map)=>({
13
- get: (key)=>map.get(key.toLowerCase()),
14
- has: (key)=>map.has(key.toLowerCase()),
15
- size: map.size,
16
- keys: ()=>map.keys(),
17
- values: ()=>map.values(),
18
- entries: ()=>map.entries(),
19
- forEach: (callback, thisArg)=>map.forEach(callback, thisArg)
20
- });
@@ -1,12 +1,25 @@
1
1
  import type { MiddlewareObj } from '@middy/core';
2
2
  import type { Static, StaticDecode, TSchema } from '@sinclair/typebox';
3
3
  import type { ValueError } from '@sinclair/typebox/compiler';
4
- import type { APIGatewayProxyEventV2, APIGatewayProxyStructuredResultV2, Context } from 'aws-lambda';
4
+ import type { APIGatewayProxyEvent, APIGatewayProxyEventV2, APIGatewayProxyStructuredResultV2, Context } from 'aws-lambda';
5
5
  export declare class ValidationFailedError extends Error {
6
6
  readonly errors: ValueError[];
7
7
  constructor(errors: ValueError[], message?: string);
8
8
  }
9
- export declare const validateInput: <Schema extends TSchema>(schema: Schema, mapInput?: (e: APIGatewayProxyEventV2) => unknown) => MiddlewareObj<APIGatewayProxyEventV2, APIGatewayProxyStructuredResultV2, Error, Context & ValidInput<Schema>>;
9
+ /**
10
+ * The event properties used by `validateInput` to construct the default input.
11
+ *
12
+ * Both REST API (v1, `APIGatewayProxyEvent`) and HTTP API (v2, `APIGatewayProxyEventV2`) events satisfy this.
13
+ */
14
+ type ValidateInputEvent = Pick<APIGatewayProxyEvent | APIGatewayProxyEventV2, 'headers' | 'body' | 'pathParameters' | 'queryStringParameters'>;
15
+ /**
16
+ * Validates the input of a Lambda function.
17
+ *
18
+ * Use the `Event` type parameter to specify the type of the event passed
19
+ * to `mapInput`, e.g. `validateInput<typeof Schema, APIGatewayProxyEvent>(Schema, (event) => ({ method: event.httpMethod }))`.
20
+ * It is inferred from `mapInput` if its parameter is typed explicitly.
21
+ */
22
+ export declare const validateInput: <Schema extends TSchema, Event extends ValidateInputEvent = APIGatewayProxyEventV2>(schema: Schema, mapInput?: (e: Event) => unknown) => MiddlewareObj<Event, APIGatewayProxyStructuredResultV2, Error, Context & ValidInput<Schema>>;
10
23
  export type ValidInput<Schema extends TSchema> = {
11
24
  /**
12
25
  * @deprecated Use `decodedInput` instead
@@ -16,3 +29,4 @@ export type ValidInput<Schema extends TSchema> = {
16
29
  validInput: Static<Schema>;
17
30
  decodedInput: StaticDecode<Schema>;
18
31
  };
32
+ export {};
@@ -10,7 +10,13 @@ export class ValidationFailedError extends Error {
10
10
  this.name = 'ValidationFailedError';
11
11
  }
12
12
  }
13
- export const validateInput = (schema, mapInput)=>{
13
+ /**
14
+ * Validates the input of a Lambda function.
15
+ *
16
+ * Use the `Event` type parameter to specify the type of the event passed
17
+ * to `mapInput`, e.g. `validateInput<typeof Schema, APIGatewayProxyEvent>(Schema, (event) => ({ method: event.httpMethod }))`.
18
+ * It is inferred from `mapInput` if its parameter is typed explicitly.
19
+ */ export const validateInput = (schema, mapInput)=>{
14
20
  const v = validateWithTypeBox(schema);
15
21
  return {
16
22
  before: async (req)=>{
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nrfcloud/lambda-helpers",
3
- "version": "1.1.8",
3
+ "version": "2.0.0",
4
4
  "description": "Helper functions for AWS Lambda functions used in nRF Cloud.",
5
5
  "homepage": "https://github.com/nRFCloud/lambda-helpers#readme",
6
6
  "bugs": {
@@ -34,12 +34,12 @@
34
34
  "access": "public"
35
35
  },
36
36
  "devDependencies": {
37
- "@bifravst/prettier-config": "2.0.5",
37
+ "@bifravst/prettier-config": "2.0.7",
38
38
  "@swc/core": "1.16.22",
39
39
  "@types/aws-lambda": "8.10.164",
40
40
  "@types/node": "26.6.6",
41
41
  "@typescript/native": "npm:typescript@7.0.2",
42
- "prettier": "3.9.10",
42
+ "prettier": "3.10.1",
43
43
  "typescript": "npm:@typescript/typescript6@6.0.2"
44
44
  },
45
45
  "prettier": "@bifravst/prettier-config",