Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Need TypeScript definitions #130

Closed
kevinclarkadstech opened this issue Nov 6, 2019 · 64 comments
Closed

Need TypeScript definitions #130

kevinclarkadstech opened this issue Nov 6, 2019 · 64 comments

Comments

@kevinclarkadstech
Copy link

Which SDK version are you using?

"^5.0.1"

What's the issue?

Need TypeScript definitions.

Steps/Sample code to reproduce the issue

  1. Create a .ts file
  2. Type import fbSdk from 'facebook-nodejs-business-sdk';

Observed Results:

  • What happened? This could be a description, log output, etc.
IDE throws an error: Could not find a declaration file for module 'facebook-nodejs-business-sdk'. '/Dev/facebook-nodejs-business-sdk-test/node_modules/facebook-nodejs-business-sdk/dist/cjs.js' implicitly has an 'any' type.
  Try `npm install @types/facebook-nodejs-business-sdk` if it exists or add a new declaration (.d.ts) file containing `declare module 'facebook-nodejs-business-sdk';`ts(7016)

Expected Results:

  • What did you expect to happen?

To get TypeScript definitions, as this is 2019 and this is an enterprise SDK library. It makes it much easier to consume a library without runtime errors and without consulting documentation frequently.

@jookovjook
Copy link

jookovjook commented Dec 13, 2019

@kevinclarkadstech Types can be generated with dts-gen using it locally:

yarn add dts-gen
./node_modules/.bin/dts-gen -m facebook-nodejs-business-sdk -d types

/types/facebook-nodejs-business-sdk should be generated under the project root

Than add generated types to tsconfig.json:

{
  "compilerOptions": {
    ...
    "typeRoots": [
      ...
      "node_modules/@types",
      "./types"
      ]
  },
  "exclude": ["node_modules"]
}

Hope it will help : )

@stale
Copy link

stale bot commented Mar 12, 2020

Hey there, it looks like there has been no activity on this issue recently. Has the issue been fixed, or does it still require the community's attention? This issue may be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the Stale label Mar 12, 2020
@xeoncross
Copy link

xeoncross commented Mar 17, 2020

Would love to see TypeScript definitions maintained as well. How do you namespace or use the type definitions provided by dts-gen?

@stale stale bot removed the Stale label Mar 17, 2020
@bduffany
Copy link

The dts-gen solution mentioned by @jookovjook didn't work for me.

@kevinclarkadstech
Copy link
Author

@kevinclarkadstech Types can be generated with dts-gen using it locally:


yarn add dts-gen

./node_modules/.bin/dts-gen -m facebook-nodejs-business-sdk -d types

/types/facebook-nodejs-business-sdk should be generated under the project root

Than add generated types to tsconfig.json:

{

  "compilerOptions": {

    ...

    "typeRoots": [

      ...

      "node_modules/@types",

      "./types"

      ]

  },

  "exclude": ["node_modules"]

}

Hope it will help : )

Thanks for responding...I just looked at the dts-gen library and noticed this:

This trade-off comes with a price -- you'll see a lot of anys in function parameters and return types. You may also see properties that are not intended for public use. dts-gen is meant to be a starting point for writing a high-quality definition file.

It would be better if Facebook devs would stop trying to create their own sht like Flow and just embrace TypeScript. I have heard from one popular Facebook developer that he never tried TypeScript. Just baffling.

@jookovjook
Copy link

@kevinclarkadstech Yeah. You'll see lots of warnings using this solution - it's not the best : ) Be prepared to use lot of @ts-ignore. But I think it's possible to make a fork of dts-gen which will work well with fb

@simllll
Copy link

simllll commented Jun 12, 2020

It's been quite a while since this issue has been created, is there anything new regarding TS support?

@kevinclarkadstech
Copy link
Author

It's been quite a while since this issue has been created, is there anything new regarding TS support?

There won't be...Facebook developers have not even tried TS, they just use Flow. 👎 I mean this issue has been open for over 1/2 a year. Thank God I moved to a different project and don't need to use this library anymore. :wipes brow

@simllll
Copy link

simllll commented Jun 12, 2020

