I got a search for contact by email working from a simple oauth app, but when I use the same code in a class method, VS Code reports an intellisense error assigning to PublicObjectSearchRequest. Digging through the library, PublicObjectSearchRequest is a class and instantiating Filter, FilterGroup and PublicObjectSearchRequest objects rather than using JSON directly eliminates the error. But using JSON directly works in the sample (not called from a class method).
What is the proper way to define the PublicObjectSearchRequest object when calling from a typescript class method?
More specifically, when I define a class method to search based on what is shown in GitHub - HubSpot/hubspot-api-nodejs: HubSpot API NodeJS Client Libraries for V3 version of the API · GitHub, like this:
/**
* Find contact by email.
*/
async findContact({
id,
email,
}: {
id?: string;
email?: string;
}): Promise<IHsContact> {
const filter = { propertyName: 'email', operator: 'EQ', value: email }
const filterGroup = { filters: [filter] }
const sort = 'firstname'
const query = ''
const properties = ['company', 'email', 'firstname', 'lastname']
const limit = 100
const after = 0
const publicObjectSearchRequest = {
filterGroups: [filterGroup],
sorts: [sort],
query,
properties,
limit,
after
};
const result = await this._client.crm.contacts.searchApi.doSearch(publicObjectSearchRequest)
return { // layout as IHsContact
"Id": result.body.results[0].id,
"Email": result.body.results[0].properties.email,
"FirstName": result.body.results[0].properties.firstname,
"LastName": result.body.results[0].properties.lastname }
}
intellisense reports these errors related to the publicObjectSearchRequest parameter:
Argument of type '{ filterGroups: { filters: { propertyName: string; operator: string; value: string; }[]; }[]; sorts: string[]; query: string; properties: string[]; limit: number; after: number; }' is not assignable to parameter of type 'PublicObjectSearchRequest'.
Types of property ''filterGroups'' are incompatible.
Type '{ filters: { propertyName: string; operator: string; value: string; }[]; }[]' is not assignable to type 'FilterGroup[]'.
Type '{ filters: { propertyName: string; operator: string; value: string; }[]; }' is not assignable to type 'FilterGroup'.
Types of property ''filters'' are incompatible.
Type '{ propertyName: string; operator: string; value: string; }[]' is not assignable to type 'Filter[]'.
Type '{ propertyName: string; operator: string; value: string; }' is not assignable to type 'Filter'.
Types of property ''operator'' are incompatible.
Type 'string' is not assignable to type 'OperatorEnum'.ts(2345)
Note that the same code defined inline (not in a class method) shows no errors and works.
But when I define PublicObjectSearchRequest by instantiating the classes like below, Intellisense reports no errors:
/**
* Find contact by email.
*/
async findContact({
id,
email,
}: {
id?: string;
email?: string;
}): Promise<IHsContact> {
const publicObjectSearchRequest = new PublicObjectSearchRequest;
const filterObj = new Filter;
filterObj.value = email;
filterObj.propertyName = 'firstname';
filterObj.operator = Filter.OperatorEnum.EQ;
const filterGroupObj = new FilterGroup;
filterGroupObj.filters = [filterObj];
publicObjectSearchRequest.filterGroups = [filterGroupObj];
const result = await this._client.crm.contacts.searchApi.doSearch(publicObjectSearchRequest)
return { // layout as IHsContact
"Id": result.body.results[0].id,
"Email": result.body.results[0].properties.email,
"FirstName": result.body.results[0].properties.firstname,
"LastName": result.body.results[0].properties.lastname }
}
Is instantiating each class preferred over simple JSON as shown in the sample app?
I’m also fairly new to typescript, so it could be something simple I’ve missed, so any tips appreciated.
Phil