Skip to content

Commit 7b5d3df

Browse files
authored
Add public API (#593)
Add public API Co-authored-by: Seth Silesky <silesky@users.noreply.github.com>
1 parent 2feb044 commit 7b5d3df

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+1557
-942
lines changed

.changeset/friendly-birds-happen.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
---
2-
'@segment/analytics-plugin-validation': major
32
'@segment/analytics-core': minor
43
---
54

.changeset/proud-pens-raise.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
'@segment/analytics-core': patch
3+
'@segment/analytics-node': patch
4+
---
5+
6+
Revise NodeJS public API. Fix core so Node SDK waits for plugins to be registered before dispatching any events.

internal/config/src/jest/config.js

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,23 @@
11
const { getJestModuleMap } = require('./get-module-map')
2+
const path = require('path')
23

34
/**
45
* Create Config
56
* @param {import('jest').Config} Overrides.
67
* @param {object} getJestModuleMap options.
78
* @returns {import('jest').Config}
89
*/
9-
const createJestTSConfig = (
10-
{ modulePathIgnorePatterns, testMatch, ...overridesToMerge } = {},
11-
{ packageRoot, skipPackageMap } = {}
12-
) => {
10+
const createJestTSConfig = ({
11+
modulePathIgnorePatterns,
12+
testMatch,
13+
...overridesToMerge
14+
} = {}) => {
15+
const moduleMap = getJestModuleMap()
1316
return {
14-
moduleNameMapper: getJestModuleMap(packageRoot, skipPackageMap),
17+
...(global.JEST_ROOT_CONFIG
18+
? {}
19+
: { displayName: path.basename(process.cwd()) }),
20+
moduleNameMapper: moduleMap,
1521
preset: 'ts-jest',
1622
modulePathIgnorePatterns: [
1723
'<rootDir>/dist/',

internal/config/src/jest/get-module-map.js

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,26 +4,37 @@ const getPackages = require('get-monorepo-packages')
44
const doNotMapPackages = process.env.JEST_SKIP_PACKAGE_MAP === 'true'
55

66
/**
7-
* Allows ts-jest to dynamically resolve packages so "build"
7+
* Create module mapping that resolve packages for ts-jest so typescript compilation happens in-memory
8+
*
89
*/
9-
const getJestModuleMap = (
10-
packageRoot = '../../',
11-
skipPackageMap = doNotMapPackages
12-
) => {
10+
const getJestModuleMap = ({ skipPackageMap = doNotMapPackages } = {}) => {
1311
// get listing of packages in the mono repo
14-
const createLocation = (name) => {
15-
return `<rootDir>/./${name}/src/$1`
12+
13+
/**
14+
* @param location - e.g. "packages/core"
15+
*/
16+
17+
const createPackageMappedPath = (location) => {
18+
// if packageRoot is the global jest file (using projects), our mappers suddenly need
19+
// to be relative to each project -- I have no idea why, seems unintuitive.
20+
// If not root config, equiv to running "yarn test" in an individual package repo (rootDir will be the root package.json)
21+
const moduleBasePath = global.JEST_ROOT_CONFIG
22+
? '<rootDir>/../..'
23+
: '<rootDir>'
24+
return `${moduleBasePath}/${location}/src/$1`
1625
}
26+
// for the sake of getPackages working correctly during a project-wide test run, the working directory must be hardcoded to the root
27+
const packageRoot = global.JEST_ROOT_CONFIG ? '.' : '../../'
1728
const moduleNameMapper = getPackages(packageRoot).reduce(
1829
(acc, el) => ({
1930
...acc,
20-
[`${el.package.name}(.*)$`]: createLocation(el.location),
31+
[`${el.package.name}(.*)$`]: createPackageMappedPath(el.location),
2132
}),
2233
{}
2334
)
2435

2536
return {
26-
'@/(.+)': '<rootdir>/../../src/$1',
37+
'@/(.+)': '<rootDir>/src/$1',
2738
...(skipPackageMap ? {} : moduleNameMapper),
2839
}
2940
}

jest.config.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
const { createJestTSConfig } = require('@internal/config')
2+
3+
/**
4+
* This config detects our monorepo, and allows
5+
* you to run every single package test in a single jest instance using 'yarn jest'.
6+
* Thus, `yarn jest --watch` works as expected (unlike say, when using turborepo)
7+
*/
8+
module.exports = () => {
9+
// we actually need to set a global variable to preserve the correct path mapping when running project-wide jest.
10+
global.JEST_ROOT_CONFIG = true
11+
12+
const config = createJestTSConfig({
13+
projects: ['<rootDir>/packages/*'],
14+
})
15+
return config
16+
}

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
"node": "^14.15.0"
1212
},
1313
"scripts": {
14-
"test": "FORCE_COLOR=1 turbo run test --filter='./packages/*'",
14+
"test": "jest",
1515
"test:scripts": "jest --config scripts/jest.config.js",
1616
"lint": "yarn constraints && turbo run lint",
1717
"build": "turbo run build --filter='./packages/*'",
@@ -37,6 +37,7 @@
3737
"@changesets/cli": "^2.23.2",
3838
"@npmcli/promise-spawn": "^3.0.0",
3939
"@types/jest": "^28.1.1",
40+
"@types/lodash": "^4",
4041
"@typescript-eslint/eslint-plugin": "^5.21.0",
4142
"@typescript-eslint/parser": "^5.21.0",
4243
"concurrently": "^7.2.1",
@@ -47,6 +48,7 @@
4748
"husky": "^8.0.0",
4849
"jest": "^28.1.0",
4950
"lint-staged": "^13.0.0",
51+
"lodash": "^4.17.21",
5052
"node-gyp": "^9.0.0",
5153
"prettier": "^2.6.2",
5254
"ts-jest": "^28.0.4",

packages/browser/package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,6 @@
6969
"@types/jquery": "^3.5.4",
7070
"@types/js-cookie": "3.0.1",
7171
"@types/jsdom": "^16.2.14",
72-
"@types/lodash": "^4.14.161",
7372
"@types/mime": "^2.0.3",
7473
"@types/node": "^12.12.14",
7574
"@types/node-fetch": "^2.5.7",
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { dispatch } from './dispatch'
2+
import { CoreContext } from '../context'
3+
4+
/* Dispatch function, but swallow promise rejections and use event emitter instead */
5+
export const dispatchAndEmit = async (
6+
...[event, queue, emitter, options]: Parameters<typeof dispatch>
7+
) => {
8+
try {
9+
const ctx = await dispatch(event, queue, emitter, options)
10+
if (ctx.failedDelivery()) {
11+
throw ctx
12+
} else {
13+
emitter.emit(event.type, ctx)
14+
return ctx
15+
}
16+
} catch (err) {
17+
emitter.emit('error', err as CoreContext)
18+
return err
19+
}
20+
}
Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
1-
import { CoreContext } from '../context'
2-
31
export interface CoreAnalytics {
4-
track(...args: unknown[]): Promise<CoreContext>
5-
page(...args: unknown[]): Promise<CoreContext>
6-
identify(...args: unknown[]): Promise<CoreContext>
7-
group(...args: unknown[]): Promise<CoreContext>
8-
alias(...args: unknown[]): Promise<CoreContext>
9-
screen(...args: unknown[]): Promise<CoreContext>
10-
register(...plugins: unknown[]): Promise<CoreContext>
11-
deregister(...plugins: unknown[]): Promise<CoreContext>
2+
track(...args: unknown[]): unknown
3+
page(...args: unknown[]): unknown
4+
identify(...args: unknown[]): unknown
5+
group(...args: unknown[]): unknown
6+
alias(...args: unknown[]): unknown
7+
screen(...args: unknown[]): unknown
8+
register(...plugins: unknown[]): Promise<unknown>
9+
deregister(...plugins: unknown[]): Promise<unknown>
1210
readonly VERSION: string
1311
}

packages/core/src/arguments-resolver/page.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { isFunction, isPlainObject, isString } from '../validation'
1+
import { isFunction, isPlainObject, isString } from '../validation/helpers'
22
import { JSONObject, EventProperties, CoreOptions } from '../events'
33
import { Callback } from '../events/interfaces'
44

0 commit comments

Comments
 (0)