@kevinclarkadstech what project do you use now? Is there any library which has ts support for the fb business sdk? I started to define some of the methods by using the dts-generated files as a basis, if we work together we could try to set something up in the https://github.com/DefinitelyTyped/DefinitelyTyped project.

@noudadrichem
Copy link

To what project did you move? Another FB SDK? @kevinclarkadstech

@kevinclarkadstech
Copy link
Author

To what project did you move? Another FB SDK? @kevinclarkadstech

I actually moved to a new project on a new job :) So I did not need the SDK anymore.

@kevinclarkadstech
Copy link
Author

@kevinclarkadstech what project do you use now? Is there any library which has ts support for the fb business sdk? I started to define some of the methods by using the dts-generated files as a basis, if we work together we could try to set something up in the https://github.com/DefinitelyTyped/DefinitelyTyped project.

Sorry, just seeing this now! Have you been using this SDK still?

I do think community supported type definitions would be better than none, but it’s pretty frustrating how against TS Facebook are. They keep using Flow. They always want to make everything for their little eco system (Yarn, Jest, Flow etc). Dan Abramov said he had never even tried TypeScript! Amusing to me because he also founded Redux, which is HORRIBLE without help from TS. 😅

Community supported type definitions usually work if a) the library is small or b) the library is big, but there are a lot of devs that use it and are willing to contribute. Otherwise it’s easy for them to get out of date.

Since you have looked at it more thoroughly, how hard does it seem to create full definitions?

@jlubeck
Copy link

jlubeck commented Jul 24, 2020

While waiting on official definitions, I went ahead and created my own with dts-gen and I've been working nicely with it.

I ran into a problem though. I want to work with pagination now, and looking at the docs it mentions a Cursor object, which is in src/cursor.js. For some reason, this file was not exported by dts-gen

Anyone knows how I can work with it so I can use pagination in my calls with Typescript?

Thanks!

@jlubeck
Copy link

jlubeck commented Jul 24, 2020

@simllll did you have to use Pagination yourself? Hopefully you are still using this library

@Desnoo
Copy link

Desnoo commented Jul 24, 2020

@jlubeck I have a little example of how to use the cursor.

This will const cursor = igUser.getMedia(fields..., options, false) return a cursor. If you set the last argument to false it will return a cursor.
Then you can call cursor.hasNext() to check if there is data for this API call.
If there is data for this cursor you can get the data by calling cursor.next(). This will return a Promise.

I hope that helps.

@jlubeck
Copy link

jlubeck commented Jul 24, 2020

hey @Desnoo thank you for your reply.

When creating my own definition, I changed the one I want from this:

getLeads(fields: any, ...args: any[]): any;

to this

getLeads(fields: any, ...args: any[]): Lead[];

That gave me all the code completion .

But with that, if I try:

let cursor = await ad.getLeads([],{},false);

I then get an error Property 'hasNext' does not exist on type 'Lead[]'

If I change the definition back to any, the hasNext doesn't error out, but how to I get the code completion back?

@Desnoo
Copy link

Desnoo commented Jul 27, 2020

@jlubeck getLeads(fields: any, ...args: any[]): Lead[];
Well you defined the return type as Lead[] and not of the type Cursor.

The Type Cursor should look something like:

type Cursor<T> { hasNext(): boolean; next(): Promise<T>; }

This will be the return type if you pass false as the last argument to the getLeads(): Cursor<Lead[]> method.
If you pass true or leave the last argument empty, then your definition should nearly work. The return type of getLeads then should be Promise<Lead[]>. I'm not completely sure, but it should look like this.

@jlubeck
Copy link

jlubeck commented Jul 27, 2020

Thank you so Much @Desnoo with your help now I implemented the methods like this:

export type Cursor<T> = Array<T> & {
    hasNext(): boolean;
    next(): Promise<Cursor<T>>;
}

getLeads(fields: any, options: any, ignoreCursor: boolean): Promise<Cursor<Lead>>;

getLeads(fields: any, ...args: any[]): Promise<Lead[]>;

and works like charm!

@abhijithvijayan
Copy link

abhijithvijayan commented Sep 10, 2020

