@metamask-previews/analytics-data-regulation-controller 0.0.0-preview-8b7a55483 → 0.0.0-preview-c3d36da85

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -23,7 +23,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
23
23
  - This package now ships ES2022 code, requiring a compatible modern environment or bundler configuration to consume.
24
24
  - `createDataDeletionTask` no longer retries on 5xx responses ([#10879](https://github.com/MetaMask/core/pull/10879))
25
25
  - Concurrent `checkDataDeleteStatus` calls for the same regulation ID now share one request ([#10879](https://github.com/MetaMask/core/pull/10879))
26
- - A malformed `createDataDeletionTask` response now throws `Mutation function for "AnalyticsDataRegulationService:createDataDeletionTask" returned an unexpected response: …` ([#10879](https://github.com/MetaMask/core/pull/10879))
26
+ - A malformed `createDataDeletionTask` response now throws `Mutation function for "AnalyticsDataRegulationService:createDataDeletionTask" returned an unexpected response: …` ([#10879](https://github.com/MetaMask/core/pull/10879), [#11213](https://github.com/MetaMask/core/pull/11213))
27
27
  - Add dependencies `@metamask/base-data-service` and `@metamask/superstruct` ([#10879](https://github.com/MetaMask/core/pull/10879))
28
28
  - Bump `@metamask/utils` from `^11.9.0` to `^12.0.1` ([#9074](https://github.com/MetaMask/core/pull/9074), [#10076](https://github.com/MetaMask/core/pull/10076), [#10192](https://github.com/MetaMask/core/pull/10192), [#11107](https://github.com/MetaMask/core/pull/11107))
29
29
  - Bump `@metamask/controller-utils` from `^11.19.0` to `^13.0.0` ([#8344](https://github.com/MetaMask/core/pull/8344), [#8755](https://github.com/MetaMask/core/pull/8755), [#8774](https://github.com/MetaMask/core/pull/8774), [#9058](https://github.com/MetaMask/core/pull/9058), [#9083](https://github.com/MetaMask/core/pull/9083), [#9218](https://github.com/MetaMask/core/pull/9218), [#10160](https://github.com/MetaMask/core/pull/10160))
@@ -138,7 +138,7 @@ export class AnalyticsDataRegulationService extends BaseDataService {
138
138
  }
139
139
  const url = `${this.#segmentRegulationsEndpoint}/regulations/sources/${this.#segmentSourceId}`;
140
140
  const jsonResponse = await this.executeMutation({
141
- mutationKey: [`${this.name}:createDataDeletionTask`, analyticsId],
141
+ mutationKey: [`${this.name}:createDataDeletionTask`],
142
142
  gcTime: 0,
143
143
  responseStruct: CreateRegulationResponseStruct,
144
144
  mutationFn: async () => {
@@ -1 +1 @@
1
- {"version":3,"file":"AnalyticsDataRegulationService.js","sourceRoot":"","sources":["../src/AnalyticsDataRegulationService.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,MAAM,6BAA6B,CAAC;AAO9D,OAAO,EAAE,SAAS,EAAE,MAAM,4BAA4B,CAAC;AAEvD,OAAO,EAAE,MAAM,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,uBAAuB,CAAC;AAI7D,OAAO,EACL,6BAA6B,EAC7B,oBAAoB,GACrB,MAAM,YAAY,CAAC;AAGpB;;GAEG;AACH,MAAM,mCAAmC,GAAG,aAAa,CAAC;AAE1D;;GAEG;AACH,MAAM,4BAA4B,GAAG,SAAS,CAAC;AAE/C;;GAEG;AACH,MAAM,oBAAoB,GAAG,iCAAiC,CAAC;AAE/D,kBAAkB;AAElB;;;GAGG;AACH,MAAM,CAAC,MAAM,WAAW,GAAG,gCAAgC,CAAC;AAE5D,oBAAoB;AAEpB,MAAM,yBAAyB,GAAG;IAChC,wBAAwB;IACxB,uBAAuB;CACf,CAAC;AAuDX,6BAA6B;AAE7B;;GAEG;AACH,MAAM,oBAAoB,GAAG,MAAM,CACjC,MAAM,EAAE,EACR,gBAAgB,EAChB,CAAC,KAAK,EAAE,EAAE,CAAC,KAAK,CAAC,IAAI,EAAE,KAAK,EAAE,CAC/B,CAAC;AAEF;;GAEG;AACH,MAAM,8BAA8B,GAAG,IAAI,CAAC;IAC1C,IAAI,EAAE,IAAI,CAAC;QACT,IAAI,EAAE,IAAI,CAAC;YACT,UAAU,EAAE,oBAAoB;SACjC,CAA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CAAC;YACZ,MAAM,EAAE,CAAC;YACT,OAAO,EAAE,KAAK,IAAI,EAAE;gBAClB,MAAM,QAAQ,GAAG,MAAM,IAAI,CAAC,MAAM,CAAC,GAAG,EAAE;oBACtC,MAAM,EAAE,KAAK;oBACb,OAAO,EAAE;wBACP,cAAc,EAAE,oBAAoB;qBACrC;iBACF,CAAC,CAAC;gBAEH,IAAI,CAAC,QAAQ,CAAC,EAAE,EAAE,CAAC;oBACjB,MAAM,IAAI,SAAS,CACjB,QAAQ,CAAC,MAAM,EACf,qDAAqD,QAAQ,CAAC,MAAM,GAAG,CACxE,CAAC;gBACJ,CAAC;gBAED,MAAM,YAAY,GAChB,CAAC,MAAM,QAAQ,CAAC,IAAI,EAAE,CAAgC,CAAC;gBACzD,MAAM,SAAS,GAAG,YAAY,EAAE,IAAI,EAAE,IAAI,EAAE,UAAU,EAAE,aAAa,CAAC;gBAEtE,OAAO;oBACL,MAAM,EAAE,6BAA6B,CAAC,OAAO;oBAC7C,gBAAgB,EAAE,kBAAkB,CAAC,SAAS,CAAC;wBAC7C,CAAC,CAAC,SAAS;wBACX,CAAC,CAAC,oBAAoB,CAAC,OAAO;iBACjC,CAAC;YACJ,CAAC;SACF,CAAC,CAAC;IACL,CAAC;CACF","sourcesContent":["import { BaseDataService } from '@metamask/base-data-service';\nimport type {\n CreateServicePolicyOptions,\n DataServiceCacheUpdatedEvent,\n DataServiceGranularCacheUpdatedEvent,\n DataServiceInvalidateQueriesAction,\n} from '@metamask/base-data-service';\nimport { HttpError } from '@metamask/controller-utils';\nimport type { Messenger } from '@metamask/messenger';\nimport { refine, string, type } from '@metamask/superstruct';\nimport type { Infer } from '@metamask/superstruct';\n\nimport type { AnalyticsDataRegulationServiceMethodActions } from './AnalyticsDataRegulationService-method-action-types.js';\nimport {\n DATA_DELETE_RESPONSE_STATUSES,\n DATA_DELETE_STATUSES,\n} from './types.js';\nimport type { DataDeleteStatus } from './types.js';\n\n/**\n * Segment API regulation type for DELETE_ONLY operations.\n */\nconst SEGMENT_REGULATION_TYPE_DELETE_ONLY = 'DELETE_ONLY';\n\n/**\n * Segment API subject type for user ID operations.\n */\nconst SEGMENT_SUBJECT_TYPE_USER_ID = 'USER_ID';\n\n/**\n * Segment API Content-Type header value.\n */\nconst SEGMENT_CONTENT_TYPE = 'application/vnd.segment.v1+json';\n\n// === GENERAL ===\n\n/**\n * The name of the {@link AnalyticsDataRegulationService}, used to namespace the\n * service's actions and events.\n */\nexport const serviceName = 'AnalyticsDataRegulationService';\n\n// === MESSENGER ===\n\nconst MESSENGER_EXPOSED_METHODS = [\n 'createDataDeletionTask',\n 'checkDataDeleteStatus',\n] as const;\n\n/**\n * Invalidates cached queries for {@link AnalyticsDataRegulationService}.\n */\nexport type AnalyticsDataRegulationServiceInvalidateQueriesAction =\n DataServiceInvalidateQueriesAction<typeof serviceName>;\n\n/**\n * Actions that {@link AnalyticsDataRegulationService} exposes to other consumers.\n */\nexport type AnalyticsDataRegulationServiceActions =\n | AnalyticsDataRegulationServiceMethodActions\n | AnalyticsDataRegulationServiceInvalidateQueriesAction;\n\n/**\n * Actions from other messengers that {@link AnalyticsDataRegulationServiceMessenger} calls.\n */\ntype AllowedActions = never;\n\n/**\n * Published when {@link AnalyticsDataRegulationService}'s cache is updated.\n */\nexport type AnalyticsDataRegulationServiceCacheUpdatedEvent =\n DataServiceCacheUpdatedEvent<typeof serviceName>;\n\n/**\n * Published when a key within {@link AnalyticsDataRegulationService}'s cache is\n * updated.\n */\nexport type AnalyticsDataRegulationServiceGranularCacheUpdatedEvent =\n DataServiceGranularCacheUpdatedEvent<typeof serviceName>;\n\n/**\n * Events that {@link AnalyticsDataRegulationService} exposes to other consumers.\n */\nexport type AnalyticsDataRegulationServiceEvents =\n | AnalyticsDataRegulationServiceCacheUpdatedEvent\n | AnalyticsDataRegulationServiceGranularCacheUpdatedEvent;\n\n/**\n * Events from other messengers that {@link AnalyticsDataRegulationService} subscribes to.\n */\ntype AllowedEvents = never;\n\n/**\n * The messenger which is restricted to actions and events accessed by\n * {@link AnalyticsDataRegulationService}.\n */\nexport type AnalyticsDataRegulationServiceMessenger = Messenger<\n typeof serviceName,\n AnalyticsDataRegulationServiceActions | AllowedActions,\n AnalyticsDataRegulationServiceEvents | AllowedEvents\n>;\n\n// === SERVICE DEFINITION ===\n\n/**\n * A string that is non-empty once trimmed.\n */\nconst NonEmptyStringStruct = refine(\n string(),\n 'NonEmptyString',\n (value) => value.trim() !== '',\n);\n\n/**\n * Response structure from Segment API for creating a regulation.\n */\nconst CreateRegulationResponseStruct = type({\n data: type({\n data: type({\n regulateId: NonEmptyStringStruct,\n }),\n }),\n});\n\n/**\n * Validated response from Segment API for creating a regulation.\n */\ntype CreateRegulationResponse = Infer<typeof CreateRegulationResponseStruct>;\n\n/**\n * Response structure from Segment API for getting regulation status.\n */\ntype GetRegulationStatusResponse = {\n data: {\n data: {\n regulation: {\n overallStatus: string;\n };\n };\n };\n};\n\n/**\n * Options for constructing {@link AnalyticsDataRegulationService}.\n */\nexport type AnalyticsDataRegulationServiceOptions = {\n /**\n * The messenger suited for this service.\n */\n messenger: AnalyticsDataRegulationServiceMessenger;\n\n /**\n * A function that can be used to make an HTTP request.\n */\n fetch: typeof fetch;\n\n /**\n * Segment API source ID (required for creating regulations).\n */\n segmentSourceId: string;\n\n /**\n * Base URL for the proxy endpoint that communicates with Segment's Regulations API.\n * This is a proxy endpoint (not Segment API directly) that forwards requests to Segment's\n * Regulations API and adds authentication tokens. The endpoint URL varies by environment\n * (e.g., development, staging, production) and should be configured accordingly.\n * Example: 'https://proxy.example.com/v1beta'\n */\n segmentRegulationsEndpoint: string;\n\n /**\n * Options to pass to `createServicePolicy`, which is used to wrap each request.\n */\n policyOptions?: CreateServicePolicyOptions;\n};\n\n/**\n * Type guard to check if a value is a valid DataDeleteStatus.\n *\n * @param status - The value to check.\n * @returns True if the value is a valid DataDeleteStatus.\n */\nfunction isDataDeleteStatus(status: unknown): status is DataDeleteStatus {\n const dataDeleteStatuses: string[] = Object.values(DATA_DELETE_STATUSES);\n return dataDeleteStatuses.includes(status as string);\n}\n\n/**\n * This service object is responsible for making requests to the Segment Regulations API\n * via a proxy endpoint for GDPR/CCPA data deletion functionality.\n *\n * @example\n *\n * ```ts\n * import type { MessengerActions, MessengerEvents } from '@metamask/messenger';\n * import { Messenger } from '@metamask/messenger';\n * import type {\n * AnalyticsDataRegulationServiceActions,\n * AnalyticsDataRegulationServiceEvents,\n * AnalyticsDataRegulationServiceMessenger,\n * } from '@metamask/analytics-data-regulation-controller';\n *\n * const rootMessenger = new Messenger<\n * 'Root',\n * AnalyticsDataRegulationServiceActions,\n * AnalyticsDataRegulationServiceEvents\n * >({ namespace: 'Root' });\n * const serviceMessenger = new Messenger<\n * 'AnalyticsDataRegulationService',\n * MessengerActions<AnalyticsDataRegulationServiceMessenger>,\n * MessengerEvents<AnalyticsDataRegulationServiceMessenger>,\n * typeof rootMessenger,\n * >({\n * namespace: 'AnalyticsDataRegulationService',\n * parent: rootMessenger,\n * });\n * // Instantiate the service to register its actions on the messenger\n * new AnalyticsDataRegulationService({\n * messenger: serviceMessenger,\n * fetch,\n * segmentSourceId: 'abc123',\n * segmentRegulationsEndpoint: 'https://proxy.example.com/v1beta',\n * });\n *\n * // Later...\n * // Create a data deletion task\n * const response = await rootMessenger.call(\n * 'AnalyticsDataRegulationService:createDataDeletionTask',\n * 'user-analytics-id',\n * );\n * ```\n */\nexport class AnalyticsDataRegulationService extends BaseDataService<\n typeof serviceName,\n AnalyticsDataRegulationServiceMessenger\n> {\n /**\n * A function that can be used to make an HTTP request.\n */\n readonly #fetch: typeof fetch;\n\n /**\n * Segment API source ID.\n */\n readonly #segmentSourceId: string;\n\n /**\n * Base URL for the proxy endpoint that communicates with Segment's Regulations API.\n * This endpoint varies by environment and forwards requests to Segment API with authentication.\n */\n readonly #segmentRegulationsEndpoint: string;\n\n /**\n * Constructs a new AnalyticsDataRegulationService object.\n *\n * @param options - The constructor options.\n */\n constructor(options: AnalyticsDataRegulationServiceOptions) {\n super({\n name: serviceName,\n messenger: options.messenger,\n policyOptions: options.policyOptions,\n });\n this.#fetch = options.fetch;\n this.#segmentSourceId = options.segmentSourceId;\n this.#segmentRegulationsEndpoint = options.segmentRegulationsEndpoint;\n\n this.messenger.registerMethodActionHandlers(\n this,\n MESSENGER_EXPOSED_METHODS,\n );\n }\n\n /**\n * Creates a DELETE_ONLY regulation for the given analyticsId.\n *\n * @param analyticsId - The analytics ID of the user for whom to create the deletion task.\n * @returns Promise resolving to a successful deletion regulation response.\n * @throws Error if the request fails or the response is invalid.\n */\n async createDataDeletionTask(analyticsId: string): Promise<{\n status: typeof DATA_DELETE_RESPONSE_STATUSES.Success;\n regulateId: string;\n }> {\n if (!this.#segmentSourceId || !this.#segmentRegulationsEndpoint) {\n throw new Error('Segment API source ID or endpoint not found');\n }\n\n const url = `${this.#segmentRegulationsEndpoint}/regulations/sources/${this.#segmentSourceId}`;\n\n const jsonResponse = await this.executeMutation({\n mutationKey: [`${this.name}:createDataDeletionTask`, analyticsId],\n gcTime: 0,\n responseStruct: CreateRegulationResponseStruct,\n mutationFn: async () => {\n const response = await this.#fetch(url, {\n method: 'POST',\n headers: {\n 'Content-Type': SEGMENT_CONTENT_TYPE,\n },\n body: JSON.stringify({\n regulationType: SEGMENT_REGULATION_TYPE_DELETE_ONLY,\n subjectType: SEGMENT_SUBJECT_TYPE_USER_ID,\n subjectIds: [analyticsId],\n }),\n });\n\n if (!response.ok) {\n throw new HttpError(\n response.status,\n `Creating data deletion task failed with status '${response.status}'`,\n );\n }\n\n return (await response.json()) as CreateRegulationResponse;\n },\n });\n\n return {\n status: DATA_DELETE_RESPONSE_STATUSES.Success,\n regulateId: jsonResponse.data.data.regulateId,\n };\n }\n\n /**\n * Checks the status of a regulation by ID.\n *\n * @param regulationId - The regulation ID to check.\n * @returns Promise resolving to a successful regulation status response.\n * @throws Error if the request fails or the response is invalid.\n */\n async checkDataDeleteStatus(regulationId: string): Promise<{\n status: typeof DATA_DELETE_RESPONSE_STATUSES.Success;\n dataDeleteStatus: DataDeleteStatus;\n }> {\n if (!regulationId || !this.#segmentRegulationsEndpoint) {\n throw new Error('Regulation ID or endpoint not configured');\n }\n\n const url = `${this.#segmentRegulationsEndpoint}/regulations/${regulationId}`;\n\n return this.fetchQuery({\n queryKey: [`${this.name}:checkDataDeleteStatus`, regulationId],\n staleTime: 0,\n gcTime: 0,\n queryFn: async () => {\n const response = await this.#fetch(url, {\n method: 'GET',\n headers: {\n 'Content-Type': SEGMENT_CONTENT_TYPE,\n },\n });\n\n if (!response.ok) {\n throw new HttpError(\n response.status,\n `Checking data deletion status failed with status '${response.status}'`,\n );\n }\n\n const jsonResponse =\n (await response.json()) as GetRegulationStatusResponse;\n const rawStatus = jsonResponse?.data?.data?.regulation?.overallStatus;\n\n return {\n status: DATA_DELETE_RESPONSE_STATUSES.Success,\n dataDeleteStatus: isDataDeleteStatus(rawStatus)\n ? rawStatus\n : DATA_DELETE_STATUSES.Unknown,\n };\n },\n });\n }\n}\n"]}
1
+ {"version":3,"file":"AnalyticsDataRegulationService.js","sourceRoot":"","sources":["../src/AnalyticsDataRegulationService.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,MAAM,6BAA6B,CAAC;AAO9D,OAAO,EAAE,SAAS,EAAE,MAAM,4BAA4B,CAAC;AAEvD,OAAO,EAAE,MAAM,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,uBAAuB,CAAC;AAI7D,OAAO,EACL,6BAA6B,EAC7B,oBAAoB,GACrB,MAAM,YAAY,CAAC;AAGpB;;GAEG;AACH,MAAM,mCAAmC,GAAG,aAAa,CAAC;AAE1D;;GAEG;AACH,MAAM,4BAA4B,GAAG,SAAS,CAAC;AAE/C;;GAEG;AACH,MAAM,oBAAoB,GAAG,iCAAiC,CAAC;AAE/D,kBAAkB;AAElB;;;GAGG;AACH,MAAM,CAAC,MAAM,WAAW,GAAG,gCAAgC,CAAC;AAE5D,oBAAoB;AAEpB,MAAM,y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,qBAAqB,CAAC,YAAoB;QAI9C,IAAI,CAAC,YAAY,IAAI,CAAC,IAAI,CAAC,2BAA2B,EAAE,CAAC;YACvD,MAAM,IAAI,KAAK,CAAC,0CAA0C,CAAC,CAAC;QAC9D,CAAC;QAED,MAAM,GAAG,GAAG,GAAG,IAAI,CAAC,2BAA2B,gBAAgB,YAAY,EAAE,CAAC;QAE9E,OAAO,IAAI,CAAC,UAAU,CAAC;YACrB,QAAQ,EAAE,CAAC,GAAG,IAAI,CAAC,IAAI,wBAAwB,EAAE,YAAY,CAAC;YAC9D,SAAS,EAAE,CAAC;YACZ,MAAM,EAAE,CAAC;YACT,OAAO,EAAE,KAAK,IAAI,EAAE;gBAClB,MAAM,QAAQ,GAAG,MAAM,IAAI,CAAC,MAAM,CAAC,GAAG,EAAE;oBACtC,MAAM,EAAE,KAAK;oBACb,OAAO,EAAE;wBACP,cAAc,EAAE,oBAAoB;qBACrC;iBACF,CAAC,CAAC;gBAEH,IAAI,CAAC,QAAQ,CAAC,EAAE,EAAE,CAAC;oBACjB,MAAM,IAAI,SAAS,CACjB,QAAQ,CAAC,MAAM,EACf,qDAAqD,QAAQ,CAAC,MAAM,GAAG,CACxE,CAAC;gBACJ,CAAC;gBAED,MAAM,YAAY,GAChB,CAAC,MAAM,QAAQ,CAAC,IAAI,EAAE,CAAgC,CAAC;gBACzD,MAAM,SAAS,GAAG,YAAY,EAAE,IAAI,EAAE,IAAI,EAAE,UAAU,EAAE,aAAa,CAAC;gBAEtE,OAAO;oBACL,MAAM,EAAE,6BAA6B,CAAC,OAAO;oBAC7C,gBAAgB,EAAE,kBAAkB,CAAC,SAAS,CAAC;wBAC7C,CAAC,CAAC,SAAS;wBACX,CAAC,CAAC,oBAAoB,CAAC,OAAO;iBACjC,CAAC;YACJ,CAAC;SACF,CAAC,CAAC;IACL,CAAC;CACF","sourcesContent":["import { BaseDataService } from '@metamask/base-data-service';\nimport type {\n CreateServicePolicyOptions,\n DataServiceCacheUpdatedEvent,\n DataServiceGranularCacheUpdatedEvent,\n DataServiceInvalidateQueriesAction,\n} from '@metamask/base-data-service';\nimport { HttpError } from '@metamask/controller-utils';\nimport type { Messenger } from '@metamask/messenger';\nimport { refine, string, type } from '@metamask/superstruct';\nimport type { Infer } from '@metamask/superstruct';\n\nimport type { AnalyticsDataRegulationServiceMethodActions } from './AnalyticsDataRegulationService-method-action-types.js';\nimport {\n DATA_DELETE_RESPONSE_STATUSES,\n DATA_DELETE_STATUSES,\n} from './types.js';\nimport type { DataDeleteStatus } from './types.js';\n\n/**\n * Segment API regulation type for DELETE_ONLY operations.\n */\nconst SEGMENT_REGULATION_TYPE_DELETE_ONLY = 'DELETE_ONLY';\n\n/**\n * Segment API subject type for user ID operations.\n */\nconst SEGMENT_SUBJECT_TYPE_USER_ID = 'USER_ID';\n\n/**\n * Segment API Content-Type header value.\n */\nconst SEGMENT_CONTENT_TYPE = 'application/vnd.segment.v1+json';\n\n// === GENERAL ===\n\n/**\n * The name of the {@link AnalyticsDataRegulationService}, used to namespace the\n * service's actions and events.\n */\nexport const serviceName = 'AnalyticsDataRegulationService';\n\n// === MESSENGER ===\n\nconst MESSENGER_EXPOSED_METHODS = [\n 'createDataDeletionTask',\n 'checkDataDeleteStatus',\n] as const;\n\n/**\n * Invalidates cached queries for {@link AnalyticsDataRegulationService}.\n */\nexport type AnalyticsDataRegulationServiceInvalidateQueriesAction =\n DataServiceInvalidateQueriesAction<typeof serviceName>;\n\n/**\n * Actions that {@link AnalyticsDataRegulationService} exposes to other consumers.\n */\nexport type AnalyticsDataRegulationServiceActions =\n | AnalyticsDataRegulationServiceMethodActions\n | AnalyticsDataRegulationServiceInvalidateQueriesAction;\n\n/**\n * Actions from other messengers that {@link AnalyticsDataRegulationServiceMessenger} calls.\n */\ntype AllowedActions = never;\n\n/**\n * Published when {@link AnalyticsDataRegulationService}'s cache is updated.\n */\nexport type AnalyticsDataRegulationServiceCacheUpdatedEvent =\n DataServiceCacheUpdatedEvent<typeof serviceName>;\n\n/**\n * Published when a key within {@link AnalyticsDataRegulationService}'s cache is\n * updated.\n */\nexport type AnalyticsDataRegulationServiceGranularCacheUpdatedEvent =\n DataServiceGranularCacheUpdatedEvent<typeof serviceName>;\n\n/**\n * Events that {@link AnalyticsDataRegulationService} exposes to other consumers.\n */\nexport type AnalyticsDataRegulationServiceEvents =\n | AnalyticsDataRegulationServiceCacheUpdatedEvent\n | AnalyticsDataRegulationServiceGranularCacheUpdatedEvent;\n\n/**\n * Events from other messengers that {@link AnalyticsDataRegulationService} subscribes to.\n */\ntype AllowedEvents = never;\n\n/**\n * The messenger which is restricted to actions and events accessed by\n * {@link AnalyticsDataRegulationService}.\n */\nexport type AnalyticsDataRegulationServiceMessenger = Messenger<\n typeof serviceName,\n AnalyticsDataRegulationServiceActions | AllowedActions,\n AnalyticsDataRegulationServiceEvents | AllowedEvents\n>;\n\n// === SERVICE DEFINITION ===\n\n/**\n * A string that is non-empty once trimmed.\n */\nconst NonEmptyStringStruct = refine(\n string(),\n 'NonEmptyString',\n (value) => value.trim() !== '',\n);\n\n/**\n * Response structure from Segment API for creating a regulation.\n */\nconst CreateRegulationResponseStruct = type({\n data: type({\n data: type({\n regulateId: NonEmptyStringStruct,\n }),\n }),\n});\n\n/**\n * Validated response from Segment API for creating a regulation.\n */\ntype CreateRegulationResponse = Infer<typeof CreateRegulationResponseStruct>;\n\n/**\n * Response structure from Segment API for getting regulation status.\n */\ntype GetRegulationStatusResponse = {\n data: {\n data: {\n regulation: {\n overallStatus: string;\n };\n };\n };\n};\n\n/**\n * Options for constructing {@link AnalyticsDataRegulationService}.\n */\nexport type AnalyticsDataRegulationServiceOptions = {\n /**\n * The messenger suited for this service.\n */\n messenger: AnalyticsDataRegulationServiceMessenger;\n\n /**\n * A function that can be used to make an HTTP request.\n */\n fetch: typeof fetch;\n\n /**\n * Segment API source ID (required for creating regulations).\n */\n segmentSourceId: string;\n\n /**\n * Base URL for the proxy endpoint that communicates with Segment's Regulations API.\n * This is a proxy endpoint (not Segment API directly) that forwards requests to Segment's\n * Regulations API and adds authentication tokens. The endpoint URL varies by environment\n * (e.g., development, staging, production) and should be configured accordingly.\n * Example: 'https://proxy.example.com/v1beta'\n */\n segmentRegulationsEndpoint: string;\n\n /**\n * Options to pass to `createServicePolicy`, which is used to wrap each request.\n */\n policyOptions?: CreateServicePolicyOptions;\n};\n\n/**\n * Type guard to check if a value is a valid DataDeleteStatus.\n *\n * @param status - The value to check.\n * @returns True if the value is a valid DataDeleteStatus.\n */\nfunction isDataDeleteStatus(status: unknown): status is DataDeleteStatus {\n const dataDeleteStatuses: string[] = Object.values(DATA_DELETE_STATUSES);\n return dataDeleteStatuses.includes(status as string);\n}\n\n/**\n * This service object is responsible for making requests to the Segment Regulations API\n * via a proxy endpoint for GDPR/CCPA data deletion functionality.\n *\n * @example\n *\n * ```ts\n * import type { MessengerActions, MessengerEvents } from '@metamask/messenger';\n * import { Messenger } from '@metamask/messenger';\n * import type {\n * AnalyticsDataRegulationServiceActions,\n * AnalyticsDataRegulationServiceEvents,\n * AnalyticsDataRegulationServiceMessenger,\n * } from '@metamask/analytics-data-regulation-controller';\n *\n * const rootMessenger = new Messenger<\n * 'Root',\n * AnalyticsDataRegulationServiceActions,\n * AnalyticsDataRegulationServiceEvents\n * >({ namespace: 'Root' });\n * const serviceMessenger = new Messenger<\n * 'AnalyticsDataRegulationService',\n * MessengerActions<AnalyticsDataRegulationServiceMessenger>,\n * MessengerEvents<AnalyticsDataRegulationServiceMessenger>,\n * typeof rootMessenger,\n * >({\n * namespace: 'AnalyticsDataRegulationService',\n * parent: rootMessenger,\n * });\n * // Instantiate the service to register its actions on the messenger\n * new AnalyticsDataRegulationService({\n * messenger: serviceMessenger,\n * fetch,\n * segmentSourceId: 'abc123',\n * segmentRegulationsEndpoint: 'https://proxy.example.com/v1beta',\n * });\n *\n * // Later...\n * // Create a data deletion task\n * const response = await rootMessenger.call(\n * 'AnalyticsDataRegulationService:createDataDeletionTask',\n * 'user-analytics-id',\n * );\n * ```\n */\nexport class AnalyticsDataRegulationService extends BaseDataService<\n typeof serviceName,\n AnalyticsDataRegulationServiceMessenger\n> {\n /**\n * A function that can be used to make an HTTP request.\n */\n readonly #fetch: typeof fetch;\n\n /**\n * Segment API source ID.\n */\n readonly #segmentSourceId: string;\n\n /**\n * Base URL for the proxy endpoint that communicates with Segment's Regulations API.\n * This endpoint varies by environment and forwards requests to Segment API with authentication.\n */\n readonly #segmentRegulationsEndpoint: string;\n\n /**\n * Constructs a new AnalyticsDataRegulationService object.\n *\n * @param options - The constructor options.\n */\n constructor(options: AnalyticsDataRegulationServiceOptions) {\n super({\n name: serviceName,\n messenger: options.messenger,\n policyOptions: options.policyOptions,\n });\n this.#fetch = options.fetch;\n this.#segmentSourceId = options.segmentSourceId;\n this.#segmentRegulationsEndpoint = options.segmentRegulationsEndpoint;\n\n this.messenger.registerMethodActionHandlers(\n this,\n MESSENGER_EXPOSED_METHODS,\n );\n }\n\n /**\n * Creates a DELETE_ONLY regulation for the given analyticsId.\n *\n * @param analyticsId - The analytics ID of the user for whom to create the deletion task.\n * @returns Promise resolving to a successful deletion regulation response.\n * @throws Error if the request fails or the response is invalid.\n */\n async createDataDeletionTask(analyticsId: string): Promise<{\n status: typeof DATA_DELETE_RESPONSE_STATUSES.Success;\n regulateId: string;\n }> {\n if (!this.#segmentSourceId || !this.#segmentRegulationsEndpoint) {\n throw new Error('Segment API source ID or endpoint not found');\n }\n\n const url = `${this.#segmentRegulationsEndpoint}/regulations/sources/${this.#segmentSourceId}`;\n\n const jsonResponse = await this.executeMutation({\n mutationKey: [`${this.name}:createDataDeletionTask`],\n gcTime: 0,\n responseStruct: CreateRegulationResponseStruct,\n mutationFn: async () => {\n const response = await this.#fetch(url, {\n method: 'POST',\n headers: {\n 'Content-Type': SEGMENT_CONTENT_TYPE,\n },\n body: JSON.stringify({\n regulationType: SEGMENT_REGULATION_TYPE_DELETE_ONLY,\n subjectType: SEGMENT_SUBJECT_TYPE_USER_ID,\n subjectIds: [analyticsId],\n }),\n });\n\n if (!response.ok) {\n throw new HttpError(\n response.status,\n `Creating data deletion task failed with status '${response.status}'`,\n );\n }\n\n return (await response.json()) as CreateRegulationResponse;\n },\n });\n\n return {\n status: DATA_DELETE_RESPONSE_STATUSES.Success,\n regulateId: jsonResponse.data.data.regulateId,\n };\n }\n\n /**\n * Checks the status of a regulation by ID.\n *\n * @param regulationId - The regulation ID to check.\n * @returns Promise resolving to a successful regulation status response.\n * @throws Error if the request fails or the response is invalid.\n */\n async checkDataDeleteStatus(regulationId: string): Promise<{\n status: typeof DATA_DELETE_RESPONSE_STATUSES.Success;\n dataDeleteStatus: DataDeleteStatus;\n }> {\n if (!regulationId || !this.#segmentRegulationsEndpoint) {\n throw new Error('Regulation ID or endpoint not configured');\n }\n\n const url = `${this.#segmentRegulationsEndpoint}/regulations/${regulationId}`;\n\n return this.fetchQuery({\n queryKey: [`${this.name}:checkDataDeleteStatus`, regulationId],\n staleTime: 0,\n gcTime: 0,\n queryFn: async () => {\n const response = await this.#fetch(url, {\n method: 'GET',\n headers: {\n 'Content-Type': SEGMENT_CONTENT_TYPE,\n },\n });\n\n if (!response.ok) {\n throw new HttpError(\n response.status,\n `Checking data deletion status failed with status '${response.status}'`,\n );\n }\n\n const jsonResponse =\n (await response.json()) as GetRegulationStatusResponse;\n const rawStatus = jsonResponse?.data?.data?.regulation?.overallStatus;\n\n return {\n status: DATA_DELETE_RESPONSE_STATUSES.Success,\n dataDeleteStatus: isDataDeleteStatus(rawStatus)\n ? rawStatus\n : DATA_DELETE_STATUSES.Unknown,\n };\n },\n });\n }\n}\n"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@metamask-previews/analytics-data-regulation-controller",
3
- "version": "0.0.0-preview-8b7a55483",
3
+ "version": "0.0.0-preview-c3d36da85",
4
4
  "description": "Controller for managing analytics privacy and GDPR/CCPA data deletion functionality",
5
5
  "keywords": [
6
6
  "Ethereum",
@@ -46,6 +46,7 @@
46
46
  "since-latest-release": "../../scripts/since-latest-release.sh",
47
47
  "test": "vitest",
48
48
  "test:clean": "vitest --clearCache",
49
+ "test:coverage": "vitest --coverage",
49
50
  "test:verbose": "vitest --reporter=verbose",
50
51
  "test:watch": "vitest --watch"
51
52
  },
@@ -65,7 +66,7 @@
65
66
  "typedoc": "^0.28.20",
66
67
  "typedoc-plugin-missing-exports": "^4.1.4",
67
68
  "typescript": "npm:@typescript/typescript6@^6.0.2",
68
- "vitest": "^5.0.0"
69
+ "vitest": "5.0.3"
69
70
  },
70
71
  "engines": {
71
72
  "node": "^22.14.0 || ^24 || ^26"