Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve setUser function #442

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 46 additions & 35 deletions library/agent/context/user.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,26 @@ t.beforeEach(() => {
createTestAgent();
});

t.test("usage outside of context", async (t) => {
let logs: string[] = [];
wrap(console, "warn", function warn() {
return function warn(message: string) {
logs.push(message);
};
});

setUser({ id: "id" });

t.same(logs, [
"setUser(...) was called without a context. The user will not be tracked. Make sure to call setUser(...) within an HTTP request. If you're using serverless functions, make sure to use the handler wrapper provided by Zen.",
]);

// Should not log again
logs = [];
setUser({ id: "id" });
t.same(logs, []);
});

t.test("it does not set user if empty id", async (t) => {
const context = createContext();

Expand Down Expand Up @@ -82,45 +102,36 @@ t.test("it logs when setUser has invalid input", async () => {
const logger = new LoggerForTesting();
createTestAgent({ logger });

// @ts-expect-error User should be an object
setUser(1);
t.same(logger.getMessages(), [
"setUser(...) expects an object with 'id' and 'name' properties, found number instead.",
]);
logger.clear();

// @ts-expect-error User is undefined
setUser(undefined);
t.same(logger.getMessages(), [
"setUser(...) can not be called with null or undefined.",
]);
logger.clear();
const context = createContext();

// @ts-expect-error ID should be string or number
setUser({ id: {} });
t.same(logger.getMessages(), [
"setUser(...) expects an object with 'id' property of type string or number, found object instead.",
]);
logger.clear();
runWithContext(context, () => {
// @ts-expect-error User should be an object
setUser(1);
t.same(logger.getMessages(), [
"setUser(...) expects an object with 'id' and 'name' properties, found number instead.",
]);
logger.clear();

setUser({ id: "" });
t.same(logger.getMessages(), [
"setUser(...) expects an object with 'id' property non-empty string.",
]);
logger.clear();
// @ts-expect-error ID should be string or number
setUser({ id: {} });
t.same(logger.getMessages(), [
"setUser(...) expects an object with 'id' property of type string or number, found object instead.",
]);
logger.clear();

// @ts-expect-error ID is missing
setUser({ name: "name" });
t.same(logger.getMessages(), [
"setUser(...) expects an object with 'id' property.",
]);
logger.clear();
setUser({ id: "" });
t.same(logger.getMessages(), [
"setUser(...) expects an object with 'id' property non-empty string.",
]);
logger.clear();

// @ts-expect-error Testing invalid input
setUser(null);
t.same(logger.getMessages(), [
"setUser(...) can not be called with null or undefined.",
]);
// @ts-expect-error ID is missing
setUser({ name: "name" });
t.same(logger.getMessages(), [
"setUser(...) expects an object with 'id' property.",
]);
logger.clear();
});
});

t.test(
Expand Down
30 changes: 20 additions & 10 deletions library/agent/context/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,14 @@ export function setUser(u: { id: string | number; name?: string }) {
return;
}

const user = u as unknown;

if (user === null || user === undefined) {
agent.log(`setUser(...) can not be called with null or undefined.`);
const context = ContextStorage.getStore();
if (!context) {
logWarningSetUserCalledWithoutContext();
return;
}

const user = u as unknown;

if (!isPlainObject(user)) {
agent.log(
`setUser(...) expects an object with 'id' and 'name' properties, found ${typeof user} instead.`
Expand Down Expand Up @@ -50,12 +51,6 @@ export function setUser(u: { id: string | number; name?: string }) {
validatedUser.name = user.name;
}

const context = ContextStorage.getStore();

if (!context) {
return;
}

if (context.executedMiddleware) {
logWarningSetUserCalledAfterMiddleware();
}
Expand Down Expand Up @@ -85,3 +80,18 @@ function logWarningSetUserCalledAfterMiddleware() {

loggedWarningSetUserCalledAfterMiddleware = true;
}

let loggedWarningSetUserCalledWithoutContext = false;

function logWarningSetUserCalledWithoutContext() {
if (loggedWarningSetUserCalledWithoutContext) {
return;
}

// eslint-disable-next-line no-console
console.warn(
"setUser(...) was called without a context. The user will not be tracked. Make sure to call setUser(...) within an HTTP request. If you're using serverless functions, make sure to use the handler wrapper provided by Zen."
);

loggedWarningSetUserCalledWithoutContext = true;
}
Loading