1
0
mirror of https://github.com/Unleash/unleash.git synced 2024-10-18 20:09:08 +02:00
unleash.unleash/src/lib/proxy/create-context.ts
Fredrik Strand Oseberg b0626d46bc
fix: respect environment if set on context (#2206)
When using the frontend api (embedded proxy) we should allow the use to self-define the environment on the proxy.
2022-10-19 12:29:00 +02:00

41 lines
1.1 KiB
TypeScript

// Copy of https://github.com/Unleash/unleash-proxy/blob/main/src/create-context.ts.
/* eslint-disable prefer-object-spread */
import { Context } from 'unleash-client';
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
export function createContext(value: any): Context {
const {
appName,
environment,
userId,
sessionId,
remoteAddress,
properties,
...rest
} = value;
// move non root context fields to properties
const context: Context = {
appName,
environment,
userId,
sessionId,
remoteAddress,
properties: Object.assign({}, rest, properties),
};
// Clean undefined properties on the context
const cleanContext = Object.keys(context)
.filter((k) => context[k])
.reduce((a, k) => ({ ...a, [k]: context[k] }), {});
return cleanContext;
}
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
export const enrichContextWithIp = (query: any, ip: string): Context => {
query.remoteAddress = query.remoteAddress || ip;
return createContext(query);
};