@@ -12,21 +12,11 @@ import {
1212 IErrorReportingService ,
1313 WSDKErrorSeverity ,
1414} from '../reporting/types' ;
15+ import { IdentityApiData , UserIdentities } from '@mparticle/web-sdk' ;
16+ import Validators from '../validators' ;
1517
1618const { HTTPCodes } = Constants ;
1719
18- /**
19- * Shape of `known_identities` accepted by `search`.
20- *
21- * The IDSync `/v1/search` endpoint accepts the same identity keys as
22- * `/v1/identify`, but for v1 of this client API we only support `email`.
23- * Additional identity types can be added here in the future without breaking
24- * existing consumers.
25- */
26- export interface IIdentitySearchKnownIdentities {
27- email : string ;
28- }
29-
3020/**
3121 * Body payload returned by the `/v1/search` endpoint, as parsed JSON.
3222 *
@@ -72,7 +62,7 @@ export interface IIdentitySearchRequestBody {
7262 environment : Environment ;
7363 request_id : string ;
7464 request_timestamp_ms : number ;
75- known_identities : IIdentitySearchKnownIdentities ;
65+ known_identities : UserIdentities ;
7666}
7767
7868interface IIdentitySearchPayload extends IFetchPayload {
@@ -88,17 +78,17 @@ interface IIdentitySearchPayload extends IFetchPayload {
8878 * with the HTTP status and parsed body.
8979 *
9080 * Defensive contract:
91- * - Missing/invalid `email` -> callback with `{ httpCode: noHttpCoverage }`,
92- * no network call.
93- * - Missing `apiKey` -> callback with `{ httpCode: noHttpCoverage }`,
81+ * - No identifier with a non-empty string value -> callback with
82+ * `{ httpCode: noHttpCoverage }`, no network call.
83+ * - Missing `apiKey` -> callback with `{ httpCode: noHttpCoverage }`,
9484 * no network call.
9585 * - Network/JSON-parse errors are caught and surfaced via the callback,
9686 * never thrown. Network errors are also reported through the optional
9787 * `errorReporter` so any registered IErrorReportingService can observe
9888 * them (matches the pattern used by identifyRequest in identityApiClient).
9989 */
10090export const sendSearchRequest = async (
101- knownIdentities : IIdentitySearchKnownIdentities ,
91+ knownIdentities : UserIdentities ,
10292 apiKey : string ,
10393 requestBuilder : ( ) => Omit < IIdentitySearchRequestBody , 'known_identities' > ,
10494 searchUrl : string ,
@@ -127,11 +117,20 @@ export const sendSearchRequest = async (
127117 }
128118 } ;
129119
130- // No valid email -> deliver httpCode: noHttpCoverage so callers waiting on
131- // the callback (e.g. to clear a loading state) don't hang.
132- if ( ! knownIdentities || typeof knownIdentities . email !== 'string' || ! knownIdentities . email ) {
120+ const cleanedKnownIdentities : UserIdentities = Validators . removeFalsyIdentityValues (
121+ { userIdentities : knownIdentities ?? { } } as IdentityApiData ,
122+ logger ,
123+ ) . userIdentities ;
124+
125+ // No usable identifier -> deliver httpCode: noHttpCoverage so callers
126+ // waiting on the callback (e.g. to clear a loading state) don't hang.
127+ if (
128+ ! Object . values ( cleanedKnownIdentities ?? { } ) . some (
129+ ( v ) => typeof v === 'string' && v . length > 0 ,
130+ )
131+ ) {
133132 logger . verbose (
134- 'search called without a valid email ; skipping request.' ,
133+ 'Identity search called with empty identifiers ; skipping request.' ,
135134 ) ;
136135 safeInvoke ( { httpCode : HTTPCodes . noHttpCoverage } ) ;
137136 return ;
@@ -153,11 +152,10 @@ export const sendSearchRequest = async (
153152 // rejecting and the caller hanging on a never-fired callback.
154153 try {
155154 const requestEnvelope = requestBuilder ( ) ;
155+
156156 const requestBody : IIdentitySearchRequestBody = {
157157 ...requestEnvelope ,
158- known_identities : {
159- email : knownIdentities . email ,
160- } ,
158+ known_identities : { ...cleanedKnownIdentities } ,
161159 } ;
162160
163161 const fetchPayload : IIdentitySearchPayload = {
0 commit comments