Setting the "typeRoots" property doesn't work. But setting "path" does.(See microsoft/TypeScript#22217 (comment))

  • Add dts-gen globally
yarn global add dts-gen
  • Create @types folder
  • Generate types
dts-gen -m facebook-nodejs-business-sdk -d @types
  • Update tsconfig
{
  "compilerOptions": {
    ...
    "baseUrl": "./",
    "paths": {
      "*": ["@types/*"]
    }
  },
  "exclude": ["node_modules"]
}

@thekip
Copy link

thekip commented Nov 6, 2020

@jlubeck could you share your forked dts-gen and typings with all latest changes? Maybe we can then push it to DefinetelyTyped

@jlubeck
Copy link

jlubeck commented Nov 11, 2020

@thekip I only update a couple that were what I was using... definitely not ready to push to a repo or something unfortunately

@kevinclarkadstech
Copy link
Author

Just popped in to say sorry you guys are dealing with this still. I was actually looking at Facebooks NLP Node.js SDK today and OF COURSE....no TypeScript. So I went on to alternatives like Azure's LUIS. 😑🙄

@anton-bot
Copy link

Posting this comment to keep the issue open. Any reasonable project needs Typescript definitions.

@kevinclarkadstech
Copy link
Author

Facebook seem to refuse to embrace TypeScript across the board. Don't hold your breath. Shit, they haven't even responded to this issue. 😡

@nmggithub
Copy link

I know people hate self-promo, and I don't know if this will work for most of you, but I've made a strongly-typed Facebook Graph API client (the nodes are typed as well). I have not tested it with ad-related endpoints, but it does work well for the Messenger Platform in my tests. Here is the link: https://github.com/nmggithub/fbsdk-ts

@mptorz
Copy link

mptorz commented May 10, 2021

Bumping up the issue

@dcgudeman
Copy link

Similar issue. Would love it if facebook would officially address it.

@yonihod
Copy link

yonihod commented Jun 10, 2021

I wonder if there is a good solution for this today?

@alexblack
Copy link

I'm hitting an issue trying to use dts-gen, has anyone else seen this? Thanks!

 ./node_modules/.bin/dts-gen -m facebook-nodejs-business-sdk -d types -o
Warning: Could not retrieve version/homepage information: HTTP Error 301: Moved Permanently for http://registry.npmjs.org/facebook-nodejs-business-sdk

@nmggithub
Copy link

I'm hitting an issue trying to use dts-gen, has anyone else seen this? Thanks!

 ./node_modules/.bin/dts-gen -m facebook-nodejs-business-sdk -d types -o
Warning: Could not retrieve version/homepage information: HTTP Error 301: Moved Permanently for http://registry.npmjs.org/facebook-nodejs-business-sdk

That seems like an issue with dts-gen and not this package.

@bogdibota
Copy link

That seems like an issue with dts-gen and not this package.

seems to be related to this PR: microsoft/dts-gen#174

@yesh-cash
Copy link

yesh-cash commented Dec 1, 2021

I can't believe this project doesn't have typings. It's a shame really.

@dima-gusyatiner
Copy link

I was very disappointed to stumble upon this thread.
I would go as far as to say this is a must in 2022.

@achie27
Copy link

achie27 commented Dec 28, 2021

Consider this bumped

@only4lee
Copy link

only4lee commented Jan 24, 2022

I'd honestly settle if FB actually created swagger/OAS definitions. I do see one in FB's "incubating" github project that captures their conversions api, but it's unmaintained (at version 8 of the api). Using this I generated my own typescript library using the Open API code generator after modifying it to align to the current version (v12).

Works great -- only issue that it is problematic that FB appears to have chosen not to publish their APIs this way, at least meaningfully. It will be super annoying to maintain my own swaggers -- FB api docs are terrible. I've had to rage-quit (browsing API information) a few times when unable to find what I've needed.

@kigorw
Copy link

kigorw commented May 31, 2022

Bump! It's time to release types.

@nmggithub
Copy link

Facebook seems to really like their own type system, Flow. They probably will never release types.

@jRexhmati
Copy link

