mirror of
https://github.com/expo/rudder-sdk-node.git
synced 2026-11-14 19:12:20 -10:00
459 lines
16 KiB
JavaScript
459 lines
16 KiB
JavaScript
"use strict";
|
||
/**
|
||
* This client was automatically generated by RudderStack Typewriter. ** Do Not Edit **
|
||
*/
|
||
var __importDefault =
|
||
(this && this.__importDefault) ||
|
||
function(mod) {
|
||
return mod && mod.__esModule ? mod : { default: mod };
|
||
};
|
||
Object.defineProperty(exports, "__esModule", { value: true });
|
||
exports.userSignedOut = exports.signInSucceeded = exports.signInSubmitted = exports.signInFailed = exports.setTypewriterOptions = exports.defaultValidationErrorHandler = void 0;
|
||
/**
|
||
* Ajv is a peer dependency for development builds. It's used to apply run-time validation
|
||
* to message payloads before passing them on to the underlying analytics instance.
|
||
*
|
||
* Note that the production bundle does not depend on Ajv.
|
||
*
|
||
* You can install it with: `npm install --save-dev ajv`.
|
||
*/
|
||
const ajv_1 = __importDefault(require("ajv"));
|
||
/**
|
||
* The default handler that is fired if none is supplied with setTypewriterOptions.
|
||
* If NODE_ENV="test", this handler will throw an error. Otherwise, it will log
|
||
* a warning message to the console.
|
||
*/
|
||
const defaultValidationErrorHandler = (message, violations) => {
|
||
const msg = JSON.stringify(
|
||
{
|
||
type: "Typewriter JSON Schema Validation Error",
|
||
description:
|
||
`You made an analytics call (${message.event}) using Typewriter that doesn't match the ` +
|
||
"Tracking Plan spec.",
|
||
errors: violations
|
||
},
|
||
undefined,
|
||
2
|
||
);
|
||
if (process.env.NODE_ENV === "test") {
|
||
throw new Error(msg);
|
||
}
|
||
console.warn(msg);
|
||
};
|
||
exports.defaultValidationErrorHandler = defaultValidationErrorHandler;
|
||
let onViolation = exports.defaultValidationErrorHandler;
|
||
const missingAnalyticsNodeError = new Error(`You must set an analytics-node instance:
|
||
|
||
> const RudderAnalytics = require('@rudderstack/rudder-sdk-node');
|
||
> const { setTypewriterOptions } = require('./analytics')
|
||
>
|
||
> const analytics = new RudderAnalytics(WRITE_KEY, DATA_PLANE_URL/v1/batch)
|
||
> setTypewriterOptions({
|
||
> analytics: analytics,
|
||
> })
|
||
|
||
For more information on analytics-node, see: https://docs.rudderstack.com/stream-sources/rudderstack-sdk-integration-guides/rudderstack-node-sdk#installing-the-rudderstack-node-js-sdk
|
||
`);
|
||
let analytics = () => {
|
||
throw missingAnalyticsNodeError;
|
||
};
|
||
/**
|
||
* Updates the run-time configuration of this Typewriter client.
|
||
* This function must be called with a configured analytics-node instance before firing
|
||
* any analytics calls, or else a `missingAnalyticsNodeError` error will be thrown.
|
||
*
|
||
* @param {TypewriterOptions} options - the options to upsert
|
||
*
|
||
* @typedef {Object} TypewriterOptions
|
||
* @property {Rudder.AnalyticsNode} analytics - Underlying analytics instance where analytics
|
||
* calls are forwarded on to.
|
||
* @property {Function} [onViolation] - Handler fired when if an event does not match its spec. This handler does not fire in
|
||
* production mode, because it requires inlining the full JSON Schema spec for each event in your Tracking Plan. By default,
|
||
* it will throw errors if NODE_ENV="test" so that tests will fail if a message does not match the spec. Otherwise, errors
|
||
* will be logged to stderr.
|
||
*/
|
||
function setTypewriterOptions(options) {
|
||
analytics = options.analytics ? () => options.analytics : analytics;
|
||
onViolation = options.onViolation || onViolation;
|
||
}
|
||
exports.setTypewriterOptions = setTypewriterOptions;
|
||
/**
|
||
* Validates a message against a JSON Schema using Ajv. If the message
|
||
* is invalid, the `onViolation` handler will be called.
|
||
*/
|
||
function validateAgainstSchema(message, schema) {
|
||
const ajv = new ajv_1.default({ allErrors: true, verbose: true });
|
||
ajv.addMetaSchema(require("ajv/lib/refs/json-schema-draft-06.json"));
|
||
if (!ajv.validate(schema, message) && ajv.errors) {
|
||
onViolation(message, ajv.errors);
|
||
}
|
||
}
|
||
/**
|
||
* Helper to attach metadata on Typewriter to outbound requests.
|
||
* This is used for attribution and debugging by the RudderStack team.
|
||
*/
|
||
function withTypewriterContext(message) {
|
||
return {
|
||
...message,
|
||
context: {
|
||
...(message.context || {}),
|
||
typewriter: {
|
||
language: "javascript",
|
||
version: "7.4.1"
|
||
}
|
||
}
|
||
};
|
||
}
|
||
/**
|
||
* A message payload for an analytics-node `.track()` call.
|
||
* See: https://docs.rudderstack.com/stream-sources/rudderstack-sdk-integration-guides/rudderstack-node-sdk#track
|
||
*
|
||
* @typedef TrackMessage<PropertiesType>
|
||
* @property {string | number} [userId] - The ID for this user in your database.
|
||
* @property {string | number} [anonymousId] - An ID to associated with the user when you don’t know who they are.
|
||
* @property {PropertiesType} [properties] - A dictionary of properties for the event.
|
||
* @property {Date} [timestamp] - A Javascript date object representing when the track took place. If the track
|
||
* just happened, leave it out and we’ll use the server’s time. If you’re importing data from the past make
|
||
* sure you to send a timestamp.
|
||
* @template PropertiesType
|
||
*/
|
||
/**
|
||
* @typedef Dimensions
|
||
* @property {number} height -
|
||
* @property {number} length -
|
||
* @property {number} width -
|
||
*/
|
||
/**
|
||
* @typedef SignInFailed
|
||
* @property {Dimensions} [dimensions] -
|
||
* @property {string} id - The user's ID.
|
||
* @property {number} [numAttempts] - How many times the user has attempted to sign-in.
|
||
* @property {boolean} [rememberMe] - Whether the user has indicated that the browser should store their login credentials.
|
||
*/
|
||
/**
|
||
* @typedef SignInSubmitted
|
||
* @property {string} id - The user's ID.
|
||
* @property {number} [numAttempts] - How many times the user has attempted to sign-in.
|
||
* @property {boolean} [rememberMe] - Whether the user has indicated that the browser should store their login credentials.
|
||
*/
|
||
/**
|
||
* @typedef SignInSucceeded
|
||
* @property {string} id - The user's ID.
|
||
* @property {number} [numAttempts] - How many times the user has attempted to sign-in.
|
||
* @property {boolean} [rememberMe] - Whether the user has indicated that the browser should store their login credentials.
|
||
*/
|
||
/**
|
||
* @typedef UserSignedOut
|
||
* @property {string} id - The user's ID.
|
||
* @property {number} [numAttempts] - How many times the user has attempted to sign-in.
|
||
* @property {boolean} [rememberMe] - Whether the user has indicated that the browser should store their login credentials.
|
||
*/
|
||
/**
|
||
* Fired after a user's signin attempt fails to pass validation.
|
||
*
|
||
* @param {TrackMessage<SignInFailed>} message - The analytics properties that will be sent to RudderStack.
|
||
* @param {Function} [callback] - An optional callback called after a short timeout after the analytics
|
||
* call is fired.
|
||
*/
|
||
function signInFailed(message, callback) {
|
||
const msg = withTypewriterContext({
|
||
properties: {},
|
||
...message,
|
||
event: "Sign In Failed"
|
||
});
|
||
const schema = {
|
||
$schema: "http://json-schema.org/draft-07/schema#",
|
||
description:
|
||
"Fired after a user's signin attempt fails to pass validation.",
|
||
properties: {
|
||
context: {},
|
||
properties: {
|
||
properties: {
|
||
dimensions: {
|
||
properties: {
|
||
height: {
|
||
type: "number"
|
||
},
|
||
length: {
|
||
type: "number"
|
||
},
|
||
width: {
|
||
type: "number"
|
||
}
|
||
},
|
||
required: ["length", "width", "height"],
|
||
type: "object"
|
||
},
|
||
id: {
|
||
description: "The user's ID.",
|
||
type: "string"
|
||
},
|
||
numAttempts: {
|
||
description: "How many times the user has attempted to sign-in.",
|
||
type: "integer"
|
||
},
|
||
rememberMe: {
|
||
description:
|
||
"Whether the user has indicated that the browser should store their login credentials.",
|
||
type: "boolean"
|
||
}
|
||
},
|
||
required: ["id"],
|
||
type: "object"
|
||
},
|
||
traits: {
|
||
type: "object"
|
||
}
|
||
},
|
||
required: ["properties"],
|
||
title: "Sign In Failed",
|
||
type: "object"
|
||
};
|
||
validateAgainstSchema(msg, schema);
|
||
const a = analytics();
|
||
if (a) {
|
||
a.track(msg, callback);
|
||
} else {
|
||
throw missingAnalyticsNodeError;
|
||
}
|
||
}
|
||
exports.signInFailed = signInFailed;
|
||
/**
|
||
* Fired when a user submits a sign in, prior to validating that user's login.
|
||
*
|
||
* @param {TrackMessage<SignInSubmitted>} message - The analytics properties that will be sent to RudderStack.
|
||
* @param {Function} [callback] - An optional callback called after a short timeout after the analytics
|
||
* call is fired.
|
||
*/
|
||
function signInSubmitted(message, callback) {
|
||
const msg = withTypewriterContext({
|
||
properties: {},
|
||
...message,
|
||
event: "Sign In Submitted"
|
||
});
|
||
const schema = {
|
||
$schema: "http://json-schema.org/draft-07/schema#",
|
||
description:
|
||
"Fired when a user submits a sign in, prior to validating that user's login.",
|
||
properties: {
|
||
context: {},
|
||
properties: {
|
||
properties: {
|
||
id: {
|
||
description: "The user's ID.",
|
||
type: "string"
|
||
},
|
||
numAttempts: {
|
||
description: "How many times the user has attempted to sign-in.",
|
||
type: "integer"
|
||
},
|
||
rememberMe: {
|
||
description:
|
||
"Whether the user has indicated that the browser should store their login credentials.",
|
||
type: "boolean"
|
||
}
|
||
},
|
||
required: ["id"],
|
||
type: "object"
|
||
},
|
||
traits: {
|
||
type: "object"
|
||
}
|
||
},
|
||
title: "Sign In Submitted",
|
||
type: "object"
|
||
};
|
||
validateAgainstSchema(msg, schema);
|
||
const a = analytics();
|
||
if (a) {
|
||
a.track(msg, callback);
|
||
} else {
|
||
throw missingAnalyticsNodeError;
|
||
}
|
||
}
|
||
exports.signInSubmitted = signInSubmitted;
|
||
/**
|
||
* Fired when a user successfully submits a sign in, prior to redirecting into the app.
|
||
*
|
||
* @param {TrackMessage<SignInSucceeded>} message - The analytics properties that will be sent to RudderStack.
|
||
* @param {Function} [callback] - An optional callback called after a short timeout after the analytics
|
||
* call is fired.
|
||
*/
|
||
function signInSucceeded(message, callback) {
|
||
const msg = withTypewriterContext({
|
||
properties: {},
|
||
...message,
|
||
event: "Sign In Succeeded"
|
||
});
|
||
const schema = {
|
||
$schema: "http://json-schema.org/draft-07/schema#",
|
||
description:
|
||
"Fired when a user successfully submits a sign in, prior to redirecting into the app.",
|
||
properties: {
|
||
context: {},
|
||
properties: {
|
||
properties: {
|
||
id: {
|
||
description: "The user's ID.",
|
||
type: "string"
|
||
},
|
||
numAttempts: {
|
||
description: "How many times the user has attempted to sign-in.",
|
||
type: "integer"
|
||
},
|
||
rememberMe: {
|
||
description:
|
||
"Whether the user has indicated that the browser should store their login credentials.",
|
||
type: "boolean"
|
||
}
|
||
},
|
||
required: ["id"],
|
||
type: "object"
|
||
},
|
||
traits: {
|
||
type: "object"
|
||
}
|
||
},
|
||
title: "Sign In Succeeded",
|
||
type: "object"
|
||
};
|
||
validateAgainstSchema(msg, schema);
|
||
const a = analytics();
|
||
if (a) {
|
||
a.track(msg, callback);
|
||
} else {
|
||
throw missingAnalyticsNodeError;
|
||
}
|
||
}
|
||
exports.signInSucceeded = signInSucceeded;
|
||
/**
|
||
* Fired when a user successfully submits a sign in, prior to redirecting into the app.
|
||
*
|
||
* @param {TrackMessage<UserSignedOut>} message - The analytics properties that will be sent to RudderStack.
|
||
* @param {Function} [callback] - An optional callback called after a short timeout after the analytics
|
||
* call is fired.
|
||
*/
|
||
function userSignedOut(message, callback) {
|
||
const msg = withTypewriterContext({
|
||
properties: {},
|
||
...message,
|
||
event: "User Signed Out"
|
||
});
|
||
const schema = {
|
||
$schema: "http://json-schema.org/draft-07/schema#",
|
||
description:
|
||
"Fired when a user successfully submits a sign in, prior to redirecting into the app.",
|
||
properties: {
|
||
context: {},
|
||
properties: {
|
||
properties: {
|
||
id: {
|
||
description: "The user's ID.",
|
||
type: "string"
|
||
},
|
||
numAttempts: {
|
||
description: "How many times the user has attempted to sign-in.",
|
||
type: "integer"
|
||
},
|
||
rememberMe: {
|
||
description:
|
||
"Whether the user has indicated that the browser should store their login credentials.",
|
||
type: "boolean"
|
||
}
|
||
},
|
||
required: ["id"],
|
||
type: "object"
|
||
},
|
||
traits: {
|
||
type: "object"
|
||
}
|
||
},
|
||
title: "User Signed Out",
|
||
type: "object"
|
||
};
|
||
validateAgainstSchema(msg, schema);
|
||
const a = analytics();
|
||
if (a) {
|
||
a.track(msg, callback);
|
||
} else {
|
||
throw missingAnalyticsNodeError;
|
||
}
|
||
}
|
||
exports.userSignedOut = userSignedOut;
|
||
const clientAPI = {
|
||
/**
|
||
* Updates the run-time configuration of this Typewriter client.
|
||
* This function must be called with a configured analytics-node instance before firing
|
||
* any analytics calls, or else a `missingAnalyticsNodeError` error will be thrown.
|
||
*
|
||
* @param {TypewriterOptions} options - the options to upsert
|
||
*
|
||
* @typedef {Object} TypewriterOptions
|
||
* @property {Rudder.AnalyticsNode} analytics - Underlying analytics instance where analytics
|
||
* calls are forwarded on to.
|
||
* @property {Function} [onViolation] - Handler fired when if an event does not match its spec. This handler does not fire in
|
||
* production mode, because it requires inlining the full JSON Schema spec for each event in your Tracking Plan. By default,
|
||
* it will throw errors if NODE_ENV="test" so that tests will fail if a message does not match the spec. Otherwise, errors
|
||
* will be logged to stderr.
|
||
*/
|
||
setTypewriterOptions,
|
||
/**
|
||
* Fired after a user's signin attempt fails to pass validation.
|
||
*
|
||
* @param {TrackMessage<SignInFailed>} message - The analytics properties that will be sent to RudderStack.
|
||
* @param {Function} [callback] - An optional callback called after a short timeout after the analytics
|
||
* call is fired.
|
||
*/
|
||
signInFailed,
|
||
/**
|
||
* Fired when a user submits a sign in, prior to validating that user's login.
|
||
*
|
||
* @param {TrackMessage<SignInSubmitted>} message - The analytics properties that will be sent to RudderStack.
|
||
* @param {Function} [callback] - An optional callback called after a short timeout after the analytics
|
||
* call is fired.
|
||
*/
|
||
signInSubmitted,
|
||
/**
|
||
* Fired when a user successfully submits a sign in, prior to redirecting into the app.
|
||
*
|
||
* @param {TrackMessage<SignInSucceeded>} message - The analytics properties that will be sent to RudderStack.
|
||
* @param {Function} [callback] - An optional callback called after a short timeout after the analytics
|
||
* call is fired.
|
||
*/
|
||
signInSucceeded,
|
||
/**
|
||
* Fired when a user successfully submits a sign in, prior to redirecting into the app.
|
||
*
|
||
* @param {TrackMessage<UserSignedOut>} message - The analytics properties that will be sent to RudderStack.
|
||
* @param {Function} [callback] - An optional callback called after a short timeout after the analytics
|
||
* call is fired.
|
||
*/
|
||
userSignedOut
|
||
};
|
||
exports.default = new Proxy(clientAPI, {
|
||
get(target, method) {
|
||
if (typeof method === "string" && target.hasOwnProperty(method)) {
|
||
return target[method];
|
||
}
|
||
return () => {
|
||
console.warn(`⚠️ You made an analytics call (${String(
|
||
method
|
||
)}) that can't be found. Either:
|
||
a) Re-generate your typewriter client: \`npx typewriter\`
|
||
b) Add it to your Tracking Plan: https://app.segment.com/segment_prod/protocols/tracking-plans/rs_1SWT1hC4xpwcltyCUud43XMIlQo`);
|
||
const a = analytics();
|
||
if (a) {
|
||
a.track(
|
||
withTypewriterContext({
|
||
event: "Unknown Analytics Call Fired",
|
||
properties: {
|
||
method
|
||
},
|
||
userId: "typewriter"
|
||
})
|
||
);
|
||
}
|
||
};
|
||
}
|
||
});
|