-
Notifications
You must be signed in to change notification settings - Fork 138
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Reduce size of create-wrapper.ts (#990)
- Loading branch information
Showing
8 changed files
with
99 additions
and
89 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@segment/analytics-consent-tools': patch | ||
--- | ||
|
||
Refactor consent wrapper; export GetCategoriesFunction |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
packages/consent/consent-tools/src/domain/pruned-categories.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import { uniq, pick } from 'lodash' | ||
import { | ||
CDNSettings, | ||
CreateWrapperSettings, | ||
Categories, | ||
GetCategoriesFunction, | ||
} from '../types' | ||
import { ValidationError } from './validation/validation-error' | ||
|
||
export const getPrunedCategories = async ( | ||
getCategories: GetCategoriesFunction, | ||
cdnSettings: CDNSettings, | ||
integrationCategoryMappings?: CreateWrapperSettings['integrationCategoryMappings'] | ||
): Promise<Categories> => { | ||
// we don't want to send _every_ category to segment, only the ones that the user has explicitly configured in their integrations | ||
let allCategories: string[] | ||
// We need to get all the unique categories so we can prune the consent object down to only the categories that are configured | ||
// There can be categories that are not included in any integration in the integrations object (e.g. 2 cloud mode categories), which is why we need a special allCategories array | ||
if (integrationCategoryMappings) { | ||
allCategories = uniq( | ||
Object.values(integrationCategoryMappings).reduce((p, n) => p.concat(n)) | ||
) | ||
} else { | ||
allCategories = cdnSettings.consentSettings?.allCategories || [] | ||
} | ||
|
||
if (!allCategories.length) { | ||
// No configured integrations found, so no categories will be sent (should not happen unless there's a configuration error) | ||
throw new ValidationError( | ||
'Invariant: No consent categories defined in Segment', | ||
[] | ||
) | ||
} | ||
|
||
const categories = await getCategories() | ||
|
||
return pick(categories, allCategories) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters