facebook-nodejs-business-sdk icon indicating copy to clipboard operation
facebook-nodejs-business-sdk copied to clipboard

Need TypeScript definitions

Open kevinclarkadstech opened this issue 5 years ago β€’ 52 comments

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.

kevinclarkadstech avatar Nov 06 '19 18:11 kevinclarkadstech

@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 : )

jookovjook avatar Dec 13 '19 10:12 jookovjook

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[bot] avatar Mar 12 '20 19:03 stale[bot]

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

xeoncross avatar Mar 17 '20 00:03 xeoncross

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

bduffany avatar Mar 19 '20 19:03 bduffany

@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.

kevinclarkadstech avatar Mar 20 '20 04:03 kevinclarkadstech

@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

jookovjook avatar Apr 06 '20 11:04 jookovjook

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

simllll avatar Jun 12 '20 07:06 simllll

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

kevinclarkadstech avatar Jun 12 '20 15:06 kevinclarkadstech

@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.

simllll avatar Jun 12 '20 15:06 simllll

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

noudadrichem avatar Jul 07 '20 07:07 noudadrichem

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 avatar Jul 07 '20 17:07 kevinclarkadstech

@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?

kevinclarkadstech avatar Jul 07 '20 17:07 kevinclarkadstech

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 avatar Jul 24 '20 20:07 jlubeck

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

jlubeck avatar Jul 24 '20 20:07 jlubeck

@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.

Desnoo avatar Jul 24 '20 21:07 Desnoo

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?

jlubeck avatar Jul 24 '20 21:07 jlubeck

@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.

Desnoo avatar Jul 27 '20 20:07 Desnoo

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!

jlubeck avatar Jul 27 '20 21:07 jlubeck

Setting the "typeRoots" property doesn't work. But setting "path" does.(See https://github.com/microsoft/TypeScript/issues/22217#issuecomment-369783776)

  • 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"]
}

abhijithvijayan avatar Sep 10 '20 05:09 abhijithvijayan

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

timofei-iatsenko avatar Nov 06 '20 12:11 timofei-iatsenko

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

jlubeck avatar Nov 11 '20 21:11 jlubeck

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. πŸ˜‘πŸ™„

kevinclarkadstech avatar Dec 16 '20 04:12 kevinclarkadstech

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

anton-bot avatar Jan 25 '21 07:01 anton-bot

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

kevinclarkadstech avatar Jan 25 '21 19:01 kevinclarkadstech

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

nmggithub avatar Feb 18 '21 17:02 nmggithub

Bumping up the issue

mptorz avatar May 10 '21 21:05 mptorz

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

dcgudeman avatar May 19 '21 23:05 dcgudeman

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

yonihod avatar Jun 10 '21 08:06 yonihod

Probably not as FB is all in the 'flow' party.

noudadrichem avatar Jun 10 '21 09:06 noudadrichem

I probably should just abandon this issue, it has been open 1.5 years with no response from anyone from facebook.

kevinclarkadstech avatar Jun 10 '21 14:06 kevinclarkadstech

Bumping up the issue

Arootin avatar Jun 29 '21 07:06 Arootin

loading . . .

wtakayama-chwy avatar Jul 07 '21 16:07 wtakayama-chwy

loading . . .

connection timeout ⚠️

AlonMiz avatar Aug 03 '21 17:08 AlonMiz

Even if FB does not like typescript they should cater to their customers who actually use their advertising services. Now more than ever since apple basically has rendered "easy" use of their tracking obsolete. Not catering to typescript customers is the same as not supporting developers who are using Python or whatever.

If a company don't serve the costumers who are paying for their service then that's a company who will never have long term success.

ugglr avatar Sep 20 '21 00:09 ugglr

bump

davidpene avatar Oct 13 '21 02:10 davidpene

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

alexblack avatar Oct 18 '21 19:10 alexblack

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.

nmggithub avatar Oct 18 '21 19:10 nmggithub

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

seems to be related to this PR: https://github.com/microsoft/dts-gen/pull/174

bogdibota avatar Nov 20 '21 22:11 bogdibota

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

yesh-cash avatar Dec 01 '21 20:12 yesh-cash

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

dima-gusyatiner avatar Dec 01 '21 20:12 dima-gusyatiner

Consider this bumped

achie27 avatar Dec 28 '21 06:12 achie27

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.

only4lee avatar Jan 24 '22 12:01 only4lee

Bump! It's time to release types.

kigorw avatar May 31 '22 18:05 kigorw

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

nmggithub avatar May 31 '22 18:05 nmggithub

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

jRexhmati avatar Jun 20 '22 08:06 jRexhmati

Ridiculous that they don't support this.

K-Mistele avatar Jul 18 '22 16:07 K-Mistele

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!

alexgrigorehww avatar Jul 22 '22 07:07 alexgrigorehww

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

kevinclarkadstech avatar Jul 22 '22 15:07 kevinclarkadstech

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.

kevinclarkadstech avatar Jul 22 '22 15:07 kevinclarkadstech

bump

nusendra avatar Jul 25 '22 17:07 nusendra