From 50ef2516eac022db92e6dcfcc10e373ff366ee82 Mon Sep 17 00:00:00 2001 From: alexander Date: Tue, 19 Nov 2024 13:21:46 +0100 Subject: [PATCH] header mapping --- .../gcp/src/__tests__/index.test.ts | 27 ++++++++++++++++++- packages/connectors/gcp/src/index.ts | 25 ++++++++++++++--- 2 files changed, 47 insertions(+), 5 deletions(-) diff --git a/packages/connectors/gcp/src/__tests__/index.test.ts b/packages/connectors/gcp/src/__tests__/index.test.ts index 705f0788..111e4262 100644 --- a/packages/connectors/gcp/src/__tests__/index.test.ts +++ b/packages/connectors/gcp/src/__tests__/index.test.ts @@ -1,9 +1,34 @@ +import type { Request as GCPRequest } from '@google-cloud/functions-framework'; import { connectorGCPHttpFunction } from '..'; describe('connector GCP', () => { + const request: GCPRequest = { + get: (header: string) => { + return { + origin: 'localhost', + 'X-Real-Ip': '127.0.0.1', + 'User-Agent': 'Mozilla/5.0', + 'Accept-Language': 'ts', + 'Accept-Encoding': 'gzip', + 'X-AppEngine-Country': 'DE', + 'X-AppEngine-Region': 'Hamburg', + 'X-AppEngine-City': 'Hamburg', + }[header]; + }, + } as GCPRequest; + beforeEach(() => {}); test('init new', () => { - expect(connectorGCPHttpFunction).toBeDefined(); + expect(connectorGCPHttpFunction(request)).toStrictEqual({ + city: 'Hamburg', + country: 'DE', + encoding: 'gzip', + ip: '127.0.0.1', + language: 'ts', + origin: 'localhost', + region: 'Hamburg', + userAgent: 'Mozilla/5.0', + }); }); }); diff --git a/packages/connectors/gcp/src/index.ts b/packages/connectors/gcp/src/index.ts index 7d2ca36c..95f43567 100644 --- a/packages/connectors/gcp/src/index.ts +++ b/packages/connectors/gcp/src/index.ts @@ -1,8 +1,25 @@ -import type { Request } from '@google-cloud/functions-framework'; +import type { Request } from '@elbwalker/types'; +import type { Request as GCPRequest } from '@google-cloud/functions-framework'; export * as ConnectorGCP from './types'; -export function connectorGCPHttpFunction(request: Request): undefined { - console.log('🚀 ~ request:', request); - return; +export function connectorGCPHttpFunction(request: GCPRequest): Request.Context { + const context: Request.Context = {}; + const headerMapping: Record = { + origin: 'origin', + 'X-Real-Ip': 'ip', + 'User-Agent': 'userAgent', + 'Accept-Language': 'language', + 'Accept-Encoding': 'encoding', + 'X-AppEngine-Country': 'country', + 'X-AppEngine-Region': 'region', + 'X-AppEngine-City': 'city', + }; + + Object.entries(headerMapping).forEach(([header, key]) => { + const value = request.get(header); + if (value) context[key] = value; + }); + + return context; }