Crazy!!! Still no support for types, such a shame :/

@K-Mistele
Copy link

Ridiculous that they don't support this.

@alexgrigorehww
Copy link

I find it impossible to use this library because only some random features are documented and absolutely no autocomplete. I'll just go with rest api!

@kevinclarkadstech
Copy link
Author

Just keep creating new issues instead of only responding to this. Flood their issues with Need TypeScript Types.

@kevinclarkadstech
Copy link
Author

LOL from their README

"The Facebook Business SDK is a one-stop shop to help our partners better serve their businesses. Partners are using multiple Facebook API's to serve the needs of their clients. Adopting all these API's and keeping them up to date across the various platforms can be time consuming and ultimately prohibitive."

Yes, adopting these APIs without TypeScript types is TIME CONSUMING and PROHIBITIVE.

@nusendra
Copy link

bump

@brent-weatherall
Copy link

Another bump for the party. How is this still not supported? :(

@KloudCoder
Copy link

I'm shocked. No comment for Facebook. NoTypings for such a big project

@gustavogr
Copy link

Bump. Just stumbled with this when a client asked to be integrated with Facebook Business.

@kigorw
Copy link

kigorw commented Oct 18, 2022

bump to keep it active

@lluchkaa
Copy link

another bump

@Vict0rd
Copy link

Vict0rd commented Oct 26, 2022

BUUUMP

@dtpietrzak
Copy link

BUUMMPPPP

@kevinclarkadstech
Copy link
Author

Stop thumb downing bumps @Desnoo . Facebook deserves the thumbs down.

@Desnoo
Copy link

Desnoo commented Oct 29, 2022

@kevinclarkadstech we also do need this. But if you wait for a response you only get notifications about the bump messages and this is really annoying. Every week is ok but not every day.

@Vict0rd
Copy link

Vict0rd commented Oct 31, 2022

PR DefinitelyTyped/DefinitelyTyped#62989

@Vict0rd
Copy link

Vict0rd commented Nov 2, 2022

The PR is merged.

@kevinclarkadstech
Copy link
Author

I guess I'm closing this so I don't have to see it anymore. Annoying how the community has to write definitions for a product that Facebook profits from.

@phillipmohr
Copy link

Could someone please explain how to use the type definitions from DefinitelyTyped?

Example:

const leadgenForms: LeadgenForm[] = await (new Page(pageId)).getLeadGenForms([
    LeadgenForm.Fields.questions,
])

Getting Type 'Cursor' is not assignable to type 'LeadgenForm[]'., which makes sense since getLeadGenForms returns Promise<Cursor>. I also tried

const leadgenForms: Cursor<LeadgenForm[]> = await (new Page(pageId)).getLeadGenForms([
    LeadgenForm.Fields.questions,
]);

Not sure how it's supposed to work... Any help is appreciated

@Vict0rd
Copy link

Vict0rd commented Mar 13, 2024

@phillipmohr I use this method

  public async *fetchInsightPages({
    fields,
    params,
  }: {
    fields: string[];
    params: Record<string, string>;
  }): AsyncGenerator<Insight[]> {
    const cursor = await this.fbAdAccount.getInsights(fields, params);

    while (true) {
      yield cursor.map((item): Insight => {
        return {
          adId: item.ad_id,
          adName: item.ad_name,
          ...
          startDate: item.date_start,
          endDate: item.date_stop,
        };
      });

      if (cursor.hasNext()) {
        await cursor.next();
      } else {
        break;
      }
    }
  }

Maybe next method will work too

  public async fetchInsights({
    fields,
    params,
  }: {
    fields: string[];
    params: Record<string, string>;
  }): Promise<Insight[]> {
    const result: Insight[] = []
    const cursor = await this.fbAdAccount.getInsights(fields, params);

    while (true) {
      for (const item of cursor) {
        result.push({
          adId: item.ad_id,
          adName: item.ad_name,
          adSetId: item.adset_id,
          // ...
          startDate: item.date_start,
          endDate: item.date_stop,
        });
      }

      if (cursor.hasNext()) {
        await cursor.next();
      } else {
        break;
      }
    }

    return result;
  }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests