Skip to content

Commit

Permalink
header mapping
Browse files Browse the repository at this point in the history
  • Loading branch information
alexanderkirtzel committed Nov 19, 2024
1 parent dffebb4 commit 50ef251
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 5 deletions.
27 changes: 26 additions & 1 deletion packages/connectors/gcp/src/__tests__/index.test.ts
Original file line number Diff line number Diff line change
@@ -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',
});
});
});
25 changes: 21 additions & 4 deletions packages/connectors/gcp/src/index.ts
Original file line number Diff line number Diff line change
@@ -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<string, keyof typeof context> = {
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;
}

0 comments on commit 50ef251

Please sign in to comment.