fa0311 / twitter-openapi-typescript

Implementation of Twitter internal API (Twitter graphql API) in TypeScript
https://www.npmjs.com/package/twitter-openapi-typescript
Other
73 stars 11 forks source link

Bug: `getUserByScreenName` throw ` Cannot read properties of undefined (reading 'result')` error when the screenName searched for user not exisit #111

Closed WendaoLee closed 3 days ago

WendaoLee commented 3 days ago

Sample Code

export const searchForUserByScreenName = async (client: TwitterOpenApiClient, screenName: string) => {
    const user = await client.getUserApi().getUserByScreenName({ screenName })
    return user
}
// assume that client has been initiated correctly
// works well
await searchForUserByScreenName(client,'elonmask')
// works well
await searchForUserByScreenName(client,'LeeWendao')
// works bad because `Wendaoleecs` is not a existed user
// and it throw error
await searchForUserByScreenName(client,'Wendoleecs')

Error

Uncaught TypeError: Cannot read properties of undefined (reading 'result')
    at UserApiUtils.<anonymous> (D:\Backup\Documents\GitHub\AI-Daily-Inner-Data-Devlop-Platform\node_modules\twitter-openapi-typescript\src\apis\userApi.ts:34:25)
    at step (D:\Backup\Documents\GitHub\AI-Daily-Inner-Data-Devlop-Platform\node_modules\twitter-openapi-typescript\dist\src\apis\userApi.js:44:23)

Suggestion

Seems that the code at src/apis/userApi.ts at line 34:

  async request<T>(param: RequestParam<i.UserResults, T>): Promise<ResponseType> {
    const apiFn: typeof param.apiFn = param.apiFn.bind(this.api);
    const args = getKwargs(this.flag[param.key], param.param);
    const response = await apiFn(args, this.initOverrides);
    const checked = errorCheck(await response.value());
    const result = param.convertFn(checked);
    // result might be undefined
    const user = result.result && userOrNullConverter(result.result);

    return {
      raw: { response: response.raw },
      header: buildHeader(response.raw.headers),
      data: {
        raw: result,
        user: user,
      },
    };
  }

Should be corrected to:

const user = result?.result && userOrNullConverter(result.result);

But I'm not sure.