Skip to content

Commit 2cdf175

Browse files
authored
chore: expose transaction context propagation to the server sdk only (#590)
## This PR - removes transaction context propagation from web SDK - refactored the transaction context propagation logic to live under the server SDK ### Notes Transaction context propagation only makes sense on the server and was simply ignored by the client. This removes the methods from the client sdk to avoid confusion. This is a **breaking change** in the web SDK. However, it's sub 1.0 and this feature effectively had no effect if it was used. Currently, this is not marked as a breaking change to avoid releasing a major version. @toddbaert do you know if it's possible to mark the server sdk changes as a chore and the web sdk changes as a breaking fix? Signed-off-by: Michael Beemer <[email protected]> --------- Signed-off-by: Michael Beemer <[email protected]>
1 parent 5a1e7fe commit 2cdf175

File tree

8 files changed

+51
-48
lines changed

8 files changed

+51
-48
lines changed

Diff for: packages/server/src/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@ export * from './client';
22
export * from './provider';
33
export * from './evaluation';
44
export * from './open-feature';
5+
export * from './transaction-context';
56
export * from '@openfeature/shared';

Diff for: packages/server/src/open-feature.ts

+44-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@ import {
66
objectOrUndefined,
77
stringOrUndefined,
88
} from '@openfeature/shared';
9+
import {
10+
ManageTransactionContextPropagator,
11+
NOOP_TRANSACTION_CONTEXT_PROPAGATOR,
12+
TransactionContext,
13+
TransactionContextPropagator,
14+
} from './transaction-context';
915
import { Client, OpenFeatureClient } from './client';
1016

1117
// use a symbol as a key for the global singleton
@@ -16,7 +22,11 @@ type OpenFeatureGlobal = {
1622
};
1723
const _globalThis = globalThis as OpenFeatureGlobal;
1824

19-
export class OpenFeatureAPI extends OpenFeatureCommonAPI<Provider> implements ManageContext<OpenFeatureAPI> {
25+
export class OpenFeatureAPI
26+
extends OpenFeatureCommonAPI<Provider>
27+
implements ManageContext<OpenFeatureAPI>, ManageTransactionContextPropagator<OpenFeatureCommonAPI<Provider>>
28+
{
29+
private _transactionContextPropagator: TransactionContextPropagator = NOOP_TRANSACTION_CONTEXT_PROPAGATOR;
2030
protected _defaultProvider: Provider = NOOP_PROVIDER;
2131

2232
private constructor() {
@@ -111,6 +121,39 @@ export class OpenFeatureAPI extends OpenFeatureCommonAPI<Provider> implements Ma
111121
clearProviders(): Promise<void> {
112122
return super.clearProvidersAndSetDefault(NOOP_PROVIDER);
113123
}
124+
125+
setTransactionContextPropagator(
126+
transactionContextPropagator: TransactionContextPropagator
127+
): OpenFeatureCommonAPI<Provider> {
128+
const baseMessage = 'Invalid TransactionContextPropagator, will not be set: ';
129+
if (typeof transactionContextPropagator?.getTransactionContext !== 'function') {
130+
this._logger.error(`${baseMessage}: getTransactionContext is not a function.`);
131+
} else if (typeof transactionContextPropagator?.setTransactionContext !== 'function') {
132+
this._logger.error(`${baseMessage}: setTransactionContext is not a function.`);
133+
} else {
134+
this._transactionContextPropagator = transactionContextPropagator;
135+
}
136+
return this;
137+
}
138+
139+
setTransactionContext<R>(
140+
transactionContext: TransactionContext,
141+
callback: (...args: unknown[]) => R,
142+
...args: unknown[]
143+
): void {
144+
this._transactionContextPropagator.setTransactionContext(transactionContext, callback, ...args);
145+
}
146+
147+
getTransactionContext(): TransactionContext {
148+
try {
149+
return this._transactionContextPropagator.getTransactionContext();
150+
} catch (err: unknown) {
151+
const error = err as Error | undefined;
152+
this._logger.error(`Error getting transaction context: ${error?.message}, returning empty context.`);
153+
this._logger.error(error?.stack);
154+
return {};
155+
}
156+
}
114157
}
115158

116159
/**

Diff for: packages/shared/src/transaction-context/no-op-transaction-context-propagator.ts renamed to packages/server/src/transaction-context/no-op-transaction-context-propagator.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { EvaluationContext } from '../evaluation';
1+
import { EvaluationContext } from '@openfeature/shared';
22
import { TransactionContextPropagator } from './transaction-context';
33

44
class NoopTransactionContextPropagator implements TransactionContextPropagator {

Diff for: packages/shared/src/transaction-context/transaction-context.ts renamed to packages/server/src/transaction-context/transaction-context.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { EvaluationContext } from '../evaluation';
1+
import { EvaluationContext } from '@openfeature/shared';
22

33
/**
44
* Transaction context is a mechanism for adding transaction specific context that

Diff for: packages/shared/src/index.ts

-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,5 @@ export * from './events';
66
export * from './logger';
77
export * from './provider';
88
export * from './evaluation';
9-
export * from './transaction-context';
109
export * from './type-guards';
1110
export * from './open-feature';

Diff for: packages/shared/src/open-feature.ts

+1-43
Original file line numberDiff line numberDiff line change
@@ -6,24 +6,13 @@ import { isDefined } from './filter';
66
import { EvaluationLifeCycle, Hook } from './hooks';
77
import { DefaultLogger, Logger, ManageLogger, SafeLogger } from './logger';
88
import { CommonProvider, ProviderMetadata, ProviderStatus } from './provider';
9-
import {
10-
ManageTransactionContextPropagator,
11-
NOOP_TRANSACTION_CONTEXT_PROPAGATOR,
12-
TransactionContext,
13-
TransactionContextPropagator,
14-
} from './transaction-context';
159
import { objectOrUndefined, stringOrUndefined } from './type-guards';
1610
import { Paradigm } from './types';
1711

1812
export abstract class OpenFeatureCommonAPI<P extends CommonProvider = CommonProvider>
19-
implements
20-
Eventing,
21-
EvaluationLifeCycle<OpenFeatureCommonAPI<P>>,
22-
ManageLogger<OpenFeatureCommonAPI<P>>,
23-
ManageTransactionContextPropagator<OpenFeatureCommonAPI<P>>
13+
implements Eventing, EvaluationLifeCycle<OpenFeatureCommonAPI<P>>, ManageLogger<OpenFeatureCommonAPI<P>>
2414
{
2515
protected _hooks: Hook[] = [];
26-
protected _transactionContextPropagator: TransactionContextPropagator = NOOP_TRANSACTION_CONTEXT_PROPAGATOR;
2716
protected _context: EvaluationContext = {};
2817
protected _logger: Logger = new DefaultLogger();
2918

@@ -350,35 +339,4 @@ export abstract class OpenFeatureCommonAPI<P extends CommonProvider = CommonProv
350339
this._logger.error(`Error during shutdown of provider ${provider.metadata.name}: ${err}`);
351340
this._logger.error((err as Error)?.stack);
352341
}
353-
354-
setTransactionContextPropagator(transactionContextPropagator: TransactionContextPropagator): OpenFeatureCommonAPI<P> {
355-
const baseMessage = 'Invalid TransactionContextPropagator, will not be set: ';
356-
if (typeof transactionContextPropagator?.getTransactionContext !== 'function') {
357-
this._logger.error(`${baseMessage}: getTransactionContext is not a function.`);
358-
} else if (typeof transactionContextPropagator?.setTransactionContext !== 'function') {
359-
this._logger.error(`${baseMessage}: setTransactionContext is not a function.`);
360-
} else {
361-
this._transactionContextPropagator = transactionContextPropagator;
362-
}
363-
return this;
364-
}
365-
366-
setTransactionContext<R>(
367-
transactionContext: TransactionContext,
368-
callback: (...args: unknown[]) => R,
369-
...args: unknown[]
370-
): void {
371-
this._transactionContextPropagator.setTransactionContext(transactionContext, callback, ...args);
372-
}
373-
374-
getTransactionContext(): TransactionContext {
375-
try {
376-
return this._transactionContextPropagator.getTransactionContext();
377-
} catch (err: unknown) {
378-
const error = err as Error | undefined;
379-
this._logger.error(`Error getting transaction context: ${error?.message}, returning empty context.`);
380-
this._logger.error(error?.stack);
381-
return {};
382-
}
383-
}
384342
}

Diff for: packages/shared/tsconfig.json

+3-1
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,9 @@
9898
},
9999
"include": [
100100
"./src/**/*"
101-
],
101+
,
102+
"../server/src/transaction-context"
103+
],
102104
"exclude": [
103105
"node_modules",
104106
"**/*.test.js"

0 commit comments

Comments
 (0)