Correct way to use PublicObjectSearchRequest in Typescrpt class method

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

I think my errors are related to strict type checking in Typescript. The sample app was Nodejs, while the new app with the class is Typescript.

So perhaps better question is how to use the api calls from TypeScript, particularly when calling the HubSpot API using variables that could be string or undefined.

Any typescript sample apps out there?

Phil

Similar issue creating a new contact clarifies the problem. Typescript class method code looks like this:

 /**
 * Create a new contact.
 */
 createContact({
 email,
 firstname,
 lastname,
 }: {
 email: string;
 firstname?: string;
 lastname?: string;
 }) {
 const contactObj = {
 properties: {
 firstname: firstname,
 lastname: lastname,
 email: email,
 }
 }
 return this._client.crm.contacts.basicApi.create(contactObj)
 }

If I replace firstname, lastname, email in contactObj with string literals rather than using parameters the error goes away, indicating it is probably strict typescript type checking causing the problem.

But I will not necessarily know all of firstname, lastname, email when I create a contact and instantiating SimplePublicObjectInput class does not provide the methods to enforce property values as strings like PublicObjectSearchRequest apparently does. So I have not found a way around this one yet.

Real issue is I do not know Typescript well enough to force undefined to empty string when defining JSON. Apparently trying to learn too many things at once.

This was my noob Typescript issue.

Solution was to assign parameters to separate variables that must be string to construct contactObj and map undefined to empty string. Here is the error free createContact method:

 /**
 * Create a new contact.
 */
 createContact({
 email,
 firstname,
 lastname,
 }: {
 email: string;
 firstname?: string;
 lastname?: string;
 }) {
 let stringFirst = (firstname === undefined?'':firstname);
 let stringLast = (lastname === undefined?'':lastname);
 const contactObj = {
 properties: {
 firstname: stringFirst,
 lastname: stringLast,
 email: email,
 }
 }
 return this._client.crm.contacts.basicApi.create(contactObj)
 }

Now strict type checking no longer rejects my create() API call.

Phil