Skip to content

Commit 50ef251

Browse files
header mapping
1 parent dffebb4 commit 50ef251

File tree

2 files changed

+47
-5
lines changed

2 files changed

+47
-5
lines changed
Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,34 @@
1+
import type { Request as GCPRequest } from '@google-cloud/functions-framework';
12
import { connectorGCPHttpFunction } from '..';
23

34
describe('connector GCP', () => {
5+
const request: GCPRequest = {
6+
get: (header: string) => {
7+
return {
8+
origin: 'localhost',
9+
'X-Real-Ip': '127.0.0.1',
10+
'User-Agent': 'Mozilla/5.0',
11+
'Accept-Language': 'ts',
12+
'Accept-Encoding': 'gzip',
13+
'X-AppEngine-Country': 'DE',
14+
'X-AppEngine-Region': 'Hamburg',
15+
'X-AppEngine-City': 'Hamburg',
16+
}[header];
17+
},
18+
} as GCPRequest;
19+
420
beforeEach(() => {});
521

622
test('init new', () => {
7-
expect(connectorGCPHttpFunction).toBeDefined();
23+
expect(connectorGCPHttpFunction(request)).toStrictEqual({
24+
city: 'Hamburg',
25+
country: 'DE',
26+
encoding: 'gzip',
27+
ip: '127.0.0.1',
28+
language: 'ts',
29+
origin: 'localhost',
30+
region: 'Hamburg',
31+
userAgent: 'Mozilla/5.0',
32+
});
833
});
934
});
Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,25 @@
1-
import type { Request } from '@google-cloud/functions-framework';
1+
import type { Request } from '@elbwalker/types';
2+
import type { Request as GCPRequest } from '@google-cloud/functions-framework';
23

34
export * as ConnectorGCP from './types';
45

5-
export function connectorGCPHttpFunction(request: Request): undefined {
6-
console.log('🚀 ~ request:', request);
7-
return;
6+
export function connectorGCPHttpFunction(request: GCPRequest): Request.Context {
7+
const context: Request.Context = {};
8+
const headerMapping: Record<string, keyof typeof context> = {
9+
origin: 'origin',
10+
'X-Real-Ip': 'ip',
11+
'User-Agent': 'userAgent',
12+
'Accept-Language': 'language',
13+
'Accept-Encoding': 'encoding',
14+
'X-AppEngine-Country': 'country',
15+
'X-AppEngine-Region': 'region',
16+
'X-AppEngine-City': 'city',
17+
};
18+
19+
Object.entries(headerMapping).forEach(([header, key]) => {
20+
const value = request.get(header);
21+
if (value) context[key] = value;
22+
});
23+
24+
return context;
825
}

0 commit comments

Comments
 (0)